Unity3D 的物理渲染和光照模型(2)

最近在游戏中常用的风格之一即是Toon shading(又称 cel shading).这是一种非逼真渲染风格,通过改变了光在一个模型上反射实际情况来给人以手绘的感觉。为了达到这样的效果,我们需要用一个自定义模型来替换至今使用的标准光照模型。最常见用于达到这种效果的方法就是使用加性纹理,在下面的着色器中叫做_RampTex。

Shader "Example/Toon Shading" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_RampTex ("Ramp", 2D) = "white" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Toon
 
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
 
sampler2D _RampTex;
fixed4 LightingToon (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
half NdotL = dot(s.Normal, lightDir);
NdotL = tex2D(_RampTex, fixed2(NdotL, 0.5));
 
fixed4 c;
c.rgb = s.Albedo * _LightColor0.rgb * NdotL * atten * 2;
c.a = s.Alpha;
 
return c;
}
 
ENDCG
}
Fallback "Diffuse"
}
Shader "Example/Toon Shading" {Properties {_MainTex ("Texture", 2D) = "white" {}_RampTex ("Ramp", 2D) = "white" {}}SubShader {Tags { "RenderType" = "Opaque" }CGPROGRAM#pragma surface surf Toonstruct Input {float2 uv_MainTex;};sampler2D _MainTex;void surf (Input IN, inout SurfaceOutput o) {o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;}sampler2D _RampTex;fixed4 LightingToon (SurfaceOutput s, fixed3 lightDir, fixed atten){half NdotL = dot(s.Normal, lightDir);NdotL = tex2D(_RampTex, fixed2(NdotL, 0.5));fixed4 c;c.rgb = s.Albedo * _LightColor0.rgb * NdotL * atten * 2;c.a = s.Alpha;return c;}ENDCG}Fallback "Diffuse"}

toon

LightingToon模型计算了光强的朗伯系数NdotL并使用ramp纹理以将其离散化。该例子中仅映射了四级光强阶。不同的ramp纹理将细化地获得不同的toon shading变形。

ramp

镜面: Blinn-Phong模型

朗伯模型并不能仿真镜面反射材料。这种情况下就需要另一种技术; Unity4.x 采用了 Blinn-Phong 模型. 不需要计算法向量

N

和光矢量

L

的点积, 而是通过

L

和视角

V

的角平分矢量

H

来计算 :

BlinnPhong

\[\mathrm{Lambertian\,model:} \,\,\,\
I = N \cdot L\]

Unity3D 的物理渲染和光照模型

数量  通过   和  设置进一步计算。如果你想深入了解Unity中光照模型的计算, 可以下载其内置着色器的源码. 朗伯和Blinn-Phong表面函数都是在Lighting.cginc文件中计算的。而在 Unity5 中需要在遗产着色器(Legacy shaders)中寻找。

在Unity5中物理渲染

就像本文在前面提出的那样, Uniy4.x使用朗伯光照模型作为其默认着色器。Unity5中发生了改变, 其引入了 物理渲染 (PBR). 这个名字听起来相当有趣啊, 但是与其它光照模型没有什么不同。相比于朗伯反射, PBR提供了一个更加逼真的光线物体作用模型。术语physically 来源于,PBR考虑了材料的物理属性, 比如能量守恒以及光的散射。Unity5为艺术家和开发者提供了两种不同的方法,用于创建他们的PBR 材料: 金属工作流 和 镜面工作流。在前者中,一个材料对光的反射取决于其是什么样的金属(或者说,含有多少金属的量)。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/fb366dea235f5a11bd4d198e9f0d2501.html