美文网首页
灰度&马赛克滤镜

灰度&马赛克滤镜

作者: E术家 | 来源:发表于2020-08-12 17:21 被阅读0次

    vertShader

    attribute vec4 Position;
    attribute vec2 TextureCoords;
    varying vec2 TextureCoordsVarying;
    
    void main (void) {
        gl_Position = Position;
        TextureCoordsVarying = TextureCoords;
    }
    

    灰度效果fragShader

    precision highp float;
    uniform sampler2D Texture;
    varying vec2 TextureCoordsVarying;
    const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);
    
    void main (void) {
        
        vec4 mask = texture2D(Texture, TextureCoordsVarying);
        float luminance = dot(mask.rgb, W);
        gl_FragColor = vec4(vec3(luminance), 1.0);
    }
    

    正方形马赛克fragShader

    varying vec2 TextureCoordsVarying;
    uniform sampler2D Texture;
    const vec2 TexSize = vec2(400.0, 400.0);
    const vec2 mosaicSize = vec2(16.0, 16.0);
    
    void main()
    {
        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;
    }
    

    六边形马赛克fragShader

    precision highp float;
    uniform sampler2D Texture;
    varying vec2 TextureCoordsVarying;
    
    const float mosaicSize = 0.03;
    
    void main (void)
    {
        float length = mosaicSize;
        
        float TR = 0.866025;
        float TB = 1.5;
        
        float x = TextureCoordsVarying.x;
        float y = TextureCoordsVarying.y;
        
        int wx = int(x / TB / length);
        int wy = int(y / TR / length);
        vec2 v1, v2, vn;
        
        if (wx/2 * 2 == wx) {
            if (wy/2 * 2 == wy) {
                //(0,0),(1,1)
                v1 = vec2(length * 1.5 * float(wx), length * TR * float(wy));
                v2 = vec2(length * 1.5 * float(wx + 1), length * TR * float(wy + 1));
            } else {
                //(0,1),(1,0)
                v1 = vec2(length * 1.5 * float(wx), length * TR * float(wy + 1));
                v2 = vec2(length * 1.5 * float(wx + 1), length * TR * float(wy));
            }
        }else {
            if (wy/2 * 2 == wy) {
                //(0,1),(1,0)
                v1 = vec2(length * 1.5 * float(wx), length * TR * float(wy + 1));
                v2 = vec2(length * 1.5 * float(wx + 1), length * TR * float(wy));
            } else {
                //(0,0),(1,1)
                v1 = vec2(length * 1.5 * float(wx), length * TR * float(wy));
                v2 = vec2(length * 1.5 * float(wx + 1), length * TR * float(wy + 1));
            }
        }
        
        float s1 = sqrt(pow(v1.x - x, 2.0) + pow(v1.y - y, 2.0));
        float s2 = sqrt(pow(v2.x - x, 2.0) + pow(v2.y - y, 2.0));
        if (s1 < s2) {
            vn = v1;
        } else {
            vn = v2;
        }
        vec4 color = texture2D(Texture, vn);
        
        gl_FragColor = color;
        
    }
    

    三角型马赛克fragShader

    precision highp float;
    uniform sampler2D Texture;
    varying vec2 TextureCoordsVarying;
    
    float mosaicSize = 0.03;
    
    void main (void){
        
        const float TR = 0.866025;
        const float PI6 = 0.523599;
        
        float x = TextureCoordsVarying.x;
        float y = TextureCoordsVarying.y;
        
     
        int wx = int(x/(1.5 * mosaicSize));
        int wy = int(y/(TR * mosaicSize));
        
        vec2 v1, v2, vn;
        
        if (wx / 2 * 2 == wx) {
            if (wy/2 * 2 == wy) {
                v1 = vec2(mosaicSize * 1.5 * float(wx), mosaicSize * TR * float(wy));
                v2 = vec2(mosaicSize * 1.5 * float(wx + 1), mosaicSize * TR * float(wy + 1));
            } else {
                v1 = vec2(mosaicSize * 1.5 * float(wx), mosaicSize * TR * float(wy + 1));
                v2 = vec2(mosaicSize * 1.5 * float(wx + 1), mosaicSize * TR * float(wy));
            }
        } else {
            if (wy/2 * 2 == wy) {
                v1 = vec2(mosaicSize * 1.5 * float(wx), mosaicSize * TR * float(wy + 1));
                v2 = vec2(mosaicSize * 1.5 * float(wx+1), mosaicSize * TR * float(wy));
            } else {
                v1 = vec2(mosaicSize * 1.5 * float(wx), mosaicSize * TR * float(wy));
                v2 = vec2(mosaicSize * 1.5 * float(wx + 1), mosaicSize * TR * float(wy+1));
            }
        }
    
        float s1 = sqrt(pow(v1.x - x, 2.0) + pow(v1.y - y, 2.0));
        float s2 = sqrt(pow(v2.x - x, 2.0) + pow(v2.y - y, 2.0));
    
        if (s1 < s2) {
            vn = v1;
        } else {
            vn = v2;
        }
        
        vec4 mid = texture2D(Texture, vn);
        float a = atan((x - vn.x)/(y - vn.y));
    
        vec2 area1 = vec2(vn.x, vn.y - mosaicSize * TR / 2.0);
        vec2 area2 = vec2(vn.x + mosaicSize / 2.0, vn.y - mosaicSize * TR / 2.0);
        vec2 area3 = vec2(vn.x + mosaicSize / 2.0, vn.y + mosaicSize * TR / 2.0);
        vec2 area4 = vec2(vn.x, vn.y + mosaicSize * TR / 2.0);
        vec2 area5 = vec2(vn.x - mosaicSize / 2.0, vn.y + mosaicSize * TR / 2.0);
        vec2 area6 = vec2(vn.x - mosaicSize / 2.0, vn.y - mosaicSize * TR / 2.0);
      
        if (a >= PI6 && a < PI6 * 3.0) {
            vn = area1;
        } else if (a >= PI6 * 3.0 && a < PI6 * 5.0) {
            vn = area2;
        } else if ((a >= PI6 * 5.0 && a <= PI6 * 6.0)|| (a<-PI6 * 5.0 && a>-PI6*6.0)) {
            vn = area3;
        } else if (a < -PI6 * 3.0 && a >= -PI6 * 5.0) {
            vn = area4;
        } else if(a <= -PI6 && a> -PI6 * 3.0) {
            vn = area5;
        } else if (a > -PI6 && a < PI6)
        {
            vn = area6;
        }
        
        vec4 color = texture2D(Texture, vn);
        gl_FragColor = color;
    }
    

    相关文章

      网友评论

          本文标题:灰度&马赛克滤镜

          本文链接:https://www.haomeiwen.com/subject/ebdmdktx.html