Built-in state variables in shader programs
Reference Manual > ShaderLab Reference > Writing shader programs > Built-in state variables in shader programs

Built-in state variables in shader programs

Often in shader programs you need to access some global state, for example, the current model*view*projection matrix, the current ambient color, and so on.

The built-in state supported by shaders in Unity is a subset of that of ARB_vertex_program. When using OpenGL it just works due to nature of OpenGL, and when using Direct3D Unity engine tracks this built-in state and makes it available to shader programs. There's no need to declare variables for the built-in state, you can just use them in shader programs.

Built-in matrices

Matrices (float4x4) supported:

glstate.matrix.mvp
Current model*view*projection matrix
glstate.matrix.modelview[0]
Current model*view matrix
glstate.matrix.projection
Current projection matrix
glstate.matrix.transpose.modelview[0]
Transpose of model*view matrix
glstate.matrix.invtrans.modelview[0]
Inverse transpose of model*view matrix
glstate.matrix.texture[0] to glstate.matrix.texture[7]
Texture transformation matrices

Built-in vectors

Vectors (float4) supported:

glstate.light[0].diffuse to glstate.light[3].diffuse
Colors of the first four vertex lights
glstate.light[0].position to glstate.light[3].position
Positions of the first four vertex lights. Point and spot lights have (x,y,z,1) values, and directional lights have their direction here, as (x,y,z,0) value.
glstate.light[0].attenuation to glstate.light[3]. attenuation
Attenuation values for first four vertex lights. x (constant attenuation) is always 1.0, y (linear attenuation) is always 0.0, and z is the quadratic attenuation.
glstate.lightmodel.ambient
Current ambient color.