完整代码查看# AndroidShaderDemo下的ShakeRender
抖动特效和缩放特效很像,除了图像缩放,还在图像颜色分界处混合了颜色。颜色分界处混合颜色和之前说的高斯模糊,边缘检测很像,找到当前纹理周围的纹理,和当前纹理颜色混在一起。这里将左下角,右上角的颜色分量和当前颜色分量组合新的RGB,fragment shader如下:
precision mediump float;
uniform sampler2D u_TextureUnit;
varying vec2 v_TextureCoordinates;
uniform float u_Offset;
void main()
{
vec4 origin = texture2D(u_TextureUnit, v_TextureCoordinates);
vec4 green = texture2D(u_TextureUnit, vec2(v_TextureCoordinates.x + u_Offset,v_TextureCoordinates.y + u_Offset));
vec4 red = texture2D(u_TextureUnit, vec2(v_TextureCoordinates.x - u_Offset,v_TextureCoordinates.y - u_Offset));
gl_FragColor = vec4(red.x, green.y, origin.z, origin.w);
}
ShakeRender的onDrawFrame方法和灵魂出窍那部分代码差不多,只是这里不用画两次了,只用画一次。代码如下:
@Override
public void onDrawFrame(GL10 gl) {
glClear(GL_COLOR_BUFFER_BIT);
float progress;
if (curFrame <= MaxFrame) {
progress = curFrame * 1.0f / MaxFrame;
} else {
curFrame = 0;
progress = 0;
}
float scale = 1f + progress * 0.3f;
float offset = progress * 0.01f;
curFrame++;
drawLayer(scale, offset);
}
最终效果:
shake.gif
网友评论