内置管线

 1 Shader "Unity Shaders Book/Chapter 6/Diffuse Vertex-Level" {
 2     Properties {
 3         _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
 4     }
 5  
 6     SubShader {
 7  
 8         Pass {
 9             
10             Tags {
11                 "LightMode"="ForwardBase"
12             }
13  
14             CGPROGRAM
15         
16             #pragma vertex vert
17             #pragma fragment frag
18             
19             #include "UnityCG.cginc"
20             #include "Lighting.cginc"
21  
22             fixed4 _Diffuse;
23  
24             struct v2f {
25                 float4 pos : SV_POSITION;
26                 fixed3 color : COLOR;
27             };
28  
29             v2f vert(appdata_full data) {
30                 v2f v;
31                 v.pos = UnityObjectToClipPos(data.vertex);
32  
33                 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
34  
35                 // 漫反射公式 -- 光的颜色 * 漫反射系数 * dot(世界空间下点的法线,世界空间下光的方向)
36                 // 点在world-space下的向量,mul中正常应该时矩阵左乘,但是对于处理法线,需要左乘逆矩阵的转置矩阵,因此通过右乘逆矩阵来模拟
37                 fixed3 worldVetNormal = normalize(mul(data.normal, (float3x3)unity_WorldToObject));
38                 fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
39                 fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldVetNormal, worldLightDir));
40                 v.color = diffuse + ambient;
41                 return v;
42             }
43  
44            fixed4 frag(v2f v) : SV_Target {
45                return fixed4(v.color, 1);
46             }
47  
48             ENDCG
49         }
50     }
51     FallBack "Diffuse"
52 }

urp管线

 1 Shader "Unity Shaders Book/Chapter 6/Diffuse Vertex-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 color : COLOR;
36             };
37  
38             v2f vert(a2v data) {
39                 v2f v;
40                 v.pos = TransformObjectToHClip(data.vertex.xyz);
41  
42                 half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
43  
44                 // 漫反射公式 -- 光的颜色 * 漫反射系数 * dot(世界空间下点的法线,世界空间下光的方向)
45                 // 点在world-space下的向量,mul中正常应该时矩阵左乘,但是对于处理法线,需要左乘逆矩阵的转置矩阵,因此通过右乘逆矩阵来模拟
46                 half3 worldNormal = normalize(mul(data.normal, (float3x3)UNITY_MATRIX_I_M));
47                 half3 worldLightDir = normalize(_MainLightPosition.xyz);
48                 half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));
49                 v.color = diffuse + ambient;
50                 return v;
51             }
52  
53            half4 frag(v2f v) : SV_Target {
54                return half4(v.color, 1);
55             }
56  
57             ENDHLSL
58         }
59     }
60     FallBack "Universal Render Pipeline/Simple Lit"
61 }