Skip to main content

mat4

Class

4x4 matrix used for 3D transformations including rotation, scale, translation, and projection.

Index

Example

const identity = new mat4();
console.log('mat4 identity column0:', identity.column0.x, identity.column0.y, identity.column0.z, identity.column0.w);

const a = new mat4();
a.column0 = new vec4(2, 0, 0, 0);
a.column1 = new vec4(0, 2, 0, 0);
a.column2 = new vec4(0, 0, 2, 0);
a.column3 = new vec4(0, 0, 0, 1);

const b = new mat4();
b.column0 = new vec4(1, 0, 0, 0);
b.column1 = new vec4(0, 1, 0, 0);
b.column2 = new vec4(0, 0, 1, 0);
b.column3 = new vec4(5, 10, 15, 1);

const result = a.mult(b);
console.log('Multiplied mat4 column3 (translation):', result.column3.x, result.column3.y, result.column3.z);

Constructors

Constructor

new mat4(): mat4

Constructs a new identity mat4.

Returns

mat4

Properties

column0

column0: vec4

First column of the matrix as a vec4.


column1

column1: vec4

Second column of the matrix as a vec4.


column2

column2: vec4

Third column of the matrix as a vec4.


column3

column3: vec4

Fourth column of the matrix as a vec4.


description

description: string

Returns a string representation of the matrix.

Methods

add()

add(mat): mat4

Adds another mat4 to this matrix and returns the result.

Parameters

ParameterType
matmat4

Returns

mat4


compMult()

static compMult(a, b): mat4

Performs component-wise multiplication of two mat4 matrices and returns the result.

Parameters

ParameterType
amat4
bmat4

Returns

mat4


compose()

static compose(translation, rotation, scale): mat4

Constructs a mat4 from translation, rotation quaternion, and scale components.

Parameters

ParameterType
translationvec3
rotationquat
scalevec3

Returns

mat4


determinant()

determinant(): number

Returns the scalar determinant of this matrix.

Returns

number


div()

div(mat): mat4

Divides this matrix by another mat4 element-wise and returns the result.

Parameters

ParameterType
matmat4

Returns

mat4


equal()

equal(mat): boolean

Returns true if this matrix equals the given mat4.

Parameters

ParameterType
matmat4

Returns

boolean


extractEulerAngles()

extractEulerAngles(): vec3

Extracts and returns the Euler angles as a vec3 from this matrix.

Returns

vec3


fromColumns()

static fromColumns(column0, column1, column2, column3): mat4

Creates a mat4 from four column vectors.

Parameters

ParameterType
column0vec4
column1vec4
column2vec4
column3vec4

Returns

mat4


fromEulerAngles()

static fromEulerAngles(angles): mat4

Creates a mat4 rotation matrix from Euler angles in radians.

Parameters

ParameterType
anglesvec3

Returns

mat4


fromEulerX()

static fromEulerX(angle): mat4

Creates a mat4 rotation matrix for a rotation around the X axis.

Parameters

ParameterType
anglenumber

Returns

mat4


fromEulerY()

static fromEulerY(angle): mat4

Creates a mat4 rotation matrix for a rotation around the Y axis.

Parameters

ParameterType
anglenumber

Returns

mat4


fromEulerZ()

static fromEulerZ(angle): mat4

Creates a mat4 rotation matrix for a rotation around the Z axis.

Parameters

ParameterType
anglenumber

Returns

mat4


fromRotation()

static fromRotation(rotation): mat4

Creates a mat4 rotation matrix from a quaternion.

Parameters

ParameterType
rotationquat

Returns

mat4


fromRows()

static fromRows(row0, row1, row2, row3): mat4

Creates a mat4 from four row vectors.

Parameters

ParameterType
row0vec4
row1vec4
row2vec4
row3vec4

Returns

mat4


fromScale()

static fromScale(scale): mat4

Creates a mat4 scale matrix from a vec3.

Parameters

ParameterType
scalevec3

Returns

mat4


fromTranslation()

static fromTranslation(translation): mat4

Creates a mat4 translation matrix from a vec3.

Parameters

ParameterType
translationvec3

Returns

mat4


identity()

static identity(): mat4

Returns a mat4 identity matrix.

Returns

mat4


inverse()

inverse(): mat4

Returns the inverse of this matrix.

Returns

mat4


lookAt()

static lookAt(eye, center, up): mat4

Creates a view matrix that orients toward a target from an eye position.

Parameters

ParameterType
eyevec3
centervec3
upvec3

Returns

mat4


makeBasis()

static makeBasis(x, y, z): mat4

Constructs a mat4 from three basis vectors.

Parameters

ParameterType
xvec3
yvec3
zvec3

Returns

mat4


mult()

mult(mat): mat4

Multiplies this matrix by another mat4 and returns the result.

Parameters

ParameterType
matmat4

Returns

mat4


multiplyDirection()

multiplyDirection(direction): vec3

Transforms a direction vector by this matrix, ignoring translation.

Parameters

ParameterType
directionvec3

Returns

vec3


multiplyPoint()

multiplyPoint(point): vec3

Transforms a point by this matrix, applying translation.

Parameters

ParameterType
pointvec3

Returns

vec3


multiplyScalar()

multiplyScalar(scalar): mat4

Multiplies this matrix by a scalar value and returns the result.

Parameters

ParameterType
scalarnumber

Returns

mat4


multiplyVector()

multiplyVector(vector): vec4

Multiplies this matrix by a vec4 and returns the result.

Parameters

ParameterType
vectorvec4

Returns

vec4


orthographic()

static orthographic(left, right, bottom, top, near, far): mat4

Creates an orthographic projection matrix.

Parameters

ParameterType
leftnumber
rightnumber
bottomnumber
topnumber
nearnumber
farnumber

Returns

mat4


outerProduct()

static outerProduct(a, b): mat4

Computes the outer product of two vec4 vectors and returns the resulting mat4.

Parameters

ParameterType
avec4
bvec4

Returns

mat4


perspective()

static perspective(fov, aspect, near, far): mat4

Creates a perspective projection matrix.

Parameters

ParameterType
fovnumber
aspectnumber
nearnumber
farnumber

Returns

mat4


sub()

sub(mat): mat4

Subtracts another mat4 from this matrix and returns the result.

Parameters

ParameterType
matmat4

Returns

mat4


toString()

toString(): string

Returns a string representation of this matrix.

Returns

string


transpose()

transpose(): mat4

Returns the transpose of this matrix.

Returns

mat4


zero()

static zero(): mat4

Returns a mat4 with all elements set to zero.

Returns

mat4