A class representing a 4x4 matrix.

The most common use of a 4x4 matrix in 3D computer graphics is as a Transformation Matrix. For an introduction to transformation matrices as used in WebGL, check out this tutorial. This allows a Vector3 representing a point in 3D space to undergo transformations such as translation, rotation, shear, scale, reflection, orthogonal or perspective projection and so on, by being multiplied by the matrix. This is known as applying the matrix to the vector.

A Note on Row-Major and Column-Major Ordering The set() method takes arguments in row-major order, while internally they are stored in the elements array in column-major order. This means that calling

const m = new THREE.Matrix4();

m.set( 11, 12, 13, 14,
21, 22, 23, 24,
31, 32, 33, 34,
41, 42, 43, 44 );

will result in the elements array containing:

m.elements = [ 11, 21, 31, 41,
12, 22, 32, 42,
13, 23, 33, 43,
14, 24, 34, 44 ];

and internally all calculations are performed using column-major ordering. However, as the actual ordering makes no difference mathematically and most people are used to thinking about matrices in row-major order, the three.js documentation shows matrices in row-major order. Just bear in mind that if you are reading the source code, you'll have to take the transpose of any matrices outlined here to make sense of the calculations.

Extracting position, rotation and scale There are several options available for extracting position, rotation and scale from a Matrix4. Vector3.setFromMatrixPosition: can be used to extract the translation component. Vector3.setFromMatrixScale: can be used to extract the scale component. Quaternion.setFromRotationMatrix, Euler.setFromRotationMatrix or extractRotation can be used to extract the rotation component from a pure (unscaled) matrix. decompose can be used to extract position, rotation and scale all at once.

Example

// Simple rig for rotating around 3 axes
const m = new THREE.Matrix4();
const m1 = new THREE.Matrix4();
const m2 = new THREE.Matrix4();
const m3 = new THREE.Matrix4();
const alpha = 0;
const beta = Math.PI;
const gamma = Math.PI/2;
m1.makeRotationX( alpha );
m2.makeRotationY( beta );
m3.makeRotationZ( gamma );
m.multiplyMatrices( m1, m2 );
m.multiply( m3 );

Hierarchy

  • Base
    • Matrix4

Implements

Constructors

  • Creates and initializes the Matrix4 to the 4x4 identity matrix.

    Returns Matrix4

Properties

elements: number[]

A column-major list of matrix values.

Accessors

  • get isBox2(): boolean
  • Returns boolean

  • get isBox3(): boolean
  • Returns boolean

  • get isColor(): boolean
  • Returns boolean

  • get isCylindrical(): boolean
  • Returns boolean

  • get isEuler(): boolean
  • Returns boolean

  • get isLine3(): boolean
  • Returns boolean

  • get isMatrix(): boolean
  • Read-only flag to check if a given object is of type Matrix.

    Returns boolean

  • get isMatrix3(): boolean
  • Returns boolean

  • get isMatrix4(): boolean
  • Read-only flag to check if a given object is of type Matrix4.

    Returns boolean

  • get isPlane(): boolean
  • Returns boolean

  • get isQuaternion(): boolean
  • Returns boolean

  • get isRay(): boolean
  • Returns boolean

  • get isSphere(): boolean
  • Returns boolean

  • get isSpherical(): boolean
  • Returns boolean

  • get isTriangle(): boolean
  • Returns boolean

  • get isVector2(): boolean
  • Returns boolean

  • get isVector3(): boolean
  • Returns boolean

  • get isVector4(): boolean
  • Returns boolean

