OpenGL ES FragmentShader 常见滤镜

1.原图

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
void main () {
    gl_FragColor = texture2D(colorMap, varingCoord);
}

取出原始颜色

2.灰度-加权平均值法

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);
void main () {
    highp vec4 textureColor = texture2D(colorMap, varingCoord);
    float luminance = dot(textureColor.rgb, W);
    gl_FragColor = vec4(vec3(luminance), textureColor.a);
}

对颜色做处理,rgb分别乘以W

3.颠倒

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
void main () {
    vec4 color = texture2D(colorMap, vec2(varingCoord.x, 1.0 - varingCoord.y));
    gl_FragColor = color;
}

对坐标做处理

4.旋涡

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
const float uD = 80.0;
const float uR = 0.5;
const float PI = 3.14159265;

void main () {
    ivec2 ires = ivec2(512, 512);
    float Res = float(ires.s);
    
    vec2 st = varingCoord;
    float Radius = Res * uR;
    
    vec2 xy = Res * st;
    
    vec2 dxy = xy - vec2(Res/2., Res/2.);
    float r = length(dxy);
    
    float beta = atan(dxy.y, dxy.x) + radians(uD) * 2.0 * (-(r/Radius)*(r/Radius) + 1.0);//(1.0 - r/Radius);
    
    vec2 xy1 = xy;
    if(r<=Radius)
    {
        xy1 = Res/2. + r*vec2(cos(beta), sin(beta));
    }
    
    st = xy1/Res;
    
    vec3 irgb = texture2D(colorMap, st).rgb;
    
    gl_FragColor = vec4( irgb, 1.0 );
}

5.方形马赛克

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
const vec2 TexSize = vec2(400.0, 400.0);
const vec2 mosaicSize = vec2(8.0, 8.0);
void main () {
    vec2 intXY = vec2(varingCoord.x*TexSize.x, varingCoord.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(colorMap, UVMosaic);
    gl_FragColor = color;
}

6.浮雕

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);
const vec2 TexSize = vec2(100.0, 100.0);
const vec4 bkColor = vec4(0.5, 0.5, 0.5, 1.0);

void main () {
    vec2 tex = varingCoord;
    vec2 upLeftUV = vec2(tex.x-1.0/TexSize.x, tex.y-1.0/TexSize.y);
    vec4 curColor = texture2D(colorMap, varingCoord);
    vec4 upLeftColor = texture2D(colorMap, upLeftUV);
    vec4 delColor = curColor - upLeftColor;
    float luminance = dot(delColor.rgb, W);
    gl_FragColor = vec4(vec3(luminance), 0.0) + bkColor;
}

7.四分屏幕

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
void main () {
    vec2 uv = varingCoord;
    if (uv.x <= 0.5) {
        uv.x = uv.x * 2.0;
    } else {
        uv.x = (uv.x - 0.5) * 2.0;
    }
    if (uv.y <= 0.5) {
        uv.y = uv.y * 2.0;
    } else {
        uv.y = (uv.y - 0.5) * 2.0;
    }
    gl_FragColor = texture2D(colorMap, fract(uv));
}

8. 6分屏

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
void main () {
    vec2 uv = varingCoord;
    if (uv.x <= 1.0/3.0) {
        uv.x = uv.x * 3.0;
    } else {
        uv.x = (uv.x - 1.0/3.0) * 3.0;
    }
    if (uv.y <= 0.5) {
        uv.y = uv.y * 2.0;
    } else {
        uv.y = (uv.y - 0.5) * 2.0;
    }
    gl_FragColor = texture2D(colorMap, fract(uv));
}

9. 9分屏

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;

void main () {
    vec2 uv = varingCoord;
    if (uv.x <= 1.0/3.0) {
        uv.x = uv.x * 3.0;
    } else {
        uv.x = (uv.x - 1.0/3.0) * 3.0;
    }
    if (uv.y <= 1.0/3.0) {
        uv.y = uv.y * 3.0;
    } else {
        uv.y = (uv.y - 1.0/3.0) * 3.0;
    }
    gl_FragColor = texture2D(colorMap, fract(uv));
}

n分屏以此类推

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
float n = 4.0;
void main () {
    vec2 uv = varingCoord;
    if (uv.x <= 1.0/n) {
        uv.x = uv.x * n;
    } else {
        uv.x = (uv.x - 1.0/n) * n;
    }
    if (uv.y <= 1.0/n) {
        uv.y = uv.y * n;
    } else {
        uv.y = (uv.y - 1.0/n) * n;
    }
    gl_FragColor = texture2D(colorMap, fract(uv));
}

10.老照片

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
void main () {
    vec3 col = texture2D(colorMap, varingCoord).rgb;
    gl_FragColor.r = 0.393*col.r+0.769*col.g+0.189*col.b;
    gl_FragColor.g = 0.349*col.r+0.686*col.g+0.168*col.b;
    gl_FragColor.b = 0.272*col.r+0.534*col.g+0.131*col.b;
    gl_FragColor.a = 1.0;
}

