6.Half Lambert光照Diffuse Shader

来源:互联网 发布:猫王收音机 知乎 编辑:程序博客网 时间:2024/06/05 05:57

简单来说,Lambert定律认为,在平面某点漫反射光的光强与该反射点的法向量和入射光角度的余弦值成正比(即我们之前使用dot函数得到的结果)。Half Lambert最初是由Valve(游戏半条命2使用的引擎即是其开发的)提出来,用于提高物体在一些光线无法照射到的区域的亮度的。简单说来,它提高了漫反射光照的亮度,使得漫反射光线可以看起来照射到一个物体的各个表面。而Half Lambert最初也是被用于游戏半条命的画面渲染,为了防止某个物体的背光面丢失形状并且显得太过平面化。这个技术是完全没有基于任何物理原理的,而仅仅是一种感性的视觉增强

相比之前的Lambert光照shader,只是多加了一行代码

float hLambert = difLight * 0.5 + 0.5;

由代码可以看出,我们定义了一个新的变量hLambert来替换difLight用于计算某点的颜色值。difLight的范围是0.0 - 1.0,而通过hLambert,我们将结果由0.0 - 1.0映射到了0.5 - 1.0,从而达到了增加亮度的目的。下图显示了这一变化:

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"}



0 0