1.最简形式,只根据顶点的法线和光照的方向做点积来计算
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
Shader "Custom/PhongVert2"
{
Properties
{
_Diffuse ("Diffuse", COLOR) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType" = "Opaque"
"LightMode" = "ForwardBase"
}
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
fixed3 color : COLOR;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Diffuse;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal = normalize( mul( v.normal,(fixed3x3)unity_WorldToObject ) );
//_WorldSpaceLightPos0 是光的方向向量
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLight));
o.color = ambient + diffuse;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(i.color,1);
}
ENDCG
}
}
}
效果: