• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Shaders simple color change shader

boi513

Member
I am trying to write a shader that checks if a sprite is all black (0,0,0,1) or all white (1,1,1,1) and change those specific pixels into other colors. The one I have written (below) is what I have. I can make it change the black pixels and white pixels accordingly but any color that is in between, such as yellow or blue, is changed to black. I want the shader to only change black or white pixels. I would appreciate it if someone can help me wrap my mind around this.

I am still new learning shaders and am trying to start small.

GML:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform sampler2D texture;
uniform vec4 colorMix1;
uniform vec4 colorMix2;
vec4 newColor;


vec4 convert( vec4 con)
{
    vec4 ogart;
    ogart.rgba = con.rgba;
    
if (con.rgba == vec4 (0,0,0,1))
        newColor.rgba = colorMix1.rgba;
     ogart.rgba = newColor.rgba;

if (con.rgba == vec4 (1,1,1,1))
        newColor.rgba = colorMix2.rgba;
     ogart.rgba = newColor.rgba;




    return ogart.rgba;
}

void main()
{
       vec4 colorPixel = texture2D( gm_BaseTexture, v_vTexcoord );
       vec4 colorOut = vec4( convert   ( colorPixel.rgba ));
       gl_FragColor = v_vColour * colorOut;
}
 
G

general_sirhc

Guest
To help I'd like to ask some questions about your code

  1. Why have the "convert" function at all? The code is fairly short and the name of the function doesn't add clarity to what is happening
  2. You have 3 possible branches, If .. else would make your life easier
    • If black newColor = colorMix1 ELSE
    • If White newColor = colorMix2 ELSE
    • newColor = v_vColour
  3. I see you multiply v_vColour by colorOut, if your intention is just to replace color A with color B, why are you doing this?
  4. varying vec4 v_vColour; will always be the color of the vertex, if you're drawing a sprite this is probably always black as the color will comes from the texture, not the vertex (Note: I'm possibly wrong on this point)
 

Tyg

Member
Just something to consider shader colors have most likely gone through bilinear interpolation
as it is on by default, thats why most color replacement shaders have a range
so you may not get the color you are expecting..google gamemaker color replacement shader
 
Top