Shaders [SOLVED] Undeclared Identifier / If-Logic Issue

Reed

Member
So, this feels like one of those situations where I'm missing something obvious, but I'm stumped. Would welcome any fresh eyes that want to take a look. I'm using GMS 1.4 on Windows if that's relevant.

I'm writing a shader where I want to change some colors based on the HSB values of a mask sprite. To do that, I have to convert RGB to HSB, so the code below is calculating chroma (saturation) and hue from RGB inputs.

The problem is with the hue calculation. It's a series of if-statements that calculate hue a certain way depending on which of R, G, or B is the highest. The results are in a range from 0.0 to 6.0, so after these initial calculations, there's a line (3rd line from the bottom in the code box below) that converts it to degrees from 0.0 to 360.0.

I'm getting an error on that line saying that hue has an undeclared identifier, which must mean that all of the if-statements are returning false, so hue is never initialized as a float. But... I don't get how that's possible. It seems like at least one of the if's has to be true.

Code:
//This is the fragment shader.
varying vec2 v_vTexcoord;
varying vec2 v_vMaskcoord;
varying vec4 v_vColour;

uniform sampler2D mask_tex;

void main()
    {
    vec4 frag_color = v_vColour*texture2D(gm_BaseTexture,v_vTexcoord);
    vec4 mask = texture2D(mask_tex,v_vMaskcoord);
 
        float M = max(max(mask.r,mask.g),mask.b); //highest of RGB values from mask
        float m = min(min(mask.r,mask.g),mask.b); //lowest of RGB values from mask

        float chroma = M - m; //chroma (saturation) in mask is equal to diff b/t highest and lowest RGB values

        //Calculate hue:
        if (chroma == 0.0) //if there is no hue
            {
            float hue = 0.0;
            }
        else
            {
            if (M == mask.r) //if red is highest RGB
                {
                float hue = ((mask.g - mask.b) / chroma);
                }
            if (M == mask.g) //if green is highest RGB
                {
                float hue = (((mask.b - mask.r) / chroma) + 2.0);
                }
            if (M == mask.b) //if blue is highest RGB
                {
                float hue = (((mask.r - mask.g) / chroma) + 4.0);
                }
            }
     
        hue = hue * 60.0; //convert hue to degrees

        // Do some modifications to frag_color based on hue and chroma

       gl_FragColor = frag_color;
 
Last edited:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Have "float hue;" prior to your if-branches GLSL and HLSL have block-scoped variables, meaning that if you declare a local variable inside a {}, it simply doesn't exist for any code outside of that (meaning that, in your case, each block has it's own variable called "hue", and none of them are visible to the line below)
 
Top