Methods

  • Creates a new Matrix4 with identical elements to this one.

    Returns

    A new Matrix4 instance identical this.

    Returns Matrix4

  • Sets this matrix to the transformation composed of position, quaternion and scale.

    Returns

    This instance

    Parameters

    Returns Matrix4

  • Copies the elements of matrix m into this matrix.

    Returns

    This instance.

    Parameters

    Returns Matrix4

  • Copies the translation component of the supplied matrix m into this matrix's translation component.

    Returns

    This instance.

    Parameters

    Returns Matrix4

  • Decomposes this matrix into its position, quaternion and scale components.

    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.

    Returns

    This instance.

    Parameters

    Returns Matrix4

  • determinant(): number
  • Computes and returns the determinant of this matrix.

    Returns

    The determinate.

    Returns number

  • Test if this matrix and another matrix are value-wise equal.

    Returns

    Returns true if equal.

    Parameters

    Returns boolean

  • Extracts the basis of this matrix into the three axis vectors provided. If this matrix is:

    a, b, c, d,
    e, f, g, h,
    i, j, k, l,
    m, n, o, p

    then the xAxis, yAxis, zAxis will be set to:

    xAxis = (a, e, i)
    yAxis = (b, f, j)
    zAxis = (c, g, k)

    Returns

    This instance.

    Parameters

    Returns Matrix4

  • Extracts the rotation component of the supplied matrix m into this matrix's rotation component.

    Returns

    This instance.

    Parameters

    • m: Matrix4

      updated to the matrix's rotation component

    Returns Matrix4

  • fromArray(array: number[], offset?: number): Matrix4
  • Sets the elements of this matrix based on an array in column-major format.

    Returns

    This instance.

    Parameters

    • array: number[]

      the array to read the elements from.

    • offset: number = 0

      offset into the array. Default is 0.

    Returns Matrix4

  • getMaxScaleOnAxis(): number
  • Gets the maximum scale value of the 3 axes.

    Returns

    The maximum scale value.

    Returns number

  • Resets this matrix to the identity matrix.

    Returns

    This instance.

    Returns 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

    This instance.

    Returns Matrix4

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

    Returns

    This instance.

    Parameters

    Returns Matrix4

  • Set this to the basis matrix consisting of the three provided basis vectors:

    xAxis.x, yAxis.x, zAxis.x, 0,
    xAxis.y, yAxis.y, zAxis.y, 0,
    xAxis.z, yAxis.z, zAxis.z, 0,
    0, 0, 0, 1

    Returns

    This instance.

    Parameters

    Returns Matrix4

  • makeOrthographic(left: number, right: number, top: number, bottom: number, near: number, far: number): Matrix4
  • Creates an orthographic projection matrix.

    Returns

    This insance.

    Parameters

    • left: number

      The left coordinate

    • right: number

      The right coordinate

    • top: number

      The top coordinate

    • bottom: number

      The bottom coordinate

    • near: number

      The closest distance.

    • far: number

      The distance to horizon

    Returns Matrix4

  • makePerspective(left: number, right: number, top: number, bottom: number, near: number, far: number): Matrix4
  • Creates a perspective projection matrix.

    Returns

    This insance.

    Parameters

    • left: number

      The left coordinate

    • right: number

      The right coordinate

    • top: number

      The top coordinate

    • bottom: number

      The bottom coordinate

    • near: number

      The closest distance.

    • far: number

      The distance to horizon

    Returns Matrix4

  • Sets this matrix as rotation transform around axis by theta radians. This is a somewhat controversial but mathematically sound alternative to rotating via Quaternions. See the discussion here

    Returns

    This instance

    Parameters

    • axis: Vector3

      Rotation axis, should be normalized.

    • angle: number

      Rotation angle in radians.

    Returns Matrix4

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

    Returns

    This instance.

    Parameters

    • euler: Euler

      The Euler to update from.

    Returns Matrix4

  • Sets the rotation component of this matrix to the rotation specified by q, as outlined here. The rest of the matrix is set to the identity. So, given q = w + xi + yj + zk, the resulting matrix will be:

    1-2y²-2z²    2xy-2zw    2xz+2yw    0
    2xy+2zw 1-2x²-2z² 2yz-2xw 0
    2xz-2yw 2yz+2xw 1-2x²-2y² 0
    0 0 0 1

    Returns

    This instance

    Parameters

    Returns Matrix4

  • makeRotationX(theta: number): Matrix4
  • Sets this matrix as a rotational transformation around the X axis by theta (θ) radians. The resulting matrix will be:

    1 0      0        0
    0 cos(θ) -sin(θ) 0
    0 sin(θ) cos(θ) 0
    0 0 0 1

    Returns

    This instance.

    Parameters

    • theta: number

      Rotation angle in radians.

    Returns Matrix4

  • makeRotationY(theta: number): Matrix4
  • Sets this matrix as a rotational transformation around the Y axis by theta (θ) radians. The resulting matrix will be:

    cos(θ)  0 sin(θ) 0
    0 1 0 0
    -sin(θ) 0 cos(θ) 0
    0 0 0 1

    Returns

    This instance.

    Parameters

    • theta: number

      Rotation angle in radians.

    Returns Matrix4

  • makeRotationZ(theta: number): Matrix4
  • Sets this matrix as a rotational transformation around the Z axis by theta (θ) radians. The resulting matrix will be:

    cos(θ) -sin(θ) 0 0
    sin(θ) cos(θ) 0 0
    0 0 1 0
    0 0 0 1

    Returns

    This instance.

    Parameters

    • theta: number

      Rotation angle in radians.

    Returns Matrix4

  • makeScale(x: number, y: number, z: number): Matrix4
  • Sets this matrix as scale transform:

    x, 0, 0, 0,
    0, y, 0, 0,
    0, 0, z, 0,
    0, 0, 0, 1

    Returns

    This instance

    Parameters

    • x: number

      the amount to scale in the X axis.

    • y: number

      the amount to scale in the Y axis.

    • z: number

      the amount to scale in the Z axis.

    Returns Matrix4

  • makeShear(xy: number, xz: number, yx: number, yz: number, zx: number, zy: number): Matrix4
  • Sets this matrix as a shear transform:

    1,   yx,  zx,  0,
    xy, 1, zy, 0,
    xz, yz, 1, 0,
    0, 0, 0, 1

    xy - the amount to shear X by Y. xz - the amount to shear X by Z. yx - the amount to shear Y by X. yz - the amount to shear Y by Z. zx - the amount to shear Z by X. zy - the amount to shear Z by Y.

    Returns

    This instance

    Parameters

    • xy: number
    • xz: number
    • yx: number
    • yz: number
    • zx: number
    • zy: number

    Returns Matrix4

  • makeTranslation(x: number, y: number, z: number): Matrix4
  • Sets this matrix as a translation transform:

    1, 0, 0, x,
    0, 1, 0, y,
    0, 0, 1, z,
    0, 0, 0, 1

    Returns

    This instance.

    Parameters

    • x: number

      the amount to translate in the X axis.

    • y: number

      the amount to translate in the Y axis.

    • z: number

      the amount to translate in the Z axis.

    Returns Matrix4

  • Post-multiplies this matrix by m.

    Returns

    This instance

    Parameters

    • m: Matrix4

      The matrix to multiply with

    Returns Matrix4

  • Sets this matrix to a x b.

    Returns

    This instance.

    Parameters

    Returns Matrix4

  • multiplyScalar(s: number): Matrix4
  • Multiplies every component of the matrix by a scalar value s.

    Returns

    This instance.

    Parameters

    • s: number

      The scalar to multiply

    Returns Matrix4

  • Pre-multiplies this matrix by m.

    Returns

    This instance.

    Parameters

    • m: Matrix4

      The matrix to multiply with.

    Returns Matrix4

  • Multiplies the columns of this matrix by vector v.

    Returns

    This instance.

    Parameters

    • v: Vector3

      The vector to scale components by.

    Returns Matrix4

  • set(n11: number, n12: number, n13: number, n14: number, n21: number, n22: number, n23: number, n24: number, n31: number, n32: number, n33: number, n34: number, n41: number, n42: number, n43: number, n44: number): Matrix4
  • Set the elements of this matrix to the supplied row-major values n11, n12, ... n44.

    Returns

    This instance.

    Parameters

    • n11: number
    • n12: number
    • n13: number
    • n14: number
    • n21: number
    • n22: number
    • n23: number
    • n24: number
    • n31: number
    • n32: number
    • n33: number
    • n34: number
    • n41: number
    • n42: number
    • n43: number
    • n44: number

    Returns Matrix4

  • Set the upper 3x3 elements of this matrix to the values of the Matrix3 m.

    Returns

    This instance.

    Parameters

    Returns Matrix4

  • Sets the position component for this matrix from vector v, without affecting the rest of the matrix

    • i.e. if the matrix is currently:
    a, b, c, d,
    e, f, g, h,
    i, j, k, l,
    m, n, o, p

    This becomes:

    a, b, c, v.x,
    e, f, g, v.y,
    i, j, k, v.z,
    m, n, o, p

    Returns

    This instance.

    Parameters

    • position: Vector3

      The position vector.

    Returns Matrix4

  • Sets the position component for this matrix from vector v, without affecting the rest of the matrix

    Returns

    This instance.

    Parameters

    • x: number

      The X component of position

    • y: number

      The Y component of position

    • z: number

      The Z component of position

    Returns Matrix4

  • toArray(array?: number[], offset?: number): number[] | Matrix4Tuple
  • Writes the elements of this matrix to a array[16] in column-major format.

    Returns

    The new array[16].

    Parameters

    • array: number[] = []

      (optional) array to store the resulting vector in.

    • offset: number = 0

      (optional) offset in the array at which to put the result.

    Returns number[] | Matrix4Tuple