2015-08-28 78 views
1

我試圖創建混合​​2種顏色(實際上圖像和顏色作爲覆蓋圖像)的效果,就像在「正常混合」模式的Photoshop「顏色覆蓋」glsl着色器 - 顏色混合,正常模式(像在Photoshop中)

進出口使用libgdx

這是我迄今爲止

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; 
} 

和碎片

#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); 
} 

我用公式顏色和alpha從wikipedia

當我使用該着色器,沒有任何反應,其被繪製與該着色器對象是透明的,不可見的

回答

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

這是溶液,由於@ Tenfour04