内置管线

 1 Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level" {
 2     Properties {
 3         _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
 4     }
 5  
 6     SubShader {
 7         Pass {
 8  
 9             Tags {
10                 "LightMode"="ForwardBase"
11             }
12             
13             CGPROGRAM
14             #pragma vertex vert
15             #pragma fragment frag
16  
17             #include "UnityCG.cginc"
18             #include "Lighting.cginc"
19  
20             fixed4 _Diffuse;
21  
22             struct v2f {
23                 float4 pos : SV_POSITION;
24                 float3 worldNor : TEXCOORD0;
25             };
26  
27             v2f vert(appdata_full data) {
28                 v2f v;
29                 v.pos = UnityObjectToClipPos(data.vertex);
30                 v.worldNor = mul(data.normal, (float3x3)unity_WorldToObject);
31                 return v;
32             }
33  
34             fixed4 frag(v2f v) : SV_Target {
35                 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
36                 fixed3 worldVetNormal = normalize(v.worldNor);
37                 fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
38                 fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldVetNormal, worldLightDir));
39                 fixed3 color = diffuse + ambient;
40                 return fixed4(color, 1);
41             }
42             ENDCG
43         }
44     }
45     FallBack "Diffuse"
46 }

urp管线

 1 Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level" {
 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 CBUFFER_START(UnityPerMaterial)
25             half4 _Diffuse;
26 CBUFFER_END
27  
28             struct a2v {
29                 float4 vertex : POSITION;
30                 half3 normal : NORMAL;
31             };
32  
33             struct v2f {
34                 float4 pos : SV_POSITION;
35                 half3 worldNormal : TEXCOORD0;
36             };
37  
38             v2f vert(a2v data) {
39                 v2f v;
40                 v.pos = TransformObjectToHClip(data.vertex.xyz);
41                 v.worldNormal = mul(data.normal, (float3x3)UNITY_MATRIX_I_M);
42                 return v;
43             }
44  
45             half4 frag(v2f v) : SV_Target {
46                 half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
47                 half3 worldNormal = normalize(v.worldNormal);
48                 half3 worldLightDir = normalize(_MainLightPosition.xyz);
49                 half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));
50                 half3 color = diffuse + ambient;
51                 return half4(color, 1);
52             }
53             ENDHLSL
54         }
55     }
56     FallBack "Universal Render Pipeline/Simple Lit"
57 }