GLSL Shader不需要的灰度效果

时间:2019-01-30 20:39:03

标签: glsl shader fragment-shader vertex-shader pixel-shader

让我以我对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);
}

我的问题是,着色器会导致奇怪的不需要的灰度效果并失去透明度。

奇怪的灰度效果

外观如何

我对可能造成这种情况的原因非常困惑,而且似乎找不到任何存在类似问题的人。

1 个答案:

答案 0 :(得分:3)

找到了解决方案!我使用的纹理是用YUVA编码的,因此实际上我需要采样4种纹理,然后将其转换为单个RGBA。

X-