Unity3D内建参数文档翻译与解析——Built-in shader variables

来源:互联网 发布:2016电力弹力系数数据 编辑:程序博客网 时间:2024/05/13 07:25

本篇将会结合Unity3D官方文档对Unity3D内置参数或函数进行讲解及文档翻译。
本篇将会持续更新,更新结束后这一句话将会删除。

光照部分

本次讲解的内容为光照相关,因为我接下来的篇章将会对Unity3D光照相关的知识进行分析,所以在此处对一些光照方面的基础性的知识作一些整理。本次的内容为光照相关的内建参数
:为了避免网络上中文术语混乱带来的理解障碍,一些专业术语将保留使用英语。


Light parameters are passed to shaders in different ways depending on which Rendering Path is used, and which LightMode Pass Tag is used in the shader.

根据使用Rendering Path的不同,以及Shader中的Pass内的LightMode标签(Pass Tag)的不同,光照相关参数通过不同的方式被传递给Shader。


Forward rendering (ForwardBase and ForwardAdd pass types):

Forward rendering(包含ForwardBase和ForwardAdd两种类型):

Name Type Value _LightColor0 (declared in Lighting.cginc) fixed4 Light color.灯光颜色 _WorldSpaceLightPos0 float4 Directional lights: (world space direction, 0). Other lights: (world space position, 1).
分为两种情况:c平行光时:float4的xyzw最后一个w分量为0,表示这是一个平行光,xyz代表其光源方向
点光源时:最后一个分量为1,表示这是一个点光源,xyz代表点光源在世界坐标系内的位置 _LightMatrix0 (declared in AutoLight.cginc) float4x4
World-to-light matrix. Used to sample cookie & attenuation textures.
这是一个由世界空间变换至光源空间(即光源的局部坐标系)的矩阵。可用于计算光照衰减。变换原理可以参考《3D Math Primer for Graphics and Game Development》或大学线性代数。在Shader中使用mul(param1 , param1)函数进行变换。 unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0 float4 (ForwardBase pass only) world space positions of first four non-important point lights.
仅在ForwardBase pass 有效,代表的是世界空间内的前四个非重要点光源的位置。有人可能会问为什么明明只有三个参数,却能代表四个位置,是因为如下规则:
unity_4LightPosX0 的四个分量xyzw分别代表的是四个点光源的x坐标,也就是说这个值的xyzw值都代表着四个点光源各自的x坐标,另外两个参数同理。这样就得到四个点光源的位置信息。 unity_4LightAtten0 float4 (ForwardBase pass only) attenuation factors of first four non-important point lights.
仅在ForwardBase pass 有效,代表着前四个非重要点光源的衰减系数。注:当没有数据时的值是1而不是0。 unity_LightColor half4[4] (ForwardBase pass only) colors of of first four non-important point lights.
仅在ForwardBase pass 有效,这是一个half4类型的4分量数组,里面的4个参数代表上述的四个点光源的颜色信息。

Deferred shading and deferred lighting, used in the lighting pass shader (all declared in UnityDeferredLibrary.cginc):

Deferred shading 和Deferred lighting相关的光照参数:

Name Type Value _LightColor float4 Light color.
灯光颜色 _LightMatrix0 float4x4 World-to-light matrix. Used to sample cookie & attenuation textures.
这是一个由世界空间变换至光源空间(即光源的局部坐标系)的矩阵。可用于计算光照衰减。变换原理可以参考《3D Math Primer for Graphics and Game Development》或大学线性代数。在Shader中使用mul(param1 , param1)函数进行变换。

Spherical harmonics coefficients (used by ambient and light probes) are set up for ForwardBase, PrePassFinal and Deferred pass types. They contain 3rd order SH to be evaluated by world space normal (see ShadeSH9 from UnityCG.cginc). The variables are all half4 type, unity_SHAr and similar names.

球谐系数(用于环境与光照探头(以后有机会再做讲解))适用于ForwardBase, PrePassFinal和Deferred光照模式的Pass。包含了由世界空间法线计算而得的第三阶球谐函数(参考 UnityCG.cginc文件中的ShaderSH9函数;此部分内容需要一定的数学知识,球谐相关涉及傅里叶变换,3rd order SH即third order Spherical Harmonics,球谐函数相关资料)。这些参数都是half4类型,具有和unity_SHAr相似的命名。


Vertex-lit rendering (Vertex pass type):

Vertex-lit rendering(LightMode为Vertex及Vertex相关的Pass):

Up to 8 lights are set up for a Vertex pass type; always sorted starting from the brightest one. So if you want to render objects affected by two lights at once, you can just take first two entries in the arrays. If there are less lights affecting the object than 8, the rest will have their color set to black.

顶点光照模式可高达8个光源,并且总是从最亮的光源排序(也就是说第一个元素就是最亮的光源的数据)。如果你想要利用两个光源来对物体进行渲染,则可以直接使用数组内的前两个数据,如果实际光源数量不满8个,则空闲的数据都为0,即代表着颜色为黑色。

Name Type Value unity_LightColor half4[8] Light color.
灯光颜色 unity_LightPosition float4[8] View-space light positions. (-direction,0) for directional lights; (position,1) for point/spot lights.
视点空间内的灯光位置。
w分量为0则代表是平行光,前三分量为光源方向的负值
w分量为1则代表为点光源或聚光灯,前三分量代表光源所有的位置 unity_LightAtten half4[8] Light attenuation factors. x is cos(spotAngle/2) or –1 for non-spot lights; y is 1/cos(spotAngle/4) or 1 for non-spot lights; z is quadratic attenuation; w is squared light range.
光源衰减系数,
非聚光灯:
X = cos(spotAngle/2)或-1
Y = 1/cos(spotAngle/4)或1
Z ,W表示光线按距离以二次函数衰减(暂时理解如是,有待实验) unity_SpotDirection float4[8] View-space spot light positions; (0,0,1,0) for non-spot lights.
聚光灯在视点空间内的位置, (0,0,1,0) 表示非聚光灯。
0 0
原创粉丝点击