广义的半兰伯特光照模型公式:
绝大多数情况下,α和β的值均为0.5,即公式为:
半兰伯特Shader代码
Shader "Custom/Chapter6-HalfLambert" {
Properties {
_Diffuse ("Diffuse", Color) = (1, 1, 1, 1)
}
SubShader {
pass{
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Diffuse;
struct a2v {
float4 vertex : POSITION;
//存储模型的顶点法线信息
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
//需要把法线传给片元着色器
fixed3 worldNormal : TEXCOORD0;
};
v2f vert(a2v v) {
v2f o;
//把顶点位置从模型空间转换到裁剪空间
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//把顶点法线从模型空间转换到世界空间
//_World2Object: 顶点变换矩阵的逆转置矩阵,这里取矩阵的前三行前三列。
o.worldNormal = mul(v.normal, (float3x3)_World2Object);
return o;
}
//漫反射光照模型——逐像素光照
fixed4 frag(v2f i) : COLOR {
//得到环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//得到在世界空间上的顶点法线
fixed3 worldNormal = normalize(i.worldNormal);
//得到光源方向(注意,此方法只适合仅有一个平行光的时候)
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 halfLambert = dot(worldNormal, worldLightDir)*0.5+0.5;
//利用半兰伯特公式计算漫反射光
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;
//加上环境光的影响
fixed3 color = ambient + diffuse;
return fixed4(color, 1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
效果对比