内置管线
1 Shader "Unity Shaders Book/Chapter 6/Blinn-Phong" { 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 Tags { 11 "LightMode"="ForwardBase" 12 } 13 14 CGPROGRAM 15 #pragma vertex vert 16 #pragma fragment frag 17 18 #include "UnityCG.cginc" 19 #include "Lighting.cginc" 20 21 fixed4 _Diffuse; 22 fixed4 _Specular; 23 float _Gloss; 24 25 struct v2f { 26 float4 pos : SV_POSITION; 27 fixed3 worldNor : TEXCOORD0; 28 fixed3 worldPos : TEXCOORD1; 29 }; 30 31 v2f vert(appdata_full data) { 32 v2f v; 33 v.pos = UnityObjectToClipPos(data.vertex); 34 v.worldPos = mul(unity_ObjectToWorld, data.vertex); 35 v.worldNor = mul(data.normal, unity_WorldToObject); 36 return v; 37 } 38 39 fixed4 frag(v2f v) : SV_Target { 40 // 环境光 41 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; 42 43 // 漫反射 44 fixed3 worldVetNormal = normalize(v.worldNor); 45 fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz); 46 fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldVetNormal, worldLightDir)); 47 48 // 高光 49 fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - v.worldPos.xyz); 50 fixed3 halfDir = normalize(viewDir + worldLightDir); 51 fixed3 specular = _LightColor0.rgb * _Specular.rbg * pow(saturate(dot(worldVetNormal, halfDir)), _Gloss); 52 53 return fixed4(ambient + diffuse + specular, 1); 54 } 55 56 ENDCG 57 } 58 } 59 FallBack "Specular" 60 }
urp管线
1 Shader "Unity Shaders Book/Chapter 6/Blinn-Phong" { 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 Tags { 14 "LightMode"="UniversalForward" 15 } 16 17 HLSLPROGRAM 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 half4 _Specular; 27 float _Gloss; 28 CBUFFER_END 29 30 struct a2v { 31 float4 vertex : POSITION; 32 half3 normal : NORMAL; 33 }; 34 35 struct v2f { 36 float4 pos : SV_POSITION; 37 half3 worldNormal : TEXCOORD0; 38 half3 worldPos : TEXCOORD1; 39 }; 40 41 v2f vert(a2v data) { 42 v2f v; 43 v.pos = TransformObjectToHClip(data.vertex.xyz); 44 v.worldPos = mul(UNITY_MATRIX_M, data.vertex).xyz; 45 v.worldNormal = mul(data.normal, (float3x3)UNITY_MATRIX_I_M); 46 return v; 47 } 48 49 half4 frag(v2f v) : SV_Target { 50 // 环境光 51 half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w); 52 53 // 漫反射 54 half3 worldNormal = normalize(v.worldNormal); 55 half3 worldLightDir = normalize(_MainLightPosition.xyz); 56 half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir)); 57 58 // 高光 59 half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - v.worldPos.xyz); 60 half3 halfDir = normalize(viewDir + worldLightDir); 61 half3 specular = _MainLightColor.rgb * _Specular.rbg * pow(saturate(dot(worldNormal, halfDir)), _Gloss); 62 63 return half4(ambient + diffuse + specular, 1); 64 } 65 66 ENDHLSL 67 } 68 } 69 FallBack "Universal Render Pipeline/Simple Lit" 70 }
                    
                
                
            
        
浙公网安备 33010602011771号