内置管线
1 Shader "Unity Shaders Book/Chapter 6/Specular Vertex-Level" { 2 Properties { 3 _Diffuse("Diffuse", Color) = (1, 1, 1, 1) 4 _Specular("Specular", Color) = (1, 1, 1, 1) 5 _Gloss("Gloss", Range(8.0, 256)) = 20 6 } 7 8 SubShader { 9 Pass { 10 CGPROGRAM 11 12 #pragma vertex vert 13 #pragma fragment frag 14 15 #include "UnityCG.cginc" 16 #include "Lighting.cginc" 17 18 fixed4 _Diffuse; 19 fixed4 _Specular; 20 float _Gloss; 21 22 struct a2v { 23 float4 vertex : POSITION; 24 float3 normal : NORMAL; 25 }; 26 27 struct v2f { 28 float4 pos : SV_POSITION; 29 fixed3 color : Color; 30 }; 31 32 v2f vert(appdata_full data) { 33 34 v2f v; 35 36 v.pos = UnityObjectToClipPos(data.vertex); 37 38 // 环境光 39 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; 40 41 // 漫反射 42 fixed3 worldVetNormal = normalize(mul(data.normal, (float3x3)unity_WorldToObject)); 43 fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz); 44 fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldVetNormal, worldLightDir)); 45 46 // 高光 47 fixed3 reflectDir = normalize(reflect(-worldLightDir, worldVetNormal)); 48 fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, data.vertex).xyz); 49 fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(viewDir, reflectDir)), _Gloss); 50 51 v.color = ambient + diffuse + specular; 52 53 return v; 54 } 55 56 fixed4 frag(v2f v) : SV_Target { 57 return fixed4(v.color, 1); 58 } 59 60 ENDCG 61 } 62 } 63 FallBack "Specular" 64 }
urp管线
1 Shader "Unity Shaders Book/Chapter 6/Specular Vertex-Level" { 2 Properties { 3 _Diffuse("Diffuse", Color) = (1, 1, 1, 1) 4 _Specular("Specular", Color) = (1, 1, 1, 1) 5 _Gloss("Gloss", Range(8.0, 256)) = 20 6 } 7 8 SubShader { 9 Tags { 10 "RenderPipeline"="UniversalPipeline" 11 } 12 13 Pass { 14 HLSLPROGRAM 15 16 #pragma vertex vert 17 #pragma fragment frag 18 19 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" 20 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" 21 22 CBUFFER_START(UnityPerMaterial) 23 half4 _Diffuse; 24 half4 _Specular; 25 float _Gloss; 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 40 v2f v; 41 42 v.pos = TransformObjectToHClip(data.vertex.xyz); 43 44 // 环境光 45 half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w); 46 47 // 漫反射 48 half3 worldNormal = normalize(mul(data.normal, (float3x3)UNITY_MATRIX_I_M)); 49 half3 worldLightDir = normalize(_MainLightPosition.xyz); 50 half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir)); 51 52 // 高光 53 // worldLightDir是从顶点指向光源,-worldLightDir就是从光源指向顶点,发射方向就是从顶点向外射 54 half3 reflectDir = normalize(reflect(-worldLightDir, worldNormal)); 55 half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(UNITY_MATRIX_M, data.vertex).xyz); 56 half3 specular = _MainLightColor.rgb * _Specular.rgb * pow(saturate(dot(viewDir, reflectDir)), _Gloss); 57 58 v.color = ambient + diffuse + specular; 59 60 return v; 61 } 62 63 half4 frag(v2f v) : SV_Target { 64 return half4(v.color, 1); 65 } 66 67 ENDHLSL 68 } 69 } 70 FallBack "Universal Render Pipeline/Simple Lit" 71 }
                    
                
                
            
        
浙公网安备 33010602011771号