// 第 4 步:透明度裁剪(Alpha Clip / Alpha Test)。alpha 低于阈值的像素直接丢弃。 Shader "Cookbook/Ch03/AlphaClip" { Properties { [MainTexture] _BaseMap ("Base Map", 2D) = "white" {} [MainColor] _BaseColor ("Base Color", Color) = (1, 1, 1, 1) _Cutoff ("Alpha Cutoff", Range(0, 1)) = 0.5 // 剪掉的地方能看到背面,所以通常关掉背面剔除:0=Off 1=Front 2=Back [Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull", Float) = 0 } SubShader { // AlphaTest 队列(2450)排在普通不透明物体之后,RenderType 让替换着色/工具识别它 Tags { "RenderType"="TransparentCutout" "RenderPipeline"="UniversalPipeline" "Queue"="AlphaTest" } Pass { Name "Unlit" Tags { "LightMode"="UniversalForward" } Cull [_Cull] // 渲染状态可以引用属性,用方括号 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; half4 color : COLOR; }; struct Varyings { float4 positionCS : SV_POSITION; float2 uv : TEXCOORD0; half4 color : COLOR; }; TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap); float4 _BaseMap_ST; half4 _BaseColor; half _Cutoff; Varyings vert (Attributes input) { Varyings output; output.positionCS = TransformObjectToHClip(input.positionOS.xyz); output.uv = TRANSFORM_TEX(input.uv, _BaseMap); output.color = input.color; return output; } //#region frag half4 frag (Varyings input) : SV_Target { half4 col = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * _BaseColor * input.color; // clip(x):x < 0 时丢弃当前片元,不写颜色也不写深度 clip(col.a - _Cutoff); return half4(col.rgb, 1); // 不透明物体输出 alpha=1 } //#endregion ENDHLSL } } }