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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
idoblenderstuffs
Prole
Posts: 3
Joined: Sun Nov 13, 2022 7:10 pm

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

Post 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?
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

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

Post 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
Last edited by Andlac028 on Tue Nov 22, 2022 7:07 pm, edited 1 time in total.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

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

Post 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
}
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests