【兰伯特光照与Blinn_Phong光照】
<1>兰伯特与半兰伯特
Shader "Tang/614/Lambert"{ Properties{ _Color("Tin Color",Color) = (1,1,1,1) } SubShader{ Pass{ Tags { "LightMode" = "ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "Lighting.cginc" fixed4 _Color; struct a2v{ float4 vertex:POSITION; float3 normal:NORMAL; }; struct v2f{ float4 pos:SV_POSITION; float3 worldNormal:TEXCOORD0; }; v2f vert(a2v v){ v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.worldNormal = UnityObjectToWorldNormal(v.normal); return o; } //兰伯特光照 fixed4 frag(v2f i):SV_Target{ fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz); fixed3 worldNormal = normalize(i.worldNormal); //兰伯特 //fixed3 diffuse = _LightColor0.rgb*_Color.rgb* saturate(dot(lightDir,worldNormal)); //半兰伯特 fixed halfLambert = dot(lightDir,worldNormal)*0.5+0.5; fixed3 diffuse = _LightColor0.rgb*_Color.rgb* halfLambert; return fixed4(diffuse,1); } ENDCG } } }
<2>Blinn与Phong高光
Shader "Tang/614/Blinn"{ Properties{ _Color("漫反射",Color) = (1,1,1,1) _Specular("高光",Color) = (1,1,1,1) _Gloss("高光光滑度",Range(1,64)) = 2 } SubShader{ Pass{ Tags {"LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "Lighting.cginc" fixed4 _Color; fixed4 _Specular; fixed _Gloss; struct a2v{ float4 vertex:POSITION; float3 normal:NORMAL; }; struct v2f{ float4 pos:SV_POSITION; float3 worldNormal:TEXCOORD0; float3 worldPos:TEXCOORD1; }; v2f vert(a2v v){ v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.worldNormal = UnityObjectToWorldNormal(v.normal); o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz; return o; } //blinn高光 fixed4 frag(v2f i):SV_Target{ fixed3 worldNormal = normalize(i.worldNormal); fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz); //环境光 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; //漫反射颜色 //fixed halfLambert = dot(worldNormal,lightDir)*0.5+0.5; fixed lambert = saturate(dot(worldNormal,lightDir)); fixed3 diffuse = _LightColor0.rgb*_Color.rgb*lambert; //高光 //blinn高光 不再求反射 而是引入一个新的向量 h = 视角+入射光 再用h与法线点乘 //phong高光 用入射和法线求出反射n,再用n与视角点乘 //上面2者的效果差不多,有些时候前者更是我们想要的效果,而且前者计算开销少些 fixed3 viewDir = UnityWorldSpaceViewDir(i.worldPos); fixed3 h = normalize(lightDir+viewDir); fixed3 specular = _LightColor0.rgb*_Color.rgb* pow( saturate( dot(h,worldNormal) ),_Gloss); return fixed4(ambient+diffuse+specular,1); } ENDCG } } }
<3>采样反照率贴图的Blinn
Shader "Tang/614/BlinnTex"{ Properties{ _MainTex("反照贴图",2D) = "white" { } _Color("Tin Color",Color) = (1,1,1,1) _Specular("Specular",Color) = (1,1,1,1) _Gloss("Specular Gloss",Range(1,64)) = 8 } SubShader{ Pass{ Tags {"LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "Lighting.cginc" struct a2v{ float4 vertex:POSITION; float3 normal:NORMAL; float4 texcoord:TEXCOORD0; }; struct v2f{ float4 pos:SV_POSITION; float3 worldNormal:TEXCOORD0; float3 worldPos:TEXCOORD1; float2 uv:TEXCOORD2; }; sampler2D _MainTex;float4 _MainTex_ST; fixed4 _Color; fixed4 _Specular; fixed _Gloss; v2f vert(a2v v){ v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.worldNormal = UnityObjectToWorldNormal(v.normal); o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz; o.uv = TRANSFORM_TEX(v.texcoord,_MainTex); return o; } fixed4 frag(v2f i):SV_Target{ fixed3 worldNormal = normalize(i.worldNormal); fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz); fixed3 viewDir = UnityWorldSpaceViewDir(i.worldPos); //反照率 fixed3 albedo = tex2D(_MainTex,i.uv).rgb*_Color.rgb; fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo; fixed lambert = saturate(dot(worldNormal,lightDir)); fixed3 diffuse = _LightColor0.rgb*albedo*lambert; fixed3 h = normalize(viewDir+lightDir); fixed sp = pow( saturate(dot(h,worldNormal)) ,_Gloss ); fixed3 specular = _LightColor0.rgb*_Specular.rgb*sp; return fixed4(ambient+diffuse+specular,1); } ENDCG } } }