Class Matrix4

The Matrix4 implements a 4x4 matrix that can be used for homogenous transformations of three dimensional vectors and points.

The class follows the 'copy-on-write' pattern, which means that every operation such as Matrix4.multiply returns a copy of the matrix with updated values. It also implies that the values of the matrix cannot be directly modified.

The concept of immutability improves sharing of objects as all types in TypeScript are assigned by identity, instead of by-value (e.g. a copy) as in other programming languages like C.

The matrix storage is "row-major" meaning that elements [0...3] represents the first row. The first column is represented by the elements[0, 4, 8, 12]

Constructors

  • Creates a new matrix with the specified elements. If no arguments are specified, an identity matrix is constructed.

    Parameters

    • Optionalm00: number
    • Optionalm01: number
    • Optionalm02: number
    • Optionalm03: number
    • Optionalm10: number
    • Optionalm11: number
    • Optionalm12: number
    • Optionalm13: number
    • Optionalm20: number
    • Optionalm21: number
    • Optionalm22: number
    • Optionalm23: number
    • Optionalm30: number
    • Optionalm31: number
    • Optionalm32: number
    • Optionalm33: number

    Returns Matrix4

Properties

identity: Matrix4

Identity matrix.

zero: Matrix4

A matrix with all zero values.

Accessors

  • get m00(): number
  • Returns number

  • set m00(m00): void
  • Parameters

    • m00: number

    Returns void

  • get m01(): number
  • Returns number

  • set m01(m01): void
  • Parameters

    • m01: number

    Returns void

  • get m02(): number
  • Returns number

  • set m02(m02): void
  • Parameters

    • m02: number

    Returns void

  • get m03(): number
  • Returns number

  • set m03(m03): void
  • Parameters

    • m03: number

    Returns void

  • get m10(): number
  • Returns number

  • set m10(m10): void
  • Parameters

    • m10: number

    Returns void

  • get m11(): number
  • Returns number

  • set m11(m11): void
  • Parameters

    • m11: number

    Returns void

  • get m12(): number
  • Returns number

  • set m12(m12): void
  • Parameters

    • m12: number

    Returns void

  • get m13(): number
  • Returns number

  • set m13(m13): void
  • Parameters

    • m13: number

    Returns void

  • get m20(): number
  • Returns number

  • set m20(m20): void
  • Parameters

    • m20: number

    Returns void

  • get m21(): number
  • Returns number

  • set m21(m21): void
  • Parameters

    • m21: number

    Returns void

  • get m22(): number
  • Returns number

  • set m22(m22): void
  • Parameters

    • m22: number

    Returns void

  • get m23(): number
  • Returns number

  • set m23(m23): void
  • Parameters

    • m23: number

    Returns void

  • get m30(): number
  • Returns number

  • set m30(m30): void
  • Parameters

    • m30: number

    Returns void

  • get m31(): number
  • Returns number

  • set m31(m31): void
  • Parameters

    • m31: number

    Returns void

  • get m32(): number
  • Returns number

  • set m32(m32): void
  • Parameters

    • m32: number

    Returns void

  • get m33(): number
  • Returns number

  • set m33(m33): void
  • Parameters

    • m33: number

    Returns void

