Built-in shader variables 内置的着色器变量
Unity provides a handful of built-in global variables for your shaders: things like current object’s transformation matrices, light parameters, current time and so on. You use them in shader programs like any other variable, the only difference is that you don’t have to declare them - they are all declared inUnityShaderVariables.cginc
include file that is included automatically.
Unity为你的着色器提供了一些内置的全局变量:比如当前对象的转换矩阵、光线参数、当前时间等等。像其他变量一样,你在着色程序中使用它们,唯一的区别是你不需要声明它们——它们都被声明为inunityshader变量。cginc包括自动包含的文件。
Transformations
- 变换
All these matrices are float4x4
type. 所有这些矩阵都是 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 摄像头和屏幕
These variables will correspond to the Camera that is rendering. For example during shadowmap rendering, they will still refer to the Camera component values, and not the “virtual camera” that is used for the shadowmap projection.
这些变量将对应于渲染的摄像机。例如,在阴影图渲染中,他们仍然会参考相机组件值,而不是用于影子地图投影的“虚拟摄像机”。
Name | Type | Value |
_WorldSpaceCameraPos | float3 | World space position of the camera 相机的世界空间位置。. |
_ProjectionParams | float4 |
x是1.0(或-1.0,如果当前渲染的是一个翻转的投影矩阵),y是摄像机的近平面,z是摄像机的远平面,w是1/far平面。. |
_ScreenParams | float4 |
x是相机的渲染目标宽度以像素为单位,y是相机的渲染目标高度为像素,z为1.0+1.0/宽度,w为1.0+1.0/高度。. |
_ZBufferParams | float4 | Used to linearize Z buffer values. 用于线性化Z缓冲区的值。x是(1/接近),y是(法/近),z是(x/很远),w是(/远) |
unity_OrthoParams | float4 |
x是正形相机的宽度,y是正相机的高度,z是未使用的,当相机正正的时候,w是1.0,当透视时是0.0。. |
unity_CameraProjection | float4x4 | Camera’s projection matrix Camera’s matrix放映。. |
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, t*2, t*3), use to animate things inside the shaders 从水平负载(t/20,t,t 2,t 3)开始,用在着色器里面的动画。. |
_SinTime | float4 | Sine of time: (t/8, t/4, t/2, t) 正弦的时间: (t / 8,t/4,t/2 t) |
_CosTime | float4 | Cosine of time: (t/8, t/4, t/2, t) 余弦的时间: (t / 8,t/4,t/2,t)。. |
unity_DeltaTime | float4 | Delta time: (dt, 1/dt, smoothDt, 1/smoothDt) 三角洲时间:(dt,1/dt,沙冰,1/平滑dt)。. |
Lighting
- 灯光
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.
光参数以不同的方式传递给着色器,这取决于使用的渲染路径,以及在着色器中使用的光模式传递标签。
Forward rendering (ForwardBase
and ForwardAdd
pass types):
前向渲染(转发和转发类型):
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 (前进基地仅通过)前4个非重点点灯的世界空间位置。. |
unity_4LightAtten0 | float4 | (ForwardBase pass only) attenuation factors of first four non-important point lights (仅通过)前4个非重要点光源的衰减因子 |
unity_LightColor | half4[4] | (ForwardBase pass only) colors of of first four non-important point lights (向前进)前4个非重点点灯的颜色。. |
Deferred shading and deferred lighting, used in the lighting pass shader (all declared in UnityDeferredLibrary.cginc):
递延阴影和递延照明,用于照明通过材质(所有UnityDeferredLibrary.cginc中声明):
Name | Type
| Value
|
_LightColor | float4 | Light color.
|
_LightMatrix0 | float4x4 | World-to-light matrix. Used to sample cookie & attenuation textures World-to-light矩阵。用于对饼干和衰减纹理进行取样。. |
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.
球的谐波系数(由环境和光探测器所使用)是为前、预通和递延传递类型设置的。它们包含了由世界空间标准评估的第三次订单(见来自unitycg.cginc.的着色)。这些变量都是半类型、unityshar和类似的名称。
Vertex-lit rendering (Vertex
pass type):
vertexlit渲染(顶点传递类型):
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小,其余的颜色就会变成黑色。
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 对空间光的位置。(方向,0)方向灯;(位置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 淡attenuation因素。因为spotAngle(x是- 1 / 2)or for non-spot灯;因为是1 / 4)或(spotAngle / 1 for z是non-spot灯;quadratic attenuation;w is squared light range。. |
unity_SpotDirection | float4[8] | View-space spot light positions; (0,0,1,0) for non-spot lights 视图-空间聚光灯的位置;(0,0,1,0)非聚光灯的位置。. |
Fog and Ambient
Name | Type | Value |
unity_AmbientSky | fixed4 | Sky ambient lighting color in gradient ambient lighting case 天空在环境lighting颜色in gradient lighting case环境。. |
unity_AmbientEquator | fixed4 | Equator ambient lighting color in gradient ambient lighting case 在梯度环境照明中,赤道环境照明颜色。. |
unity_AmbientGround | fixed4 | Ground ambient lighting color in gradient ambient lighting case 在梯度环境照明中,地面环境照明颜色。. |
UNITY_LIGHTMODEL_AMBIENT | fixed4 | Ambient lighting color (sky color in gradient ambient case). Legacy variable 环境照明颜色(在渐变环境中的天空颜色)。遗留的变量。. |
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 雾计算的参数:(密度/sqrt(ln(2))、密度/ln(2)、-1/(结束开始)、结束/(结束开始))。x对于用于线性模式的进出口模式,y表示模式,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未被使用。. |