闪白效果

flashWhite.effect 复制代码
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

CCEffect %{
  techniques:
  - passes:
    - vert: vs
      frag: fs
      blendState:
        targets:
        - blend: true
      rasterizerState:
        cullMode: none
      properties:
        texture: { value: white }
        alphaThreshold: { value: 0.5 }
        u_flashColor: { value: [1.0, 1.0, 1.0, 0.0], editor: { type: 'color' } }
        u_flashIntensity: { value: 0.0 }
}%

CCProgram vs %{
  precision highp float;

  #include <cc-global>
  #include <cc-local>

  in vec3 a_position;
  in vec4 a_color;
  out vec4 v_color;

  #if USE_TEXTURE
  in vec2 a_uv0;
  out vec2 v_uv0;
  #endif

  void main () {
    vec4 pos = vec4(a_position, 1);

    #if CC_USE_MODEL
    pos = cc_matViewProj * cc_matWorld * pos;
    #else
    pos = cc_matViewProj * pos;
    #endif

    #if USE_TEXTURE
    v_uv0 = a_uv0;
    #endif

    v_color = a_color;

    gl_Position = pos;
  }
}%

CCProgram fs %{
  precision highp float;

  #include <alpha-test>
  #include <texture>

  in vec4 v_color;
  
  #if USE_TEXTURE
  in vec2 v_uv0;
  uniform sampler2D texture;
  #endif

  // 将非采样器uniform放入uniform block中
  uniform FlashParams {
    vec4 u_flashColor;
    float u_flashIntensity;
  };

  void main () {
    vec4 o = vec4(1, 1, 1, 1);

    #if USE_TEXTURE
      CCTexture(texture, v_uv0, o);
    #endif

    o *= v_color;

    ALPHA_TEST(o);

    // 应用闪白效果
    o.rgb = mix(o.rgb, u_flashColor.rgb, u_flashIntensity * u_flashColor.a);

    #if USE_BGRA
      gl_FragColor = o.bgra;
    #else
      gl_FragColor = o.rgba;
    #endif
  }
}%
FlashEffect.ts 复制代码
const { ccclass, property } = cc._decorator;

@ccclass
export default class FlashEffect extends cc.Component {
    @property(cc.Sprite)
    private img: cc.Sprite = null;

    @property(cc.Color)
    flashColor: cc.Color = cc.Color.WHITE;

    @property
    flashDuration: number = 0.5;

    private _flashIntensity: number = 0;
    private _isFlashing: boolean = false;
    private material: cc.Material = null;
    private _flashTimer: any = null;  // 新增计时器引用

    start() {
        this.material = this.img.getMaterial(0);
        this.setFlashColor(this.flashColor);
    }

    setFlashColor(color: cc.Color) {
        this.flashColor = color;
        this.material.setProperty('u_flashColor', [color.r / 255, color.g / 255, color.b / 255, color.a / 255]);
    }

    flash() {
        // 先停止之前的闪白效果
        this.stopFlash();

        this._isFlashing = true;
        this._flashIntensity = 1.0;
        this.material.setProperty('u_flashIntensity', this._flashIntensity);

        const interval = 0.016;
        // 计算每次减少的量,根据flashDuration决定
        const step = interval / this.flashDuration;

        this._flashTimer = setInterval(() => {
            this._flashIntensity -= step;
            
            // 当强度小于等于0时停止
            if (this._flashIntensity <= 0) {
                this._flashIntensity = 0;
                this.stopFlash();
            }
            
            this.material.setProperty('u_flashIntensity', this._flashIntensity);
        }, interval * 1000);  // 转换为毫秒
    }

    stopFlash() {
        if (this._flashTimer) {
            clearInterval(this._flashTimer);
            this._flashTimer = null;
        }
        this._isFlashing = false;
        this._flashIntensity = 0;
        this.material.setProperty('u_flashIntensity', this._flashIntensity);
    }

    // 添加组件销毁时的清理
    onDestroy() {
        this.stopFlash();
    }
}
相关推荐
ObjectX前端实验室40 分钟前
【图形编辑器架构】🧠 Figma 风格智能选择工具实现原理【猜测】
前端·react.js
天桥下的卖艺者42 分钟前
R语言基于shiny开发随机森林预测模型交互式 Web 应用程序(应用程序)
前端·随机森林·r语言·shiny
技术钱1 小时前
vue3 两份json数据对比不同的页面给于颜色标识
前端·vue.js·json
路很长OoO1 小时前
Flutter 插件开发实战:桥接原生 SDK
前端·flutter·harmonyos
开水好喝2 小时前
Code Coverage Part I
前端
DoraBigHead2 小时前
🧭 React 理念:让时间屈服于 UI —— 从同步到可中断的演化之路
前端·javascript·面试
敢敢J的憨憨L2 小时前
GPTL(General Purpose Timing Library)使用教程
java·服务器·前端·c++·轻量级计时工具库
喝拿铁写前端3 小时前
Vue 组件通信的两种世界观:`.sync` 与普通 `props` 到底有什么不同?
前端·vue.js·前端框架
美酒没故事°3 小时前
npm源管理器:nrm
前端·npm·npm源
用户22152044278003 小时前
vue3组件间的通讯方式
前端·vue.js