内置管线
1 Shader "Unity Shaders Book/Chapter 6/Half Lambert" { 2 Properties { 3 _Diffuse("Diffuse", Color) = (1, 1, 1, 1) 4 } 5 6 SubShader{ 7 Pass { 8 Tags { 9 "LightMode"="ForwardBase" 10 } 11 12 CGPROGRAM 13 14 #pragma vertex vert 15 #pragma fragment frag 16 #include "UnityCG.cginc" 17 #include "Lighting.cginc" 18 19 struct v2f { 20 float4 pos : SV_POSITION; 21 float3 worldNor : TEXCOORD0; 22 }; 23 24 fixed4 _Diffuse; 25 26 v2f vert(appdata_full data) { 27 v2f v; 28 v.pos = UnityObjectToClipPos(data.vertex); 29 v.worldNor = mul(data.normal, unity_WorldToObject); 30 return v; 31 } 32 33 fixed4 frag(v2f v) : SV_Target { 34 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; 35 fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz); 36 fixed3 worldVetNormal = normalize(v.worldNor); 37 fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * (dot(worldLightDir, worldVetNormal) * 0.5 + 0.5); 38 return fixed4(diffuse + ambient, 1); 39 } 40 41 ENDCG 42 } 43 } 44 FallBack "Diffuse" 45 }
urp管线
1 Shader "Unity Shaders Book/Chapter 6/Half Lambert" { 2 Properties { 3 _Diffuse("Diffuse", Color) = (1, 1, 1, 1) 4 } 5 6 SubShader{ 7 Tags { 8 "RenderPipeline"="UniversalPipeline" 9 } 10 11 Pass { 12 Tags { 13 "LightMode"="UniversalForward" 14 } 15 16 HLSLPROGRAM 17 18 #pragma vertex vert 19 #pragma fragment frag 20 21 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" 22 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" 23 24 struct a2v { 25 float4 vertex : POSITION; 26 half3 normal : NORMAL; 27 }; 28 29 struct v2f { 30 float4 pos : SV_POSITION; 31 half3 worldNormal : TEXCOORD0; 32 }; 33 34 CBUFFER_START(UnityPerMaterial) 35 half4 _Diffuse; 36 CBUFFER_END 37 v2f vert(a2v data) { 38 v2f v; 39 v.pos = TransformObjectToHClip(data.vertex.xyz); 40 v.worldNormal = mul(data.normal, (float3x3)UNITY_MATRIX_I_M); 41 return v; 42 } 43 44 half4 frag(v2f v) : SV_Target { 45 half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w); 46 half3 worldLightDir = normalize(_MainLightPosition.xyz); 47 half3 worldNormal = normalize(v.worldNormal); 48 half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * (dot(worldLightDir, worldNormal) * 0.5 + 0.5); 49 return half4(diffuse + ambient, 1); 50 } 51 52 ENDHLSL 53 } 54 } 55 FallBack "Universal Render Pipeline/Simple Lit" 56 }
浙公网安备 33010602011771号