0 votes

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()
in Engine by (14 points)

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.

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, donotreceive_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.

ussues

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)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.