Built-in shader variables reference
Reference for the global variables Unity provides for your custom shaders, such as transformation matrices and light parameters.
Read time 6 minutesLast updated 5 days ago
Unity's built-in include files contain 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, but if you include the relevant include file, you don't have to declare them.
For more information on include files, see Built-in include files.
Inline sampler suffixes
Add the following suffixes when you declare an inline sampler in HLSL code, to manually set texture filtering and wrapping modes. Separate multiple words with an underscore (). For example, to set a sampler to use linear filtering and repeat wrapping mode, name it .
SamplerState_sampler_linear_repeatYou must include a single filtering mode and a single texture wrapping mode. The other suffixes are optional.
Suffix | Description |
|---|---|
| Enables nearest texture filtering. |
| Enables bilinear texture filtering. |
| Enables trilinear texture filtering. |
| Sets the texture wrapping mode to clamp. |
| Sets the texture wrapping mode to repeat. |
| Sets the texture wrapping mode to mirror. |
| Sets the texture wrapping mode to mirror once. If you target a mobile platform that doesn't support |
| Sets the sampler to compare depth values. For more information refer to SampleCmp and SampleCmpLevelZero in the Microsoft HLSL documentation. |
| Enables anisotropic filtering with 2x, 4x, 8x, or 16x sampling. Depending on the graphics API and platform you target, Unity might clamp the filtering to a lower value, or disable anisotropic filtering completely. |
Add , or to a wrap mode to set the wrapping mode for that axis only. For example, sets the U axis to clamp mode, and leaves the V and W axes at their default wrapping mode.
UVWsampler_clampu_pointFor more information, refer to:
Transformations
All these matrices are type, and are column major.
float4x4Name | 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. |
| unity_ObjectToWorld | Current model matrix. |
| unity_WorldToObject | 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 | |
| _ScreenParams | float4 | |
| _ZBufferParams | float4 | Used to linearize Z buffer values.
|
| unity_OrthoParams | float4 | The following parameters:
|
| 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
Time is measured in seconds, and is scaled by the Time multiplier in your Project's Time settings. There is no built-in variable that provides access to unscaled time.
Time matches , which is the time since the application started. If you use the Built-In Render Pipeline, time matches instead.
Time.timeTime.timeSinceLevelLoadName | Type | Value |
|---|---|---|
| _Time | float4 | |
| _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). |
Lightmaps
Name | Type | Value |
|---|---|---|
| unity_Lightmap | Texture2D | Contains lightmap information. |
| unity_LightmapST | float4[8] | Scales and translates the UV information to the correct range to sample the lightmap texture. |
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_IndirectSpecColor | fixed4 | If you use a skybox, this is the average color of the skybox, which Unity calculates using the |
| 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 LODGroup. x is fade (0..1), y is fade quantized to 16 levels, z and w unused. |
| _TextureSampleAdd | float4 | Set automatically by Unity for UI only based on whether the texture being used is in Alpha8 format (the value is set to (1,1,1,0)) or not (the value is set to (0,0,0,0)). |