image.png

前情提要

这篇滤镜效果的实现是在上一篇 分屏滤镜 的基础上来进行实现的,同样的前提是可以利用GLSL加载一张正常的图片。

缩放滤镜

缩放滤镜实际上基本的原理是可以通过修改顶点坐标和纹理坐标的对应关系来实现放大缩小效果。

这个放大缩小的实现其实可以在顶点着色器中实现,也可以在片元着色器中实现。(注意:在运行时,着色器代码中最好不要有中文)
顶点着色器代码:

  1. // 顶点坐标
  2. attribute vec4 Position;
  3. // 纹理坐标
  4. attribute vec2 TextureCoords;
  5. varying vec2 TextureCoordsVarying;
  6. // 时间(通过uniform传入一个时间Time)
  7. uniform float Time;
  8. const float PI = 3.1415926;
  9. void main (void) {
  10. // 一次缩放效果时长
  11. float duration = 0.4;
  12. // 最大缩放幅度
  13. float maxAmplitude = 0.3;
  14. // 表示时间周期
  15. float time = mod(Time, duration);
  16. // 缩放幅度 [1.0,1.3]
  17. float amplitude = 1.0 + maxAmplitude * abs(sin(time * (PI / duration)));
  18. // 顶点坐标x/y 分别乘以放大系数[1.0,1.3]
  19. gl_Position = vec4(Position.x * amplitude, Position.y * amplitude, Position.zw);
  20. // 纹理坐标
  21. TextureCoordsVarying = TextureCoords;
  22. }

实现效果:
7、 缩放,出窍,抖动,闪白,毛刺、幻觉滤镜 - 图2

出窍滤镜

灵魂出窍滤镜的原理: 是两个层的叠加,并且上面的那层随着时间的推移,会逐渐放大且不透明度逐渐降低。这里也⽤到了放大的效果(基于缩放的原理),我们这次用片元着⾊器来实现该效果。
片元着色器代码:

  1. precision highp float;
  2. // 纹理采样器
  3. uniform sampler2D Texture;
  4. // 纹理坐标
  5. varying vec2 TextureCoordsVarying;
  6. // 时间(通过uniform传入一个时间Time)
  7. uniform float Time;
  8. void main (void) {
  9. // 一次灵魂出窍效果的时长 1.0
  10. float duration = 1.0;
  11. // 透明度上限
  12. float maxAlpha = 0.5;
  13. // 放大图片上限
  14. float maxScale = 1.8;
  15. // 进度值[0,1]
  16. float progress = mod(Time, duration) / duration; // 0~1
  17. // 透明度范围[0,0.5]
  18. float alpha = maxAlpha * (1.0 - progress);
  19. // 缩放比例[1.0,1.8]
  20. float scale = 1.0 + (maxScale - 1.0) * progress;
  21. // 放大纹理坐标
  22. // 根据放大比例,得到放大纹理坐标 [0,0],[0,1],[1,1],[1,0]
  23. float weakX = 0.5 + (TextureCoordsVarying.x - 0.5) / scale;
  24. float weakY = 0.5 + (TextureCoordsVarying.y - 0.5) / scale;
  25. // 放大纹理坐标
  26. vec2 weakTextureCoords = vec2(weakX, weakY);
  27. // 获取对应放大纹理坐标下的纹素(颜色值rgba)
  28. vec4 weakMask = texture2D(Texture, weakTextureCoords);
  29. // 原始的纹理坐标下的纹素(颜色值rgba)
  30. vec4 mask = texture2D(Texture, TextureCoordsVarying);
  31. // 颜色混合 默认颜色混合方程式 = mask * (1.0-alpha) + weakMask * alpha;
  32. gl_FragColor = mask * (1.0 - alpha) + weakMask * alpha;
  33. }

实现效果:
7、 缩放,出窍,抖动,闪白,毛刺、幻觉滤镜 - 图3

可以看到这这个效果中,下面的那一层根本就没有变化,只是上面的一层做了放大,以此来造成视觉上的偏差。

