Unity projection matrix

A projection matrix is a 4x4 matrix that maps 3D coordinates in a scene into 2D coordinates on the screen. The individual elements of this matrix control various aspects of the projection, such as scaling, translation, and depth range mapping. Here's a breakdown of the typical elements in a perspective projection matrix:

General Form of a Projection Matrix

For a perspective projection, the matrix typically looks like this:

Matrix=[m00m01m02m03m10m11m12m13m20m21m22m23m30m31m32m33]\text{Matrix} = \begin{bmatrix} m_{00} & m_{01} & m_{02} & m_{03} \\ m_{10} & m_{11} & m_{12} & m_{13} \\ m_{20} & m_{21} & m_{22} & m_{23} \\ m_{30} & m_{31} & m_{32} & m_{33} \end{bmatrix}

Row 1: Horizontal Projection (X-axis)


Row 2: Vertical Projection (Y-axis)


Row 3: Depth Projection (Z-axis)

For example, in the matrix:

m22=(f+n)fn,m23=2fnfnm_{22} = \frac{-(f + n)}{f - n}, \quad m_{23} = \frac{-2fn}{f - n}

where ff is the far clip plane and nn is the near clip plane.


Row 4: Homogeneous Coordinates


Practical Values in Unity's Matrix

For Unity's PerspectiveOffCenter matrix example:

[x0a00yb000cd00e0]\begin{bmatrix} x & 0 & a & 0 \\ 0 & y & b & 0 \\ 0 & 0 & c & d \\ 0 & 0 & e & 0 \end{bmatrix}
  1. x=2nrightleftx = \frac{2n}{\text{right} - \text{left}}: Scales the X-axis based on the horizontal size of the near clipping plane.
  2. y=2ntopbottomy = \frac{2n}{\text{top} - \text{bottom}}: Scales the Y-axis based on the vertical size of the near clipping plane.
  3. a=right+leftrightlefta = \frac{\text{right} + \text{left}}{\text{right} - \text{left}}: Shifts the X-axis to adjust for an off-center frustum.
  4. b=top+bottomtopbottomb = \frac{\text{top} + \text{bottom}}{\text{top} - \text{bottom}}: Shifts the Y-axis to adjust for an off-center frustum.
  5. c=(f+n)fnc = \frac{-(f + n)}{f - n}: Depth scaling for normalized device coordinates.
  6. d=2fnfnd = \frac{-2fn}{f - n}: Depth translation to map the near and far planes to NDC.
  7. e=1e = -1: Ensures the perspective divide.

Conclusion

If you're manipulating the matrix directly, you need to understand how these values interact to avoid distortion or unexpected rendering artifacts.