Shaders Mysterious syntax error

jujubs

Member
So I followed this tutorial right here to learn how to change my sprites' colors using shaders:


It all seemed simple enough, and indeed, I managed to add some color to my game sprites without much hassle. However, the method used in the video also changes how transparency behaves. My player sprite ended up looking like this whenever I apply some color using the shader:


I thought this would be simple enough to fix. Based on how the shader works, I put an intercept that checks if the alpha is bigger than 0 before it changes its color. But then, this happens:
palette.png
I've no idea what's wrong with the syntax. Even the compiler error just spits out zilch between apostrophes, and contrary to what it says, there is no additional info in the Compile window.
I'm a noob when it comes to GLSL and shaders in general, but I know a bit of C and I can't see anything wrong there :(
I even tried using curly brackets, but it just puts the syntax error right in its line. What am I doing wrong?
 
Youve already declared the variable "ref" so you don't need the vec4 in the if statement.

I don't see any place where you create the foo variable, you need to declare it and assign a value to it before using it in the if statement, and also drop the vec4 in front of it.

Edit: in fact, you should replace vec4 foo.a with 0.0, as your just checking for greater than 0 alpha.

And you are declaring uvcoord inside an if statement, that variable should be created prior to the if statement.
 

jujubs

Member
Thanks! Actually, it was written like this because removing the vec4 and using 0.0 instead of my foo var (which was declared just above the code) returned all sorts of weird errors.
I declared uv_coord along with the other vars before the main and it fixed it... kinda. I'm still getting colors in my alpha :(
What'd be the right way to do this?
 
Your current code selects the output fragment colour from the Palette, regardless of whether alpha is 0.0 or greater.

You need to add some code, where if alpha is 0.0, then just use the original colour from the gm_BaseTexture.

EDIT: Also, it's much easier to assist you if you post your shader code (or any code for that matter) in plain text inside a code block.
 

jujubs

Member
Oh yes, sorry. The fragment shader looks like this right now:

Code:
vec4 foo = vec4(0.0, 0.0, 0.0, 0.0);

//
// Simple passthrough fragment shader
//
/*****************************************************************************/
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
vec2 uv_coord;
vec4 new_color;

//  Get palette as texture
uniform sampler2D Palette;

//  Color Offset Index
uniform float Offset;

void main() {

    vec4 ref = texture2D(gm_BaseTexture, v_vTexcoord);   

    uv_coord = vec2(ref.r, Offset);
   
    new_color = texture2D(Palette, uv_coord);
   
    gl_FragColor = new_color;
}
I tried isolating the alpha before, passing everything through a "filter" variable, but whenever I tried just setting the "filter.a" to 0, it just deactivated the shader, it seems.


EDIT: I think I've got it!

Check this out:
Code:
void main() {

    vec4 ref = texture2D(gm_BaseTexture, v_vTexcoord);

    if(ref.a == 0.0)
        uv_coord = vec2(ref.r, 250);
    else
        uv_coord = vec2(ref.r, Offset);
    
    new_color = texture2D(Palette, uv_coord);
    
    gl_FragColor = new_color;
}
It's appears to be working perfectly. It just catches whatever color is at the end of the palette (which, at line 250, is NONE) and passes through if the alpha is 0.0.
Is this the right way to go about it, though? My game runs at a ridiculously small resolution, but I still get worried about performance.
 
Last edited:
It looks good to me.

This should also work, and avoid a Palette lookup and [else] branch for transparent pixels.
Code:
void main() {

   vec4 new_color = texture2D(gm_BaseTexture, v_vTexcoord);

   if(new_color.a > 0.0)
   {
       uv_coord = vec2(new_color.r, Offset);
       new_color = texture2D(Palette, uv_coord);
   }
   
   gl_FragColor = new_color;
}
If you search for advice on optimising shaders, you will find that its recommended to avoid if/else branches in shaders. On the other hand, modern graphics cards are fairly efficient at handling these conditions as well.

However this would probably be considered premature optimisation at this stage of your game.

What I would do for now is check what FPS you are getting with and without the shader enabled. If the difference is slight, then no need to check further, continue with what you have.
 
Top