内置管线

 1 Shader "Unity Shaders Book/Chapter 6/Specular Pixel-Level" {
 2     Properties {
 3         _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
 4         _Specular("Specular", Color) = (1, 1, 1, 1)
 5         _Gloss("Gloss", Range(10, 256)) = 20
 6     }
 7  
 8     SubShader {
 9         Pass {
10             CGPROGRAM
11             #pragma vertex vert
12             #pragma fragment frag
13  
14             #include "UnityCG.cginc"
15             #include "Lighting.cginc"
16  
17             struct v2f {
18                 float4 pos : SV_POSITION;
19                 fixed3 worldNor : TEXCOORD0;
20                 fixed3 worldPos : TEXCOORD1;
21             };
22  
23             fixed4 _Diffuse;
24             fixed4 _Specular;
25             float _Gloss;
26  
27             v2f vert(appdata_full data) {
28                 v2f v;
29                 v.pos = UnityObjectToClipPos(data.vertex);
30                 v.worldNor = mul(unity_WorldToObject, data.normal);
31                 v.worldPos = mul(data.vertex, unity_ObjectToWorld);
32  
33                 return v;
34             }
35  
36             fixed4 frag(v2f v) : SV_Target {
37                 // 环境光
38                 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
39  
40                 // per-pixel漫反射
41                 fixed3 worldVetNormal = normalize(v.worldNor);
42                 fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
43                 fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldVetNormal, worldLightDir));
44  
45                 // per-pixel高光
46                 fixed3 reflectDir = normalize(reflect(-worldLightDir, worldVetNormal));
47                 fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - v.worldPos.xyz);
48                 fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);
49  
50                 return fixed4(ambient + diffuse + specular, 1);
51             }
52             ENDCG
53         }
54     }
55     FallBack "Specular"
56 }

urp管线

 1 Shader "Unity Shaders Book/Chapter 6/Specular Pixel-Level" {
 2     Properties {
 3         _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
 4         _Specular("Specular", Color) = (1, 1, 1, 1)
 5         _Gloss("Gloss", Range(10, 256)) = 20
 6     }
 7  
 8     SubShader {
 9         Tags {
10             "RenderPipeline"="UniversalPipeline"
11         }
12         Pass {
13             HLSLPROGRAM
14  
15             #pragma vertex vert
16             #pragma fragment frag
17  
18             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
19             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
20  
21             struct a2v {
22                 float4 vertex : POSITION;
23                 half3 normal : NORMAL;
24             };
25  
26             struct v2f {
27                 float4 pos : SV_POSITION;
28                 half3 worldNormal : TEXCOORD0;
29                 float3 worldPos : TEXCOORD1;
30             };
31  
32 CBUFFER_START(UnityPerMaterial)
33             half4 _Diffuse;
34             half4 _Specular;
35             float _Gloss;
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                 v.worldPos = mul(UNITY_MATRIX_M, data.vertex).xyz;
42  
43                 return v;
44             }
45  
46             half4 frag(v2f v) : SV_Target {
47                 // 环境光
48                 half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
49  
50                 // per-pixel漫反射
51                 half3 worldNormal = normalize(v.worldNormal);
52                 half3 worldLightDir = normalize(_MainLightPosition.xyz);
53                 half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));
54  
55                 // per-pixel高光
56                 half3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
57                 half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - v.worldPos.xyz);
58                 half3 specular = _MainLightColor.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);
59  
60                 return half4(ambient + diffuse + specular, 1);
61             }
62             ENDHLSL
63         }
64     }
65     FallBack "Universal Render Pipeline/Simple Lit"
66 }