抖音的抖動(dòng)特效的實(shí)現(xiàn)原理是,分別對(duì)RGB通道進(jìn)行分離計(jì)算不同的大小得到。廢話不多數(shù),直接上fragment shader 代碼:
precision highp float;
varying vec2 textureCoordinate;
uniform sampler2D inputTexture;
uniform float scale;
void main()
{
vec2 uv = textureCoordinate.xy;
vec2 scaleCoordinate = vec2((scale - 1.0) * 0.5 + uv.x / scale ,
(scale - 1.0) * 0.5 + uv.y / scale);
vec4 smoothColor = texture2D(inputTexture, scaleCoordinate);
// 計(jì)算紅色通道偏移值
vec4 shiftRedColor = texture2D(inputTexture,
scaleCoordinate + vec2(-0.1 * (scale - 1.0), - 0.1 *(scale - 1.0)));
// 計(jì)算綠色通道偏移值
vec4 shiftGreenColor = texture2D(inputTexture,
scaleCoordinate + vec2(-0.075 * (scale - 1.0), - 0.075 *(scale - 1.0)));
// 計(jì)算藍(lán)色偏移值
vec4 shiftBlueColor = texture2D(inputTexture,
scaleCoordinate + vec2(-0.05 * (scale - 1.0), - 0.05 *(scale - 1.0)));
vec3 resultColor = vec3(shiftRedColor.r, shiftGreenColor.g, shiftBlueColor.b);
gl_FragColor = vec4(resultColor, smoothColor.a);
}
縮放計(jì)算如下:
@Override
public void onDrawFrameBegin() {
super.onDrawFrameBegin();
mScale = 1.0f + 0.3f * getInterpolation(mOffset);
mOffset += 0.06f;
if (mOffset > 1.0f) {
mOffset = 0.0f;
}
GLES20.glUniform1f(mScaleHandle, mScale);
}
private float getInterpolation(float input) {
return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
}
如果用在視頻編輯階段,scale的值可以跟播放器的播放時(shí)間綁定得到。