Search code examples
opengl-esgpuimage

Fragment shader - "strange" behaviour when changing color of fragment - detected as different value


I'm using GPUImage library and I'm writing my own custom filters using fragment shaders.

I have an image with a black text on it. I would like to create something like a "drop shadow", but in a red colour. That's pretty easy, I simply offset coordinate, check colour and if I hit something "not white" I change it to red.

When I created the filter, I decided that pure red is not the way to go and I wanted to adjust the colour a bit. So I have modified the green and blue values. Once I did that, the whole image became red.... I'm not sure what I'm doing wrong here... why do I get this kind of effect?

varying highp vec2 textureCoordinate;

uniform sampler2D inputImageTexture;


lowp vec4 offsetCoordinate(highp vec2 coordinate, lowp float x, lowp float y) {
    highp vec2 newCoordinate = textureCoordinate;
    newCoordinate.x = textureCoordinate.x + x;
    newCoordinate.y = textureCoordinate.y + y;
    return texture2D(inputImageTexture, newCoordinate);
}

void main()
{

    lowp vec4 inputColor = texture2D(inputImageTexture, textureCoordinate);

    if (inputColor.r > 0.1 && inputColor.g > 0.1 && inputColor.b > 0.1) {

        lowp vec4 outputColor = offsetCoordinate(textureCoordinate, 0.1, 0.03);
        outputColor.r = 1.0;
        outputColor.g = 0.09;
        outputColor.b = 0.09;

        gl_FragColor = outputColor;

    } else {

        gl_FragColor = inputColor;

    }

}

Here's a version where I don't change the g & b values:

enter image description here

And here version with g & b values set.

enter image description here

I'm wondering why would that happen?


Solution

  • The result you get is exactly what you should get with your shader.

    Your input image does contain only white and black pixels, so

    lowp vec4 outputColor = offsetCoordinate(textureCoordinate, 0.1, 0.03);
    

    will yield either (0,0,0,A) or (1,1,1,A) (don't know what alpha value you will get, but it won't matter).

    If you overwrite outputColor.r by 1, youll change black to red (1,0,0,A), and white will stay white.

    If you overwrite r, g and b, you will get (r,g,b,A), so the color will be totally independent of the color you fetched from the texture.