
go
// Effect Syntax Guide: https://docs.cocos.com/creator/manual/zh/shader/index.html
// Effect Syntax Guide: https://docs.cocos.com/creator/manual/zh/shader/index.html
// Effect Syntax Guide: https://docs.cocos.com/creator/manual/zh/shader/index.html
CCEffect %{
techniques:
- name: glow
passes:
- vert: vs:vert
frag: fs:frag
blendState:
targets:
- blend: true
blendSrc: src_alpha
blendDst: one_minus_src_alpha
depthStencilState:
depthTest: false
depthWrite: false
rasterizerState:
cullMode: none
properties:
rowsCount: { value: 8.0, editor: { slide: true, range: [2, 100] } }
o: { value: -1, editor: { slide: true, range: [0, 100] } }
}%
CCProgram vs %{
precision highp float;
#include <cc-global>
#if USE_LOCAL
#include <builtin/uniforms/cc-local>
#endif
in vec3 a_position;
in vec2 a_texCoord;
out vec2 uv;
#if USE_LOCAL
in vec4 a_color;
out vec4 v_color;
#endif
vec4 vert() {
vec4 pos = vec4(a_position, 1);
#if USE_LOCAL
pos = cc_matWorld * pos;
v_color = a_color;
#endif
pos = cc_matViewProj * pos;
uv = a_texCoord;
return pos;
}
}%
CCProgram fs %{
precision highp float;
#include <sprite-texture>
#include <cc-global> // 添加cc_time支持
in vec2 uv;
uniform sampler2D mainTexture;
uniform UBO {
float rowsCount; // 切成几行
float o; // 整排下落到底部所需时间
};
// 随机数函数,用于决定方块消失顺序
float random(vec2 st) {
return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43768.5453123);
}
vec4 frag () {
// 切成方块
vec2 gridPos = floor(uv * vec2(rowsCount));
vec2 blockCoord = (gridPos + 0.5) / vec2(rowsCount);
vec4 color = texture(cc_spriteTexture, blockCoord);
// ===== 消散动画 =====
float time = cc_time.x * o / 2.0;
float dissolve = fract(time); // 循环消散
// 方法1:从中心向外消散
vec2 centerDist = abs(blockCoord - 0.5) * 2.0; // 0=中心,1=边缘
float distanceFromCenter = length(centerDist);
// 距离中心越远,消散越早
float appearThreshold = dissolve * 2.0 - distanceFromCenter;
if (appearThreshold < 0.0) {
color = vec4(0.0);
}
// 方法2:向上消散
// float fadeStart = 0.5 - blockCoord.y * 0.5; // 底部先消散
// if (dissolve > fadeStart) {
// color = vec4(0.0);
// }
return color;
}
}%