大自然中,我们看到植被一般都是绿色,什么原因?
因为植被把光线中的其他颜色的光吸收了,只反色出绿色。这里我们明白两个道理
1、人看到的物体都是物体反射光线到眼睛中
2、材质决定了物体和灯光的作用
我觉得材质是对我们人眼可见物体的一种封装,不仅封装了视觉,同时也是对触觉、味觉等这些感知的一种封装。
慢反色、折射等具体物理方面的原理我就不讲了。
慢反色结论:1、跟灯光的入射角度有关 2、跟观察角度无关,360度看慢反色都是一样
镜面反色 :1、只有眼睛跟手电筒反色光线平行的时候能看到光 2、从其他角度查看是黑色的 3、跟入射角和观察角都有关
大自然中任何物体都不是单一的慢反色或镜面反色,都是两种情况的结合,在Unity当中同样也是如此。根据材质的不同,只是两者的权重不一样罢了。我们在Unity当中,正是通过计算多少发生镜面反色,多少发生慢反色,最后得到的材质就不一样。
比如:金属的镜面反色肯定比慢反色的权重大,木材质的慢反色肯定比镜面反色权重大。
慢反色灯光计算的模拟
L:灯光入射角
N:顶点的法线
E:观察角(也就是摄像机)
慢反色 = Dot(L,N)*灯光的颜色*衰减率
镜面反色灯光计算的模拟
L:入射光线
N:法线
E:观察角
R:反色角度
Phone式模型:R Dot E
BillingPhone式模型:
L:入射光线
N:法线
E:(摄像机)
H:(E-L)向量
BilingPhone:(E-L)dot N = H dot E*灯光的颜色*衰减值
最终结果就是:镜面反色+慢反色+自发光+环境光
关于灯光的计算
Declaring lighting models
A lighting model consists of regular functions with names that begin Lighting
. You can declare them anywhere in your shader file, or one of the included files. The functions are:
half4 Lighting<Name> (SurfaceOutput s, UnityGI gi);
Use this in forward rendering paths for light models that are not dependent on the view direction.half4 Lighting<Name> (SurfaceOutput s, half3 viewDir, UnityGI gi);
Use this in forward rendering paths for light models that are dependent on the view direction.half4 Lighting<Name>_Deferred (SurfaceOutput s, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal);
Use this in deferred lighting paths.half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light);
Use this in light prepass (legacy deferred) lighting paths.
以上就是unity用于计算灯光的API,我们只需要调用。最常用的是第一种和第二种
half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
我在这里用代码模拟了一下慢反色和镜面反色的计算方式
要注意灯光入口函数的声明和最终版式
以上就是unity渲染光照模型经常用到的慢反色和镜面反色
写完了,卧槽。