Struct Matrix4x4
Represents a 4×4 column-major transformation matrix for 3D geometry.
public struct Matrix4x4
- Inherited Members
Remarks
Vectors and points are treated as column vectors and transformed by pre-multiplication:
v' = M * v. Translation is stored in the last column (M14, M24, M34).
The matrix is stored in row-major memory order (M_row_col) as 16 sequential floats (64 bytes), fitting exactly into four 128-bit SIMD registers.
To interoperate with Matrix4x4 (which uses row-vector convention) use the explicit cast operators, which perform the necessary transpose.
Constructors
Matrix4x4(float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float)
Initializes a new Matrix4x4 with the specified row-ordered coefficients.
public Matrix4x4(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44)
Parameters
m11floatm12floatm13floatm14floatm21floatm22floatm23floatm24floatm31floatm32floatm33floatm34floatm41floatm42floatm43floatm44float
Fields
Identity
The identity matrix.
public static readonly Matrix4x4 Identity
Field Value
M11
Row 1, Column 1 — X basis X component (scale X for pure-scale matrices).
public float M11
Field Value
M12
Row 1, Column 2 — X basis Y shear.
public float M12
Field Value
M13
Row 1, Column 3 — X basis Z shear.
public float M13
Field Value
M14
Row 1, Column 4 — Translation along X.
public float M14
Field Value
M21
Row 2, Column 1 — Y basis X shear.
public float M21
Field Value
M22
Row 2, Column 2 — Y basis Y component (scale Y for pure-scale matrices).
public float M22
Field Value
M23
Row 2, Column 3 — Y basis Z shear.
public float M23
Field Value
M24
Row 2, Column 4 — Translation along Y.
public float M24
Field Value
M31
Row 3, Column 1 — Z basis X shear.
public float M31
Field Value
M32
Row 3, Column 2 — Z basis Y shear.
public float M32
Field Value
M33
Row 3, Column 3 — Z basis Z component (scale Z for pure-scale matrices).
public float M33
Field Value
M34
Row 3, Column 4 — Translation along Z.
public float M34
Field Value
M41
Row 4, Column 1 — Perspective X component (0 for affine transforms).
public float M41
Field Value
M42
Row 4, Column 2 — Perspective Y component (0 for affine transforms).
public float M42
Field Value
M43
Row 4, Column 3 — Perspective Z component (0 for affine transforms).
public float M43
Field Value
M44
Row 4, Column 4 — Homogeneous W scale (1 for affine transforms).
public float M44
Field Value
Properties
Determinant
Returns the determinant of the matrix. A determinant of zero means the matrix is not invertible.
public float Determinant { get; }
Property Value
Inverse
Returns the inverse of this matrix.
public Matrix4x4 Inverse { get; }
Property Value
Remarks
For rotation-only or TRS (translation-rotation-scale) matrices, consider computing the inverse analytically for better performance. Throws a division-by-zero style error if the matrix is singular (determinant ≈ 0).
IsIdentity
Returns true when this matrix equals the identity matrix.
public bool IsIdentity { get; }
Property Value
Transposed
Returns the transpose of this matrix (swaps rows and columns).
public Matrix4x4 Transposed { get; }
Property Value
Methods
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
Equals(Matrix4x4)
Returns true if this matrix equals another Matrix4x4.
public bool Equals(Matrix4x4 other)
Parameters
otherMatrix4x4
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
LookAt(Point3, Point3, Vector3)
Creates a look-at view matrix (right-handed coordinate system).
public static Matrix4x4 LookAt(Point3 eye, Point3 target, Vector3 up)
Parameters
eyePoint3The position of the camera.
targetPoint3The position the camera is looking at.
upVector3The world up direction (typically Up).
Returns
Orthographic(float, float, float, float)
Creates a symmetric orthographic projection matrix (right-handed, depth range [-1, 1]).
public static Matrix4x4 Orthographic(float width, float height, float near, float far)
Parameters
widthfloatThe width of the view volume.
heightfloatThe height of the view volume.
nearfloatDistance to the near clipping plane.
farfloatDistance to the far clipping plane.
Returns
Perspective(float, float, float, float)
Creates a symmetric perspective projection matrix (right-handed, depth range [-1, 1]).
public static Matrix4x4 Perspective(float fovY, float aspect, float near, float far)
Parameters
fovYfloatVertical field of view in radians.
aspectfloatAspect ratio (width / height).
nearfloatDistance to the near clipping plane (must be positive).
farfloatDistance to the far clipping plane (must be greater than
near).
Returns
Rotate(Quaternion)
Creates a rotation matrix from a unit quaternion.
public static Matrix4x4 Rotate(Quaternion q)
Parameters
Returns
RotateX(float)
Creates a rotation matrix around the X axis by the given angle in radians.
public static Matrix4x4 RotateX(float angle)
Parameters
anglefloat
Returns
RotateY(float)
Creates a rotation matrix around the Y axis by the given angle in radians.
public static Matrix4x4 RotateY(float angle)
Parameters
anglefloat
Returns
RotateZ(float)
Creates a rotation matrix around the Z axis by the given angle in radians.
public static Matrix4x4 RotateZ(float angle)
Parameters
anglefloat
Returns
Scale(float)
Creates a uniform scale matrix.
public static Matrix4x4 Scale(float s)
Parameters
sfloat
Returns
Scale(Vector3)
Creates a non-uniform scale matrix.
public static Matrix4x4 Scale(Vector3 v)
Parameters
vVector3
Returns
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
Translate(Vector3)
Creates a translation matrix that moves a point by the given vector.
public static Matrix4x4 Translate(Vector3 v)
Parameters
vVector3
Returns
Operators
operator ==(Matrix4x4, Matrix4x4)
Returns true if two matrices have equal coefficients.
public static bool operator ==(Matrix4x4 lhs, Matrix4x4 rhs)
Parameters
Returns
explicit operator Matrix4x4(Matrix4x4)
Explicitly converts from a Matrix4x4.
public static explicit operator Matrix4x4(Matrix4x4 m)
Parameters
Returns
Remarks
Matrix4x4 uses row-vector convention. This conversion transposes the input matrix so that it works correctly with the column-vector convention used by this type.
explicit operator Matrix4x4(Matrix4x4)
Explicitly converts to a Matrix4x4.
public static explicit operator Matrix4x4(Matrix4x4 m)
Parameters
Returns
Remarks
Matrix4x4 uses row-vector convention, which is the transpose of the column-vector convention used by this type. The conversion transposes the matrix so that row-vector math on the result produces the same geometric transformation.
operator !=(Matrix4x4, Matrix4x4)
Returns true if any coefficient of two matrices differs.
public static bool operator !=(Matrix4x4 lhs, Matrix4x4 rhs)
Parameters
Returns
operator *(Matrix4x4, Matrix4x4)
Composes two transformation matrices.
The result applies rhs first, then lhs.
public static Matrix4x4 operator *(Matrix4x4 lhs, Matrix4x4 rhs)
Parameters
Returns
operator *(Matrix4x4, Point3)
Transforms a Point3 by this matrix, including the translation component. Use this for positions.
public static Point3 operator *(Matrix4x4 m, Point3 p)
Parameters
Returns
operator *(Matrix4x4, Vector3)
Transforms a Vector3 by this matrix, ignoring the translation component. Use this for directions and normals.
public static Vector3 operator *(Matrix4x4 m, Vector3 v)