game-developmentgodotgdscript

How to Get Value of a GradientTexture2d


I am trying to make a procedurally generated island. I think I am almost finished with actual terrain generation part using OpenSimplex noise, only thing left is making it into a island surrounded by water. For that I am using a radial gradient Texture something that looks a bit like the picture below:

enter image description here

Only issue is I don't know how to get the Gradiant value. Preferably I am looking for a function that can return value from 0-1 based on the white and black areas of the map. I checked docs countless times and searched online, only thing I could find is get_offset() function but I am not sure how it works or if its what I am looking for. Thanks in advance.


Solution

  • If you want to read the pixels of a Texture from GDScript, you need to:

    On the other hand, if you want to work with the Gradient from GDScript, you need to:

    Either way, you would have a Color. Now, you probably want to read one of the channels of the Color. For that you can access the r, g, b and a properties of the Color. If your image is gray scale, the r, g, b properties of the Colors you get from it should be equal, so either of them will do.

    Addendum: On Godot 4.x we no longer need to use lock/unlock.