如何结合2个简单着色器OpenGL?

时间:2017-11-01 21:15:16

标签: android opengl-es shader

所以我想要合并2个着色器。即,着色器1的输出(边缘检测)应该是着色器2的输入(模糊)。

这两种着色器完全独立工作。但是,我不知道如何组合它们,因为着色器1的输出是vec4,而着色器2需要使用纹理坐标和sampler2D。

着色器1(边缘检测)

precision highp float;
varying vec3 n;
varying vec2 uv;
uniform sampler2D tex;
precision mediump float;

void main() {
    /*
     * Edge Detection
     */
    vec3 irgb = texture2D(tex, uv).rgb;
    float ResS = 720.;
    float ResT = 720.;

    vec2 stp0 = vec2(1./ResS, 0.);
    vec2 st0p = vec2(0., 1./ResT);
    vec2 stpp = vec2(1./ResS, 1./ResT);
    vec2 stpm = vec2(1./ResS, -1./ResT);

    const vec3 W = vec3(0.2125, 0.7154, 0.0721);
    float i00 = dot(texture2D(tex, uv).rgb, W);
    float im1m1 = dot(texture2D(tex, uv-stpp).rgb, W);
    float ip1p1 = dot(texture2D(tex, uv+stpp).rgb, W);
    float im1p1 = dot(texture2D(tex, uv-stpm).rgb, W);
    float ip1m1 = dot(texture2D(tex, uv+stpm).rgb, W);
    float im10 = dot(texture2D(tex, uv-stp0).rgb, W);
    float ip10 = dot(texture2D(tex, uv+stp0).rgb, W);
    float i0m1 = dot(texture2D(tex, uv-st0p).rgb, W);
    float i0p1 = dot(texture2D(tex, uv+st0p).rgb, W);
    float h = -1.*im1p1 - 2.*i0p1 - 1.*ip1p1 + 1.*im1m1 + 2.*i0m1 + 1.*ip1m1;
    float v = -1.*im1m1 - 2.*im10 - 1.*im1p1 + 1.*ip1m1 + 2.*ip10 + 1.*ip1p1;

    float mag = length(vec2(h, v));
    vec3 target = vec3(mag, mag, mag);
    vec4 nonWhiteToBlack = vec4(mix(irgb, target, 1.0),1.);

    if (nonWhiteToBlack.r < 0.2 && nonWhiteToBlack.g < 0.2 && nonWhiteToBlack.b < 0.2) {
        nonWhiteToBlack = vec4(0,0,0,1);
    }

    gl_FragColor = vec4(nonWhiteToBlack);
}

着色器2(模糊)

    /*
     * blur
     */

    vec3 irgb = texture2D(tex, uv).rgb;
    float ResS = 720.;
    float ResT = 720.;

    vec2 stp0 = vec2(1./ResS, 0.);
    vec2 st0p = vec2(0., 1./ResT);
    vec2 stpp = vec2(1./ResS, 1./ResT);
    vec2 stpm = vec2(1./ResS, -1./ResT);

    vec3 i00 = texture2D(tex, uv).rgb;
    vec3 im1m1 = texture2D(tex, uv-stpp).rgb;
    vec3 ip1p1 = texture2D(tex, uv+stpp).rgb;
    vec3 im1p1 = texture2D(tex, uv-stpm).rgb;
    vec3 ip1m1 = texture2D(tex, uv+stpm).rgb;
    vec3 im10 = texture2D(tex, uv-stp0).rgb;
    vec3 ip10 = texture2D(tex, uv+stp0).rgb;
    vec3 i0m1 = texture2D(tex, uv-st0p).rgb;
    vec3 i0p1 = texture2D(tex, uv+st0p).rgb;

    vec3 target = vec3(0., 0., 0.);
    target += 1.*(im1m1+ip1m1+ip1p1+im1p1); 
    target += 2.*(im10+ip10+i0p1);
    target += 4.*(i00);
    target /= 16.;
    gl_FragColor = vec4(target, 1.);

任何帮助都会受到赞赏

0 个答案:

没有答案