抖动滤镜

抖动的过程中也是基于缩放的原理,而且它的颜色值产生一定的偏差。
抖动效果: 颜⾊偏移 + 微弱的放大效果

片元着色代码:

  1. precision highp float;
  2. // 纹理
  3. uniform sampler2D Texture;
  4. // 纹理坐标
  5. varying vec2 TextureCoordsVarying;
  6. // 时间(通过uniform传入一个时间Time)
  7. uniform float Time;
  8. void main (void) {
  9. // 一次抖动滤镜的时长
  10. float duration = 1.0;
  11. // 放大图片上限
  12. float maxScale = 1.2;
  13. // 颜色偏移步长
  14. float offset = 0.02;
  15. // 进度[0,1]
  16. float progress = mod(Time, duration) / duration;
  17. // 颜色偏移值范围[0,0.02]
  18. vec2 offsetCoords = vec2(offset, offset) * progress;
  19. // 缩放范围[1.0-1.2];
  20. float scale = 1.0 + (maxScale - 1.0) * progress;
  21. // 放大纹理坐标.
  22. vec2 ScaleTextureCoords = vec2(0.5, 0.5) + (TextureCoordsVarying - vec2(0.5, 0.5)) / scale;
  23. // 获取3组颜色rgb
  24. // 原始颜色+offsetCoords
  25. vec4 maskR = texture2D(Texture, ScaleTextureCoords + offsetCoords);
  26. // 原始颜色-offsetCoords
  27. vec4 maskB = texture2D(Texture, ScaleTextureCoords - offsetCoords);
  28. // 原始颜色
  29. vec4 mask = texture2D(Texture, ScaleTextureCoords);
  30. // 从3组来获取颜色:
  31. // maskR.r,mask.g,maskB.b 注意这3种颜色取值可以打乱或者随意发挥.只是效果会有不一样.
  32. // mask.a 获取原图的透明度
  33. gl_FragColor = vec4(mask.r, maskR.g, maskB.b, mask.a);
  34. }

实现效果:
7、 缩放,出窍,抖动,闪白,毛刺、幻觉滤镜 - 图4

闪白滤镜

闪白滤镜的原理: 在上层添加⽩色图层 ,⽩色图层的透明度随着时间的变化而变化。

片元着色器代码:

  1. precision highp float;
  2. // 纹理采样器
  3. uniform sampler2D Texture;
  4. // 纹理坐标
  5. varying vec2 TextureCoordsVarying;
  6. // 时间(通过uniform传入一个时间Time)
  7. uniform float Time;
  8. void main (void) {
  9. // 一次闪白滤镜的时长
  10. float duration = 0.5;
  11. // 表示时间周期[0.0,0.5]
  12. float time = mod(Time, duration);
  13. // 白色颜色遮罩层
  14. vec4 whiteMask = vec4(1.0, 1.0, 1.0, 1.0);
  15. // 振幅: (0.0,1.0)
  16. float amplitude = abs(sin(time * (PI / duration)));
  17. // 纹理坐标对应的纹素(RGBA)
  18. vec4 mask = texture2D(Texture, TextureCoordsVarying);
  19. // 利用混合方程式; 白色图层 + 原始纹理图片颜色 来进行混合
  20. gl_FragColor = mask * (1.0 - amplitude) + whiteMask * amplitude;
  21. }

实现效果:
7、 缩放,出窍,抖动,闪白,毛刺、幻觉滤镜 - 图5

毛刺滤镜

⽑刺滤镜的原理: 撕裂 + 微弱的颜⾊偏移。