11.冰冻

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
void main () {
    vec3 col = texture2D(colorMap,varingCoord).rgb;
    gl_FragColor.r = abs(col.r-col.g-col.b)*3.0/2.0;
    gl_FragColor.g = abs(col.g-col.b-col.r)*3.0/2.0;
    gl_FragColor.b = abs(col.b-col.r-col.g)*3.0/2.0;
    gl_FragColor.a = 1.0;
}

12.熔铸

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
void main () {
    vec3 col = texture2D(colorMap,varingCoord).rgb;
    gl_FragColor.r = col.r*0.5/(col.g+col.b);
    gl_FragColor.g = col.g*0.5/(col.r+col.b);
    gl_FragColor.b = col.b*0.5/(col.r+col.g);
    gl_FragColor.a = 1.0;
}

13.暗调

 precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
void main () {
    vec3 col = texture2D(colorMap,varingCoord).rgb;
    gl_FragColor.r = col.r*col.r;
    gl_FragColor.g = col.g*col.g;
    gl_FragColor.b = col.b*col.b;
    gl_FragColor.a = 1.0;
}

14.饱和度 -->GPUImageSaturationFilter

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
float saturation = 2.0;

// Values from "Graphics Shaders: Theory and Practice" by Bailey and Cunningham
const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);

void main()
{
    lowp vec4 textureColor = texture2D(colorMap, varingCoord);
    lowp float luminance = dot(textureColor.rgb, luminanceWeighting);
    lowp vec3 greyScaleColor = vec3(luminance);
    gl_FragColor = vec4(mix(greyScaleColor, textureColor.rgb, saturation), textureColor.w);
}

15.亮度 GPUImageBrightnessFilter

varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
const float brightness = 0.5;
void main() {
    lowp vec4 textureColor = texture2D(colorMap, varingCoord);
    gl_FragColor = vec4((textureColor.rgb + vec3(brightness)), textureColor.w);
}

16.对比度 GPUImageContrastFilter

precision highp float;
varying lowp vec2 varingCoord;
uniform sampler2D colorMap;
const float contrast = 0.5;
void main() {
    vec4 textureColor = texture2D(colorMap, varingCoord);
    gl_FragColor = vec4(((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5)), textureColor.w);
}

17.饱和度

varying lowp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform lowp float saturation;
const lowp vec3 warmFilter = vec3(0.93, 0.54, 0.0);
const mediump mat3 RGBtoYIQ = mat3(0.299, 0.587, 0.114, 0.596, -0.274, -0.322, 0.212, -0.523, 0.311);
const mediump mat3 YIQtoRGB = mat3(1.0, 0.956, 0.621, 1.0, -0.272, -0.647, 1.0, -1.105, 1.702);
const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
void main()
{
    lowp vec4 source = texture2D(inputImageTexture, textureCoordinate);
    lowp float luminance = dot(source.rgb, luminanceWeighting);
    lowp vec3 greyScaleColor = vec3(luminance);
    
    gl_FragColor = vec4(mix(greyScaleColor, source.rgb, saturation), source.w);
}

18.色温

varying lowp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform lowp float temperature;
const lowp vec3 warmFilter = vec3(0.93, 0.54, 0.0);
const mediump mat3 RGBtoYIQ = mat3(0.299, 0.587, 0.114, 0.596, -0.274, -0.322, 0.212, -0.523, 0.311);
const mediump mat3 YIQtoRGB = mat3(1.0, 0.956, 0.621, 1.0, -0.272, -0.647, 1.0, -1.105, 1.702);
const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
void main()
{
    lowp vec4 source = texture2D(inputImageTexture, textureCoordinate);
    
    mediump vec3 yiq = RGBtoYIQ * source.rgb;
    yiq.b = clamp(yiq.b, -0.5226, 0.5226);
    lowp vec3 rgb = YIQtoRGB * yiq;
    
    lowp vec3 processed = vec3(
                               (rgb.r < 0.5 ? (2.0 * rgb.r * warmFilter.r) : (1.0 - 2.0 * (1.0 - rgb.r) * (1.0 - warmFilter.r))),                               (rgb.g < 0.5 ? (2.0 * rgb.g * warmFilter.g) : (1.0 - 2.0 * (1.0 - rgb.g) * (1.0 - warmFilter.g))),
                               (rgb.b < 0.5 ? (2.0 * rgb.b * warmFilter.b) : (1.0 - 2.0 * (1.0 - rgb.b) * (1.0 - warmFilter.b))));
    
    gl_FragColor = vec4(mix(rgb, processed, temperature), source.a);
}

你可能感兴趣的:(OpenGL ES FragmentShader 常见滤镜)