How to have shading on sprites included in a palette swap shader?

Godot Version

4.1.3

Question

Hey all, I have a basic palette swap shader in use for my game. It can easily identify RGB values and swap them for another. I would like to know however how this would work for parts of a sprite that has shading as opposed to just solid colour. I have a precision system of sorts which can detect a small range, however this only replaces the shading with the same solid colour instead of only a hint of it to keep the shading.

Here are some images to explain. Image one has a little bit of darker green around the edge of the hand for a bit of shading. After I apply the shader to turn it to red, the darker shading has also been palette swapped to the solid red colour and is lost. I’m fairly new to shaders so if anyone could suggest something, that’d be awesome! Thanks

image

It would be easier to help if you posted your shader script but if I had to guess, the reason that it doesn’t work might be that you are comparing vec4 colors which is not accurate enough. I used to have the same problem when I started learning shaders. Converting them to ivec3 makes the comparison accurate.

Example of converting vec4 color to ivec3

vec4 color = texture(TEXTURE, UV);
ivec3 color_i = ivec3(round(color.rgb * 255.0));

You can compare two ivec3 colors simply with == operator

Depending on how many regions you want to change to unique colors, what you’ll want to do is have one channel of your sprite texture be the shading for the entire sprite, just black and white and everything in between.

Then you’ll want to pack in some masks in other channels that determine which pixels you’ll want to tint, like if you have two areas you want to tint independently, you’d store TintableAreaA masking in the G channel and TintableAreaB masking in the B channel (the shading is in the R channel), and then in your shader you would multiply the color values you want to the shading, using the G and B masks to tell where each of your colors go.

You can get a lot more complicated there like if you needed to tint more than 2 areas, or you wanted a more exotic blend mode than mutiply (like overlay), but that is the basic idea.