利用OpenGL ES实现灰度滤镜及马赛克滤镜

2020-08-27  本文已影响0人  丸疯

实现效果

马赛克灰度.gif

灰度滤镜

precision highp float;
uniform sampler2D Texture;
varying vec2 TextureCoordsVarying;

const highp vec3 W = vec3(0.19757, 0.73846, 0.06397);

void main (void){
    vec4 mask = texture2D(Texture, TextureCoordsVarying);
    float luminance = dot(mask.rgb, W);
    gl_FragColor = vec4(vec3(luminance), 1.0);
}

dot(mask.rgb, W),点乘函数,参数为两个向量。得到常量,我们使用得到的常量构建一个三维向量,加上透明度,就构成的RGBA

颠倒滤镜

    vec4 color = texture2D(Texture, vec2(TextureCoordsVarying.x, 1.0 - TextureCoordsVarying.y));
    
    gl_FragColor = color;

矩形马赛克

    vec2 intXY = vec2(TextureCoordsVarying.x*TexSize.x, TextureCoordsVarying.y*TexSize.y);
    vec2 XYMosaic = vec2(floor(intXY.x/mosaicSize.x)*mosaicSize.x, floor(intXY.y/mosaicSize.y)*mosaicSize.y);
    vec2 UVMosaic = vec2(XYMosaic.x/TexSize.x, XYMosaic.y/TexSize.y);
    vec4 color = texture2D(Texture, UVMosaic);
    gl_FragColor = color;
上一篇下一篇

猜你喜欢

热点阅读