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:
Row 1: Horizontal Projection (X-axis)
- : Controls the scaling of the X-axis, related to the aspect ratio and field of view. Larger values shrink objects horizontally, while smaller values stretch them.
- : Usually 0 in standard perspective projection. They represent any skewing or translation along the X-axis (used in advanced custom projections).
Row 2: Vertical Projection (Y-axis)
- : Controls the scaling of the Y-axis, determined by the field of view. Larger values shrink objects vertically, while smaller values stretch them.
- : Usually 0 in standard perspective projection. They represent any skewing or translation along the Y-axis.
Row 3: Depth Projection (Z-axis)
- : Maps the Z-axis depth into normalized device coordinates (NDC), which range from -1 to 1 in most conventions. It depends on the near and far clipping planes.
- : This value helps map the depth into the projection, adjusting how objects closer to or farther from the camera appear.
For example, in the matrix:
where is the far clip plane and is the near clip plane.
Row 4: Homogeneous Coordinates
- : Set to -1 in perspective projections. This ensures the perspective divide happens, converting 3D points into 2D coordinates by dividing by the w-component.
- : Typically 0 in perspective projections. It controls the transition from 3D to 2D space.
Practical Values in Unity's Matrix
For Unity's PerspectiveOffCenter
matrix example:
- : Scales the X-axis based on the horizontal size of the near clipping plane.
- : Scales the Y-axis based on the vertical size of the near clipping plane.
- : Shifts the X-axis to adjust for an off-center frustum.
- : Shifts the Y-axis to adjust for an off-center frustum.
- : Depth scaling for normalized device coordinates.
- : Depth translation to map the near and far planes to NDC.
- : Ensures the perspective divide.
Conclusion
- Rows 1 and 2 handle scaling and translation for X and Y axes.
- Row 3 handles depth transformation and clipping.
- Row 4 manages perspective division, converting from 3D space to 2D screen coordinates.
If you're manipulating the matrix directly, you need to understand how these values interact to avoid distortion or unexpected rendering artifacts.