mathue
    Preparing search index...

    Class Matrix4

    4x4 matrix class. It looks column-major order. And post multiplied.

    Implements

    Index

    Properties

    order: 4
    const m = Matrix4.identity();
    console.log(m.order); // 4
    elements: Float32Array
    const m = Matrix4.identity();
    console.log(m.elements);
    // [ 1, 0, 0, 0,
    // 0, 1, 0, 0,
    // 0, 0, 1, 0,
    // 0, 0, 0, 1 ]

    Constructors

    • Creates a new 4x4 matrix with the specified elements.
      The internal data is stored in column-major order in a Float32Array.

      The parameters e(column)(row) correspond to the following matrix positions: \

      | e00 e10 e20 e30 |
      | e01 e11 e21 e31 |
      | e02 e12 e22 e32 |
      | e03 e13 e23 e33 |

      The elements array stores each column sequentialy:
      [e00, e01, e02, e03, e10, e11, e12, e13, e20, e21, e22, e23, e30, e31, e32, e33]

      Parameters

      • e00: number

        element in column 0, row 0

      • e01: number

        element in column 0, row 1

      • e02: number

        element in column 0, row 2

      • e03: number

        element in column 0, row 3

      • e10: number

        element in column 1, row 0

      • e11: number

        element in column 1, row 1

      • e12: number

        element in column 1, row 2

      • e13: number

        element in column 1, row 3

      • e20: number

        element in column 2, row 0

      • e21: number

        element in column 2, row 1

      • e22: number

        element in column 2, row 2

      • e23: number

        element in column 2, row 3

      • e30: number

        element in column 3, row 0

      • e31: number

        element in column 3, row 1

      • e32: number

        element in column 3, row 2

      • e33: number

        element in column 3, row 3

      Returns Matrix4

      const m = new Matrix4(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
      console.log(m.elements);
      // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]

    Factory Methods

    • Creates a identity matrix instance

      Returns Matrix4

      new identity matrix instance

      const m = Matrix4.identity();
      console.log(m.elements);
      // [ 1, 0, 0, 0,
      // 0, 1, 0, 0,
      // 0, 0, 1, 0,
      // 0, 0, 0, 1 ]
    • Creates a new zero matrix instance

      Returns Matrix4

      new zero matrix instance

      const m = Matrix4.zero();
      console.log(m.elements);
      // [ 1, 0, 0, 0,
      // 0, 1, 0, 0,
      // 0, 0, 1, 0,
      // 0, 0, 0, 1 ]

    Methods

    • Creates new instance has same elements (pure)

      Returns Matrix4

      new cloned matrix instance

      const m = Matrix4.identity();
      const c = m.clone();
      console.log(c.elements);
      // [ 1, 0, 0, 0,
      // 0, 1, 0, 0,
      // 0, 0, 1, 0,
      // 0, 0, 0, 1 ]
    • Sets all elements (mutates this)

      Parameters

      • e00: number

        element in column 0, row 0

      • e01: number

        element in column 0, row 1

      • e02: number

        element in column 0, row 2

      • e03: number

        element in column 0, row 3

      • e10: number

        element in column 1, row 0

      • e11: number

        element in column 1, row 1

      • e12: number

        element in column 1, row 2

      • e13: number

        element in column 1, row 3

      • e20: number

        element in column 2, row 0

      • e21: number

        element in column 2, row 1

      • e22: number

        element in column 2, row 2

      • e23: number

        element in column 2, row 3

      • e30: number

        element in column 3, row 0

      • e31: number

        element in column 3, row 1

      • e32: number

        element in column 3, row 2

      • e33: number

        element in column 3, row 3

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.zero();
      m.set(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
      console.log(m.elements);
      // [ 1, 0, 0, 0,
      // 0, 1, 0, 0,
      // 0, 0, 1, 0,
      // 0, 0, 0, 1 ]
    • Copies all elements from other matrix (mutates this)

      Parameters

      Returns void

      this instance, for method chaining

      const m = Matrix4.zero();
      const i = Matrix4.identity();
      m.set(i);
      console.log(m.elements);
      // [ 1, 0, 0, 0,
      // 0, 1, 0, 0,
      // 0, 0, 1, 0,
      // 0, 0, 0, 1 ]
    • Sets identity matrix (mutates this)

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.zero();
      m.setIdentity();
      console.log(m.elements);
      // [ 1, 0, 0, 0,
      // 0, 1, 0, 0,
      // 0, 0, 1, 0,
      // 0, 0, 0, 1 ]
    • Sets scale transformation matrix (mutates this)

      Parameters

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.identity();
      const s = new Vector3(2, 3, 4);
      m.setScale(s);
      console.log(m.elements);
      // [ 2, 0, 0, 0,
      // 0, 3, 0, 0,
      // 0, 0, 4, 0,
      // 0, 0, 0, 1 ]
    • Sets translation transformation matrix (mutates this)

      Parameters

      • translation: Vector3

        translation vector

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.identity();
      const t = new Vector3(2, 3, 4);
      m.setTranslation(t);
      console.log(m.elements);
      // [ 1, 0, 0, 0,
      // 0, 1, 0, 0,
      // 0, 0, 1, 0,
      // 2, 3, 4, 1 ]
    • Sets rotation matrix from quaternion (mutates this)

      Parameters

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.zero();
      const q = Quaternion.identity();
      m.setRotation(q);
      console.log(m.elements);
      // [ 0, 0, 0, 0,
      // 0, 0, 0, 0,
      // 0, 0, 0, 0,
      // 0, 0, 0, 0 ]
    • Adds by other matrix (mutates this)

      Parameters

      Returns Matrix4

      this instance, for method chaining

      const m1 = Matrix4.zero();
      const m2 = Matrix4.identity();
      m1.add(m2);
      console.log(m1.elements);
      // [ 1, 0, 0, 0,
      // 0, 1, 0, 0,
      // 0, 0, 1, 0,
      // 0, 0, 0, 1 ]
    • Subtracts by other matrix (mutates this)

      Parameters

      Returns Matrix4

      this instance, for method chaining

      const m1 = Matrix4.zero();
      const m2 = Matrix4.identity();
      m1.subtract(m2);
      console.log(m1.lements);
      // [ -1, 0, 0, 0,
      // 0, -1, 0, 0,
      // 0, 0, -1, 0,
      // 0, 0, 0, -1 ]
    • Multiplies all elements by scalar (mutates this)

      Parameters

      • scalar: number

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.identity();
      m.multiplyScalar(2);
      console.log(m.elements);
      // [ 2, 0, 0, 0,
      // 0, 2, 0, 0,
      // 0, 0, 2, 0,
      // 0, 0, 0, 2 ]
    • Divides all elements by scalar (mutates this)

      Parameters

      • scalar: number

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.identity();
      m.divideScalar(2);
      console.log(m.elements);
      // [ 0.5, 0, 0, 0,
      // 0, 0.5, 0, 0,
      // 0, 0, 0.5, 0,
      // 0, 0, 0, 0.5 ]
    • Multiplies this matrix by other matrix (mutates this)

      Parameters

      Returns Matrix4

      this instance, for method chaining

      const m1 = Matrix4.identity();
      const m2 = Matrix4.zero();
      m1.multiply(m2);
      console.log(m.elements);
      // [ 0, 0, 0, 0,
      // 0, 0, 0, 0,
      // 0, 0, 0, 0,
      // 0, 0, 0, 0 ]
    • Multiplies translation matrix to this instance (mutates this)

      Parameters

      • position: Vector3

        translation vector

      Returns Matrix4

      this instance, for method chaining

    • Calculates determinant of this matrix (pure)

      Returns number

      determinant of this matrix

    • Sets view transformation matrix (mutates this)

      Parameters

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.identity();
      const p = new Vector3(0, -2, 0);
      const t = Vector3.zero();
      const u = new Vector3(0, 0, 1);
      m.lookAt(p, t, u);
    • Sets projection matrix of orthographic camera (mutates this)

      Parameters

      • left: number

        left boundary of the view frustum (negative X coordinate)

      • right: number

        right boundary of the view frustum (positive X coordinate)

      • bottom: number

        bottom boundary of the view frustum (negative Y coordinate)

      • top: number

        top boundary of the view frustum (positive Y coordinate)

      • near: number

        near clipping plane distance (positive value)

      • far: number

        far clipping plane distance (positive value)

      • Optionaloptions: ProjectionOptions

        options for orthographic projection matrix

      Returns Matrix4

      this instance, for method chaining

    • Sets projection matrix of perspective camera (mutates this)

      Parameters

      • verticalFov: number

        vertical field of view in radians

      • near: number

        near clipping plane distance

      • far: number

        far clipping plane distance

      • aspect: number

        aspect ratio (width / height)

      • Optionaloptions: ProjectionOptions

        options for perspective projection matrix

      Returns Matrix4

      this instance, for method chaining

      const m = Matrix4.identity();
      const fov = Math.PI / 4;
      const near = 0.01;
      const far = 4.0;
      const aspect = 300 / 150;

      // for OpenGL, WebGL
      m.perspective(fov, near, far, aspect);

      // for WebGPU, Vulkan, DirectX, Metal
      m.perspective(fov, near, far, aspect, {depthZeroToOne: true});