@web-engine-dev/math / Mat4
Class: Mat4
A 4x4 matrix stored in column-major order.
Memory layout (column-major, matching WebGPU/OpenGL): | m0 m4 m8 m12 | | m1 m5 m9 m13 | | m2 m6 m10 m14 | | m3 m7 m11 m15 |
Column vectors: [m0,m1,m2,m3], [m4,m5,m6,m7], [m8,m9,m10,m11], [m12,m13,m14,m15]
Default operations are immutable and return new instances. For performance-critical code paths (game loops, hot paths), use the mutable variants (suffix Mut) or static out-parameter methods (suffix Out) to avoid allocations.
Constructors
Constructor
new Mat4(
data?):Mat4
Parameters
data?
ArrayLike<number>
Returns
Mat4
Methods
add()
add(
m):Mat4
Parameters
m
Mat4
Returns
Mat4
copyMut()
copyMut(
other):this
Copies another matrix into this matrix.
Parameters
other
Mat4
Source matrix
Returns
this
this for chaining
copyToArray()
copyToArray(
arr,offset?):void
Parameters
arr
number[] | Float32Array<ArrayBufferLike>
offset?
number = 0
Returns
void
decompose()
decompose(
outTranslation,outRotation,outScale):boolean
Decomposes this matrix into translation, rotation, and scale. Assumes the matrix has no skew.
Parameters
outTranslation
Output translation vector
outRotation
Output rotation quaternion
w
number
x
number
y
number
z
number
outScale
Output scale vector
Returns
boolean
true if decomposition succeeded
Example
const worldMatrix = entity.getWorldMatrix();
const position = new Vec3();
const rotation = { x: 0, y: 0, z: 0, w: 1 };
const scale = new Vec3();
worldMatrix.decompose(position, rotation, scale);determinant()
determinant():
number
Returns
number
equals()
equals(
m,epsilon?):boolean
Parameters
m
Mat4
epsilon?
number = 0
Returns
boolean
get()
get(
row,col):number
Parameters
row
number
col
number
Returns
number
getColumn()
getColumn(
col):Vec4
Parameters
col
number
Returns
getRotationScale()
getRotationScale():
Mat3
Extract the upper-left 3x3 rotation/scale matrix.
Returns
getRow()
getRow(
row):Vec4
Parameters
row
number
Returns
getScale()
getScale():
Vec3
Returns
getTranslation()
getTranslation():
Vec3
Returns
invert()
invert():
Mat4|null
Returns
Mat4 | null
invertMut()
invertMut():
Mat4|null
Inverts this matrix in place.
Returns
Mat4 | null
this for chaining, or null if matrix is singular
mul()
mul(
s):Mat4
Parameters
s
number
Returns
Mat4
mulMat()
mulMat(
m):Mat4
Matrix-matrix multiplication (this * m).
Parameters
m
Mat4
Returns
Mat4
multiplyMut()
multiplyMut(
m):this
Multiplies this matrix by another matrix in place (this = this * m).
Parameters
m
Mat4
Matrix to multiply with
Returns
this
this for chaining
mulVec3Direction()
mulVec3Direction(
v):Vec3
Transform a Vec3 as a direction (w=0).
Parameters
v
Returns
mulVec3Point()
mulVec3Point(
v):Vec3
Transform a Vec3 as a point (w=1) and divide by w.
Parameters
v
Returns
mulVec4()
mulVec4(
v):Vec4
Transform a Vec4 by this matrix.
Parameters
v
Returns
normalMatrix()
normalMatrix():
Mat3|null
Compute the normal matrix (inverse transpose of upper-left 3x3).
Returns
Mat3 | null
normalMatrixOut()
normalMatrixOut(
target):boolean
Compute the normal matrix and write to target Mat3 (no allocations). Normal matrix = transpose(inverse(upper-left 3x3))
This is the allocation-free version for performance-critical code paths. Use this in render loops to avoid GC pressure from per-frame allocations.
Parameters
target
Pre-allocated Mat3 to write result into
Returns
boolean
true if successful, false if matrix is singular
Example
// In a render loop - reuse the same Mat3 each frame
const normalMatrix = Mat3.identity();
worldMatrix.normalMatrixOut(normalMatrix);
// normalMatrix now contains the resultpreMultiplyMut()
preMultiplyMut(
m):this
Pre-multiplies this matrix by another (result = m * this). Useful for applying parent transforms.
Parameters
m
Mat4
Matrix to pre-multiply with
Returns
this
this for chaining
scaleMut()
scaleMut(
s):this
Applies a uniform scale to this matrix (post-multiply). Equivalent to this = this * scale(s) but faster.
Parameters
s
number
Scale factor
Returns
this
this for chaining
scaleVecMut()
scaleVecMut(
v):this
Applies a scale from a vector to this matrix.
Parameters
v
Scale vector
Returns
this
this for chaining
scaleXYZMut()
scaleXYZMut(
sx,sy,sz):this
Applies a non-uniform scale to this matrix (post-multiply).
Parameters
sx
number
Scale X
sy
number
Scale Y
sz
number
Scale Z
Returns
this
this for chaining
setFromElementsMut()
setFromElementsMut(
m00,m01,m02,m03,m10,m11,m12,m13,m20,m21,m22,m23,m30,m31,m32,m33):this
Sets this matrix from individual elements (no allocations). Order matches Mat4.fromArray and internal column-major layout.
Parameters
m00
number
m01
number
m02
number
m03
number
m10
number
m11
number
m12
number
m13
number
m20
number
m21
number
m22
number
m23
number
m30
number
m31
number
m32
number
m33
number
Returns
this
setFromRotationMut()
setFromRotationMut(
q):this
Sets this matrix to a rotation matrix from a quaternion.
Parameters
q
Quaternion representing rotation
w
number
x
number
y
number
z
number
Returns
this
this for chaining
setFromScaleMut()
setFromScaleMut(
v):this
Sets this matrix to a scale matrix.
Parameters
v
Scale vector
Returns
this
this for chaining
setFromTranslationMut()
setFromTranslationMut(
v):this
Sets this matrix to a translation matrix.
Parameters
v
Translation vector
Returns
this
this for chaining
setFromTRSMut()
setFromTRSMut(
translation,rotation,scale):this
Sets this matrix to a TRS matrix.
Parameters
translation
Translation vector
rotation
Rotation quaternion
w
number
x
number
y
number
z
number
scale
Scale vector
Returns
this
this for chaining
setIdentityMut()
setIdentityMut():
this
Sets this matrix to the identity matrix.
Returns
this
this for chaining
setTranslationMut()
setTranslationMut(
x,y,z):this
Sets the translation component of this matrix without affecting rotation/scale.
Parameters
x
number
Translation X
y
number
Translation Y
z
number
Translation Z
Returns
this
this for chaining
setTranslationVecMut()
setTranslationVecMut(
v):this
Sets the translation component from a vector.
Parameters
v
Translation vector
Returns
this
this for chaining
sub()
sub(
m):Mat4
Parameters
m
Mat4
Returns
Mat4
toArray()
toArray():
number[]
Returns
number[]
toFloat32Array()
toFloat32Array():
Float32Array
Returns
Float32Array
toString()
toString():
string
Returns
string
transformDirectionMut()
transformDirectionMut(
v):Vec3
Transforms a Vec3 as a direction in place.
Parameters
v
Direction vector (modified in place)
Returns
v for chaining
transformVec3Mut()
transformVec3Mut(
v):Vec3
Transforms a Vec3 as a point (w=1) in place. Returns a large vector if w is near zero to avoid Infinity/NaN.
Parameters
v
Vector to transform (modified in place)
Returns
v for chaining
translateMut()
translateMut(
x,y,z):this
Applies a translation to this matrix (post-multiply). Equivalent to this = this * translation(x, y, z) but faster.
Parameters
x
number
Translation X
y
number
Translation Y
z
number
Translation Z
Returns
this
this for chaining
translateVecMut()
translateVecMut(
v):this
Applies a translation from a vector to this matrix.
Parameters
v
Translation vector
Returns
this
this for chaining
transpose()
transpose():
Mat4
Returns
Mat4
transposeMut()
transposeMut():
this
Transposes this matrix in place.
Returns
this
this for chaining
fromArray()
staticfromArray(arr,offset?):Mat4
Parameters
arr
ArrayLike<number>
offset?
number = 0
Returns
Mat4
fromColumns()
staticfromColumns(c0,c1,c2,c3):Mat4
Parameters
c0
c1
c2
c3
Returns
Mat4
fromTRS()
staticfromTRS(translation,rotation,scale):Mat4
Creates a TRS (Translation-Rotation-Scale) matrix in a single operation. More efficient than multiplying separate matrices.
Parameters
translation
Translation vector
rotation
Rotation quaternion
w
number
x
number
y
number
z
number
scale
Scale vector
Returns
Mat4
New TRS matrix
Example
const position = new Vec3(10, 0, 5);
const rotation = Quat.fromEuler(0, Math.PI / 4, 0); // 45° Y rotation
const scale = Vec3.one();
const worldMatrix = Mat4.fromTRS(position, rotation, scale);identity()
staticidentity():Mat4
Returns
Mat4
invertOut()
staticinvertOut(m,out):Mat4|null
Inverts a matrix and writes the result to out.
Parameters
m
Mat4
Matrix to invert
out
Mat4
Output matrix
Returns
Mat4 | null
out for chaining, or null if matrix is singular
lookAt()
staticlookAt(eye,target,up):Mat4
Creates a look-at view matrix.
Parameters
eye
Camera position.
target
Point to look at.
up
Up vector.
Returns
Mat4
Example
const eye = new Vec3(0, 5, 10);
const target = Vec3.zero();
const up = Vec3.up();
const viewMatrix = Mat4.lookAt(eye, target, up);lookAtOut()
staticlookAtOut(eye,target,up,out):Mat4
Writes a look-at view matrix into an existing Mat4 (no allocations).
Parameters
eye
Camera position.
target
Point to look at.
up
Up vector.
out
Mat4
Output matrix to write into.
Returns
Mat4
multiplyChainOut()
staticmultiplyChainOut(matrices,out):Mat4
Computes the product of multiple matrices efficiently. More efficient than chaining multiply calls.
Parameters
matrices
readonly Mat4[]
Array of matrices to multiply (left to right)
out
Mat4
Output matrix
Returns
Mat4
out for chaining
multiplyOut()
staticmultiplyOut(a,b,out):Mat4
Multiplies two matrices and writes the result to out.
Parameters
a
Mat4
First matrix
b
Mat4
Second matrix
out
Mat4
Output matrix
Returns
Mat4
out for chaining
multiplyOutSafe()
staticmultiplyOutSafe(a,b,out):Mat4
Multiplies this matrix by another, storing result in out. Allows a === out or b === out safely.
Parameters
a
Mat4
First matrix
b
Mat4
Second matrix
out
Mat4
Output matrix
Returns
Mat4
out for chaining
orthographic()
staticorthographic(left,right,bottom,top,near,far):Mat4
Creates an orthographic projection matrix.
Parameters
left
number
right
number
bottom
number
top
number
near
number
far
number
Returns
Mat4
perspective()
staticperspective(fovY,aspect,near,far):Mat4
Creates a perspective projection matrix. Uses standard WebGPU depth range (near=0, far=1). For reverse-Z (better depth precision), use perspectiveReverseZ().
Parameters
fovY
number
Vertical field of view in radians.
aspect
number
Aspect ratio (width / height).
near
number
Near plane distance.
far
number
Far plane distance.
Returns
Mat4
Example
const fov = Math.PI / 4; // 45 degrees
const aspect = canvas.width / canvas.height;
const projMatrix = Mat4.perspective(fov, aspect, 0.1, 1000);perspectiveOut()
staticperspectiveOut(fovY,aspect,near,far,out):Mat4
Writes a perspective projection matrix into an existing Mat4 (no allocations).
Parameters
fovY
number
aspect
number
near
number
far
number
out
Mat4
Returns
Mat4
perspectiveReverseZ()
staticperspectiveReverseZ(fovY,aspect,near,far):Mat4
Creates a perspective projection matrix with reverse-Z. Depth goes from 1 (near) to 0 (far) for better precision.
Parameters
fovY
number
aspect
number
near
number
far
number
Returns
Mat4
rotationAxis()
staticrotationAxis(axis,angle):Mat4
Creates a rotation matrix around an arbitrary axis.
Parameters
axis
Normalized rotation axis.
angle
number
Rotation angle in radians.
Returns
Mat4
rotationX()
staticrotationX(angle):Mat4
Creates a rotation matrix around the X axis.
Parameters
angle
number
Rotation angle in radians.
Returns
Mat4
rotationY()
staticrotationY(angle):Mat4
Creates a rotation matrix around the Y axis.
Parameters
angle
number
Rotation angle in radians.
Returns
Mat4
rotationZ()
staticrotationZ(angle):Mat4
Creates a rotation matrix around the Z axis.
Parameters
angle
number
Rotation angle in radians.
Returns
Mat4
scale()
staticscale(s):Mat4
Creates a uniform scale matrix.
Parameters
s
number
Returns
Mat4
scaling()
staticscaling(sx,sy,sz):Mat4
Creates a non-uniform scale matrix.
Parameters
sx
number
sy
number
sz
number
Returns
Mat4
scalingVec()
staticscalingVec(v):Mat4
Parameters
v
Returns
Mat4
setTRSMut()
staticsetTRSMut(out,translation,rotation,scale):Mat4
Sets this matrix to a TRS (Translation-Rotation-Scale) matrix. More efficient than multiplying separate matrices.
Parameters
out
Mat4
Output matrix
translation
Translation vector
rotation
Rotation quaternion
w
number
x
number
y
number
z
number
scale
Scale vector
Returns
Mat4
out for chaining
transformDirectionOut()
statictransformDirectionOut(m,v,out):Vec3
Transforms a Vec3 as a direction (w=0) and writes the result to out. Ignores translation component.
Parameters
m
Mat4
Transformation matrix
v
Direction vector
out
Output vector
Returns
out for chaining
transformVec3Out()
statictransformVec3Out(m,v,out):Vec3
Transforms a Vec3 as a point (w=1) and writes the result to out.
Parameters
m
Mat4
Transformation matrix
v
Vector to transform
out
Output vector
Returns
out for chaining
translation()
statictranslation(x,y,z):Mat4
Creates a translation matrix.
Parameters
x
number
y
number
z
number
Returns
Mat4
translationVec()
statictranslationVec(v):Mat4
Parameters
v
Returns
Mat4
transposeOut()
statictransposeOut(m,out):Mat4
Transposes a matrix and writes the result to out.
Parameters
m
Mat4
Matrix to transpose
out
Mat4
Output matrix
Returns
Mat4
out for chaining
zero()
staticzero():Mat4
Returns
Mat4