//漫反射 半兰伯特 diffuse = (c.m)(0.5(n.i)+0.5)
//模型背光面有可以有明暗变化
Shader "Unity shader book/Chapter 6/HalfLambert" {
Properties{
_Diffuse("Diffuse",Color) = (1,1,1,1)
}
SubShader{
Pass{
Tags{"LightMode" = "ForwardBase"} //指定光照模式,只有定义正确的光照模式,才能得到unity内置光照变量 比如——LIghtColor0
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Diffuse;//定义和属性类型匹配的变量
//定义顶点着色器输入和输出结构体;float最高精度浮点值,32位;fixed最低浮点值,11位存储,范围-2.0到2.0;
struct a2v {
float4 vertext :POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION; //结构体必须包含顶点在剪裁空间的坐标位置信息
float3 worldNormal:TEXCOORD0;
};
v2f vert(a2v i) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, i.vertext);
o.worldNormal = UnityObjectToWorldNormal(i.normal);
return o;
}
fixed4 frag(v2f i) :SV_Target{
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"
}