片元着色器代码:

  1. precision highp float;
  2. // 纹理
  3. uniform sampler2D Texture;
  4. // 纹理坐标
  5. varying vec2 TextureCoordsVarying;
  6. // 时间(通过uniform传入一个时间Time)
  7. uniform float Time;
  8. // 随机数
  9. float rand(float n) {
  10. //fract(x),返回x的小数部分数据
  11. return fract(sin(n) * 43758.5453123);
  12. }
  13. void main (void) {
  14. // 最大抖动
  15. float maxJitter = 0.06;
  16. // 一次毛刺滤镜的时长
  17. float duration = 0.5;
  18. // 红色颜色偏移量
  19. float colorROffset = 0.01;
  20. //绿色颜色偏移量
  21. float colorGOffset = -0.02;
  22. // 蓝色颜色偏移量
  23. float colorBOffset = -0.035;
  24. // 时间周期[0.0,1.0];
  25. float time = mod(Time, duration * 2.0);
  26. // 振幅:[0,1];
  27. float amplitude = max(sin(time * (PI / duration)), 0.0);
  28. // 像素随机偏移[-1,1]
  29. float jitter = rand(TextureCoordsVarying.y) * 2.0 - 1.0; // -1~1
  30. // 是否要做偏移.
  31. bool needOffset = abs(jitter) < maxJitter * amplitude;
  32. // 获取纹理X值.根据needOffset,来计算它X撕裂.
  33. // needOffset = YES,撕裂较大;
  34. // needOffset = NO,撕裂较小.
  35. float textureX = TextureCoordsVarying.x + (needOffset ? jitter : (jitter * amplitude * 0.006));
  36. // 撕裂后的纹理坐标x,y
  37. vec2 textureCoords = vec2(textureX, TextureCoordsVarying.y);
  38. // 颜色偏移3组颜色
  39. // 根据撕裂后获取的纹理颜色值
  40. vec4 mask = texture2D(Texture, textureCoords);
  41. // 撕裂后的纹理颜色偏移
  42. vec4 maskR = texture2D(Texture, textureCoords + vec2(colorROffset * amplitude, 0.0));
  43. vec4 maskG = texture2D(Texture, textureCoords + vec2(colorGOffset * amplitude, 0.0));
  44. vec4 maskB = texture2D(Texture, textureCoords + vec2(colorBOffset * amplitude, 0.0));
  45. // 颜色部分发生撕裂.
  46. gl_FragColor = vec4(maskR.r, maskG.g, maskB.b, mask.a);
  47. }

实现效果:
7、 缩放,出窍,抖动,闪白,毛刺、幻觉滤镜 - 图6

毛刺效果具体的思路是:我们让每一行像素随机偏移 -1 ~ 1 的距离(这里的 -1 ~ 1 是对于纹理坐标来说的),但是如果整个 画面都偏移⽐较大的值,那我们可能都看不出原来图像的样子。所以,设定⼀个阈值,⼩于这个阈值才进行偏移,超过这个阈值则乘上⼀个缩小系数。
则最终呈现的效果是:绝⼤部分的行都会进行微小的偏移,只有少量的行会进行较⼤偏移。

幻觉滤镜

⽑刺滤镜的原理:残影和颜⾊色偏移的叠加

