ini
CCEffect %{
techniques:
- passes:
- vert: sprite-vs:vert
frag: sprite-fs:frag
depthStencilState:
depthTest: false
depthWrite: false
blendState:
targets:
- blend: true
blendSrc: src_alpha
blendDst: one_minus_src_alpha
blendDstAlpha: one_minus_src_alpha
rasterizerState:
cullMode: none
properties:
alphaThreshold: { value: 0.5 }
u_flowSpeed: { value: 1.0 }
u_flowWidth: { value: 0.3 }
u_flowAngle: { value: 30.0 }
u_flowTime: { value: 0.0 }
u_flowColor: { value: [1,0,0,1] }
}%
CCProgram sprite-vs %{
precision highp float;
#include <builtin/uniforms/cc-global>
#if USE_LOCAL
#include <builtin/uniforms/cc-local>
#endif
in vec3 a_position;
in vec2 a_texCoord;
in vec4 a_color;
out vec4 v_color;
out vec2 v_uv;
#if TWO_COLORED
in vec4 a_color2;
out vec4 v_color2;
#endif
// 必须包含名为 vert 的入口函数
vec4 vert () {
vec4 pos = vec4(a_position, 1);
#if USE_LOCAL
pos = cc_matWorld * pos;
#endif
pos = cc_matViewProj * pos;
v_uv = a_texCoord;
v_color = a_color;
#if TWO_COLORED
v_color2 = a_color2;
#endif
return pos;
}
}%
CCProgram sprite-fs %{
precision highp float;
#include <builtin/internal/alpha-test>
uniform FlowLight {
vec4 u_flowColor;
float u_flowTime;
float u_flowSpeed;
float u_flowWidth;
float u_flowAngle;
};
in vec4 v_color;
in vec2 v_uv;
#if TWO_COLORED
in vec4 v_color2;
#endif
#pragma builtin(local)
layout(set = 2, binding = 12) uniform sampler2D cc_spriteTexture;
float toRadians(float degrees) {
return degrees * 0.017453292; // π/180
}
vec4 frag () {
vec4 col = texture(cc_spriteTexture, v_uv);
#if TWO_COLORED
col.rgb = mix(v_color2.rgb, v_color.rgb, col.rgb);
col.a *= v_color.a;
#else
col *= v_color;
#endif
// 斜向平行四边形流光
float angle = toRadians(u_flowAngle);
float slope = tan(angle);
float flowPos = fract(u_flowTime * u_flowSpeed) * (1.0 + abs(slope));
float distance = v_uv.x + slope * v_uv.y - flowPos;
float mask = smoothstep(-u_flowWidth, 0.0, distance) *
(1.0 - smoothstep(0.0, u_flowWidth, distance));
col.rgb += u_flowColor.rgb * mask * u_flowColor.a;
ALPHA_TEST(col);
return col;
}
}%