内置管线
1 Shader "Unity Shaders Book/Chapter 6/Blinn-Phong Use Built-in Functions" { 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 fixed4 _Diffuse; 18 fixed4 _Specular; 19 float _Gloss; 20 21 struct v2f { 22 float4 pos : SV_POSITION; 23 fixed3 worldNor : TEXCOORD0; 24 fixed3 worldPos : TEXCOORD1; 25 }; 26 27 v2f vert(appdata_full data) { 28 v2f v; 29 v.pos = UnityObjectToClipPos(data.vertex); 30 v.worldPos = mul(unity_ObjectToWorld, data.vertex); 31 // v.worldNor = mul(data.normal, unity_WorldToObject); 32 v.worldNor = UnityObjectToWorldNormal(data.normal); 33 return v; 34 } 35 36 fixed4 frag(v2f v) : SV_Target { 37 // 环境光 38 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; 39 40 // 漫反射 41 fixed3 worldVetNormal = normalize(v.worldNor); 42 // fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz); 43 fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(v.worldPos)); 44 fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldVetNormal, worldLightDir)); 45 46 // 高光 47 // fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - v.worldPos.xyz); 48 fixed3 viewDir = normalize(UnityWorldSpaceViewDir(v.worldPos)); 49 fixed3 halfDir = normalize(viewDir + worldLightDir); 50 fixed3 specular = _LightColor0.rgb * _Specular.rbg * pow(saturate(dot(worldVetNormal, halfDir)), _Gloss); 51 52 return fixed4(ambient + diffuse + specular, 1); 53 } 54 55 ENDCG 56 } 57 } 58 FallBack "Specular" 59 }
urp管线
1 Shader "Unity Shaders Book/Chapter 6/Blinn-Phong Use Built-in Functions" { 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 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 worldNormal : TEXCOORD0; 36 half3 worldPos : TEXCOORD1; 37 }; 38 39 v2f vert(a2v data) { 40 v2f v; 41 v.pos = TransformObjectToHClip(data.vertex.xyz); 42 v.worldPos = mul(UNITY_MATRIX_M, data.vertex).xyz; 43 // v.worldNormal = mul(data.normal, (float3x3)UNITY_MATRIX_I_M); 44 v.worldNormal = TransformObjectToWorldNormal(data.normal); 45 return v; 46 } 47 48 half4 frag(v2f v) : SV_Target { 49 50 // return half4(1,1,1,1); 51 52 // 环境光 53 half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w); 54 55 // 漫反射 56 half3 worldNormal = normalize(v.worldNormal); 57 half3 worldLightDir = normalize(_MainLightPosition.xyz); 58 // half3 worldLightDir = normalize(UnityWorldSpaceLightDir(v.worldPos)); // URP 中已舍弃UnityWorldSpaceLightDir 59 // half3 worldLightDir = normalize(v.worldPos - _MainLightPosition.xyz); // URP 中结果错误 60 half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir)); 61 // return half4(ambient + diffuse, 1); 62 63 // 高光 64 half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - v.worldPos.xyz); 65 // half3 viewDir = normalize(UnityWorldSpaceViewDir(v.worldPos)); // URP 中已舍弃UnityWorldSpaceViewDir 66 half3 halfDir = normalize(viewDir + worldLightDir); 67 half3 specular = _MainLightColor.rgb * _Specular.rbg * pow(saturate(dot(worldNormal, halfDir)), _Gloss); 68 69 return half4(ambient + diffuse + specular, 1); 70 71 } 72 73 ENDHLSL 74 } 75 } 76 FallBack "Universal Render Pipeline/Simple Lit" 77 }
浙公网安备 33010602011771号