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

孤街浪徒 提交于 2019-12-11 08:35:40

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/32275318/glsl-shader-color-blend-normal-mode-like-in-photoshop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!