The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

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")
var texture = texture_rect.get_texture()
var data = texture.get_data()
print(position, ": ", data.get_pixelv(position).to_html(false))

I get:

(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 (143 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,422 points)
selected by

Isn't there an easier approach to get a pixel at a given position from a Gradient Texture in Godot 3.5?

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 Frequently asked questions and 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 [email protected] with your username.