Unity Shader:(八)高光反射光照模型
1 Shader "Unlit/Blinn-Phong" 2 { 3 Properties 4 { 5 _Diffuse("漫反射颜色",Color) = (1,1,1,1) 6 _Specular("高光反射颜色",Color) = (1,1,1,1) 7 _Gloss("高光区域大小",Range(8.0,256)) = 20 8 } 9 SubShader 10 { 11 Pass 12 { 13 Tags { "LightMode" = "ForwardBase" } 14 15 CGPROGRAM 16 #pragma vertex vert 17 #pragma fragment frag 18 19 #include "Lighting.cginc" //为了使用_LightColor0变量 20 21 fixed4 _Diffuse; 22 fixed4 _Specular; 23 float _Gloss; 24 25 struct a2f 26 { 27 float4 vertex:POSITION; 28 float3 normal:NORMAL; 29 }; 30 struct v2f 31 { 32 float4 pos : SV_POSITION; 33 fixed3 worldNormal : TEXCOORD0; 34 fixed3 worldPos : TEXCOORD1; 35 }; 36 37 v2f vert(a2f v) 38 { 39 v2f o; 40 o.pos = UnityObjectToClipPos(v.vertex); 41 o.worldNormal = UnityObjectToWorldNormal(v.normal); //UnityCG.cginc帮助函数 42 o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz; 43 return o; 44 } 45 46 fixed4 frag(v2f i) : SV_Target 47 { 48 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; 49 fixed3 worldNormal = normalize(i.worldNormal); 50 fixed3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldPos));//UnityCG.cginc帮助函数 51 fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight)); 52 fixed3 reflectDir = normalize(reflect(-worldLight, worldNormal)); 53 fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos)); //UnityCG.cginc帮助函数 54 fixed3 halfDir = normalize(worldLight + viewDir); 55 fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(worldNormal,halfDir)), _Gloss); 56 return fixed4(ambient + diffuse + specular,1.0); 57 } 58 ENDCG 59 } 60 } 61 Fallback "Specular" 62 }
在材质面板的显示:
在物体上的效果: