简介
鼠标点击某个位置后,周围生成波纹的一个效果。核心思路是以点击中心基点,周围所有的点与其求得一个方向向量,那么这个方向向量与这个点的关系通过计算就能够得到一个圆圈的效果。
具体代码
顶点着色器只干了传递坐标转换坐标的事情
仅包含有关片元着色的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| uniform ARGS{ vec2 center; vec2 canvas_size; float wave_radius; float wave_offset; }; CCProgram fs %{ precision highp float; #include <alpha-test> #include <cc-global> in vec4 v_color; #if USE_TEXTURE in vec2 v_uv0; uniform sampler2D texture; #endif uniform ARGS{ vec2 center; vec2 canvas_size; float wave_radius; float wave_offset; }; void main() {
vec2 distance_vec = center - v_uv0;
distance_vec = distance_vec * vec2(canvas_size.x / canvas_size.y, 1.0);
float distance = sqrt(distance_vec.x * distance_vec.x + distance_vec.y * distance_vec.y);
float sin_factor = sin(distance * 100.0 + cc_time.x) * 0.05; float discard_factor = clamp(wave_radius - abs(wave_offset - distance), 0.0, 1.0);
vec2 offset = normalize(distance_vec) * sin_factor * discard_factor; vec2 uv = offset + v_uv0; gl_FragColor = texture(texture, uv); }
|