mat4
The Mat4 interface represents a 4x4 matrix, stored as a 16-element array in column-major order. This type of matrix is commonly used in 3D geometry to represent transformations, including position, rotation, and scale (also known as a TRS matrix). These matrices are essential for defining the position, orientation, and size of objects in a 3D scene.
Certain matrices, such as TRS matrices, have efficiently computable inverses. In these cases, Mat4 allows for the inverse to be calculated in constant time, making it a very fast O(1) operation. Mat4 objects are created using the ecs.math.mat4 factory (Mat4Factory) or through operations on existing Mat4 instances.
Factory
i
Identity matrix
ecs.math.mat4.i() // -> mat4
of
Creates a matrix with directly specified data, using column-major order. An optional inverse can be provided. If not provided, the inverse will be calculated automatically if the matrix is invertible. Attempting to calculate the inverse for a non-invertible matrix will throw an error.
ecs.math.mat4.of(data: number[], inverseData?: number[]) // -> mat4
r
Creates a rotation matrix from a quaternion.
ecs.math.mat4.r(q: QuatSource) // -> mat4
rows
Creates a matrix using specified row data. You can also optionally provide inverse row data. Both dataRows and inverseDataRows should be arrays, each containing four numbers. If the inverse is not provided, it will be computed automatically if the matrix is invertible.
Attempting to calculate the inverse for a non-invertible matrix will throw an error.
ecs.math.mat4.rows(dataRows: number[][], inverseDataRows?: number[][]) // -> mat4
s
Creates a scale matrix. Specify the scale factors along the x, y, and z axes.
ecs.math.mat4.s(x: number, y: number, z: number) // -> mat4
t
Creates a translation matrix. Specify the translation offsets along the x, y, and z axes.
ecs.math.mat4.t(x: number, y: number, z: number) // -> mat4
tr
Creates a combined translation and rotation matrix using a translation vector and a quaternion for the rotation.
ecs.math.mat4.tr(t: Vec3Source, r: QuatSource) // -> mat4
trs
Creates a combined translation, rotation, and scale matrix. Use a translation vector, a quaternion for rotation, and scale factors for x, y, and z axes.
ecs.math.mat4.trs(t: Vec3Source, r: QuatSource, s: Vec3Source) // -> mat4
Immutable
The following methods perform computations using the current value of a Mat4 without altering its contents. Methods that return Mat4 types generate new instances. While immutable APIs are generally safer, more readable, and reduce errors compared to mutable APIs, they may be less efficient in scenarios where thousands of objects are created each frame.
If garbage collection becomes a performance issue, consider using the Mutable API.
clone
Create a new matrix with the same components as this matrix.
ecs.math.mat4.clone() // -> mat4
data
Get the raw data of the matrix, in column-major order.
ecs.math.mat4.data() // -> number[]
decomposeTrs
Decompose the matrix into its translation, rotation, and scale components, assuming it was formed by a translation, rotation, and scale in that order. If ‘target’ is supplied, the result will be stored in ‘target’ and ‘target’ will be returned. Otherwise, a new {t, r, s} object will be created and returned.
ecs.math.mat4.decomposeTrs(target?: {t: Vec3, r: Quat, s: Vec3}) // -> {t: Vec3, r: Quat, s: Vec3}
determinant
Compute the determinant of the matrix.
ecs.math.mat4.determinant() // -> number
equals
Check whether two matrices are equal, with a specified floating point tolerance.
ecs.math.mat4.equals(m: Mat4, tolerance: number) // -> boolean
inv
Invert the matrix, or throw if the matrix is not invertible. Because Mat4 stores a precomputed inverse, this operation is very fast.
ecs.math.mat4.inv() // -> mat4
inverseData
Get the raw data of the inverse matrix, in column-major order, or null if the matrix is not invertible.
ecs.math.mat4.inverseData() // -> number[] | null
lookAt
Get a matrix with the same position and scale as this matrix, but with the rotation set to look at the target.
ecs.math.mat4.lookAt(target: Vec3Source, up: Vec3Source) // -> mat4