这篇滤镜效果的实现是在上一篇分屏滤镜的基础上来进行实现的,一样的前提是能够利用GLSL加载一张正常的图片。bash
缩放滤镜实际上基本的原理是能够经过修改顶点坐标和纹理坐标的对应关系来实现放大缩小效果。ui
这个放大缩小的实现其实能够在顶点着色器中实现,也能够在片元着色器中实现。(注意:在运行时,着色器代码中最好不要有中文) 顶点着色器代码:spa
// 顶点坐标
attribute vec4 Position;
// 纹理坐标
attribute vec2 TextureCoords;
varying vec2 TextureCoordsVarying;
// 时间(经过uniform传入一个时间Time)
uniform float Time;
const float PI = 3.1415926;
void main (void) {
// 一次缩放效果时长
float duration = 0.4;
// 最大缩放幅度
float maxAmplitude = 0.3;
// 表示时间周期
float time = mod(Time, duration);
// 缩放幅度 [1.0,1.3]
float amplitude = 1.0 + maxAmplitude * abs(sin(time * (PI / duration)));
// 顶点坐标x/y 分别乘以放大系数[1.0,1.3]
gl_Position = vec4(Position.x * amplitude, Position.y * amplitude, Position.zw);
// 纹理坐标
TextureCoordsVarying = TextureCoords;
}
复制代码
实现效果: 3d
灵魂出窍滤镜的原理: 是两个层的叠加,而且上面的那层随着时间的推移,会逐渐放大且不透明度逐渐下降。这里也⽤到了放大的效果(基于缩放的原理),咱们此次用片元着⾊器来实现该效果。code
片元着色器代码:orm
precision highp float;
// 纹理采样器
uniform sampler2D Texture;
// 纹理坐标
varying vec2 TextureCoordsVarying;
// 时间(经过uniform传入一个时间Time)
uniform float Time;
void main (void) {
// 一次灵魂出窍效果的时长 1.0
float duration = 1.0;
// 透明度上限
float maxAlpha = 0.5;
// 放大图片上限
float maxScale = 1.8;
// 进度值[0,1]
float progress = mod(Time, duration) / duration; // 0~1
// 透明度范围[0,0.5]
float alpha = maxAlpha * (1.0 - progress);
// 缩放比例[1.0,1.8]
float scale = 1.0 + (maxScale - 1.0) * progress;
// 放大纹理坐标
// 根据放大比例,获得放大纹理坐标 [0,0],[0,1],[1,1],[1,0]
float weakX = 0.5 + (TextureCoordsVarying.x - 0.5) / scale;
float weakY = 0.5 + (TextureCoordsVarying.y - 0.5) / scale;
// 放大纹理坐标
vec2 weakTextureCoords = vec2(weakX, weakY);
// 获取对应放大纹理坐标下的纹素(颜色值rgba)
vec4 weakMask = texture2D(Texture, weakTextureCoords);
// 原始的纹理坐标下的纹素(颜色值rgba)
vec4 mask = texture2D(Texture, TextureCoordsVarying);
// 颜色混合 默认颜色混合方程式 = mask * (1.0-alpha) + weakMask * alpha;
gl_FragColor = mask * (1.0 - alpha) + weakMask * alpha;
}
复制代码
实现效果: cdn
抖动的过程当中也是基于缩放的原理,并且它的颜色值产生必定的误差。 抖动效果: 颜⾊偏移 + 微弱的放大效果blog
片元着色代码:图片
precision highp float;
// 纹理
uniform sampler2D Texture;
// 纹理坐标
varying vec2 TextureCoordsVarying;
// 时间(经过uniform传入一个时间Time)
uniform float Time;
void main (void) {
// 一次抖动滤镜的时长
float duration = 1.0;
// 放大图片上限
float maxScale = 1.2;
// 颜色偏移步长
float offset = 0.02;
// 进度[0,1]
float progress = mod(Time, duration) / duration;
// 颜色偏移值范围[0,0.02]
vec2 offsetCoords = vec2(offset, offset) * progress;
// 缩放范围[1.0-1.2];
float scale = 1.0 + (maxScale - 1.0) * progress;
// 放大纹理坐标.
vec2 ScaleTextureCoords = vec2(0.5, 0.5) + (TextureCoordsVarying - vec2(0.5, 0.5)) / scale;
// 获取3组颜色rgb
// 原始颜色+offsetCoords
vec4 maskR = texture2D(Texture, ScaleTextureCoords + offsetCoords);
// 原始颜色-offsetCoords
vec4 maskB = texture2D(Texture, ScaleTextureCoords - offsetCoords);
// 原始颜色
vec4 mask = texture2D(Texture, ScaleTextureCoords);
// 从3组来获取颜色:
// maskR.r,mask.g,maskB.b 注意这3种颜色取值能够打乱或者随意发挥.只是效果会有不同.
// mask.a 获取原图的透明度
gl_FragColor = vec4(mask.r, maskR.g, maskB.b, mask.a);
}
复制代码
实现效果: ci
闪白滤镜的原理: 在上层添加⽩色图层 ,⽩色图层的透明度随着时间的变化而变化。
片元着色器代码:
precision highp float;
// 纹理采样器
uniform sampler2D Texture;
// 纹理坐标
varying vec2 TextureCoordsVarying;
// 时间(经过uniform传入一个时间Time)
uniform float Time;
void main (void) {
// 一次闪白滤镜的时长
float duration = 0.5;
// 表示时间周期[0.0,0.5]
float time = mod(Time, duration);
// 白色颜色遮罩层
vec4 whiteMask = vec4(1.0, 1.0, 1.0, 1.0);
// 振幅: (0.0,1.0)
float amplitude = abs(sin(time * (PI / duration)));
// 纹理坐标对应的纹素(RGBA)
vec4 mask = texture2D(Texture, TextureCoordsVarying);
// 利用混合方程式; 白色图层 + 原始纹理图片颜色 来进行混合
gl_FragColor = mask * (1.0 - amplitude) + whiteMask * amplitude;
}
复制代码
实现效果:
⽑刺滤镜的原理: 撕裂 + 微弱的颜⾊偏移。
片元着色器代码:
precision highp float;
// 纹理
uniform sampler2D Texture;
// 纹理坐标
varying vec2 TextureCoordsVarying;
// 时间(经过uniform传入一个时间Time)
uniform float Time;
// 随机数
float rand(float n) {
//fract(x),返回x的小数部分数据
return fract(sin(n) * 43758.5453123);
}
void main (void) {
// 最大抖动
float maxJitter = 0.06;
// 一次毛刺滤镜的时长
float duration = 0.5;
// 红色颜色偏移量
float colorROffset = 0.01;
//绿色颜色偏移量
float colorGOffset = -0.02;
// 蓝色颜色偏移量
float colorBOffset = -0.035;
// 时间周期[0.0,1.0];
float time = mod(Time, duration * 2.0);
// 振幅:[0,1];
float amplitude = max(sin(time * (PI / duration)), 0.0);
// 像素随机偏移[-1,1]
float jitter = rand(TextureCoordsVarying.y) * 2.0 - 1.0; // -1~1
// 是否要作偏移.
bool needOffset = abs(jitter) < maxJitter * amplitude;
// 获取纹理X值.根据needOffset,来计算它X撕裂.
// needOffset = YES,撕裂较大;
// needOffset = NO,撕裂较小.
float textureX = TextureCoordsVarying.x + (needOffset ? jitter : (jitter * amplitude * 0.006));
// 撕裂后的纹理坐标x,y
vec2 textureCoords = vec2(textureX, TextureCoordsVarying.y);
// 颜色偏移3组颜色
// 根据撕裂后获取的纹理颜色值
vec4 mask = texture2D(Texture, textureCoords);
// 撕裂后的纹理颜色偏移
vec4 maskR = texture2D(Texture, textureCoords + vec2(colorROffset * amplitude, 0.0));
vec4 maskG = texture2D(Texture, textureCoords + vec2(colorGOffset * amplitude, 0.0));
vec4 maskB = texture2D(Texture, textureCoords + vec2(colorBOffset * amplitude, 0.0));
// 颜色部分发生撕裂.
gl_FragColor = vec4(maskR.r, maskG.g, maskB.b, mask.a);
}
复制代码
实现效果: