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;
  }
}%
相关推荐
天***889622 分钟前
js封装一个双精度算法实现
开发语言·前端·javascript
Algebraaaaa34 分钟前
什么是前端、后端与全栈开发,Qt属于什么?
开发语言·前端·qt
胡斌附体43 分钟前
使用Electron创建helloworld程序
前端·javascript·electron·nodejs·pc
toobeloong1 小时前
Electron 从低版本升级到高版本 - webview通信的改造
前端·javascript·electron
im_AMBER1 小时前
React 01
前端·javascript·笔记·react.js·前端框架·web
@大迁世界1 小时前
React 19.2.0 有哪些新变化
前端·javascript·react.js·前端框架·ecmascript
华仔啊2 小时前
用 Vue3 + Canvas 做了个超实用的水印工具,同事都在抢着用
前端·vue.js·canvas
Bacon3 小时前
前端:从0-1实现一个脚手架
前端
Bacon3 小时前
前端项目部署实战 nginx+docker持续集成
前端