GLSL:以前输出的样本,不是texture2D

时间:2019-04-01 17:14:05

标签: glsl webgl

我试图编写一个自定义着色器,在该着色器中我首先使纹理模糊,然后运行sobel边缘查找。

我通过以下脚本让sobel正常运行

    vec4 colorSobel = texture2D(texture, uv);
    float bottomLeftIntensity = texture2D(texture, uv + vec2(-0.0015625, 0.0020833)).r;
    float topRightIntensity = texture2D(texture, uv + vec2(0.0015625, -0.0020833)).r;
    float topLeftIntensity = texture2D(texture, uv + vec2(-0.0015625, 0.0020833)).r;
    float bottomRightIntensity = texture2D(texture, uv + vec2(0.0015625, 0.0020833)).r;
    float leftIntensity = texture2D(texture, uv + vec2(-0.0015625, 0)).r;
    float rightIntensity = texture2D(texture, uv + vec2(0.0015625, 0)).r;
    float bottomIntensity = texture2D(texture, uv + vec2(0, 0.0020833)).r;
    float topIntensity = texture2D(texture, uv + vec2(0, -0.0020833)).r;
    float h = -secondary * topLeftIntensity - coef * topIntensity - secondary * topRightIntensity + secondary * bottomLeftIntensity + coef * bottomIntensity + secondary * bottomRightIntensity;
    float v = -secondary * bottomLeftIntensity - coef * leftIntensity - secondary * topLeftIntensity + secondary * bottomRightIntensity + coef * rightIntensity + secondary * topRightIntensity;

    float mag = length(vec2(h, v));
    //  alpha values removed atm
    if (mag < 0.5) {
        colorSobel.rgb *= (1.0 - 1.0);
        colorSobel.r += 0.0 * 1.0;
        colorSobel.g += 0.0 * 1.0;
        colorSobel.b += 0.0 * 1.0;
        colorSobel.rgb += 1.0 * mag;
    } else { 
        colorSobel.rgb *= (1.0 - 1.0);
        colorSobel.r += 0.0 * 1.0;
        colorSobel.g += 0.0 * 1.0;
        colorSobel.b += 0.0 * 1.0;
        colorSobel.rgb += 1.0 * mag;
    }
    gl_FragColor = colorSobel; 

但是我知道它可以通过texture2D对纹理进行采样来实现。

如果我要首先通过一个简单的脚本来操纵输出,例如这样可以减少颜色

  vec4 bg = texture2D(texture,uv);
  gl_FragColor = vec4(gb.rgb, 1.0);
  gl_FragColor.r = float(floor(gl_FragColor.r * 0.5 ) / 0.5);
  gl_FragColor.g = float(floor(gl_FragColor.g * 0.5 ) / 0.5);
  gl_FragColor.b = float(floor(gl_FragColor.b * 0.5 ) / 0.5);

输出仍然从纹理中采样,并且忽略第一个绘制。

有没有一种方法可以从颜色输出中进行采样而不是使用texture2D? 我问的原因是我是否在运行时根据用户交互方式链接着色器?

0 个答案:

没有答案