Search code examples
libgdxglslshaderblend

glsl shader - color blend, normal mode (like in Photoshop)


Im trying to create effect of blending 2 colors (actually image and color as overlay over image), like in photoshop "Color Overlay" with "Normal Blending" mode

Im using libgdx

This is what i have so far

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;

varying vec4 v_color;
varying vec2 v_texCoords;

void main()
{
   v_color = a_color;
   v_texCoords = a_texCoord0;
   gl_Position =  u_projTrans * a_position;
}

And fragment

#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif

varying LOWP vec4 v_color;
varying vec2 v_texCoords;

uniform sampler2D u_texture;

void main()
{
  vec4 curColor = texture2D(u_texture, v_texCoords);

  vec3 cA = curColor.rgb;//color A, background
  vec3 cB = v_color.rgb;//color B, overlay

  float aA = curColor.a;
  float aB = v_color.a;

  vec3 color = (cA * aA + cB * aB * (1 - aA)) / aA + aB * (1 - aA);
  float alpha = aA + aB * (1 - aA);
  gl_FragColor = vec4(color, alpha);
}

I used formula for color and alpha from wikipedia

When i use this shader, nothing happens, object which is drawn with this shader is transparent, invisible


Solution

  • gl_FragColor = texture2D(u_texture, v_texCoords); 
    gl_FragColor.rgb = mix(gl_FragColor.rgb, vec3(1.0), v_color.a);
    

    This is the solution, thanks to @Tenfour04