Mat4
Interface representing a 4x4 matrix. A 4x4 matrix is represented by a 16 dimensional array of data,
with elements stored in column major order. A special kind of matrix, known as a TRS matrix (for
Translation, Rotation, and Scale) is common in 3D geometry for representing the position,
orientation, and size of points in a 3D scene. Many special types of matrices have easily specified
inverses. By specifying these ahead of time, Mat4 allows for matrix inverse to be a very fast O(1)
operation. Mat4 objects are created with the ecs.math.mat4
Mat4Factory, or through operations on
other Mat4 objects.
Code Example
const {mat4, quat, vec3} = ecs.math
const [targetX, targetY, targetZ] = [1, 2, 3]
const [pitch, yaw, distance] = [30, 90, 5]
// Compute orbit controls position based on target position, pitch, yaw, and distance.
const orbitPos = mat4.t(vec3.xyz(targetX, targetY, targetZ))
.times(mat4.r(quat.pitchYawRollDegrees(vec3.xyz(pitch, yaw, 0))))
.times(mat4.t(vec3.xyz(0, 0, distance)))
.times(mat4.r(quat.yDegrees(180)))
Factory
Mat4 objects are created with the ecs.math.mat4
MatFactory, with the following methods:
mat4.i()
i: () => Mat4
Identity matrix.
mat4.of()
of: (data: number[], inverseData?: number[]) => Mat4
Create the matrix with directly specified data, in column major order. An optional inverse can be specified. If the inverse is not specified, it will be computed if the matrix is invertible. If the matrix is not invertible, calling inv() will throw an error.
mat4.r()
r: (q: QuatSource) => Mat4
Create a rotation matrix from a quaternion.
mat4.rows()
rows: (dataRows: number[][], inverseDataRows?: number[][]) => Mat4
Create a matrix with specified row data, and optionally specified inverse row data. dataRows and inverseDataRows should be four arrays, each with four numbers. If the inverse is not specified, it will be computed if the matrix is invertible. If the matrix is not invertible, calling inv() will throw an error.
mat4.s()
s: (v: Vec3Source) => Mat4
Create a scale matrix. No scale element should be zero.
mat4.t()
t: (v: Vec3Source) => Mat4
Create a translation matrix.
mat4.tr()
tr: (t: Vec3Source, r: QuatSource) => Mat4
Create a translation and rotation matrix.
mat4.trs()
trs: (t: Vec3Source, r: QuatSource, s: Vec3Source) => Mat4
Create a translation, rotation and scale matrix.