Skip to main content

m4

Copyright 2014, Gregg Tavares. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright

notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Gregg Tavares. nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Type Aliases

Mat4

Mat4: Float32Array

A [4, 4] matrix represented as a flattened length-16 array

Vec3

Vec3: Float32Array

A length-3 vector

Vec4

Vec4: Float32Array

A length-4 vector

Variables

constsetDefaultType

setDefaultType: never

Do not touch

Functions

addVectors

  • addVectors(vec1, vec2, dst): m4.Vec3
  • Parameters

    • vec1: Float32Array
    • vec2: Float32Array
    • optionaldst: Float32Array

    Returns m4.Vec3

axisRotate

  • axisRotate(m, axis, angleInRadians, dst): m4.Mat4
  • Modifies the given 4-by-4 matrix by rotation around the given axis by the given angle.


    Parameters

    • m: Float32Array

      The matrix.

    • axis: Float32Array

      The axis about which to rotate.

    • angleInRadians: number

      The angle by which to rotate (in radians).

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    m once modified.

axisRotation

  • axisRotation(axis, angleInRadians, dst): m4.Mat4
  • Creates a 4-by-4 matrix which rotates around the given axis by the given angle.


    Parameters

    • axis: Float32Array

      The axis about which to rotate.

    • angleInRadians: number

      The angle by which to rotate (in radians).

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    A matrix which rotates angle radians around the axis.

compose

  • Parameters

    • m: Float32Array
    • a: number
    • b: number
    • optionaldst: Float32Array

    Returns m4.Mat4

copy

  • Copies a matrix.


    Parameters

    • m: Float32Array

      The matrix.

    • optionaldst: Float32Array

      The matrix.

    Returns m4.Mat4

    A copy of m.

cross

  • Computes the cross product of two vectors; assumes both vectors have three entries.


    Parameters

    • a: Float32Array

      Operand vector.

    • b: Float32Array

      Operand vector.

    • optionaldst: Float32Array

      vector to hold result. If not new one is created..

    Returns m4.Vec3

    The vector a cross b.

decompose

  • Parameters

    • a: Float32Array
    • b: Float32Array
    • c: Float32Array
    • dst: Float32Array

    Returns m4.Mat4

distance

  • distance(vec1, vec2): number
  • Parameters

    • vec1: Float32Array
    • vec2: Float32Array

    Returns number

distanceSq

  • distanceSq(vec1, vec2): number
  • Parameters

    • vec1: Float32Array
    • vec2: Float32Array

    Returns number

dot

  • dot(a, b): number
  • Computes the dot product of two vectors; assumes both vectors have three entries.


    Parameters

    • a: Float32Array

      Operand vector.

    • b: Float32Array

      Operand vector.

    Returns number

    dot product

frustum

  • frustum(left, right, bottom, top, near, far, dst): m4.Mat4
  • Computes a 4-by-4 perspective transformation matrix given the left, right, top, bottom, near and far clipping planes. The arguments define a frustum extending in the negative z direction. The arguments near and far are the distances to the near and far clipping planes. Note that near and far are not z coordinates, but rather they are distances along the negative z-axis. The matrix generated sends the viewing frustum to the unit box. We assume a unit box extending from -1 to 1 in the x and y dimensions and from 0 to 1 in the z dimension.


    Parameters

    • left: number

      The x coordinate of the left plane of the box.

    • right: number

      The x coordinate of the right plane of the box.

    • bottom: number

      The y coordinate of the bottom plane of the box.

    • top: number

      The y coordinate of the right plane of the box.

    • near: number

      The negative z coordinate of the near plane of the box.

    • far: number

      The negative z coordinate of the far plane of the box.

    • optionaldst: Float32Array

      Output matrix.

    Returns m4.Mat4

    The perspective projection matrix.

identity

  • identity(dst): number
  • Creates an n-by-n identity matrix.


    Parameters

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns number

    An n-by-n identity matrix.

inverse

  • Computes the inverse of a 4-by-4 matrix.


    Parameters

    • m: Float32Array

      The matrix.

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    The inverse of m.

length

  • length(v): number
  • Computes the length of vector


    Parameters

    • v: Float32Array

      vector.

    Returns number

    length of vector.

lookAt

  • lookAt(eye, target, up, dst): m4.Mat4
  • Computes a 4-by-4 look-at transformation.

    This is a matrix which positions the camera itself. If you want a view matrix (a matrix which moves things in front of the camera) take the inverse of this.


    Parameters

    • eye: Float32Array

      The position of the eye.

    • target: Float32Array

      The position meant to be viewed.

    • up: Float32Array

      A vector pointing up.

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    The look-at matrix.

multiply

  • Multiplies two 4-by-4 matrices; assumes that the given matrices are 4-by-4; assumes matrix entries are accessed in [row][column] fashion.


    Parameters

    • a: Float32Array

      The matrix on the left.

    • b: Float32Array

      The matrix on the right.

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    The matrix product of a and b.

normalize

  • Divides a vector by its Euclidean length and returns the quotient.


    Parameters

    • a: Float32Array

      The vector.

    • optionaldst: Float32Array

      vector to hold result. If not new one is created..

    Returns m4.Vec3

    The normalized vector.

orthographic

  • orthographic(left, right, bottom, top, near, far, dst): m4.Mat4
  • Parameters

    • left: number
    • right: number
    • bottom: number
    • top: number
    • near: number
    • far: number
    • optionaldst: Float32Array

    Returns m4.Mat4

