cocos shader三角流光

更直观的视频演示在我的B站更直观的视频演示在我的B站

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

// 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:
        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, 5] } }
        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);
    float texAlpha = texColor.a;
    
    // 如果完全透明,直接返回
    if (texAlpha < 0.01) {
        return texColor;
    }
    
    float angle = atan(uv.y - 0.5, uv.x - 0.5);
    float dist = distance(uv, vec2(0.5));
    
    // 🔴 危险脉冲(基于角度和距离)
    float dangerPulse = sin(cc_time.x * 5.0 + angle * 3.0) * 0.5 + 0.5;
    dangerPulse *= 1.0 - smoothstep(0.0, 0.5, dist); // 从中心向外的脉冲
    
    // 🌈 颜色根据危险程度变化
    vec3 dangerColor = mix(
        vec3(1.0, 0.5, 0.0),   // 橙色
        vec3(1.0, 0.0, 0.0),   // 红色
        glowsize * 0.1
    );
    
    // ⚫ 裂纹效果
    float crack = sin(angle * glowsize * 2.0 + cc_time.x) * 0.5 + 0.5;
    
    // 基于纹理alpha控制效果强度
    float effectStrength = texAlpha; // 不透明部分效果更强
    
    // 组合效果
    vec3 dangerEffect = dangerColor * dangerPulse * effectStrength * lightIntensity;
    dangerEffect += vec3(0.5, 0.2, 0.0) * crack * effectStrength * 0.5;
    
    // 混合原始颜色和效果
    vec3 finalColor = texColor.rgb + dangerEffect;
    
    // 边缘脉冲光晕
    float edgeGlow = 1.0 - smoothstep(0.3, 0.5, dist);
    vec3 edgeEffect = dangerColor * edgeGlow * sin(cc_time.x * 8.0) * 0.5 * texAlpha;
    finalColor += edgeEffect;
    
    return vec4(finalColor, texColor.a);
  }


}%
相关推荐
天人合一peng16 分钟前
unity 通过代码修改button及其名字字体的属性
unity·游戏引擎
GLDbalala4 小时前
Unity基于自定义管线实现经典经验光照模型
unity·游戏引擎
心疼你的一切7 小时前
Unity异步编程神器:Unitask库深度解析(功能+实战案例+API全指南)
深度学习·unity·c#·游戏引擎·unitask
呆呆敲代码的小Y9 小时前
【Unity 实用工具篇】 | Book Page Curl 快速实现翻书效果
游戏·unity·游戏引擎·u3d·免费游戏·翻书插件
星夜泊客2 天前
C# 基础:为什么类可以在静态方法中创建自己的实例?
开发语言·经验分享·笔记·unity·c#·游戏引擎
心前阳光2 天前
Unity 模拟父子关系
android·unity·游戏引擎
咸鱼永不翻身2 天前
Unity视频资源压缩详解
unity·游戏引擎·音视频
nnsix3 天前
Unity Physics.Raycast的 QueryTriggerInteraction枚举作用
unity·游戏引擎
ۓ明哲ڪ3 天前
Unity功能——创建新脚本时自动添加自定义头注释
unity·游戏引擎
熬夜敲代码的小N3 天前
Unity大场景卡顿“急救包”:从诊断到落地的全栈优化方案
java·unity·游戏引擎