Advanced grayscale /w option to spare color
GRAYSCALING
Adjust threshold_r, threshold_g and threshold_b to tinker grayscale to your preferences.
Those are factors of R, G and B channels to the final grayscale output.
Lastly, adjust threshold to make grayscale image brighter or darker generally.
SPARE COLOR
This is if you want to leave certain color unaffected by grayscaling.
If you don’t need it and want to optimise, just comment out everything after // Spare color control line (but leave } in the last line).
How to:
- Set spare_color to be the color you desire to spare.
- Adjust tolerance value till the desired result. tolerance value is just calculating how much difference from the exact spare_color will be tolerated.
(thanks to https://godotshaders.com/shader/stencil-masking-in-3d/ for the idea)
Shader code
shader_type canvas_item;
uniform float threshold_r: hint_range(0.0, 1.0);
uniform float threshold_g: hint_range(0.0, 1.0);
uniform float threshold_b: hint_range(0.0, 1.0);
uniform float threshold: hint_range(0.0, 1.0);
uniform vec3 spare_color: source_color;
uniform float tolerance: hint_range(0.0, 1.0);
void fragment() {
vec3 orig_color = texture(TEXTURE,UV).rgb;
// Grayscale control
float r = COLOR.r + threshold_r;
float g = COLOR.g + threshold_g;
float b = COLOR.g + threshold_b;
float color = r * g * b;
color = clamp(color,0.,color-threshold);
COLOR.rgb = vec3(color);
// Spare color control
// difference between target color and original texture color
float diff = distance(spare_color,orig_color);
// create mask for selected color
float spare_mask = diff > tolerance ? 0. : 1.;
COLOR.rgb = mix(vec3(color),spare_color,spare_mask);
}