perspective

  • perspective(fieldOfViewYInRadians, aspect, zNear, zFar, dst): m4.Mat4
  • Computes a 4-by-4 perspective transformation matrix given the angular height of the frustum, the aspect ratio, and the near and far clipping planes. The arguments define a frustum extending in the negative z direction. The given angle is the vertical angle of the frustum, and the horizontal angle is determined to produce the given aspect ratio. The arguments near and far are the distances to the near and far clipping planes. Note that near and far are not z coordinates, but rather they are distances along the negative z-axis. The matrix generated sends the viewing frustum to the unit box. We assume a unit box extending from -1 to 1 in the x and y dimensions and from 0 to 1 in the z dimension.


    Parameters

    • fieldOfViewYInRadians: number

      The camera angle from top to bottom (in radians).

    • aspect: number

      The aspect ratio width / height.

    • zNear: number

      The depth (negative z coordinate) of the near clipping plane.

    • zFar: number

      The depth (negative z coordinate) of the far clipping plane.

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    The perspective matrix.

scale

  • Modifies the given 4-by-4 matrix, scaling in each dimension by an amount given by the corresponding entry in the given vector; assumes the vector has three entries.


    Parameters

    • m: Float32Array

      The matrix to be modified.

    • x: number

      X component of the vector by which to scale

    • y: number

      Y component of the vector by which to scale

    • z: number

      Z component of the vector by which to scale

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    m once modified.

scaling

  • Creates a 4-by-4 matrix which scales in each dimension by an amount given by the corresponding entry in the given vector; assumes the vector has three entries.


    Parameters

    • x: number

      X component of the vector by which to scale

    • y: number

      Y component of the vector by which to scale

    • z: number

      Z component of the vector by which to scale

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    The scaling matrix.

subtractVectors

  • subtractVectors(vec1, vec2, dst): m4.Vec3
  • Parameters

    • vec1: Float32Array
    • vec2: Float32Array
    • optionaldst: Float32Array

    Returns m4.Vec3

transformDirection

  • transformDirection(m, v, dst): m4.Vec3
  • Takes a 4-by-4 matrix and a vector with 3 entries, interprets the vector as a direction, transforms that direction by the matrix, and returns the result; assumes the transformation of 3-dimensional space represented by the matrix is parallel-preserving, i.e. any combination of rotation, scaling and translation, but not a perspective distortion. Returns a vector with 3 entries.


    Parameters

    • m: Float32Array

      The matrix.

    • v: Float32Array

      The direction.

    • optionaldst: Float32Array

      optional Vec3 to store result

    Returns m4.Vec3

    dst or new Vec3 if not provided

transformNormal

  • transformNormal(m, v, dst): m4.Vec3
  • Takes a 4-by-4 matrix m and a vector v with 3 entries, interprets the vector as a normal to a surface, and computes a vector which is normal upon transforming that surface by the matrix. The effect of this function is the same as transforming v (as a direction) by the inverse-transpose of m. This function assumes the transformation of 3-dimensional space represented by the matrix is parallel-preserving, i.e. any combination of rotation, scaling and translation, but not a perspective distortion. Returns a vector with 3 entries.


    Parameters

    • m: Float32Array

      The matrix.

    • v: Float32Array

      The normal.

    • optionaldst: Float32Array

      The direction.

    Returns m4.Vec3

    The transformed direction.

transformPoint

  • Takes a 4-by-4 matrix and a vector with 3 entries, interprets the vector as a point, transforms that point by the matrix, and returns the result as a vector with 3 entries.


    Parameters

    • m: Float32Array

      The matrix.

    • v: Float32Array

      The point.

    • optionaldst: Float32Array

      optional vec3 to store result

    Returns m4.Vec3

    dst or new vec3 if not provided

transformVector

  • transformVector(a, b, dst): m4.Vec4
  • Parameters

    • a: Float32Array
    • b: Float32Array
    • optionaldst: Float32Array

    Returns m4.Vec4

translate

  • translate(m, x, y, z, dst): m4.Mat4
  • Modifies the given 4-by-4 matrix by translation by the given vector v.


    Parameters

    • m: Float32Array

      The matrix.

    • x: number

      X component of the vector by which to translate

    • y: number

      Y component of the vector by which to translate

    • z: number

      Z component of the vector by which to translate

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    m once modified.

translation

  • Creates a 4-by-4 matrix which translates by the given vector v.


    Parameters

    • x: number

      X component of the vector by which to translate

    • y: number

      Y component of the vector by which to translate

    • z: number

      Z component of the vector by which to translate

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    The translation matrix.

transpose

  • Takes the transpose of a matrix.


    Parameters

    • m: Float32Array

      The matrix.

    • optionaldst: Float32Array

      matrix to hold result. If none new one is created..

    Returns m4.Mat4

    The transpose of m.

xRotate

  • xRotate(m, angleInRadians, dst): m4.Mat4
  • Parameters

    • m: Float32Array
    • angleInRadians: number
    • optionaldst: Float32Array

    Returns m4.Mat4

xRotation

  • xRotation(angleInRadians, dst): m4.Mat4
  • Parameters

    • angleInRadians: number
    • optionaldst: Float32Array

    Returns m4.Mat4

yRotate

  • yRotate(m, angleInRadians, dst): m4.Mat4
  • Parameters

    • m: Float32Array
    • angleInRadians: number
    • optionaldst: Float32Array

    Returns m4.Mat4

yRotation

  • yRotation(angleInRadians, dst): m4.Mat4
  • Parameters

    • angleInRadians: number
    • optionaldst: Float32Array

    Returns m4.Mat4

zRotate

  • zRotate(m, angleInRadians, dst): m4.Mat4
  • Parameters

    • m: Float32Array
    • angleInRadians: number
    • optionaldst: Float32Array

    Returns m4.Mat4

zRotation

  • zRotation(angleInRadians, dst): m4.Mat4
  • Parameters

    • angleInRadians: number
    • optionaldst: Float32Array

    Returns m4.Mat4