先看一下效果,然后再一一分析
缩放
缩放主要改变顶点坐标的位置,和片元中的纹理没有太直接的关系,我们让图片,先放大,再缩小,再放大,再缩小,这样一个循环逻辑。我们引入弧度的正弦值,我们知道弧度在[0,π]范围正弦值的变化是[0,1,0],我们通过取莫再变化得出此变化规律
attribute vec4 Position;
attribute vec2 TextureCoords;
varying vec2 TextureCoordsVarying;
uniform float Time;
const float PI = 3.1415926;
void main (void) {
float duration = 0.6;
float maxAmplitude = 0.3;
float time = mod(Time, duration); //time [0,0.6]
float amplitude = 1.0 + maxAmplitude * abs(sin(time * (PI / duration)));
//time * (PI / duration) 变化规律是0到π,然后又0到π
gl_Position = vec4(Position.x * amplitude, Position.y * amplitude, Position.zw);
TextureCoordsVarying = TextureCoords;
}
//灵魂出窍
灵魂出窍是两个图层组合成的,一个图层是位置纹理不变的,另一个纹理是出窍纹理,围着中心点,放大淡出。变化规律,放大淡出一个周期后在放大淡出
precision highp float;
uniform sampler2D Texture;
varying vec2 TextureCoordsVarying;
uniform float Time;
void main (void) {
float duration = 0.7;
float maxAlpha = 0.4;
float maxScale = 1.8;
float progress = mod(Time, duration) / duration; // 0~1
float alpha = maxAlpha * (1.0 - progress);
float scale = 1.0 + (maxScale - 1.0) * progress;
float weakX = 0.5 + (TextureCoordsVarying.x - 0.5) / scale;
float weakY = 0.5 + (TextureCoordsVarying.y - 0.5) / scale;
vec2 weakTextureCoords = vec2(weakX, weakY);
vec4 weakMask = texture2D(Texture, weakTextureCoords);
vec4 mask = texture2D(Texture, TextureCoordsVarying);
gl_FragColor = mask * (1.0 - alpha) + weakMask * alpha;
}
weakX 和 weakY显示的是该位置要显示的纹理坐标,
我们可以先计算本该显示的纹理坐标,经过放大后的位置
(x -0.5 )*scale + 0.5 = XR;
和该像素叠加的另一纹理是其他地方经过放大后的纹理坐标,我们知道 让XR是该像素的坐标,计算出和该像素叠加的其他像素纹理是
反推到就是 weakX = 0.5 + (TextureCoordsVarying.x - 0.5) / scale;
抖动滤镜
颜⾊偏移 + 微弱的放大效果
precision highp float;
uniform sampler2D Texture;
varying vec2 TextureCoordsVarying;
uniform float Time;
void main (void) {
//动画时长
float duration = 0.7;
//放大
float maxScale = 1.1;
//纹理坐标偏移
float offset = 0.02;
float progress = mod(Time, duration) / duration; // 0~1
//(0,0)->(0.2,0.2)
vec2 offsetCoords = vec2(offset, offset) * progress;
//[1.0,1.1]
float scale = 1.0 + (maxScale - 1.0) * progress;
//其他地方的坐标经过放大后显示该像素的坐标
vec2 ScaleTextureCoords = vec2(0.5, 0.5) + (TextureCoordsVarying - vec2(0.5, 0.5)) / scale;
//取微小偏移后其他纹理的色值
vec4 maskR = texture2D(Texture, ScaleTextureCoords + offsetCoords);
vec4 maskB = texture2D(Texture, ScaleTextureCoords - offsetCoords);
vec4 mask = texture2D(Texture, ScaleTextureCoords);
gl_FragColor = vec4(maskR.r, mask.g, maskB.b, mask.a);
}
闪白
两个图层混合,一个纹理图层,一个白色颜色图层,一个周期变化是,纹理图层从alpha值1->0,白色图层从alpha 值0->1,然后纹理图层从alpha 0->1,白色图层alpha值从 1->0
precision highp float;
uniform sampler2D Texture;
varying vec2 TextureCoordsVarying;
uniform float Time;
const float PI = 3.1415926;
void main (void) {
float duration = 0.6;
float time = mod(Time, duration);
vec4 whiteMask = vec4(1.0, 1.0, 1.0, 1.0);
float amplitude = abs(sin(time * (PI / duration)));
vec4 mask = texture2D(Texture, TextureCoordsVarying);
gl_FragColor = mask * (1.0 - amplitude) + whiteMask * amplitude;
}
毛刺
具体的思路路是,我们让每一⾏像素随机偏移 -1 ~ 1 的距离(这⾥的 -1 ~ 1 是对于纹理坐标来说的),但是如果整个 画面都偏移⽐较⼤的值,那我们可能都看不出原来图像的样子。所以我们的逻辑是,设定一个阈值,小于这个阈值才进行偏 移,超过这个阈值则乘上一个缩小系数。
则最终呈现的效果是:绝大部分的⾏都会进⾏微⼩的偏移,只有少量的⾏会进行较⼤大偏移
precision highp float;
uniform sampler2D Texture;
varying vec2 TextureCoordsVarying;
uniform float Time;
const float PI = 3.1415926;
float rand(float n) {
return fract(sin(n) * 43758.5453123);
}
void main (void) {
float maxJitter = 0.06;
float duration = 0.3;
float colorROffset = 0.01;
float colorBOffset = -0.025;
float time = mod(Time, duration * 2.0); //[0,0.6]
//time * (PI / duration) [0,2π]
// time [0,0.3] amplitude值为[0,1,0],time [0.3,0.6] amplitude的值为0
float amplitude = max(sin(time * (PI / duration)), 0.0);//[0,1]
// rand() [0,1] jitter[-1,1]
float jitter = rand(TextureCoordsVarying.y) * 2.0 - 1.0; // -1~1
// abs(jitter) [0,1] maxJitter * amplitude [0,0.06] needOffset很小概率是True
//周期内某一个时间,和特定的 TextureCoordsVarying.y范围 needOffset为true,所以毛刺位置不发生变化。
bool needOffset = abs(jitter) < maxJitter * amplitude;
float textureX = TextureCoordsVarying.x + (needOffset ? jitter : (jitter * amplitude * 0.006));
vec2 textureCoords = vec2(textureX, TextureCoordsVarying.y);
vec4 mask = texture2D(Texture, textureCoords);
vec4 maskR = texture2D(Texture, textureCoords + vec2(colorROffset * amplitude, 0.0));
vec4 maskB = texture2D(Texture, textureCoords + vec2(colorBOffset * amplitude, 0.0));
gl_FragColor = vec4(maskR.r, mask.g, maskB.b, mask.a);
}
幻觉
残影和颜色偏移的叠加
残影的效果: 是在移动的过程中,每经过一段时间隔,根据当前的位置去创建一个新层,并且新层的不透明度随着时间逐 渐减弱。于是在一个移动周期内,可以看到很多透明度不同的层叠加在一起,从⽽形成残影的效果。残影,让图片随着时间 做圆周运动
颜⾊偏移: 物体移动的过程是蓝色在前面,红⾊在后面。所以整个过程可以理解成:在移动的过程中,每间隔一段时间,遗 失了一部分红色通道的值在原来的位置,并且这部分红色通道的值,随着时间偏移,会逐渐恢复.
precision highp float;
uniform sampler2D Texture;
varying vec2 TextureCoordsVarying;
uniform float Time;
const float PI = 3.1415926;
const float duration = 2.0;
vec4 getMask(float time, vec2 textureCoords, float padding) {
vec2 translation = vec2(sin(time * (PI * 2.0 / duration)),
cos(time * (PI * 2.0 / duration)));
vec2 translationTextureCoords = textureCoords + padding * translation;
vec4 mask = texture2D(Texture, translationTextureCoords);
return mask;
}
float maskAlphaProgress(float currentTime, float hideTime, float startTime) {
float time = mod(duration + currentTime - startTime, duration);
return min(time, hideTime);
}
void main (void) {
float time = mod(Time, duration);
float scale = 1.2;
// padding =1/12
float padding = 0.5 * (1.0 - 1.0 / scale);
vec2 textureCoords = vec2(0.5, 0.5) + (TextureCoordsVarying - vec2(0.5, 0.5)) / scale;
float hideTime = 0.9;
float timeGap = 0.2;
float maxAlphaR = 0.5; // max R
float maxAlphaG = 0.05; // max G
float maxAlphaB = 0.05; // max B
vec4 mask = getMask(time, textureCoords, padding);
float alphaR = 1.0; // R
float alphaG = 1.0; // G
float alphaB = 1.0; // B
vec4 resultMask = vec4(0, 0, 0, 0);
for (float f = 0.0; f < duration; f += timeGap) {
float tmpTime = f;
vec4 tmpMask = getMask(tmpTime, textureCoords, padding);
//
float tmpAlphaR = maxAlphaR - maxAlphaR * maskAlphaProgress(time, hideTime, tmpTime) / hideTime;
float tmpAlphaG = maxAlphaG - maxAlphaG * maskAlphaProgress(time, hideTime, tmpTime) / hideTime;
float tmpAlphaB = maxAlphaB - maxAlphaB * maskAlphaProgress(time, hideTime, tmpTime) / hideTime;
resultMask += vec4(tmpMask.r * tmpAlphaR,
tmpMask.g * tmpAlphaG,
tmpMask.b * tmpAlphaB,
1.0);
alphaR -= tmpAlphaR;
alphaG -= tmpAlphaG;
alphaB -= tmpAlphaB;
}
resultMask += vec4(mask.r * alphaR, mask.g * alphaG, mask.b * alphaB, 1.0);
gl_FragColor = resultMask;
}