Skip to main content
Represents a 4x4 column-major transformation matrix, suitable for 3D transforms and direct upload to a GPU uniform buffer.

Fields

m11

Element at row R, column C (1-indexed). For example m.m12 is the entry in row 1, column 2.

m12

m13

m14

m21

m22

m23

m24

m31

m32

m33

m34

m41

m42

m43

m44

Constructors

identity

Returns the 4x4 identity matrix.

values

Creates a matrix from 16 components, supplied column-major (column 0 first: c0x, c0y, c0z, c0w, c1x, …).

fromTranslation

Creates a translation matrix.

fromScale

Creates a scale matrix. If y and z are omitted, uniform scaling by x is applied to all axes.

fromRotationX

Creates a rotation matrix around the X axis (radians).

fromRotationY

Creates a rotation matrix around the Y axis (radians).

fromRotationZ

Creates a rotation matrix around the Z axis (radians).

perspective

Creates a right-handed perspective projection matrix that maps z to the [0, 1] NDC depth range. fovY is in radians; near and far are positive distances.

perspectiveReverseZ

Right-handed perspective with reverse-Z (near -> 1, far -> 0) and an infinite far plane. Combined with a float depth buffer this gives a near-uniform 1/z depth distribution across the entire frustum — the best precision an arbitrary scene can achieve. The depth buffer must be cleared to 0 (not 1) and the depth test flipped to GREATER.

lookAt

Creates a right-handed view matrix looking from eye toward center (GLM convention). up need not be normalized.

ortho

Creates a right-handed orthographic projection matrix that maps z to the [0, 1] NDC depth range (matching Mat4.perspective), e.g. for shadow-map cascades or 2D overlays.

multiply

Writes a * b into output, reusing its storage. Returns output. Use this in tight loops to avoid allocating a new Mat4 per multiplication. output may alias a or b.

multiplyAffine

Like Mat4.multiply, but assumes both inputs are affine (bottom row [0, 0, 0, 1] — any composition of translation/scale/rotation matrices qualifies). Skips the bottom-row work for ~25% fewer FLOPs per multiplication. The result is always affine. Caller must ensure both inputs are affine; passing a non-affine input gives an incorrect result.

Static Functions

invert

Writes the inverse of input into output, returning true on success or false if input is singular.

invertAffine

Writes the affine inverse of input into output. See Mat4:invertAffine for the contract; faster than Mat4.invert.

Methods

invert

Linear column-major access. Indices 1..4 are column 0, 5..8 are column 1, 9..12 are column 2, 13..16 are column 3 (translation).
Returns the inverse of the matrix, or nil if the matrix is singular.

transpose

Returns the transpose of the matrix.

transformPoint

Transforms the given 3D point by the matrix (treating w = 1) and returns the result as a Vector. If the homogeneous w of the result is non-unit (e.g. after a perspective matrix), the returned point is divided through.

transformVec4

Transforms the given homogeneous 4D vector by the matrix and returns the four components as multiple values, without perspective divide. Use this when the caller needs to inspect or carry the homogeneous w (e.g. clip-space coordinates).

writeToBuffer

Writes the matrix as 64 bytes (column-major, 16 float32 entries) into the given buffer at byteOffset. The buffer must have at least 64 bytes available from the offset.

__eq

Returns true if all 16 components of the two matrices are equal.

__mul

Returns the matrix product of two matrices. Useful for composing transforms, e.g. proj * view * model. Allocates a new Mat4; in tight loops prefer Mat4.multiply(out, a, b) to reuse storage.

invertAffine

Closed-form inverse for affine matrices (bottom row [0, 0, 0, 1] — any composition of fromTranslation/fromScale/fromRotationX/Y/Z qualifies). Returns nil if the linear part is singular. Faster than :invert() and avoids the full 4x4 cofactor work.