Trying to get the Color value out of a specific image's pixel given a LMB click. This script is attached to a basic Node (I prefer to use them as my scene roots):
func _process(delta):
if Input.is_action_just_pressed("lmb"):
var data: Image = get_viewport().get_texture().get_data()
data.lock()
var world_click_pos: Vector2 = get_viewport().get_mouse_position()
print("click at: %s" % world_click_pos)
var color: Color = data.get_pixelv(world_click_pos);
ColorNode.color = color # A ColorRect child, aliased out of scope of _process()
print(str(color))
data.unlock()
I fairly consistently get incorrect Color values out of this. The printed Vector4 is incorrect and the ColorRect displays the wrong color. It's almost like the position I'm feeding to get_pixelv() isn't right.
I've tried world_click_pos = get_global_mouse_pos()
to no avail as well. (That I had to try in a script attached to the Sprite itself, since bare Node objects don't have access to that method).
I've tried it by keeping the Sprite at (0, 0) with offset = false
, as well as setting the position programmatically to the center of the screen. Both give me false color readings. For clarification, here is the scene tree:
