内置的着色器变量

Unity为您的着色器提供了一些内置的全局变量:当前对象的变换矩阵,光参数,当前时间等等。你使用它们着色程序与其他变量一样,唯一的区别在于,你不必声明它们——它们都在UnityShaderVariables声明。包括自动cginc包含文件。

Transformations

这些矩阵是float4x4类型。

Name Value
UNITY_MATRIX_MVP Current model view projection matrix.当前的模型视图投影矩阵。
UNITY_MATRIX_MV Current model view matrix.当前模型视图矩阵。
UNITY_MATRIX_V Current view matrix.当前视图矩阵。
UNITY_MATRIX_P Current projection matrix.当前的投影矩阵。
UNITY_MATRIX_VP Current view * projection matrix.当前视图投影矩阵。
UNITY_MATRIX_T_MV Transpose of model view matrix.模型视图矩阵的转置。
UNITY_MATRIX_IT_MV Inverse transpose of model view matrix.逆模型视图矩阵的转置。
_Object2World Current model matrix.当前的模型矩阵。
_World2Object Inverse of current world matrix.当前的世界矩阵的逆。

Camera and screen

这些变量对应于渲染相机。例如在shadowmap呈现,他们仍将参考相机组件值,而不是“虚拟摄像机”,用于shadowmap投影。

Name Type Value
_WorldSpaceCameraPos float3 World space position of the camera.世界空间相机的位置。
_ProjectionParams float4 x is 1.0 (or –1.0 if currently rendering with a flipped projection matrix), y is the camera’s near plane, z is the camera’s far plane and w is 1/FarPlane.x = 1.0(或-1.0如果目前呈现了投影矩阵),y是相机的进阶近截屏面,z是相机的远截平面和w是1 / FarPlane。
_ScreenParams float4 x is the camera’s render target width in pixels, y is the camera’s render target height in pixels, z is 1.0 + 1.0/width and w is 1.0 + 1.0/height.x是相机的渲染目标像素宽度,y是相机的渲染目标高度的像素,z是1.0 + 1.0 /宽度和w是1.0 + 1.0 /高度。
_ZBufferParams float4 Used to linearize Z buffer values. x is (1-far/near), y is (far/near), z is (x/far) and w is (y/far).用于线性化Z缓冲区的值。x(1-far /附近),y(远/近)、z(x /远)和w(y /远)。
unity_OrthoParams float4 x is orthographic camera’s width, y is orthographic camera’s height, z is unused and w is 1.0 when camera is orthographic, 0.0 when perspective.x是直角的相机的宽度,y是直角的相机的高度,z是未使用的和1.0 w是直角的相机时,0.0时的角度。
unity_CameraProjection float4x4 Camera’s projection matrix.Camera的投影矩阵。
unity_CameraInvProjection float4x4 Inverse of camera’s projection matrix.相机的投影矩阵的逆。
unity_CameraWorldClipPlanes[6] float4 Camera frustum plane world space equations, in this order: left, right, bottom, top, near, far.相机平截头体平面世界空间方程,在这个秩序:左,右,下,上,附近,远。

Time

Name Type Value
_Time float4 Time since level load (t/20, t, t2, t3), use to animate things inside the shaders.
_SinTime float4 Sine of time: (t/8, t/4, t/2, t).
_CosTime float4 Cosine of time: (t/8, t/4, t/2, t).
unity_DeltaTime float4 Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).

Lighting

光参数以不同的方式传递给着色器根据渲染路径,并且LightMode通过标记用于着色器。

Forward渲染(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).世界空间方向,方向灯:(0)。其他灯:(世界空间位置,1)。
_LightMatrix0 (declared in AutoLight.cginc) float4x4 World-to-light matrix. Used to sample cookie & attenuation textures.World-to-light矩阵。用于样品饼干和衰减纹理。
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0 float4 (ForwardBase pass only) world space positions of first four non-important point lights.(仅ForwardBase通过)世界前四的位置点光源也好不重要的空间。
unity_4LightAtten0 float4 (ForwardBase pass only) attenuation factors of first four non-important point lights.(仅ForwardBase通过)衰减因素的四个点光源也好不重要。
unity_LightColor half4[4] (ForwardBase pass only) colors of of first four non-important point lights.(仅ForwardBase通过)颜色的前四个点光源也好不重要。
延迟阴影和延迟照明,用于照明通过材质(所有定义在UnityDeferredLibrary.cginc中):
Name Type Value
LightColor / float4 / Light color. / LightMatrix0 float4x4 World-to-light matrix. Used to sample cookie & attenuation textures.World-to-light矩阵。用于样品饼干和衰减纹理。

球谐系数(被环境光探测器使用)设置为ForwardBase、PrePassFinal和Deferred pass 类型。它们含有第三阶SH被世界空间法线计算(在UnityCG.cginc参见ShadeSH9)。变量都是half4型,unity_SHAr和类似的名字。

Vertex-lit rendering (Vertex pass type):

高达8灯设置为一个顶点Pass类型;总是排序从最亮的一个。因此,如果你想同时渲染受两个光影响的对象,你可以只在数组中取前两个条目。如果有比8更少的灯光影响对象,其余将他们的颜色设置为黑色。

Name Type Value
unity_LightColor half4[8] Light colors.
unity_LightPosition float4[8] View-space light positions. (-direction,0) for directional lights; (position,1) for point/spot lights.
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.
unity_SpotDirection float4[8] View-space spot light positions; (0,0,1,0) for non-spot lights.

Fog and Ambient(雾和环境)

Name Type Value
UNITY_LIGHTMODEL_AMBIENT fixed4 Ambient lighting color (Equator ambient in three-color lighting case).环境照明的颜色(赤道周围的三色的照明情况下)。
unity_AmbientSky fixed4 Sky ambient lighting color in three-color lighting case.

在三色的照明情况下天空环境照明的颜色。| |unity_AmbientGround|fixed4|Ground ambient lighting color in three-color lighting case. 地面环境照明色三色的照明情况。| |unity_FogColor|fixed4|Fog color.| |unity_FogParams|float4|Parameters for fog calculation: (density / sqrt(ln(2)), density / ln(2), –1/(end-start), end/(end-start)). x is useful for Exp2 fog mode, y for Exp mode, z and w for Linear mode.雾计算参数:密度/√(ln(2)),密度/ ln(2),1 /(end-start),结束/(end-start))。x是y用于Exp2雾模式,对于实验模式,z和w线性模式。|

Various(各种各样的)

Name Type Value
unity_LODFade float4 Level-of-detail fade when using LODGroup. x is fade (0..1), y is fade quantized to 16 levels, z and w unused.的详细级别使用LODGroup时消失。x是褪色(0 . . 1),y是褪色量化到16的水平,z和w未使用。