0 votes

I have a TextureRect with a GradientTexture. I'm trying to get a pixel at a given position. I follow the accepted answer of this question:

var texture_rect = get_node("../TextureRect")
print(texture_rect)
var texture = texture_rect.get_texture()
print(texture)
var data = texture.get_data()
data.lock()
print(data)
print(position, ": ", data.get_pixelv(position).to_html(false))
data.unlock()

I get:

[TextureRect:1141]
[GradientTexture:1135]
[Image:2231]
(447.540558, 218.679306): 000000

even though the color in that position is not 00000. How do I fix this?

Godot version v3.1.1-stable
in Engine by (68 points)

1 Answer

+1 vote
Best answer

GradientTexture produces one-dimensional texture (2048x1 by default). If you use it for TextureRect it will just fill and repeat the texture along the region.

To workaround this, you can create a Viewport node containing TextureRect, and then fetch the resulting viewport texture with $viewport.get_texture(), from which you can download Image with ViewportTexture.get_data().

Also, consider supporting the proposal for adding GradientTexture2D class in Godot.

by (1,356 points)
selected by
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.