Help needed with color recognition

Godot Version



The problem is that there is a mismatch in the colors processed in Godot vs the color in the shader, using the same image both places.
My process is as follows:

I have an image of different colored regions that I i sample with a get_pixel function, it gives me a color code, Ex. (0.549, 0.1255, 0.3922, 1). The import settings are the following:

In the shader i sample the image using following code:
"uniform sampler2D regionmap_image : source_color, filter_nearest;"

Which i then process to a texture:
"vec4 regionmap = texture(regionmap_image, world_pos.xy);"

Finally i want to match the color sent from Gscript to the color on the map in the shader:
In order to get a match I have to apply a near match, while I want exact match.

To get a match i use:
"if(distance(regionmap, target_colors1) < 0.005)"

But I want to use:
"if(regionmap == target_colors1)"

Anyone have an idea how I can make sure shader and Godot reads the image the same way?

Colors need to be converted to ivec3 or ivec4 for accurate comparison. For example this asset uses a shader that converts colors to ivec3.

1 Like

Thank you!
It clarified the floating point issue when it comes to the color comparison issue.
If someone else stumbles across this problem there are additional explanations on the Godot git:

1 Like