让我以我对 GLSL 非常陌生的事实作为序言。
我正在尝试使用着色器在插槽卷轴符号旋转时添加模糊效果。我有一个正在运行的模糊效果,为了简单起见,我将其注释掉并隔离了这个问题。
顶点着色器:
//Vertex Shader
attribute vec4 position;
attribute vec4 inColor;
attribute vec2 inUV;
//To be passed to the Fragment Shader "BlurShader.fsh"
varying vec4 colorVarying;
varying vec2 uvOut;
void main()
{
colorVarying = inColor;
gl_Position = position;
uvOut = inUV;
}
片段着色器:
//Fragment Shader
uniform sampler2D tex0;
//Passed from Vertex Shader "BlurShader.vsh"
varying vec4 colorVarying; //ISSUE: this is always solid white
varying vec2 uvOut;
//This is the intensity of the blur. Higher is a larger blur
// 25 is a good starting point
// Could possibly make it a varible to be passed in to control directly from Lua
#define INTENSITY 25.0
vec4 BlurVertical(vec2 size, vec2 uv, float radius) {
if (radius >= 1.0)
{
vec4 C = vec4(0.0);
float height = 1.0 / size.y;
float divisor = 0.0;
for (float y = -radius; y <= radius; y++)
{
C += texture(tex0, uv + vec2(0.0, y * height));
divisor++;
}
return vec4(C.r / divisor, C.g / divisor, C.b / divisor, 1.0);
}
return texture2D(tex0, uv);
}
void main()
{
//Apply blur to final output
//gl_FragColor = BlurVertical(textureSize(tex0, 0), uvOut, INTENSITY);
//gl_FragColor = texture2D(tex0, uvOut) * colorVarying; SAME RESULT
gl_FragColor = texture2D(tex0, uvOut);
}
我的问题是着色器会导致奇怪的不需要的灰度效果和透明度损失。
我对可能导致此问题的原因感到非常困惑,而且我似乎找不到任何有类似问题的人。
最佳答案
找到解决方案了!我使用的纹理是用 YUVA 编码的,所以实际上我需要采样 4 个纹理,然后转换为单个 RGBA。
uniform sampler2D tex[4];
vec4 YUVAtoRGBA()
{
mat4 xForm = mat4(
1.164, 1.164, 1.164, 0.0,
0.0, -0.391, 2.018, 0.0,
1.596, -0.813, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0);
vec4 yuva = vec4(
texture2D(tex[0], (uvOut.xy / uvOut.z).x - 0.0625,
texture2D(tex[1], (uvOut.xy / uvOut.z).x - 0.5,
texture2D(tex[2], (uvOut.xy / uvOut.z).x - 0.5,
texture2D(tex[3], (uvOut.xy / uvOut.z).x);
return colorVarying * (xForm * yuva);
}
关于GLSL 着色器不需要的灰度效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54449158/