cocos shader流光文字 不显示透明部分

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

自动从左到右是用了#include <builtin/uniforms/cc-local> time

为了显示字但是不显示背景

// 如果完全透明,直接返回

if (o.a < 0.01) {

return o;

复制代码
  }

用//if(o.a>0.0){ 有时会显示错

这两个条件语句的区别在于处理透明区域的方式不同。让我解释一下:

问题原因分析:

  1. if (o.a > 0.0) { 的情况:

    GLSL

    if (o.a > 0.0) {

    // 意思:如果像素有透明度(包括半透明和完全不透明)

    float strength = 1.0 - dist / glow;

    vec3 glowEffect = mix(vec3(1.0), glowColor.rgb, dist / glow);

    // ⚠️ 问题在这里:修改了所有有透明度像素的颜色

    o.rgb = original.rgb + glowEffect * strength;

    o.a = max(original.a, strength); // ⚠️ 修改了透明度!

    }

// 完全透明的像素(o.a == 0)不会执行上面代码

// 完全透明的像素返回 o(完全透明)

问题:半透明区域(如图片边缘羽化部分)的o.a被max(original.a, strength)提高了,变得更不透明,导致背景色泄露。

  1. if (o.a < 0.01) { 的情况:
    GLSL
    if (o.a < 0.01) {
    // 意思:如果像素几乎完全透明(alpha阈值0.01)
    return o; // 直接返回原像素,不做任何处理
    }

// 只有不透明和半透明像素继续执行发光逻辑

float strength = 1.0 - dist / glow;

vec3 glowEffect = mix(vec3(1.0), glowColor.rgb, dist / glow);

// 这里会修改所有非完全透明像素

o.rgb = original.rgb + glowEffect * strength;

o.a = max(original.a, strength); // 还是会修改透明度!

go 复制代码
// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
// Effect Syntax Guide: https://docs.cocos.com/creator/manual/zh/shader/index.html

// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
// Effect Syntax Guide: https://docs.cocos.com/creator/manual/zh/shader/index.html

// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
// 最简兼容版光柱Shader

/** glow-simple.effect **/


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:
        glowColor:  { value: [1.0, 0.8, 0.3, 1.0], editor: { type: color } }
        glowSpread: { value: 0.04, editor: { range: [0.0, 4, 0.005] } }







        glow: { value: 0.03, editor: { range: [0.01,7,0.01] } }
        glowIntensity: { value: 0.04, editor: { range: [0.0, 4, 0.005] } }
}%

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 UBO {
    vec4  glowColor;    // 发光颜色
    float glowSpread;   // 发光最大半径(基于 UV 坐标体系)
    float glowIntensity;
    float glow;
    
  };

  vec4 frag () {
    vec4 o =  texture(cc_spriteTexture,uv);
    vec4 original=o;

     // 固定光柱宽度和移动速度
    float beamPos = mod(cc_time.x -0.3, 3.0) ;
    float dist = abs(uv.x - beamPos);
    
    if(dist < glow) { // 固定光柱宽度为0.1
    
    //if(o.a>0.0){


// 计算光柱强度 (中心最强,边缘最弱)
// 如果完全透明,直接返回
      if (o.a < 0.01) {
          return o;

      }
        float strength = 1.0 - dist / glow;
        
        // 从白色渐变到 glowColor
        
        vec3 glowEffect = mix(vec3(1.0), glowColor.rgb, dist / glow);
        
        // 应用发光效果
        
        o.rgb = original.rgb + glowEffect * strength;
        o.a = max(original.a, strength);
    
    }
        
        
    //}
    
    return o;

  }
}%
相关推荐
暴走约伯1 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
寂寞的船2 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
寂寞的船3 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
探求虚无4 天前
用 AI 在 Unity 里做一款 RTS:最难的不是生成代码,而是证明它真的能玩
人工智能·unity·游戏引擎
寂寞的船6 天前
【Unity知识速递】第10期 Unity GC 零分配实战:从 UnityEngine.Pool 到手动 GC 控制的全链路优化
jvm·unity·c#·游戏引擎·lucene
Martina_03217 天前
AI生成3D场景的霓虹灯一开就糊成光团?用6步检查Emissive、Bloom与曝光
人工智能·3d·ai·自然语言处理·游戏引擎·材质·贴图
寂寞的船8 天前
【Unity知识速递】第8期 IL2CPP 托管堆内存碎片与原生堆交互机制深度解析
unity·游戏引擎·交互
淡海水9 天前
09-04-内存-Unity原生对象生命周期
unity·c#·游戏引擎·gc
寂寞的船10 天前
【Unity Bug挑战】第10期 SerializeReference 内存泄漏之谜
unity·c#·游戏引擎·bug
tealcwu12 天前
【Unity高级】Unity URP:构建同时支持 2D 与 3D 场景的项目完全指南
3d·unity·游戏引擎