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;
  

  }

}%
相关推荐
LcGero4 天前
Cocos Creator 热更新地址动态化方案
cocos·动态化·热更
mxwin6 天前
Unity Shader 逐像素光照 vs 逐顶点光照性能与画质的权衡策略
unity·游戏引擎·shader·着色器
mxwin6 天前
Unity URP 全局光照 (GI) 完全指南 Lightmap 采样与实时 GI(光照探针、反射探针)的 Shader 集成
unity·游戏引擎·shader·着色器
mxwin6 天前
Unity URP 溶解效果基于噪声纹理与 clip 函数实现物体渐隐渐显
unity·游戏引擎·shader
mxwin6 天前
Unity Shader 顶点色:利用模型顶点颜色传递渲染数据
unity·游戏引擎·shader
mxwin6 天前
Unity URP 下的 GPU Instancing减少 DrawCall 的关键技术
unity·游戏引擎·shader
mxwin6 天前
Unity URP SRP Batcher 完全指南 URP/HDRP 下的核心批处理机制,大幅降低 CPU 开销
unity·游戏引擎·shader·单一职责原则
mxwin7 天前
Unity Shader UV 坐标与纹理平铺Tiling & Offset 深度解析
unity·游戏引擎·shader·uv
mxwin8 天前
Unity Shader Blinn-Phong vs PBR传统经验模型与现代物理基础渲染
unity·游戏引擎·shader
mxwin9 天前
Unity URP 阴影映射 深度纹理、阴影采样与分辨率控制的深度解析
unity·游戏引擎·shader·着色器