如何修复此着色器以正确隐藏 alpha 像素?
How to fix this shader to properly hide alpha pixels?
我正在开发一个基本上为 2D 图像创建阴影投射效果的着色器,几乎一切正常,除非我尝试更改阴影的颜色,默认情况下颜色为黑色并且隐藏 alpha 像素,但是当我调整颜色 (modColor
) 时,alpha 像素会充满亮色
这是着色器代码
顶点
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec2 texCoord;
uniform vec4 modColor;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main() {
vColor = modColor;
vTexCoord = texCoord;
gl_Position = modelViewProjection * position;
}
片段
precision highp float;
uniform lowp sampler2D texture;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
uniform float shadowAlpha;
void main() {
lowp vec4 bl = texture2D( texture, vTexCoord ) * vec4(0,0,0,1);
lowp vec4 col = vec4(bl.rgb + vColor.rgb, bl.a * vColor.a);
float useAlpha = shadowAlpha * col.a * (1.0 - vTexCoord.y);
gl_FragColor = vec4(col.rgb, useAlpha);
}
由于纹理的 alpha 通道 (bl.a
) 在没有阴影的区域为 0,在有阴影的区域为 1,您可以将片段的颜色通道乘以 bl.a
:
gl_FragColor = vec4(col.rgb, useAlpha);
gl_FragColor = vec4(col.rgb * bl.a, useAlpha);
我正在开发一个基本上为 2D 图像创建阴影投射效果的着色器,几乎一切正常,除非我尝试更改阴影的颜色,默认情况下颜色为黑色并且隐藏 alpha 像素,但是当我调整颜色 (modColor
) 时,alpha 像素会充满亮色
这是着色器代码
顶点
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec2 texCoord;
uniform vec4 modColor;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main() {
vColor = modColor;
vTexCoord = texCoord;
gl_Position = modelViewProjection * position;
}
片段
precision highp float;
uniform lowp sampler2D texture;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
uniform float shadowAlpha;
void main() {
lowp vec4 bl = texture2D( texture, vTexCoord ) * vec4(0,0,0,1);
lowp vec4 col = vec4(bl.rgb + vColor.rgb, bl.a * vColor.a);
float useAlpha = shadowAlpha * col.a * (1.0 - vTexCoord.y);
gl_FragColor = vec4(col.rgb, useAlpha);
}
由于纹理的 alpha 通道 (bl.a
) 在没有阴影的区域为 0,在有阴影的区域为 1,您可以将片段的颜色通道乘以 bl.a
:
gl_FragColor = vec4(col.rgb, useAlpha);
gl_FragColor = vec4(col.rgb * bl.a, useAlpha);