cocos shader消失

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

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:
        rowsCount: { value: 8.0, editor: { slide: true, range: [2, 100] } } 
        
        o: { value: -1, editor: { slide: true, range: [0, 100] } } 
        
}%

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 {


    float rowsCount;   // 切成几行
    float o;    // 整排下落到底部所需时间
  };

// 随机数函数,用于决定方块消失顺序
  float random(vec2 st) {
    return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43768.5453123);
  }

  vec4 frag () {
    // 切成方块
    vec2 gridPos = floor(uv * vec2(rowsCount));
    vec2 blockCoord = (gridPos + 0.5) / vec2(rowsCount);
    
    vec4 color = texture(cc_spriteTexture, blockCoord);
    
    // ===== 消散动画 =====
    float time = cc_time.x * o / 2.0;
    float dissolve = fract(time); // 循环消散
    
    // 方法1:从中心向外消散
    vec2 centerDist = abs(blockCoord - 0.5) * 2.0; // 0=中心,1=边缘
    float distanceFromCenter = length(centerDist);
    
    // 距离中心越远,消散越早
    float appearThreshold = dissolve * 2.0 - distanceFromCenter;
    if (appearThreshold < 0.0) {
      color = vec4(0.0);
    }
    
    // 方法2:向上消散
    // float fadeStart = 0.5 - blockCoord.y * 0.5; // 底部先消散
    // if (dissolve > fadeStart) {
    //   color = vec4(0.0);
    // }
    
    return color;
  

  }

}%
相关推荐
mxwin18 小时前
Unity Shader URP:法线如何进行光照计算
unity·游戏引擎·shader
周胡杰5 天前
【Cocos 集成鸿蒙】
鸿蒙·鸿蒙系统·cocos
mxwin6 天前
Unity Shader 切线空间数据是如何计算出来的
unity·游戏引擎·shader
mxwin7 天前
Unity Shader 法线贴图跟切线空间有什么关系
unity·游戏引擎·贴图·shader
mxwin7 天前
Unity Shader 贴图和采样的关系 如何保证贴图清晰
unity·游戏引擎·贴图·shader
mxwin7 天前
Unity Shader 什么是球谐光照 原理是什么
unity·游戏引擎·shader
玉夏9 天前
【Shader基础】CG/HLSL 基础语法
unity·shader
mxwin11 天前
Unity Shader 冰面 Shader 制作原理与流程
unity·游戏引擎·shader
mxwin12 天前
Unity URP下新技术MSSPT 取代SSR和光线追踪
unity·游戏引擎·shader
mxwin16 天前
Unity Shader Shiny SSRR
unity·游戏引擎·shader