摘自冯乐乐的《unity shader 入门精要》
这个跟前面的比,只是多了一行代码:
Shader "Custom/HalfLambertLevelMat" {
Properties
{
_Diffuse("Diffuse",Color)=(1,1,1,1)
}
SubShader
{
Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Diffuse;
struct a2v
{
float4 vertex:POSITION;
float4 normal:NORMAL;
};
struct v2f
{
float4 pos:SV_POSITION;
fixed3 worldNormal:TEXCOORDO;
};
v2f vert(a2v v)
{
v2f o;
//Transform the vertex from object space to projection space
//变换对象空间的顶点投影空间
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
// Transform the normal from object space to world space
//把局部法线转换成空间法线
o.worldNormal = mul(v.normal, (float3x3)_World2Object);
return o;
}
fixed4 frag(v2f i):SV_Target
{
//get ambient term
//获取局部法线
fixed3 ambient =UNITY_LIGHTMODEL_AMBIENT.xyz;
//get the normal in world space
//获取空间法线
fixed3 worldNormal=normalize(i.worldNormal);
//get the light direction in world space
//获取空间光照
fixed3 worldLightDir=normalize(_WorldSpaceLightPos0.xyz);
//compute diffuse term
//计算漫反射
fixed halfLambert=dot(worldNormal,worldLightDir)*0.5+0.5;//多了这一行代码
fixed3 diffuse=_LightColor0.rgb*_Diffuse.rgb*saturate(dot(worldNormal,worldLightDir));
fixed3 color=ambient+diffuse;
return fixed4(color,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
从左至右,分别是半兰伯特,逐像素和漫反射的效果,个人觉得,三者间的差别是在于光暗交汇处所表现出来的效果,最左边的效果当然是比较好的。