unity Shader 的漫反射逐顶点的光照模型

Pass { 
Tags { "LightMode"="ForwardBase" }

CGPROGRAM

#pragma vertex vert
#pragma fragment frag

#include "Lighting.cginc"

fixed4 _Diffuse;

struct a2v {
float4 vertex : POSITION;//模型空间的定点位置
float3 normal : NORMAL;//模型顶点的法线
};

struct v2f {
float4 pos : SV_POSITION;//裁剪空间的顶点位置
fixed3 color : COLOR;//用于传递顶点空间得到的着色器颜色
};

v2f vert(a2v v) {
v2f o;
// Transform the vertex from object space to projection space
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

// Get ambient term
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

// Transform the normal fram object space to world space
fixed3 worldNormal = normalize(mul(v.normal, (float3x3)_World2Object));
// Get the light direction in world space
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
// Compute diffuse term
//漫反射 = 入射光的颜色和强度 * 材质的漫反射 max(0 , 法线点乘光源方向)
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.0);
}

ENDCG
}

猜你喜欢

转载自blog.csdn.net/qq_16603297/article/details/53144552