- Edited
I'm trying to write a spatial shader that highlights any pixel with the same color as the last pixel the user clicked. In my C# script located on the map mesh, I locate the desired pixel, grab its color, and send it to the uniform on the shader:
[Export]
MeshInstance3D mapMesh; //reference set manually in map tscn
void SetPixelColor(Vector2 pixelCoord, Texture2D texture)
{
Image mapImage = texture.GetImage(); //get and decompress image
if (mapImage.IsCompressed())
{
mapImage.Decompress();
}
Color pixelColor = mapImage.GetPixel((int)pixelCoord.X, (int)pixelCoord.Y); //read color
ShaderMaterial highlightMat = (ShaderMaterial)mapMesh.MaterialOverride.NextPass; //locate highlight shader and set uniform
Vector3 colorVec = new Vector3(pixelColor.R, pixelColor.G, pixelColor.B);
highlightMat.SetShaderParameter("colorToHighlight", colorVec);
}
Then in the shader, I check the uniform for each fragment and hide each pixel of the highlight texture that doesn't correspond to the desired color:
uniform vec3 colorToHighlight;
uniform sampler2D screen_texture : source_color;
void fragment() {
vec3 texColor = textureLod(screen_texture, SCREEN_UV, 0.0).rgb;
if (ColorMatch(texColor, colorToHighlight, 0.0001)){
ALPHA = 1.0;
} else{
ALPHA = 0.0;
}
}
This doesn't work, and I think the reason is because get_pixel is returning the wrong color. Here's an example output: the 4-color image on the left is the correct colors, it's what I get from both the raw image and from texColor. But if I click on the green quadrant and have my shader set every pixel to colourToHighlight, I get the much lighter shade of green on the right:
I'm obviously just getting started with godot, but I could really use help figuring out where to start debugging. My first thought was that the two images might have different color compression, but I verified that the raw image was imported as Lossless, and I'm not sure how to check what the shader's compression is.