fbpx
🚀Get the Unity Shaders Bible with a 15% OFF! Only for a limited time🚀

Here's your free sample!

1.1.6. Matrices and coordinate systems

This post is also available in…

One of the concepts seen frequently in the creation of shaders is matrices. A matrix is a list of numeric elements that follow certain arithmetic rules and are frequently used in Computer Graphics. 

In Unity the matrices represent a spatial transformation and among them are: 

  • UNITY_MATRIX_MVP. 
  • UNITY_MATRIX_MV. 
  • UNITY_MATRIX_V. 
  • UNITY_MATRIX_P. 
  • UNITY_MATRIX_VP. 
  • UNITY_MATRIX_T_MV. 
  • UNITY_MATRIX_IT_MV. 
  • unity_ObjectToWorld. 
  • unity_WorldToObject. 

All of these correspond to four-by-four matrices (4×4), that is, each of them has four rows and four columns of numerical values. 

They are conceptually represented is as follows:

UNITY_MATRIX
(
Xx,     Yx,    Zx,     Tx,
Xy,     Yy,     Zy,     Ty,
Xz,     Yz,     Zz,     Tz,
Xt,     Yt,     Zt,     Tw
);

As previously explained in section 1.0.2 talking about vertices, a polygon object has two nodes by default. In Maya, these nodes are known as Transform and Shape, and both are in charge of calculating the position of the vertices in a space called Object-Space, which defines the position of the vertices in relation to the position of the object’s center. 

The final value of each vertex in the object is multiplied by a matrix known as the Model Matrix (UNITY_MATRIX_M), which allows you to modify its transformation, rotation and scale values. Every time the object is rotated, has its position or scale changed the Model Matrix is ​​updated.

How does this process occur? To understand it try transforming a Cube in the scene. Start by taking a vertex of the Cube that is in the position 0.5X, -0.5Y, -0.5Z, 1.0W with respect to its center. 

It is worth mentioning that the channel W corresponds to a coordinate system called homogeneous, which allows the uniform handling of vectors and points. In matrix transformations, the W coordinate can be equal to zero or one. When nW equals 1, it refers to a point in space, while when it equals 0, it refers to a direction.

Later, this book talks about this system when vectors are multiplied by matrices and vice versa.

(Fig. 1.1.6a. Identity matrix refers to the matrix default values) 

One thing to consider with respect to matrices is that a multiplication can be carried out only when the number of columns of the first matrix is equal to the number of rows of the second. As already known, this Model Matrix has a dimension of four rows and four columns, and the position of the vertices has a dimension of four rows and one column. 

Since the number of columns in the Model Matrix is equal to the number of rows in the position of the vertices, they can be multiplied and the result will be equal to a new matrix of four rows and one column, which would define a new position for the vertices. This multiplication process occurs for all vertices in the object and is carried out in the Vertex Shader Stage.

Up to this point you already know that Object-Space refers to the position of a vertex according to its own center, so what does World-Space, View-Space or Clip-Space mean?  The concept is basically the same. 

World-Space corresponds to the position of a vertex according to the center of the world; to the distance between the starting point of the grid in our scene (0X, 0Y, 0Z, 1W) and the position of a vertex on the object. 

If you want to transform a space coordinate from Object-Space to World-Space you can use the internal variable unity_ObjectToWorld.

(Fig. 1.1.6b)

View-Space refers to the position of a vertex of our object relative to the camera view. If you want to transform a space coordinate from World-Space to View-Space, you can use the UNITY_MATRIX_V matrix.

(Fig. 1.1.6c)

Finally, Clip-Space, also known as Projection-Space, refers to the position of an object vertex in relation to the camera’s frustum. So, this factor will be affected by the Near Clipping Plane, Far Clipping Plane and Field of View.

If you want to transform a space coordinate from View-Space to Clip-Space, you can do it using the UNITY_MATRIX_P matrix.

(Fig. 1.1.6d)

In general, the different space coordinates have been talked about at a conceptual level, but what the transformation matrices refer to, has not yet been defined. 

For example, the Built-in shader variable UNITY_MATRIX_MVP refers to the multiplication of three different matrices. M refers to the Model Matrix, V the View Matrix, and P the Projection Matrix. This matrix is mainly used to transform object vertices from Object-Space to Clip-Space. Remember that the polygonal object has been created in a three-dimensional environment while the screen of the computer, where it will be projected, is two-dimensional, therefore you will have to transform the object from one space to another. 
Later, this book will review these concepts in detail when using the UnityObjectToClipPos( VRG ) function included in the shader, in the Vertex Shader Stage.

Follow us to stay informed about all the latest news, updates, and more.

Join the group to share your experiences with other developers.

Subscribe to our channel and keep learning game dev!

Jettelly Team

We are a team of indie developers with more than 9 years of experience in video games. As an independent studio, we have developed Nom Noms in which we published with Hyperbeard in 2019. We are currently developing The Unity Shader Bible.

Follow us on our social networks.