Matrix4

Represents a 4x4 matrix.

Constructor

new Matrix4()

Constructs a new 4x4 matrix.

Members

elements :Array.<number>

A column-major list of matrix values.

Type:
  • Array.<number>

(readonly) isMatrix4 :boolean

This flag can be used for type testing.

Type:
  • boolean
Default Value
  • true

Methods

clone() → {Matrix4}

Returns a matrix with copied values from this instance.

Returns:

A clone of this instance.

Type: 
Matrix4

compose(position, quaternion, scale) → {Matrix4}

Sets this matrix to the transformation composed of the given position, rotation (Quaternion) and scale.

Parameters:
NameTypeDescription
positionVector3

The position vector.

quaternionQuaternion

The rotation as a Quaternion.

scaleVector3

The scale vector.

Returns:

A reference to this matrix.

Type: 
Matrix4

copy(m) → {Matrix4}

Copies the values of the given matrix to this instance.

Parameters:
NameTypeDescription
mMatrix4

The matrix to copy.

Returns:

A reference to this matrix.

Type: 
Matrix4

decompose(position, quaternion, scale) → {Matrix4}

Decomposes this matrix into its position, rotation and scale components and provides the result in the given objects. Note: Not all matrices are decomposable in this way. For example, if an object has a non-uniformly scaled parent, then the object's world matrix may not be decomposable, and this method may not be appropriate.

Parameters:
NameTypeDescription
positionVector3

The position vector.

quaternionQuaternion

The rotation as a Quaternion.

scaleVector3

The scale vector.

Returns:

A reference to this matrix.

Type: 
Matrix4

determinant() → {number}

Computes and returns the determinant of this matrix. Based on the method outlined here.

Returns:

The determinant.

Type: 
number

equals(matrix) → {boolean}

Returns true if this matrix is equal with the given one.

Parameters:
NameTypeDescription
matrixMatrix4

The matrix to test for equality.

Returns:

Whether this matrix is equal with the given one.

Type: 
boolean

extractBasis(xAxis, yAxis, zAxis) → {Matrix4}

Extracts the basis of this matrix into the three axis vectors provided.

Parameters:
NameTypeDescription
xAxisVector3

The basis's x axis.

yAxisVector3

The basis's y axis.

zAxisVector3

The basis's z axis.

Returns:

A reference to this matrix.

Type: 
Matrix4

extractRotation(m) → {Matrix4}

Extracts the rotation component of the given matrix into this matrix's rotation component.

Note: This method does not support reflection matrices.

Parameters:
NameTypeDescription
mMatrix4

The matrix.

Returns:

A reference to this matrix.

Type: 
Matrix4

fromArray(array, offsetopt) → {Matrix4}

Sets the elements of the matrix from the given array.

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<number>

The matrix elements in column-major order.

offsetnumber<optional>
0

Index of the first element in the array.

Returns:

A reference to this matrix.

Type: 
Matrix4

getMaxScaleOnAxis() → {number}

Gets the maximum scale value of the three axes.

Returns:

The maximum scale.

Type: 
number

identity() → {Matrix4}

Sets this matrix to the 4x4 identity matrix.

Returns:

A reference to this matrix.

Type: 
Matrix4

invert() → {Matrix4}

Inverts this matrix, using the analytic method. You can not invert with a determinant of zero. If you attempt this, the method produces a zero matrix instead.

Returns:

A reference to this matrix.

Type: 
Matrix4

isIdentity() → {boolean}

Checks if the matrix is an identity matrix.

Returns:
  • True if the matrix is an identity matrix, false otherwise.
Type: 
boolean

lerpMatrices(m1, m2, ratio) → {Matrix4}

Linearly interpolates between two matrix4.

Parameters:
NameTypeDescription
m1Matrix4
m2Matrix4
rationumber
Returns:
Type: 
Matrix4

lookAtRH(eye, target, up) → {Matrix4}

Constructs a rotation matrix, looking from eye towards center oriented by the up vector.

Parameters:
NameTypeDescription
eyeVector3

The eye vector.

targetVector3

The target vector.

upVector3

The up vector.

Returns:

A reference to this matrix.

Type: 
Matrix4

makeBasis(xAxis, yAxis, zAxis) → {Matrix4}

Sets the given basis vectors to this matrix.

Parameters:
NameTypeDescription
xAxisVector3

The basis's x axis.

yAxisVector3

The basis's y axis.

zAxisVector3

The basis's z axis.

Returns:

A reference to this matrix.

Type: 
Matrix4

makeRotationAxis(axis, angle) → {Matrix4}

Sets this matrix as a rotational transformation around the given axis by the given angle. This is a somewhat controversial but mathematically sound alternative to rotating via Quaternions. See the discussion here.

