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);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345