内置管线
1 Shader "Unity Shaders Book/Chapter 8/Alpha Blending With ZWrite" { 2 Properties { 3 _Color("Color Tint", Color) = (1,1,1,1) 4 _MainTex("Main Tex", 2D) = "white" {} 5 _AlphaScale("Alpha Scale", Range(0, 1)) = 0.5 6 } 7 8 SubShader { 9 Tags {"Queue"="Transparent" "IngoreProjector"="True" "RenderType"="Transparent"} 10 11 Pass { 12 ZWrite On 13 ColorMask 0 14 } 15 16 17 Pass { 18 19 Tags {"LightMode"="ForwardBase"} 20 21 ZWrite Off 22 Blend SrcAlpha OneMinusSrcAlpha 23 24 CGPROGRAM 25 #include "Lighting.cginc" 26 27 #pragma vertex vert 28 #pragma fragment frag 29 30 fixed4 _Color; 31 sampler2D _MainTex; 32 float4 _MainTex_ST; 33 float _AlphaScale; 34 35 struct a2v { 36 float4 vertex : POSITION; 37 float3 normal : NORMAL; 38 float4 texcoord : TEXCOORD0; 39 }; 40 41 struct v2f { 42 float4 pos : SV_POSITION; 43 float3 worldNormal : TEXCOORD0; 44 float3 worldPos : TEXCOORD1; 45 float2 uv : TEXCOORD2; 46 }; 47 48 v2f vert(a2v i) { 49 v2f o; 50 o.pos = UnityObjectToClipPos(i.vertex); 51 o.worldPos = mul(unity_ObjectToWorld, i.vertex).xyz; 52 o.worldNormal = UnityObjectToWorldNormal(i.normal); 53 o.uv = i.vertex.xy * _MainTex_ST.xy + _MainTex_ST.zw; 54 55 return o; 56 } 57 58 fixed4 frag(v2f i) : SV_Target { 59 fixed3 worldNormal = normalize(i.worldNormal); 60 fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos)); 61 62 fixed4 texColor = tex2D(_MainTex, i.uv); 63 fixed3 albedo = texColor.rgb * _Color.rgb; 64 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo; 65 fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(worldLightDir, worldNormal)); 66 67 return fixed4(ambient + diffuse, texColor.a * _AlphaScale); 68 } 69 70 ENDCG 71 } 72 } 73 FallBack "Transparent/VertexLit" 74 }
urp管线
1 Shader "Unity Shaders Book/Chapter 8/Alpha Blending With ZWrite" { 2 Properties { 3 _Color("Color Tint", Color) = (1,1,1,1) 4 _MainTex("Main Tex", 2D) = "white" {} 5 _AlphaScale("Alpha Scale", Range(0, 1)) = 0.5 6 } 7 8 SubShader { 9 Tags { 10 "RenderPipeline"="UniversalPipeline" 11 "Queue"="Transparent" "IngoreProjector"="True" "RenderType"="Transparent" 12 } 13 14 Pass { 15 ZWrite On 16 ColorMask 0 17 } 18 19 20 Pass { 21 22 Tags {"LightMode"="UniversalForward"} 23 24 ZWrite Off 25 Blend SrcAlpha OneMinusSrcAlpha 26 27 HLSLPROGRAM 28 29 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" 30 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" 31 32 #pragma vertex vert 33 #pragma fragment frag 34 35 CBUFFER_START(UnityPerMaterial) 36 half4 _Color; 37 float4 _MainTex_ST; 38 float _AlphaScale; 39 CBUFFER_END 40 41 TEXTURE2D(_MainTex); 42 SAMPLER(sampler_MainTex); 43 44 struct a2v { 45 float4 vertex : POSITION; 46 half3 normal : NORMAL; 47 float4 texcoord : TEXCOORD0; 48 }; 49 50 struct v2f { 51 float4 pos : SV_POSITION; 52 half3 worldNormal : TEXCOORD0; 53 half3 worldPos : TEXCOORD1; 54 float2 uv : TEXCOORD2; 55 }; 56 57 v2f vert(a2v i) { 58 v2f o; 59 o.pos = TransformObjectToHClip(i.vertex.xyz); 60 o.worldPos = mul(UNITY_MATRIX_M, i.vertex).xyz; 61 o.worldNormal = TransformObjectToWorldNormal(i.normal); 62 o.uv = i.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw; 63 64 return o; 65 } 66 67 half4 frag(v2f i) : SV_Target { 68 half3 worldNormal = normalize(i.worldNormal); 69 half3 worldLightDir = normalize(_MainLightPosition.xyz); 70 71 half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv); 72 half3 albedo = texColor.rgb * _Color.rgb; 73 half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w) * albedo; 74 half3 diffuse = _MainLightColor.rgb * albedo * saturate(dot(worldLightDir, worldNormal)); 75 return half4(ambient + diffuse, texColor.a * _AlphaScale); 76 } 77 78 ENDHLSL 79 } 80 } 81 FallBack "Univeral Render Pipeline/Simple Lit" 82 }
浙公网安备 33010602011771号