cocos角色受击闪白shader教程
·
教程在这教程在这
// 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:
lightColor: { value: [1.0, 0.0, 0.0, 1.0], editor: { type: color } }
lightIntensity: { value: 3.0, editor: { slide: true, range: [1, 10] } }
glowsize: { value: 0.0, editor: { slide: true, range: [0, 10] } }
pulseSpeed: { value: 2.0, editor: { slide: true, range: [0, 17] } }
isBlinking: { value: 0, editor: { type: boolean } }
}%
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 {
vec4 lightColor;
float lightIntensity;
float pulseSpeed;
float time;
float isBlinking;
float glowsize;
};
vec4 frag () {
vec4 texColor = texture(cc_spriteTexture, uv);
// 简单条件:只要glowsize大于0就发光
if (glowsize > 0.5) {
// ========== 快速闪烁逻辑 ==========
// 每秒闪5次(亮5次暗5次)
float flashRate = pulseSpeed;
// 计算时间:比如cc_time.x=3.2秒
float currentTime = cc_time.x;
// 关键判断:通过正弦波判断现在是亮还是暗
// sin(值) 的结果在-1到1之间变化
// 当结果>0时,我们认为是"亮"
if (sin(currentTime * flashRate) > 0.0) {
// 现在是"亮"的时候:纯白色
return vec4(1.0, 1.0, 1.0, texColor.a);
}else{
return texColor;
}
// 不闪的时候或没开启效果:返回原图
}
return texColor;
}
}%
更多推荐
所有评论(0)