Parameters:
NameTypeDescription
axisVector3

The normalized rotation axis.

anglenumber

The rotation in radians.

Returns:

A reference to this matrix.

Type: 
Matrix4

makeRotationFromEuler(euler) → {Matrix4}

Sets the rotation component (the upper left 3x3 matrix) of this matrix to the rotation specified by the given Euler angles. The rest of the matrix is set to the identity. Depending on the Euler#order, there are six possible outcomes. See this page for a complete list.

Parameters:
NameTypeDescription
eulerEuler

The Euler angles.

Returns:

A reference to this matrix.

Type: 
Matrix4

makeRotationFromQuaternion(q) → {Matrix4}

Sets the rotation component of this matrix to the rotation specified by q, as outlined here.

Parameters:
NameTypeDescription
qQuaternion
Returns:
Type: 
Matrix4

makeScale(x, y, z) → {Matrix4}

Sets this matrix as a scale transformation.

Parameters:
NameTypeDescription
xnumber

The amount to scale in the X axis.

ynumber

The amount to scale in the Y axis.

znumber

The amount to scale in the Z axis.

Returns:

A reference to this matrix.

Type: 
Matrix4

makeTranslation(x, y, z) → {Matrix4}

Sets this matrix as a translation transform from the given vector.

Parameters:
NameTypeDescription
xnumber | Vector3

The amount to translate in the X axis or alternatively a translation vector.

ynumber

The amount to translate in the Y axis.

znumber

The amount to translate in the z axis.

Returns:

A reference to this matrix.

Type: 
Matrix4

multiply(m) → {Matrix4}

Post-multiplies this matrix by the given 4x4 matrix.

Parameters:
NameTypeDescription
mMatrix4

The matrix to multiply with.

Returns:

A reference to this matrix.

Type: 
Matrix4

multiplyMatrices(a, b) → {Matrix4}

Multiples the given 4x4 matrices and stores the result in this matrix.

Parameters:
NameTypeDescription
aMatrix4

The first matrix.

bMatrix4

The second matrix.

Returns:

A reference to this matrix.

Type: 
Matrix4

premultiply(m) → {Matrix4}

Pre-multiplies this matrix by the given 4x4 matrix.

Parameters:
NameTypeDescription
mMatrix4

The matrix to multiply with.

Returns:

A reference to this matrix.

Type: 
Matrix4

set(n11opt, n12opt, n13opt, n14opt, n21opt, n22opt, n23opt, n24opt, n31opt, n32opt, n33opt, n34opt, n41opt, n42opt, n43opt, n44opt) → {Matrix4}

Sets the elements of the matrix.The arguments are supposed to be in row-major order.

Parameters:
NameTypeAttributesDescription
n11number<optional>

1-1 matrix element.

n12number<optional>

1-2 matrix element.

n13number<optional>

1-3 matrix element.

n14number<optional>

1-4 matrix element.

n21number<optional>

2-1 matrix element.

n22number<optional>

2-2 matrix element.

n23number<optional>

2-3 matrix element.

n24number<optional>

2-4 matrix element.

n31number<optional>

3-1 matrix element.

n32number<optional>

3-2 matrix element.

n33number<optional>

3-3 matrix element.

n34number<optional>

3-4 matrix element.

n41number<optional>

4-1 matrix element.

n42number<optional>

4-2 matrix element.

n43number<optional>

4-3 matrix element.

n44number<optional>

4-4 matrix element.

Returns:

A reference to this matrix.

Type: 
Matrix4

setFromMatrix3(m) → {Matrix4}

Set the upper 3x3 elements of this matrix to the values of given 3x3 matrix.

Parameters:
NameTypeDescription
mMatrix3

The 3x3 matrix.

Returns:

A reference to this matrix.

Type: 
Matrix4

setPosition(x, y, z) → {Matrix4}

Sets the position component for this matrix from the given vector, without affecting the rest of the matrix.

Parameters:
NameTypeDescription
xnumber | Vector3

The x component of the vector or alternatively the vector object.

ynumber

The y component of the vector.

znumber

The z component of the vector.

Returns:

A reference to this matrix.

Type: 
Matrix4

toArray(arrayopt, offsetopt) → {Array.<number>}

Writes the elements of this matrix to the given array. If no array is provided, the method returns a new instance.

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<number><optional>
[]

The target array holding the matrix elements in column-major order.

offsetnumber<optional>
0

Index of the first element in the array.

Returns:

The matrix elements in column-major order.

Type: 
Array.<number>

transpose() → {Matrix4}

Transposes this matrix in place.

Returns:

A reference to this matrix.

Type: 
Matrix4