cocos creator 3.x shader 流光

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;
  }
}%
相关推荐
牛奶2 小时前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶2 小时前
前端人为什么要学AI?
前端·人工智能·ai编程
Kagol4 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路5 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide5 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter6 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸6 小时前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000007 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉7 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化