片元着色器代码:

  1. precision highp float;
  2. // 纹理采样器
  3. uniform sampler2D Texture;
  4. // 纹理坐标
  5. varying vec2 TextureCoordsVarying;
  6. // 时间戳
  7. uniform float Time;
  8. // PI常量
  9. const float PI = 3.1415926;
  10. // 一次幻觉滤镜的时长
  11. const float duration = 2.0;
  12. // 这个函数可以计算出,在某个时刻图片的具体位置。通过它我们可以每经过一段时间,去生成一个新的层
  13. vec4 getMask(float time, vec2 textureCoords, float padding) {
  14. // 圆周坐标
  15. vec2 translation = vec2(sin(time * (PI * 2.0 / duration)),
  16. cos(time * (PI * 2.0 / duration)));
  17. // 纹理坐标 = 纹理坐标+偏移量*圆周坐标
  18. vec2 translationTextureCoords = textureCoords + padding * translation;
  19. // 根据这个坐标获取新图层的坐标
  20. vec4 mask = texture2D(Texture, translationTextureCoords);
  21. return mask;
  22. }
  23. // 这个函数可以计算出,某个时刻创建的层,在当前时刻的透明度
  24. float maskAlphaProgress(float currentTime, float hideTime, float startTime) {
  25. float time = mod(duration + currentTime - startTime, duration);
  26. return min(time, hideTime);
  27. }
  28. void main (void) {
  29. // 表示将传入的时间转换到一个周期内,即time的范围是 0~2.0
  30. float time = mod(Time, duration);
  31. // 放大倍数
  32. float scale = 1.2;
  33. // 偏移量
  34. float padding = 0.5 * (1.0 - 1.0 / scale);
  35. // 放大后的纹理坐标
  36. vec2 textureCoords = vec2(0.5, 0.5) + (TextureCoordsVarying - vec2(0.5, 0.5)) / scale;
  37. // 隐藏时间
  38. float hideTime = 0.9;
  39. // 时间间隔
  40. float timeGap = 0.2;
  41. // 注意:只保留了红色的透明的通道值,因为幻觉效果残留红色
  42. // 新图层的-R色透明度 0.5
  43. float maxAlphaR = 0.5; // max R
  44. // 新图层的-G色透明度 0.05
  45. float maxAlphaG = 0.05; // max G
  46. // 新图层的-B色透明度 0.05
  47. float maxAlphaB = 0.05; // max B
  48. // 最终图层颜色
  49. vec4 mask = getMask(time, textureCoords, padding);
  50. float alphaR = 1.0; // R
  51. float alphaG = 1.0; // G
  52. float alphaB = 1.0; // B
  53. vec4 resultMask = vec4(0, 0, 0, 0);
  54. for (float f = 0.0; f < duration; f += timeGap) {
  55. // 获取到0~2.0秒内所获取的运动后的纹理坐标
  56. float tmpTime = f;
  57. // 某个时刻创建的层,在当前时刻的红绿蓝的透明度
  58. vec4 tmpMask = getMask(tmpTime, textureCoords, padding);
  59. //
  60. float tmpAlphaR = maxAlphaR - maxAlphaR * maskAlphaProgress(time, hideTime, tmpTime) / hideTime;
  61. float tmpAlphaG = maxAlphaG - maxAlphaG * maskAlphaProgress(time, hideTime, tmpTime) / hideTime;
  62. float tmpAlphaB = maxAlphaB - maxAlphaB * maskAlphaProgress(time, hideTime, tmpTime) / hideTime;
  63. // 累积每一层每个通道乘以透明度颜色通道
  64. resultMask += vec4(tmpMask.r * tmpAlphaR,
  65. tmpMask.g * tmpAlphaG,
  66. tmpMask.b * tmpAlphaB,
  67. 1.0);
  68. // 透明度递减
  69. alphaR -= tmpAlphaR;
  70. alphaG -= tmpAlphaG;
  71. alphaB -= tmpAlphaB;
  72. }
  73. // 最终颜色 += 红绿蓝*透明度
  74. resultMask += vec4(mask.r * alphaR, mask.g * alphaG, mask.b * alphaB, 1.0);
  75. // 将最终颜色填充到像素点里
  76. gl_FragColor = resultMask;
  77. }

实现效果:
7、 缩放,出窍,抖动,闪白,毛刺、幻觉滤镜 - 图7

残影的效果: 是在移动的过程中,每经过⼀一段时间间隔,根据当前的位置去创建⼀一个新层,并且新层的不不透明度随着时间逐 渐减弱。于是在⼀一个移动周期内,可以看到很多透明度不不同的层叠加在⼀一起,从⽽而形成残影的效果。残影,让图⽚片随着时间 做圆周运动
颜⾊色偏移: 物体移动的过程是蓝⾊色在前⾯面,红⾊色在后⾯面。所以整个过程可以理理解成:在移动的过程中,每间隔⼀一段时间,遗 失了了⼀一部分红⾊色通道的值在原来的位置,并且这部分红⾊色通道的值,随着时间偏移,会逐渐恢复.