gophergfx
    Preparing search index...

    Class Matrix4

    This class holds a 4x4 transformation matrix. It includes routines for using the matrix to transform points and vectors. It includes routines for constructing many common types of matrices (see make*), for inverting the matrix, and for accessing the underlying 16-elements by row, column.

    Most of the functions in the class are defined both as member functions that can be called on a specific instance of Vector3 and as static functions. The static functions return a new result, leaving the original inputs unchanged, whereas, in general, member functions save the result in the matrix itself and return void:

    const T = Matrix4.makeTranslation(new Vector3(1, 2, 3));
    const R = Matrix4.makeRotationX(Math.PI);
    const S = Matrix4.makeScale(new Vector3(2, 2, 2));

    // saves the result in M, leaving T, R, and S unchanged.
    const M = Matrix4.multiplyAll(T, R, S);

    // each call to multiply overwrites the previous contents of M2 with the result of the multiplication
    const M2 = Matrix4.makeIdentity();
    M2.multiply(T);
    M2.multiply(R);
    M2.multiply(S);
    Index
    mat: number[]

    Array of 16 numbers representing the elements in the Matrix4

    IDENTITY: Matrix4 = ...

    Static field to provide quick access to the identity matrix. (Note: Be careful not to change the value of this field! It is marked readonly, but typescipt do not completely enforce this!)

    // Good use of IDENTITY:
    const M = gfx.Matrix4();
    if (M.equals(gfx.Matrix4.IDENTITY)) {
    console.log("We have an identity matrix.")
    }

    // Dangerous use of IDENTITY!!!!
    const M = gfx.Matrix4.IDENTITY; // makes M a reference to gfx.Matrix4.IDENTITY
    M.set(3.0, 3, 3); // changes the underlying matrix in gfx.Matrix4.IDENTITY!

    // Do this instead:
    const M = gfx.Matrix4.makeIdentity(); // create a new identity matrix M
    M.set(3.0, 3, 3
    • Creates a new Matrix4 object with the same values as this Matrix4.

      Returns Matrix4

      A new Matrix4 object with the same values as this Matrix4.

    • Sets the matrix to a composition of three basic transformations (this = T * R * S), where scale is applied first, then rotation, then translation.

      Parameters

      • position: Vector3

        The position of the Matrix4 object (default Vector3.ZERO)

      • rotation: Quaternion

        The rotation of the Matrix4 object (default Quaternion.IDENTITY)

      • scale: Vector3

        The scale of the Matrix4 object (default Vector3.ONE)

      Returns void

    • Copy the values of another Matrix4 into this one

      Parameters

      • m: Matrix4

        The Matrix4 object to copy from

      Returns void

    • Decomposes the 4x4 transformation matrix into separate translation, rotation, and scale components such that the original matrix can be represented as a combination of transformations in the form T * R * S. The decomposition is straightforward (relatively efficient) if the matrix does not include a negative scale. If the matrix includes a negative scale factor, a slower polar decomposition must be used, and the caller must explicitly set the containsNegScale to true.

      Parameters

      • containsNegScale: boolean

        Set to true if the matrix includes a negative scale factor.

      Returns [Vector3, Quaternion, Vector3]

      An array of three elements [translation: Vector3, rotation: Quaternion, scale: Vector3]

    • Computes the determinant of the Matrix4 object

      Returns number

      The determinant of the Matrix4 object

    • Returns the element at the given row and column in this Matrix4.

      Parameters

      • row: number

        The row of the element to return.

      • col: number

        The column of the element to return.

      Returns number

      The element at the given row and column.

    • Checks if every element of this Matrix4 is exactly equal to every element of the given Matrix4

      Parameters

      • other: Matrix4

        The other Matrix3 to compare to

      Returns boolean

      A boolean value indicating if the two matrices are equal

    • Checks if every element of this Matrix4 is exactly equal (within a small value of epsilon) to every element of the given Matrix4

      Parameters

      • other: Matrix4

        The other Matrix3 to compare to

      • epsilon: number = MathUtils.EPSILON

        A small value of acceptable variance to account for numerical instability

      Returns boolean

      A boolean value indicating if the two matrices are equal

    • Returns the first three elements of column i as a new Vector3.

      Parameters

      • i: number

        The zero-based index of the column (0..3)

      Returns Vector3

      A new Vector3

    • Returns a new object that is the inverse of this Matrix4 object, leaves the original unchanged.

      Returns Matrix4

      A Matrix4 object that is the inverse of the current Matrix4 object

    • Returns the first three elements of row i as a new Vector3.

      Parameters

      • i: number

        The zero-based index of the row (0..3)

      Returns Vector3

      A new Vector3.

    • Gets the translation vector of this Matrix4 object

      Returns Vector3

      The Vector3 representing the translation vector

    • Transposes the Matrix4 object and returns a new Matrix4 object

      Returns Matrix4

      A new Matrix4 object which is the transposed version of the current object

    • Inverts the matix and writes the result back into this matrix.

      Returns void

    • Sets the Matrix to a view matrix of a camera. The matrix will position the camera at eyePoint and orient it to look directly toward the targetPoint so that the camera's look vector will be (targetPoint - eyePoint). The camera's rotation around the look vector is controlled by the upVector, which only needs to point roughly in the Up direction, i.e., it does not need to be completely perpendicular to the look vector.

      Parameters

      Returns void

      A new Matrix4 object for the view matrix of a camera

    • Multiplies this Matrix4 with another Matrix4 on the right hand side (i.e., this = this * rhs) and sets this Matrix4 to the result.

      Parameters

      • rhs: Matrix4

        The Matrix4 to multiply with.

      Returns void

    • this.mat = this.mat * M1 * M2 * ... * M(n-1): Multiplies this matrix with one or more additional
      4x4 matrices.

      (Remember, matrix multiplication is not commutitive; so, the order of the matrices is important! The order that transformations are applied to points and vectors is right-to-left. To transform point p into p', as in the equation below, think of M(n-1) as being the first transformation to be applied to p and the current value of this.mat as being the last transformation to be applied in order to produce p'.)

        p' = this.mat * M1 * M2 * ... * M(n-1) * p
      

      Parameters

      Returns void

    • Multiplies all elements of this Matrix4 object by a scalar

      Parameters

      • x: number

        The scalar to multiply by

      Returns void

    • Multiplies the given Matrix4 with another Matrix4 on the left hand side (i.e., this = lhs * this) and sets this Matrix4 to the result.

      Parameters

      • lhs: Matrix4

        The Matrix4 to multiply with.

      Returns void

    • Sets the element at the given row and column in this Matrix4.

      Parameters

      • value: number

        The value to set at the given row and column.

      • row: number

        The row of the element to set.

      • col: number

        The column of the element to set.

      Returns void

    • Set the axis angle for this Matrix4 object

      Parameters

      • axis: Vector3

        The Vector3 representing the axis

      • angle: number

        The angle to set the axis to

      Returns void

    • Sets the first three elements of column i using the x, y, z components of the supplied vector.

      Parameters

      • col: number

        The zero-based index of the column (0..3)

      • v: Vector3

        A vector containing the new values for the column

      Returns void

    • Set the values of the Matrix4 in column-major order

      Parameters

      • n1: number

        Column 1, Row 1 value

      • n2: number

        Column 1, Row 2 value

      • n3: number

        Column 1, Row 3 value

      • n4: number

        Column 1, Row 4 value

      • n5: number

        Column 2, Row 1 value

      • n6: number

        Column 2, Row 2 value

      • n7: number

        Column 2, Row 3 value

      • n8: number

        Column 2, Row 4 value

      • n9: number

        Column 3, Row 1 value

      • n10: number

        Column 3, Row 2 value

      • n11: number

        Column 3, Row 3 value

      • n12: number

        Column 3, Row 4 value

      • n13: number

        Column 4, Row 1 value

      • n14: number

        Column 4, Row 2 value

      • n15: number

        Column 4, Row 3 value

      • n16: number

        Column 4, Row 4 value

      Returns void

    • Sets the matrix to a rotation matrix defined by Euler angles.

      Parameters

      • x: number

        The x-axis euler angle

      • y: number

        The y-axis euler angle

      • z: number

        The z-axis euler angle

      • order: string = 'YZX'

        The order in which the euler angles should be applied

      Returns void

    • Sets a frustum projection matrix on this Matrix4 object

      Parameters

      • left: number

        The leftmost coordinate

      • right: number

        The rightmost coordinate

      • bottom: number

        The bottom coordinate

      • top: number

        The top coordinate

      • near: number

        The near coordinate

      • far: number

        The far coordinate

      Returns void

    • Sets this Matrix4 to the identity matrix.

      Returns void

    • Sets an orthographic projection matrix on this Matrix4 object

      Parameters

      • left: number

        The leftmost coordinate

      • right: number

        The rightmost coordinate

      • bottom: number

        The bottom coordinate

      • top: number

        The top coordinate

      • near: number

        The near coordinate

      • far: number

        The far coordinate

      Returns void

    • Sets a perspective projection matrix on this Matrix4 object

      Parameters

      • fov: number

        The field of view angle

      • aspectRatio: number

        The aspect ratio of the view

      • near: number

        The near coordinate

      • far: number

        The far coordinate

      Returns void

    • Sets this Matrix4 to a rotation matrix given a Quaternion.

      Parameters

      • rotation: Quaternion

        The Quaternion to construct the rotation matrix with.

      Returns void

    • Sets this Matrix4 to a rotation matrix around the X axis with the given angle in radians.

      Parameters

      • angle: number

        The angle in radians.

      Returns void

    • Sets this Matrix4 to a rotation matrix around the Y axis with the given angle in radians.

      Parameters

      • angle: number

        The angle in radians.

      Returns void

    • Sets this Matrix4 to a rotation matrix around the Z axis with the given angle in radians.

      Parameters

      • angle: number

        The angle in radians.

      Returns void

    • Sets the first three elements of row i using the x, y, z components of the supplied vector.

      Parameters

      • row: number

        The zero-based index of the row (0..3)

      • v: Vector3

        A vector containing the new values for the row

      Returns void

    • Set the values of the Matrix4 in row-major order

      Parameters

      • n1: number

        Column 1, Row 1 value

      • n2: number

        Column 1, Row 2 value

      • n3: number

        Column 1, Row 3 value

      • n4: number

        Column 1, Row 4 value

      • n5: number

        Column 2, Row 1 value

      • n6: number

        Column 2, Row 2 value

      • n7: number

        Column 2, Row 3 value

      • n8: number

        Column 2, Row 4 value

      • n9: number

        Column 3, Row 1 value

      • n10: number

        Column 3, Row 2 value

      • n11: number

        Column 3, Row 3 value

      • n12: number

        Column 3, Row 4 value

      • n13: number

        Column 4, Row 1 value

      • n14: number

        Column 4, Row 2 value

      • n15: number

        Column 4, Row 3 value

      • n16: number

        Column 4, Row 4 value

      Returns void

    • Set the scale of this Matrix4 object

      Parameters

      • scale: Vector3

        The Vector3 representing the scale to set

      Returns void

    • Sets this Matrix4 to a translation matrix given a translation Vector3.

      Parameters

      • v: Vector3

        The translation Vector3.

      Returns void

    • Multiplies point p by this 4x4 transformation matrix and returns the result as a new point. This has the effect of transforming p from the matrix's local coordinate system to its parent coordinate system. The multiplication is done using homogeneous coordinates (p is treated as having a w=1 coordinate).

      Parameters

      Returns Vector3

      A new point transformed by m

    • Multiplies vector v by this 4x4 transformation matrix and returns the result as a new vector. This has the effect of transforming v from the matrix's local coordinate system to its parent coordinate system. The multiplication is done using homogeneous coordinates (v is treated as having a w=0 coordinate).

      Parameters

      Returns Vector3

      A new vector transformed by m

    • Creates a new Matrix4 object with the same values as the input matrix

      Parameters

      • m: Matrix4

        The input Matrix4 object

      Returns Matrix4

      A new Matrix4 object with the same values as the input matrix

    • Creates a new Matrix4 object representing the combined transform of position, rotation and scale

      Parameters

      • position: Vector3 = Vector3.ZERO

        The Vector3 object representing the position

      • rotation: Quaternion = Quaternion.IDENTITY

        The Quaternion object representing the rotation

      • scale: Vector3 = Vector3.ONE

        The Vector3 object representing the scale

      Returns Matrix4

      A new Matrix4 object representing the combined transform of position, rotation and scale

    • Checks if all elements of two Matrix4 objects are exactly equal.

      Parameters

      Returns boolean

      A boolean value indicating if the two matrices are equal

    • Creates a new Matrix4 object from the given values in column-major order

      Parameters

      • n1: number

        Element [0,0] in the matrix

      • n2: number

        Element [1,0] in the matrix

      • n3: number

        Element [2,0] in the matrix

      • n4: number

        Element [3,0] in the matrix

      • n5: number

        Element [0,1] in the matrix

      • n6: number

        Element [1,1] in the matrix

      • n7: number

        Element [2,1] in the matrix

      • n8: number

        Element [3,1] in the matrix

      • n9: number

        Element [0,2] in the matrix

      • n10: number

        Element [1,2] in the matrix

      • n11: number

        Element [2,2] in the matrix

      • n12: number

        Element [3,2] in the matrix

      • n13: number

        Element [0,3] in the matrix

      • n14: number

        Element [1,3] in the matrix

      • n15: number

        Element [2,3] in the matrix

      • n16: number

        Element [3,3] in the matrix

      Returns Matrix4

      A new Matrix4 object created from the given values

    • Creates a new Matrix4 object from the given values in row-major order

      Parameters

      • n1: number

        Element [0,0] in the matrix

      • n2: number

        Element [0,1] in the matrix

      • n3: number

        Element [0,2] in the matrix

      • n4: number

        Element [0,3] in the matrix

      • n5: number

        Element [1,0] in the matrix

      • n6: number

        Element [1,1] in the matrix

      • n7: number

        Element [1,2] in the matrix

      • n8: number

        Element [1,3] in the matrix

      • n9: number

        Element [2,0] in the matrix

      • n10: number

        Element [2,1] in the matrix

      • n11: number

        Element [2,2] in the matrix

      • n12: number

        Element [2,3] in the matrix

      • n13: number

        Element [3,0] in the matrix

      • n14: number

        Element [3,1] in the matrix

      • n15: number

        Element [3,2] in the matrix

      • n16: number

        Element [3,3] in the matrix

      Returns Matrix4

      A new Matrix4 object created from the given values

    • Checks if all elements of two Matrix4 objects are equal within a small value of epsilon.

      Parameters

      • m1: Matrix4

        The first matrix.

      • m2: Matrix4

        The second matrix.

      • epsilon: number = MathUtils.EPSILON

        A small value of acceptable variance to account for numerical instability

      Returns boolean

      A boolean value indicating if the two matrices are equal

    • Creates a new Matrix4 object for the view matrix of a camera. The matrix will position the camera at eyePoint and orient it to look directly toward the targetPoint so that the camera's look vector will be (targetPoint - eyePoint). The camera's rotation around the look vector is controlled by the upVector, which only needs to point roughly in the Up direction, i.e., it does not need to be completely perpendicular to the look vector.

      Parameters

      • eyePoint: Vector3

        The position of the camera

      • targetPoint: Vector3

        A point that the camera should look directly toward

      • upVector: Vector3

        The "up" direction for the camera

      Returns Matrix4

      A new Matrix4 object for the view matrix of a camera

    • Creates a rotation matrix that will align a reference direction to some new direction. This is similar to a lookAt function but more flexible in that the reference direction does not need to be -Z. The routine can optionally include a second reference direction and new direction. This is interpreted similarly to the Up parameter in a typical lookAt function. (The rotation matrix will always align referenceDir with newDir and it will try to get referenceDir2 to align as closely as possible with newDir2.)

      Parameters

      • referenceDir: Vector3

        A reference direction

      • newDir: Vector3

        The direction that referenceDir should rotate into

      • referenceDir2: Vector3 = ...

        Optionally, a second reference direction

      • newDir2: Vector3 = ...

        The direction that the second reference direction should rotate into

      Returns Matrix4

    • Creates a new Matrix4 object for rotation around an arbitrary axis

      Parameters

      • axis: Vector3

        The Vector3 object representing the axis of rotation

      • angle: number

        The angle of rotation around the axis

      Returns Matrix4

      A new Matrix4 object for rotation around an arbitrary axis

    • Creates a matrix that represents a right-handed X,Y,Z coordinate frame (an orthonormal basis) where: 1. the vector provided by the reference direction is aligned with the X axis, and optionally 2. the vector provided by a second reference direction is aligned as closely as possible (subject to the requirements of a right-handed, orthonormal basis) with the Y axis. This means the first column of the matrix will contain the normalized x, y, z values of the reference direction and the second and third columns can be interpreted as the directions of the Y and Z axes of the basis.

      Parameters

      • referenceDir: Vector3

        A vector to use as the X-axis.

      • referenceDir2: Vector3 = ...

        A vector to try to use as the Y-axis.

      Returns Matrix4

      A 4x4 transformation matrix.

    • Creates a new Matrix4 object for rotation using Euler angles

      Parameters

      • x: number

        The angle of rotation around the x axis

      • y: number

        The angle of rotation around the y axis

      • z: number

        The angle of rotation around the z axis

      • order: string = 'YZX'

        The order of the rotations (default is 'YZX')

      Returns Matrix4

      A new Matrix4 object for rotation using Euler angles

    • Create a frustum projection Matrix4

      Parameters

      • left: number

        Left coordinate of the viewing volume

      • right: number

        Right coordinate of the viewing volume

      • bottom: number

        Bottom coordinate of the viewing volume

      • top: number

        Top coordinate of the viewing volume

      • near: number

        Near clipping plane of the viewing volume

      • far: number

        Far clipping plane of the viewing volume

      Returns Matrix4

      A Matrix4 representing a frustum projection

    • Creates a new Matrix4 object with the identity matrix

      Returns Matrix4

      A new Matrix4 object with the identity matrix

    • Create an orthographic projection Matrix4

      Parameters

      • left: number

        Left coordinate of the viewing volume

      • right: number

        Right coordinate of the viewing volume

      • bottom: number

        Bottom coordinate of the viewing volume

      • top: number

        Top coordinate of the viewing volume

      • near: number

        Near clipping plane of the viewing volume

      • far: number

        Far clipping plane of the viewing volume

      Returns Matrix4

      A Matrix4 representing an orthographic projection

    • Create a perspective projection Matrix4

      Parameters

      • fov: number

        Field of view of the projection in radians

      • aspectRatio: number

        Aspect ratio of the viewport (width / height)

      • near: number

        Near clipping plane of the viewing volume

      • far: number

        Far clipping plane of the viewing volume

      Returns Matrix4

      A Matrix4 representing a perspective projection

    • Creates a new Matrix4 object for rotation around the X axis

      Parameters

      • angle: number

        The angle of rotation around the X axis

      Returns Matrix4

      A new Matrix4 object for rotation around the X axis

    • Creates a new Matrix4 object for rotation around the Y axis

      Parameters

      • angle: number

        The angle of rotation around the Y axis

      Returns Matrix4

      A new Matrix4 object for rotation around the Y axis

    • Creates a new Matrix4 object for rotation around the Z axis

      Parameters

      • angle: number

        The angle of rotation around the Z axis

      Returns Matrix4

      A new Matrix4 object for rotation around the Z axis

    • Creates a new Matrix4 object for translation

      Parameters

      • v: Vector3

        The Vector3 object representing the translation vector

      Returns Matrix4

      A new Matrix4 object for translation

    • Mn = M1 * M2 * M3 * ... * M(n-1): Composes (i.e., multiplies) two or more 4x4 matrices together and returns the result in a new matrix.

      (Remember, matrix multiplication is not commutitive; so, the order of the matrices is important! The order that transformations are applied to points and vectors is right-to-left. To transform point p into p', as in the equation below, think of M(n-1) as being the first transformation to be applied to p and M1 as being the last transformation to be applied in order to produce p'.)

        p' = M1 * M2 * M3 * ... * M(n-1) * p
      

      Parameters

      • m1: Matrix4

        The first (leftmost) Matrix4 object

      • m2: Matrix4

        The next Matrix4 object

      • ...mAdditional: Matrix4[]

        Zero or more additional Matrix4 objects

      Returns Matrix4

      A new Matrix4 object = m1 * m2 * ...

    • Multiplies point p by a 4x4 transformation matrix and returns the result as a new point. This has the effect of transforming p from m's local coordinate system to m's parent coordinate system. The multiplication is done using homogeneous coordinates (p is treated as having a w=1 coordinate).

      Parameters

      • m: Matrix4

        A 4x4 transformation matrix

      • p: Vector3

        The original point

      Returns Vector3

      A new point transformed by m

    • Multiplies vector v by a 4x4 transformation matrix and returns the result as a new vector. This has the effect of transforming v from m's local coordinate system to m's parent coordinate system. The multiplication is done using homogeneous coordinates (p is treated as having a w=0 coordinate).

      Parameters

      • m: Matrix4

        A 4x4 transformation matrix

      • v: Vector3

        The original vector

      Returns Vector3

      A new vector transformed by m