0

我正在尝试创建一个模糊的着色器,我已经做到了。但是,有一个问题。纹理周围有一个黑色背景,当着色器运行时,它会从该背景中获取颜色,最终使我的整个图像变暗(但它仍然很好地模糊了图像,就好像它也在降低亮度一样) . 只是为了确定这就是我的想法,我将背景设置为白色,果然它使我的图像更暗淡。我真的不知道如何防止这种情况,我想它会在外面抓住它们,因为模糊大小已经到达纹理边界之外,有没有办法防止纹理查找发生在我正在处理的纹理之外?

碎片:

precision mediump float;

uniform sampler2D CC_Texture0;  
uniform float u_time;

const lowp int SAMPLES = 9;
varying vec2 v_texCoord;        
varying highp vec2 transCord[SAMPLES];

void main()
{
      vec4 sum = vec4(0.0);

    sum += texture2D(CC_Texture0, transCord[0]) * 0.05;
    sum += texture2D(CC_Texture0, transCord[1]) * 0.09;
    sum += texture2D(CC_Texture0, transCord[2]) * 0.12;
    sum += texture2D(CC_Texture0, transCord[3]) * 0.15;
    sum += texture2D(CC_Texture0, transCord[4]) * 0.16;
    sum += texture2D(CC_Texture0, transCord[5]) * 0.15;
    sum += texture2D(CC_Texture0, transCord[6]) * 0.12;
    sum += texture2D(CC_Texture0, transCord[7]) * 0.09;
    sum += texture2D(CC_Texture0, transCord[8]) * 0.05;

      gl_FragColor = sum;
}

顶点着色器

precision mediump float;

attribute vec4 a_position;
attribute vec2 a_texCoord;

const lowp int SAMPLES = 9;

uniform highp float texelWidthOffset;
uniform highp float texelHeightOffset;
uniform highp float blurSize;
uniform float u_time;

varying vec2 v_texCoord;        
varying highp vec2 transCord[SAMPLES];

void main()
{
      gl_Position = CC_MVPMatrix * a_position;
    v_texCoord = a_texCoord;    
    highp vec2 offset = vec2(texelWidthOffset, texelHeightOffset) * (u_time * blurSize);

    transCord[0] = a_texCoord - offset * 4.0;
    transCord[1] = a_texCoord - offset * 3.0;
    transCord[2] = a_texCoord - offset * 2.0;
    transCord[3] = a_texCoord - offset * 1.0;
    transCord[4] = a_texCoord;
    transCord[5] = a_texCoord + offset * 1.0;
    transCord[6] = a_texCoord + offset * 2.0;
    transCord[7] = a_texCoord + offset * 3.0;
    transCord[8] = a_texCoord + offset * 4.0;
}

更新:这似乎只是在渲染到 CCRenderTexture 时出现的问题。我不确定为什么。

4

1 回答 1

0

将 S 和 T 中纹理的环绕模式设置为 GL_CLAMP_TO_EDGE - 这将意味着超出范围的 UV 坐标值返回更合理的结果

于 2013-04-20T18:25:56.957 回答