6.Half Lambert光照Diffuse Shader
簡(jiǎn)單來(lái)說(shuō),Lambert定律認(rèn)為,在平面某點(diǎn)漫反射光的光強(qiáng)與該反射點(diǎn)的法向量和入射光角度的余弦值成正比(即我們之前使用dot函數(shù)得到的結(jié)果)。Half Lambert最初是由Valve(游戲半條命2使用的引擎即是其開(kāi)發(fā)的)提出來(lái),用于提高物體在一些光線無(wú)法照射到的區(qū)域的亮度的。簡(jiǎn)單說(shuō)來(lái),它提高了漫反射光照的亮度,使得漫反射光線可以看起來(lái)照射到一個(gè)物體的各個(gè)表面。而Half Lambert最初也是被用于游戲半條命的畫(huà)面渲染,為了防止某個(gè)物體的背光面丟失形狀并且顯得太過(guò)平面化。這個(gè)技術(shù)是完全沒(méi)有基于任何物理原理的,而僅僅是一種感性的視覺(jué)增強(qiáng)
相比之前的Lambert光照shader,只是多加了一行代碼
float hLambert = difLight * 0.5 + 0.5;
由代碼可以看出,我們定義了一個(gè)新的變量hLambert來(lái)替換difLight用于計(jì)算某點(diǎn)的顏色值。difLight的范圍是0.0 - 1.0,而通過(guò)hLambert,我們將結(jié)果由0.0 - 1.0映射到了0.5 - 1.0,從而達(dá)到了增加亮度的目的。下圖顯示了這一變化:
 
Shader "Study/6_HalfLambertDiffuse" { <span style="white-space:pre"> </span>Properties{ <span style="white-space:pre"> </span>_Color("Main Color", Color) = (1,1,1,1) <span style="white-space:pre"> </span>_MainTex("Base (RGB)", 2D) = "white" {} <span style="white-space:pre"> </span>_EmissiveColor("Emissive Color", Color) = (1,1,1,1) <span style="white-space:pre"> </span>_AmbientColor("Ambient Color", Color) = (1,1,1,1) <span style="white-space:pre"> </span>_MySliderValue("This is a Slider", Range(0,10)) = 2.5 <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>SubShader{ <span style="white-space:pre"> </span>Tags{ "RenderType" = "Opaque" } <span style="white-space:pre"> </span>LOD 200 <span style="white-space:pre"> </span>CGPROGRAM #pragma surface surf Lambert MyLambertDiffuse<span style="white-space:pre"> </span>sampler2D _MainTex; <span style="white-space:pre"> </span>fixed4 _Color; <span style="white-space:pre"> </span>//We need to declare the properties variable type inside of the ? <span style="white-space:pre"> </span>//CGPROGRAM so we can access its value from the properties block. ? <span style="white-space:pre"> </span>float4 _EmissiveColor; <span style="white-space:pre"> </span>float4 _AmbientColor; <span style="white-space:pre"> </span>float _MySliderValue;<span style="white-space:pre"> </span>struct Input <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>float2 uv_MainTex; <span style="white-space:pre"> </span>};<span style="white-space:pre"> </span>void surf(Input IN, inout SurfaceOutput o) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//We can then use the properties values in our shader ? <span style="white-space:pre"> </span>//float4 c; <span style="white-space:pre"> </span>fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; <span style="white-space:pre"> </span>c = c * pow((_EmissiveColor + _AmbientColor), _MySliderValue); <span style="white-space:pre"> </span>o.Albedo = c.rgb; <span style="white-space:pre"> </span>o.Alpha = c.a; <span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>inline float4 LightingMyLambertDiffuse(SurfaceOutput s, fixed3 lightDir, fixed atten) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>float difLight = max(0, dot(s.Normal, lightDir)); <span style="white-space:pre"> </span>float hLambert = difLight * 0.5 + 0.5; <span style="white-space:pre"> </span>float4 col; <span style="white-space:pre"> </span>col.rgb = s.Albedo * _LightColor0.rgb * (difLight * atten * 2); <span style="white-space:pre"> </span>col.a = s.Alpha; <span style="white-space:pre"> </span>return col; <span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>ENDCG <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>FallBack "Diffuse" }
 
總結(jié)
以上是生活随笔為你收集整理的6.Half Lambert光照Diffuse Shader的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: 5.Lambert光照Diffuse S
 - 下一篇: 大学生能申请信用卡吗?大学办信用卡全攻略