Page 1 of 1

How do I make a shader that changes white pixels to green?

Posted: Sat Nov 19, 2022 12:02 am
by idoblenderstuffs
I've already got shaders working. This example shader is definitely doing something to make my game look wrong:

Code: Select all

vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
{
    vec4 c = Texel(texture, texture_coords); // This reads a color from our texture at the coordinates LOVE gave us (0-1, 0-1)
    return vec4(vec3(1.0, 1.0, 1.0) * (max(c.r, max(c.g, c.b))), 0.2); // This just returns a white color that's modulated by the brightest color channel at the given pixel in the texture. Nothing too complex, and not exactly the prettiest way to do B&W :P
}
I've not been able to find out how to make a shader that just goes:

Code: Select all

if pixel = white then make pixel green
How could I do this?

Re: How do I make a shader that changes white pixels to green?

Posted: Mon Nov 21, 2022 2:46 pm
by Andlac028
(Not tested)

Code: Select all

vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
{
    vec4 c = Texel(texture, texture_coords) * color;
    if (c.r == 1.0 && c.g == 1.0 && c.b == 1.0) { // if white, if you want more tolerancy, use something like c.r >= 0.9
        c.r = 0.0; c.b = 0.0; // set red and blue component to 0, so only green component is left
    }
    return c;
}
Edit: decimals for colors

Re: How do I make a shader that changes white pixels to green?

Posted: Tue Nov 22, 2022 5:12 pm
by milon
Another option, potentially faster (untested), is to streamline the color test:

Code: Select all

if (c.r + c.g + c.b >= 3.0) { // you can set a threshold by changing 3.0 to a lower value like 2.7 or so
    c.r = 0.0; c.g = 0.0; c.b = 1.0; // don't forget decimal notation for color values!  And it's a good idea to set green if you're using a threshold less than 3.0
}