How to get pixel color via RayCast with mouse?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By z0nics

I have this code and one mesh in Viewport.
I’m tryin to get pixel color of the albedo mesh texture.
Both mesh and ray in same layer

onready var ray
onready var cam

const ray_length = 1000

func _ready():

    ray = RayCast.new()
    ray.set_name("RayCast_cells")
    
    # Atach the RayCast to the camera
    cam = $Viewport/Cam_Mask
    cam.add_child(ray)
    
    ray.clear_exceptions()
    ray.set_collision_mask(20)
    ray.set_enabled(true)

    set_physics_process(true)

func _physics_process(_delta):
    var mouse = $Viewport.get_mouse_position()

    var from = cam.project_ray_origin(mouse)
    var to = from + cam.project_ray_normal(mouse) * ray_length
    
    ray.cast_to = to
    ray.translation = from
    
    ray.get_collider()

Hi z0nics,

I know this isn’t really answering your question (I don’t know how to access the UV mapping data in the way you’re after). But could you make do with something like this?

var mouse = get_viewport().get_mouse_position()
var tex = get_viewport().get_texture().get_data()
tex.lock()
var colour = tex.get_pixelv(mouse)
tex.unlock()

This should get the final, fully-shaded pixel colour.

Tim Martin | 2020-05-10 12:54

Thank you for yor answer!
I tried this way but i have an issue with incorrect color picking position (or something else).

I have model with albedo texture.
Spatial material. White albedo color and flags: unshaded, do_not_receive_shadows.

Texture is totally black, with different areas shades of gray (cheeks and nose).

albedo_texture

But when my cursor is quite near the model, or even on some black part of it, albedo color ic changing.

My code is (yes, i know that i can do it with shader and it will be much better color mixing but its just a quick test):

func _process(_delta):
	var mouse = get_tree().root.get_mouse_position() #$Viewport
	var mask_render = get_tree().root.get_texture().get_data()
	mask_render.lock()
	var pixel = mask_render.get_pixelv(mouse)
	#print(mouse)
	print(pixel)
	mask_render.unlock()
	if pixel.r <= 0.3:
		$head/full.get_surface_material(0).albedo_color = Color(255,0,0,1)
	else:
		$head/full.get_surface_material(0).albedo_color = Color(0,0,0,1)
	

z0nics | 2020-05-10 17:25