Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


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
SamplerState
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
sampler_linear_repeat
.
Note
These sampler suffixes only work if you target the DX11, DX12, Metal or Vulkan graphics API.
You must include a single filtering mode and a single texture wrapping mode. The other suffixes are optional.

Suffix

Description

point
Enables nearest texture filtering.
linear
Enables bilinear texture filtering.
trilinear
Enables trilinear texture filtering.
clamp
Sets the texture wrapping mode to clamp.
repeat
Sets the texture wrapping mode to repeat.
mirror
Sets the texture wrapping mode to mirror.
mirroronce
Sets the texture wrapping mode to mirror once. If you target a mobile platform that doesn't support
mirroronce
, Unity falls back to
mirror
.
compare
Sets the sampler to compare depth values. For more information refer to SampleCmp and SampleCmpLevelZero in the Microsoft HLSL documentation.
aniso2
,
aniso4
,
aniso8
,
aniso16
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
U
,
V
or
W
to a wrap mode to set the wrapping mode for that axis only. For example,
sampler_clampu_point
sets the U axis to clamp mode, and leaves the V and W axes at their default wrapping mode.
For more information, refer to:

Transformations

All these matrices are
float4x4
type, and are column major.
Note
If you use the Universal Render Pipeline (URP), you can also use variables from the
Core.hlsl
library file. For more information, refer to Transform positions in a custom URP shader.

Name

Value

UNITY_MATRIX_MVPCurrent model * view * projection matrix.
UNITY_MATRIX_MVCurrent model * view matrix.
UNITY_MATRIX_VCurrent view matrix.
UNITY_MATRIX_PCurrent projection matrix.
UNITY_MATRIX_VPCurrent view * projection matrix.
UNITY_MATRIX_T_MVTranspose of model * view matrix.
UNITY_MATRIX_IT_MVInverse transpose of model * view matrix.
unity_ObjectToWorldCurrent model matrix.
unity_WorldToObjectInverse 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.
Note
If you use the Universal Render Pipeline (URP), you can also use variables from the
Core.hlsl
library file. For more information, refer to Use the camera in a custom URP shader.

Name

Type

Value

_WorldSpaceCameraPosfloat3World space position of the camera.
_ProjectionParamsfloat4
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.
_ScreenParamsfloat4
x
is the width of the camera's target texture in pixels,
y
is the height of the camera's target texture in pixels,
z
is 1.0 + 1.0/width and
w
is 1.0 + 1.0/height.
_ZBufferParamsfloat4Used to linearize Z buffer values.
unity_OrthoParamsfloat4The following parameters:
  • x
    : half the width of the camera view in world space
  • y
    : half the height of the camera view in world space
  • z
    : unused
  • w
    : 1.0 if the camera is orthographic, or 0.0 if the camera is perspective
unity_CameraProjectionfloat4x4Camera's projection matrix.
unity_CameraInvProjectionfloat4x4Inverse of camera's projection matrix.
unity_CameraWorldClipPlanes[6]float4Camera 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
Time.time
, which is the time since the application started. If you use the Built-In Render Pipeline, time matches
Time.timeSinceLevelLoad
instead.

Name

Type

Value

_Timefloat4
(t/20, t, t\*2, t\*3)
. Use for animations inside shaders.
_SinTimefloat4Sine of time: (t/8, t/4, t/2, t).
_CosTimefloat4Cosine of time: (t/8, t/4, t/2, t).
unity_DeltaTimefloat4Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).

Lightmaps

Name

Type

Value

unity_LightmapTexture2DContains lightmap information.
unity_LightmapSTfloat4[8]Scales and translates the UV information to the correct range to sample the lightmap texture.

Fog and Ambient

Name

Type

Value

unity_AmbientSkyfixed4Sky ambient lighting color in gradient ambient lighting case.
unity_AmbientEquatorfixed4Equator ambient lighting color in gradient ambient lighting case.
unity_AmbientGroundfixed4Ground ambient lighting color in gradient ambient lighting case.
unity_IndirectSpecColorfixed4If you use a skybox, this is the average color of the skybox, which Unity calculates using the
DC
component of the spherical harmonics data in the ambient probe. Otherwise this is black.
UNITY_LIGHTMODEL_AMBIENTfixed4Ambient lighting color (sky color in gradient ambient case). Legacy variable.
unity_FogColorfixed4Fog color.
unity_FogParamsfloat4Parameters 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_LODFadefloat4Level-of-detail fade when using LODGroup. x is fade (0..1), y is fade quantized to 16 levels, z and w unused.
_TextureSampleAddfloat4Set 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)).