Unity Shader 内置变量(Built-in shader variables)

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 in UnityShaderVariables.cgincinclude file that is included automatically.

Unity提供了非常有用的内置全局变量,比如:当前Object的变换矩阵、光照参数、当前时间等。内置变量与其他变量一样能在Shader程序中使用,唯一不同的是,不需要再重新定义它们,这些变量都定义在 UnityShaderVariables.cginc(unity安装目录下查找)中

Transformations

All these matrices are float4x4 type.

   
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 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.x。
_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.
_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/near), y 是(far/near), z 为(x / far )和 w 是(y / far )。
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没有使用,当时正交相机的时候w是1,是透视相机的时候为0
unity_CameraProjection float4x4 Camera’s projection 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.
_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

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):
光照参数:根据使用的渲染路径传入的参数,及在Shader中使用的光照模式
     
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).
_LightMatrix0 (declared in AutoLight.cginc) float4x4 World-to-light matrix. Used to sample cookie & attenuation textures.
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0 float4 (ForwardBase pass only) world space positions of first four non-important point lights.
unity_4LightAtten0 float4 (ForwardBase pass only) attenuation factors of first four non-important point lights.
unity_LightColor half4[4] (ForwardBase pass only) colors of of first four non-important point lights.

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

     
Name Type Value
_LightColor float4 Light color.
_LightMatrix0 float4x4 World-to-light matrix. Used to sample cookie & attenuation textures.

Spherical harmonics coefficients (used by ambient and light probes) are set up for ForwardBasePrePassFinal 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.

Vertex-lit rendering (Vertex pass type):

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.

     
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_AmbientSky fixed4 Sky ambient lighting color in gradient ambient 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.

Various

     
Name Type Value
unity_LODFade float4 Level-of-detail fade when using LODGroupx is fade (0..1), y is fade quantized to 16 levels, z and w unused.
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值