Lambert/Diffuse光照模型的特点:各向同性,即与观察的方向无关,反射光只与入射光和入射角度相关。

1.光源垂直照射平面

Lambert/Diffuse 光照模型-LMLPHP

如图,设入射光量为Ф, 平面面积为A, 则可以认为平面上每一点获取的光量为 Ф/A

2.光源斜射平面, 与平面法线N成角度θ

Lambert/Diffuse 光照模型-LMLPHP

如图,设入射光量为Ф,光线与法线的交角为θ, 光源的横截面的宽度为L,受光面的宽度为L',则有 L/L' = cosθ.相对于垂直照射,斜照的时候,受光面更大了,光就更分散了,平均每一点获取到的光量就变小了,只有垂直照射的百分之cosθ. 由于cos0° = 1, 两种情况可以统一在一起。另外, cosθ的数值会存在小于0的情况,所以光照模型在实现时一般会取 max(0, cosθ)值。

3.Lambert/Diffuse 的 Unity Shader 实现。

(Unity5.3.4)Shader代码:

 Shader "Custom/SimpleDiffuse"
{
Properties
{
[NoScaleOffset]_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
LOD Pass
{
// indicate that our pass is the "base" pass in forward
// rendering pipeline. It gets ambient and main directional
// light data set up; light direction in _WorldSpaceLightPos0
// and color in _LightColor0
Tags{ "LightMode"="ForwardBase" } CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc"
#include "UnityLightingCommon.cginc" //for _LightColor0 struct v2f
{
float2 uv : TEXCOORD0;
float4 diff: COLOR0;
float4 vertex : SV_POSITION;
}; sampler2D _MainTex;
float4 _MainTex_ST; v2f vert (appdata_base v)
{
v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord; half3 worldNormal = UnityObjectToWorldNormal(v.normal); half diffuse = max(, dot(worldNormal, _WorldSpaceLightPos0.xyz));//calculate cosθ o.diff = diffuse * _LightColor0 ;//diffuse return o;
} fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv); col *= i.diff; return col;
} ENDCG
}
}
}

此Shader是Vertex/Fragment Shader而不是SurfaceShader,因为涉及到与光源交互,需要包含UnityLightingComming.cginc文件(第24行代码)。 Tags{ "LightMode"="ForwardBase" } (第17行代码)的目的是声明使用的是Forward Rendering Path, 并且LightMode是ForwardBase,这意味着如果你场景有Directional Light的话,Unity的ShaderLab会把它的位置和颜色和_WorldSpaceLightPos0与_LightColor0这个两个内置变量关联起来,提供给Shader代码引用。所以如果你的场景中并没有Direction Light的话,而是只有Spot Light或者是只有Area Light甚至没有任何光源的话,下图中的猩猩表面会是一片黑,不会有现在这种带毛发效果的图像。(http://docs.unity3d.com/Manual/RenderTech-ForwardRendering.html是关于Forward Rendering的介绍)。

效果图:

Lambert/Diffuse 光照模型-LMLPHP

4.Half Lambert (主要作用是“让较暗的地方看起来不会过于太暗”)

half diffuse = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
这里得到的diffuse范围在0~1,而且很时候存在很多点的diffuse值很小、很接近0,反映在画面的话就是模型存在局部地方很暗。
Half Lambert 将diffuse的值映射到0.5~1.0,从而将较暗的地方亮度提高。 代码:diffuse = diffuse * 0.5f + 0.5f;
效果:
Lambert/Diffuse 光照模型-LMLPHPLambert/Diffuse 光照模型-LMLPHP 左边的是Half Lambert,相对右图整体会亮一点,更主要的是猩猩的臀部和胸口不会显得特别暗,更接近真实。 5.Lambert/Diffuse 优点:简单高效,比较适合“建筑物”一类的物体材质。缺点:无高光,没有考虑环境光,不适合带镜面反射的物体材质。
05-17 14:22