Shader "Custom/SpriteDissolve" { // 敌人死亡消隐 Shader // 基于噪声的溶解效果,带发光边缘 Properties { _MainTex ("Texture", 2D) = "white" {} _Color ("颜色", Color) = (1, 1, 1, 1) [Header(Dissolve)] _DissolveAmount ("溶解进度", Range(0, 1)) = 0 _DissolveEdgeWidth ("边缘宽度", Range(0, 0.3)) = 0.05 _DissolveEdgeColor ("边缘颜色", Color) = (0.3, 0.6, 1, 1) _DissolveNoiseScale ("噪声缩放", Float) = 10 // 阴影设置(保持与项目一致) [MaterialToggle] _CastShadow ("投射阴影", Float) = 0 } SubShader { Tags { "RenderPipeline" = "UniversalPipeline" "Queue" = "Transparent" "RenderType" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Off Pass { Name "SpriteDissolve" HLSLPROGRAM #pragma vertex vert #pragma fragment frag #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" struct Attributes { float4 positionOS : POSITION; float2 uv : TEXCOORD0; float4 color : COLOR; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct Varyings { float4 positionCS : SV_POSITION; float2 uv : TEXCOORD0; float4 color : COLOR; UNITY_VERTEX_INPUT_INSTANCE_ID }; TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); half4 _Color; half _DissolveAmount; half _DissolveEdgeWidth; half4 _DissolveEdgeColor; float _DissolveNoiseScale; // 简单的哈希噪声 float hash(float2 p) { float3 p3 = frac(float3(p.xyx) * 0.1031); p3 += dot(p3, p3.yzx + 33.33); return frac((p3.x + p3.y) * p3.z); } // 值噪声(平滑插值) float valueNoise(float2 p) { float2 i = floor(p); float2 f = frac(p); f = f * f * (3.0 - 2.0 * f); // smoothstep float a = hash(i); float b = hash(i + float2(1, 0)); float c = hash(i + float2(0, 1)); float d = hash(i + float2(1, 1)); return lerp(lerp(a, b, f.x), lerp(c, d, f.x), f.y); } // 分形噪声(多层叠加) float fbm(float2 p) { float value = 0.0; float amplitude = 0.5; for (int i = 0; i < 4; i++) { value += amplitude * valueNoise(p); p *= 2.0; amplitude *= 0.5; } return value; } Varyings vert(Attributes input) { Varyings output; UNITY_SETUP_INSTANCE_ID(input); UNITY_TRANSFER_INSTANCE_ID(input, output); output.positionCS = TransformObjectToHClip(input.positionOS.xyz); output.uv = input.uv; output.color = input.color; return output; } half4 frag(Varyings input) : SV_Target { UNITY_SETUP_INSTANCE_ID(input); half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv); half4 color = texColor * _Color * input.color; // 如果 alpha 太小,直接丢弃 if (color.a < 0.01) discard; // 计算噪声 float noise = fbm(input.uv * _DissolveNoiseScale); // 溶解判定 float dissolveEdge = smoothstep( _DissolveAmount - _DissolveEdgeWidth, _DissolveAmount + _DissolveEdgeWidth, noise ); // 完全溶解区域 if (noise < _DissolveAmount) discard; // 边缘发光 half edgeGlow = (1.0 - dissolveEdge) * step(_DissolveAmount, noise); color.rgb += _DissolveEdgeColor.rgb * edgeGlow * _DissolveEdgeColor.a; // 整体淡出(溶解后期整体变淡) color.a *= lerp(1.0, 0.3, _DissolveAmount); return color; } ENDHLSL } } FallBack "Sprites/Default" }