I have not tried it myself, but it looks like it would be a bit of an involved process. By default, the Raycast node (and manually coded version) does not return a color, just a position and normal. However, if you can access the MeshInstance node from the raycast, then I think you could use the MeshDataTool to get the vertex color.
Below is one way I think it could be done, but it is really inefficient since it loops through all the vertices to find the closest (untested):
var mesh_data_tool = MeshDataTool.new()
func get_closest_vertex(local_position):
var closest_dist_squared = INF
var closest_vertex_index = -1
var vertex_count = mesh_data_tool.get_vertex_count()
for v in range(0, vertex_count):
var v_pos = mesh_data_tool.get_vertex(v)
var tmp = local_position.distance_squared_to(v_pos)
if (tmp <= closest_dist_squared):
closest_dist_squared = tmp
closest_vertex_index = v
return closest_vertex_index
func convert_raycast_position_to_mesh_position(raycast_pos, mesh_node):
# I *think* this is correct. It might be just "xform" instead of "xform_inv"...
return mesh_node.xform_inv(raycast_pos)
func _physics_process(delta):
# other code here...
if $Raycast.is_colliding():
var other_mesh = $Raycast.get_collider().get_node("MeshInstance")
var raycast_local_pos = convert_raycast_position_to_mesh_position(
$Raycast.get_collision_point(), other_mesh
)
mesh_data_tool.create_from_surface(other_mesh.mesh, 0)
var vertex_index = get_closest_vertex(raycast_local_pos)
if (vertex_index != -1):
var vertex_color = mesh_data_tool.get_vertex_color(vertex_index)
print ("Raycast near vertex with color: ", vertex_color)
Hopefully this helps a bit. I'm sure there is a better solution, I'm just not sure what it would be right off.