Skip to content

Built in Shader Variables

mika edited this page Oct 16, 2017 · 1 revision

Transform Matrices

  • unity_ObjectToWorld
    • Transforms the mesh vertices from their local mesh space to Unity world space. This is the same world space as your scene
  • unity_WorldToObject
    • Transforms world space into local mesh space
    • Its important to understand here is this is not necessarily local gameObject space, this is local mesh space, and these can be different. Most commonly when your model's import settings have a scale factor. If you're using skeletal meshes it's the local position after being transformed by skeletal animation.

Then there's the other UNITY_MATRIX_* values:

  • UNITY_MATRIX_MVP
    • This is the most important one, this single matrix does all the transforms from the initial mesh space position into projection space (aka clip space)
    • This matrix is the product of UNITY_MATRIX_M, UNITY_MATRIX_V, and UNITY_MATRIX_P together
  • UNITY_MATRIX_M
    • This is identical to unity_ObjectToWorld, also called the Model transform
  • UNITY_MATRIX_V
    • This is the transform from world space to local View space
    • This is similar to if you had an gameObject as a child of a camera gameObject, but without any scale applied
    • This means the positions are all in world space distances, but with the camera at 0,0,0 and rotated to match the camera's orientation
  • UNITY_MATRIX_P
    • This is the transform from view space to Projection space
    • Projection space, or clip space, can be thought of as the position on screen, with anything on the far left edge of the screen, regardless of how far away, has an x of "-1", and on the right "1". It's actually going to be negative and positive "w", but we'll skip that for now.

There also exist combinations of these 3 main matrices, likes UNITY_MATRIX_MV, or UNITY_MATRIX_VP, which do exactly what you might expect. UNITY_MATRIX_MV transforms from model into view space, and UNITY_MATRIX_VP from world space into projection space.

There are also UNITY_MATRIX_T_MV, UNITY_MATRIX_IT_MV, UNITY_MATRIX_I_V and many other matrices that don't have the UNITY_MATRIX_* prefix, some of which are duplicates (like unity_ObjectToWorld and UNITY_MATRIX_M). We'll ignore those for now.

A float4x4 transform matrix stores the scale, rotation, and translation with the first 3x3 being the scale and rotation and the last row float3 being the translation.

Resources:

Original Source: