《Unity3D ShaderLab开发实战详解》第4章 基本的光照模型

漫反射(Lambert)

  • Lum = C * max(0, cos(L, N))
    • Lum:漫反光亮度。
    • C:漫反光强度(漫反光颜色)。
    • L:入射光方向。
    • N:法线方向。

镜面发射(Phong)

  • Lum = C * pow(max(0, cos(R, V)), gloss)
    • Lum:高光亮度。
    • C:高光强度(高光颜色)。
    • R:反射光方向。
    • V:视角方向。
    • gloss:镜面光滑程度。

半角向量(BlinnPhong)

  • Lum = C * pow(max(0, cos(H, N)), gloss)
    • Lum:高光亮度。
    • C:高光强度(高光颜色)。
    • H:半角向量。
    • N:法线方向。
    • gloss:镜面光滑程度。

Unity 内部的光照函数

  • Forward下的漫反射光照函数:
inline fixed4 LightingLambert(SurfaceOutput s, fixed3 lightDir, fixed atten){
	fixed diff = max(0, dot(s.Normal, lightDir));
	fixed4 c;
	c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);
	c.a = s.Alpha;
	return c;
}
  • Forward下的高光函数:
inline fixed4 LightingBlinnPhong(SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten){
	half3 h = normalize(lightDir + viewDir);
	fixed diff = max(0, dot(s.Normal, lightDir));
	float nh = max(0, dot(s.Normal, h));
	float spec = pow(nh, s.Specular * 128.0) * s.Gloss;

	fixed4 c;
	c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
	c.a = s.Alpha + _LightColor0.a * _SpecColor.a * spec * atten;
	return c;
}
发布了41 篇原创文章 · 获赞 4 · 访问量 3896

猜你喜欢

转载自blog.csdn.net/weixin_42487874/article/details/103228464