Matching Texture Coordinates to GetPixel() values in gdscript

Godot Version

4.7

Question

Hi,

I have been trying to figure out how to get pixels in gdscript in the same way as in a shader.

The Image.get_pixel(x,y) function uses array type coordinates (e.g (0-512)) and the coordinates do not wrap around so a number bigger than 512 causes an error.

so if I had

uv_scale * world_pos.xz / 128.0

in the shader, then I would have something like

512 * int( uv_scale * x / 128.0 )

the uv_scale * x / 128.0 can be 4.0 * 127.0 / 128.0, the x / 128.0 is like a percentage, and the final result is bigger than 512, so I just added the floor() function

512 * int( uv_scale * x / 128.0 - floor( uv_scale * x / 128.0 ))

And a similar function for the v coordinate.

This is still wrong … any help would be appreciated.

What is that supposed to do? Get the color of a repeating, scaled image at a specific position?

In that case you should calculate your scaled UV, use posmod() to wrap it in a 0-1 range, multiply it by your image size, and only then cast it to int:

int( 512.0 * fposmod(uv_scale * x / 128.0, 1.0) )

Thanks I will try this

Also i had to divide by 127 … The x value is never 128 it is 0-127 very elementary error.