Methods

  • Copies all properties defined by the prototype of Matrix4 from other to this instance.

    Parameters

    Returns Matrix4

  • Calculates the determinant of the matrix.

    Returns number

    Determinant of the matrix.

  • Clones this instance and returns a new Matrix4 with identical values.

    Returns Matrix4

  • Calculates the inverse of the matrix. If matrix is affine use invertAffine as it is faster.

    Returns void

  • Calculates the inverse of the matrix. Matrix must be affine.

    Returns void

  • Sets values of a specific column in the matrix.

    Parameters

    • columnIndex: number

      Index of the column in range [0, 3].

    • column: Immutable<Vector4>

      Values to set in the column.

    Returns Matrix4

  • Transposes the matrix (switched columns and rows).

    Returns void

  • Gets a specific column in the matrix.

    Parameters

    • columnIndex: number

      Index of the column in range [0, 3].

    Returns Vector4

    The column as Vector4.

  • Gets the value of the specified element in the matrix.

    Parameters

    • row: number

      Row index of the element to retrieve.

    • column: number

      Column index of the element to retrieve.

    Returns number

    Value of the element.

  • Returns value of the specified element in the matrix using a linear index. Linear index can be calculated using the following formula: idx = row * 4 + column.

    Parameters

    • index: number

      Linear index to get the value of.

    Returns number

    Value of the element.

  • Gets the translation part from the matrix.

    Returns Vector3

    The translation as Vector3.

  • Decompose a matrix to translation, rotation and scale components. Matrix must consist only of translation, rotation and uniform scale transformations, otherwise accurate results are not guaranteed. Applying non-uniform scale guarantees results will not be accurate.

    Returns {
        rotation: Quaternion;
        scale: Vector3;
        translation: Vector3;
    }

    An object containing:

    • translation the translation
    • rotation the rotation
    • scale the scale.
  • Calculates the inverse of the matrix.

    Returns Matrix4

  • Calculates the inverse of the matrix. Matrix must be affine.

    Returns Matrix4

  • Determines if this matrix is are element-wise equal to the provided one.

    Parameters

    Returns boolean

  • Multiplies this matrix with the provided and returns a copy matrix with the result.

    Parameters

    Returns Matrix4

  • Transform a 3D point by this matrix. Matrix must be affine. Affine multiplication offers better performance than the general case.

    Parameters

    Returns Vector3

    Point transformed by this matrix.

  • Transform a 4D vector by this matrix. Matrix must be affine. Affine multiplication offers better performance than the general case.

    Parameters

    Returns Vector4

    Vector transformed by this matrix.

  • Transform a 3D direction vector by this matrix. w component is assumed to be 0.

    Parameters

    Returns Vector3

    Direction vector transformed by this matrix.

  • Transform a 3D point by this matrix. w component of the vector is assumed to be 1. After transformation all components are projected back so that w remains 1. If your matrix doesn't contain projection components use multiplyAffineVector4 as it is faster.

    Parameters

    Returns Vector3

    Point transformed by this matrix.

  • Transform a 4D vector by this matrix. If your matrix doesn't contain projection components use multiplyAffineVector4 as it is faster.

    Parameters

    Returns Vector4

    Vector transformed by this matrix.

  • Sets values of a specific column in the matrix.

    Parameters

    • columnIndex: number

      Index of the column in range [0, 3].

    • column: Immutable<Vector4>

      Values to set in the column.

    Returns Matrix4

  • Sets the value of the specified element in the matrix.

    Parameters

    • row: number

      Row index of the element to retrieve.

    • column: number

      Column index of the element to retrieve.

    • value: number

    Returns void

    Value of the element.

  • Sets value of the specified element in the matrix using a linear index. Linear index can be calculated using the following formula: idx = row * 4 + column.

    Parameters

    • index: number

      Linear index to gsetet the value of.

    • value: number

      New value of the element.

    Returns void

  • Sets the translation part from the matrix.

    Parameters

    • translation: Vector3

      The translation as Vector3.

    Returns void

  • Creates a matrix from translation, rotation and scale.

    Parameters

    Returns void

    The transformation are applied in scale->rotation->translation order.

  • Returns string

  • Returns a transpose of the matrix (switched columns and rows).

    Returns Matrix4

  • Returns the rotation/scaling parts of the 4x4 matrix.

    Parameters

    Returns Matrix3

    3x3 matrix representing an upper left portion of the provided matrix.

  • Creates a new matrix that performs inverse translation, rotation and scale.

    Parameters

    Returns Matrix4

    Inverted matrix that performs scale, followed by rotation, followed by translation.

  • Creates a new matrix that performs translation, rotation and scale.

    Parameters

    Returns Matrix4

    Matrix that performs scale, followed by rotation, followed by translation.