6.Half Lambert光照Diffuse Shader
生活随笔
收集整理的這篇文章主要介紹了
6.Half Lambert光照Diffuse Shader
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
簡單來說,Lambert定律認(rèn)為,在平面某點漫反射光的光強與該反射點的法向量和入射光角度的余弦值成正比(即我們之前使用dot函數(shù)得到的結(jié)果)。Half Lambert最初是由Valve(游戲半條命2使用的引擎即是其開發(fā)的)提出來,用于提高物體在一些光線無法照射到的區(qū)域的亮度的。簡單說來,它提高了漫反射光照的亮度,使得漫反射光線可以看起來照射到一個物體的各個表面。而Half Lambert最初也是被用于游戲半條命的畫面渲染,為了防止某個物體的背光面丟失形狀并且顯得太過平面化。這個技術(shù)是完全沒有基于任何物理原理的,而僅僅是一種感性的視覺增強
相比之前的Lambert光照shader,只是多加了一行代碼
float hLambert = difLight * 0.5 + 0.5;
由代碼可以看出,我們定義了一個新的變量hLambert來替換difLight用于計算某點的顏色值。difLight的范圍是0.0 - 1.0,而通過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的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5.Lambert光照Diffuse S
- 下一篇: 7.Phong 和 BlinnPhong