您的位置:首页 > 其它

给出高斯模糊的shader实现

2016-04-11 11:11 218 查看
主要是片元着色器的实现

precision mediump float;

varying vec2 vTexCoord;

uniform sampler2D u_texture;

const float resolution=1024.0;
uniform float radius;//radius可为1.4

uniform vec2 dir;//若为x模糊,可传入(1.0,0.0)  y模糊  (0.0,1.0)


void main() {

    //this will be our RGBA sum

    vec4 sum = vec4(0.0);

    

    //our original texcoord for this fragment

    vec2 tc = vTexCoord;

    

    //the amount to blur, i.e. how far off center to sample from

    //1.0 -> blur by one pixel

    //2.0 -> blur by two pixels, etc.

    float blur = radius/resolution;

    

    //the direction of our blur

    //(1.0, 0.0) -> x-axis blur

    //(0.0, 1.0) -> y-axis blur

    float hstep = dir.x;

    float vstep = dir.y;

    

    

    //apply blurring, using a 9-tap filter with predefined gaussian weights

    

    sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;

    sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;

    sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;

    sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;

    

    sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.2270270270;

    

    sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;

    sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;

    sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;

    sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;

    vec4 cc= texture2D(u_texture,vTexCoord );

    //discard alpha for our simple demo, multiply by vertex color and return

    gl_FragColor =vec4(sum.rgb, cc.a);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: