Table of Contents

Built-in variables and functions

Besides the material's properties, shader code can read the values Unity provides every frame, such as the time and the camera, and call three libraries of functions.

Built-in variables

The variables are properties of the shader's base class, so shader code uses them directly by name.

Time

Variable Value
Time Time since the level loaded, in seconds: (t / 20, t, t * 2, t * 3). Time.y is the time itself.
SinTime (sin(t / 8), sin(t / 4), sin(t / 2), sin(t)).
CosTime (cos(t / 8), cos(t / 4), cos(t / 2), cos(t)).
TimeParameters (t, sin(t), cos(t), dt).
unity_DeltaTime (dt, 1 / dt, smoothed dt, 1 / smoothed dt).
// Scrolls the texture by half a UV unit per second.
float2 uv = BaseMap.GetTransformedUV(IN.uv0) + new float2(Time.y * 0.5f, 0);

Camera and screen

Variable Value
WorldSpaceCameraPos The camera's position in world space.
ProjectionParams x: 1, or -1 with a flipped projection; y: near plane; z: far plane; w: 1 / far plane.
ScreenParams The render target size: x width and y height in pixels.
ZBufferParams Parameters for converting depth to linear depth; usually you use ShaderApi.LinearEyeDepth instead.
unity_OrthoParams Orthographic camera size; w is 1 for an orthographic camera, 0 for a perspective one.
Important

On HDRP, WorldSpaceCameraPos is an absolute position while IN.positionWS is relative to the camera, so don't subtract one from the other. For the direction to the camera, use ShaderApi.GetWorldSpaceViewDir or ShaderApi.GetWorldSpaceNormalizeViewDir, which work on both pipelines.

Object

Variable Value
unity_LODFade LOD Group cross-fade progress, in x.
unity_WorldTransformParams w is -1 when the object is mirrored by a negative scale.
unity_RenderingLayer The renderer's rendering layer mask.
unity_LightmapST, unity_DynamicLightmapST How the object's lightmap UVs map into the baked and realtime lightmaps.
unity_SHAr … unity_SHC The ambient lighting, as spherical harmonics coefficients.

URP-only variables

ScaledScreenParams, the matrices unity_ObjectToWorld, unity_WorldToObject, unity_PrevObjectToWorld, unity_PrevWorldToObject, unity_MatrixV, unity_MatrixVP, glstate_matrix_projection, unity_CameraProjection and unity_CameraInvProjection exist on URP only. Using them makes the shader fail to compile on HDRP. For code that works on both pipelines, use the ShaderApi transform functions instead, such as ShaderApi.TransformObjectToWorld or ShaderApi.TransformWorldToHClip.

ShaderApi

ShaderApi holds functions from Unity's render pipeline shader libraries. They work the same on URP and HDRP, unless noted otherwise.

Group Functions
Space transforms TransformObjectToWorld, TransformWorldToObject, TransformObjectToWorldNormal, TransformObjectToWorldDir, TransformWorldToView, TransformWorldToHClip and the other directions; GetAbsolutePositionWS, GetCameraRelativePositionWS
View direction GetWorldSpaceViewDir, GetWorldSpaceNormalizeViewDir, GetObjectSpaceNormalizeViewDir
Normals and tangent space UnpackNormal, UnpackNormalScale, UnpackNormalRGB, BlendNormalRNM, CreateTangentToWorld, TransformTangentToWorld, TransformObjectToTangent
Depth LinearEyeDepth, Linear01Depth
Screen GetNormalizedScreenSpaceUV; SampleSceneColor, SampleSceneDepth: the scene behind a transparent object (see The screen)
Parallax GetViewDirectionTangentSpace, ParallaxOffset1Step, ClampNdotV
Object GetObjectScale, GetObjectScaleInverse, GetOddNegativeScale
Environment GetAmbientSkyColor, GetAmbientEquatorColor, GetAmbientGroundColor, GetFog; on HDRP these return 0, like Shader Graph's Ambient and Fog nodes
Pixel control clip, which discards the pixel when its argument is negative
Screen-space derivatives ddx, ddy, fwidth: how fast a value changes between neighboring pixels; Frag only
// A rim of light where the surface turns away from the camera.
float3 viewDir = ShaderApi.GetWorldSpaceNormalizeViewDir(IN.positionWS);
float rim = 1 - math.saturate(math.dot(math.normalize(IN.normalWS), viewDir));

ShaderLib

ShaderLib is a library of ready-made shader functions. Many of them produce the same result as Shader Graph nodes of the same name, so an effect made in Shader Graph can be rebuilt in code.

Group Functions
Noise GradientNoise, SimpleNoise, Voronoi
Shapes Ellipse, Rectangle, RoundedRectangle, Polygon, RoundedPolygon, Checkerboard; Frag only
UV TilingAndOffset, RotateRadians, RotateDegrees, Twirl, Spherize, PolarCoordinates, RadialShear, Flipbook
Utility Remap, Posterize, RandomRange, FresnelEffect, Dither
Lighting units Ev100ToNits, NitsToEv100, ApplyExposureWeight
// A dissolve effect: noise decides which pixels disappear first.
float noise = ShaderLib.SimpleNoise(uv, 20);
ShaderApi.clip(noise - DissolveAmount);

math

math holds the HLSL math functions and constants: sin, lerp, smoothstep, saturate, dot, normalize, mul and the rest, plus constants like math.PI. They are described with the language in Math.