Is this possible? can anyone give me some advice on how to return a color on a models surface by using a raycas/intersect ray? This is reading vertex color, not screen/view-port color on a static mesh.

I would appreciate any pointers. I'm not the strongest coder :)...I'm more of a get it done in the most basic way possible type of guy :)

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.

I do appreciate your response, but it is going to be too heavy a process for slower systems to do every frame.... I just need to find a more grounded solution...

no need to respond..I will think about this some more before I commit to anything...I may just do some area groups and do a raycast(node) upward to check those grouped areas for a property....not sure if that would be much better...due to all the areas I would have in the world...I am only doing one massive(well, large) world after all...

I'd be curious to hear what it is you'd like to achieve with it? Maybe there is a better solution we can offer.

I want to use it as an environmental change...sounds, possibly weather etc...so if I read green(grass), white(or vectors closer to the high end) snow..etc....

Primarily for changing sounds...I figured I could do a lot more with it though. I was considering weather and environmental changes.

3 years later