webgl-3d-math

Various 3d math functions.

Methods

(static) addVectors(a, b, dst) → {Vector3}

adds 2 vectors3s

Parameters:
Name Type Description
a Vector3

a

b Vector3

b

dst Vector3

optional vector3 to store result

Returns:
Type:
Vector3

dst or new Vector3 if not provided

(static) axisRotate(m, axis, angleInRadians, dstopt) → {Matrix4}

Multiply by an axis rotation matrix

Parameters:
Name Type Attributes Description
m Matrix4

matrix to multiply

axis Vector3

axis to rotate around

angleInRadians number

amount to rotate

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) axisRotation(axis, angleInRadians, dstopt) → {Matrix4}

Makes an rotation matrix around an arbitrary axis

Parameters:
Name Type Attributes Description
axis Vector3

axis to rotate around

angleInRadians number

amount to rotate

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) cross(a, b, dst) → {Vector3}

Computes the cross product of 2 vectors3s

Parameters:
Name Type Description
a Vector3

a

b Vector3

b

dst Vector3

optional vector3 to store result

Returns:
Type:
Vector3

dst or new Vector3 if not provided

(static) dot(a, b) → {number}

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

Parameters:
Name Type Description
a Vector3

Operand vector.

b Vector3

Operand vector.

Returns:
Type:
number

dot product

(static) frustum(left, right, bottom, top, near, far, dstopt) → {Matrix4}

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 -1 to 1 in the z
dimension.

Parameters:
Name Type Attributes Description
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.

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) identity(dstopt) → {Matrix4}

Makes an identity matrix.

Parameters:
Name Type Attributes Description
dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) inverse(m, dstopt) → {Matrix4}

Computes the inverse of a matrix.

Parameters:
Name Type Attributes Description
m Matrix4

matrix to compute inverse of

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) lookAt(cameraPosition, target, up, dstopt) → {Matrix4}

Creates a lookAt matrix.
This is a world matrix for a camera. In other words it will transform
from the origin to a place and orientation in the world. For a view
matrix take the inverse of this.

Parameters:
Name Type Attributes Description
cameraPosition Vector3

position of the camera

target Vector3

position of the target

up Vector3

direction

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) normalize(v, dst) → {Vector3}

normalizes a vector.

Parameters:
Name Type Description
v Vector3

vector to normalzie

dst Vector3

optional vector3 to store result

Returns:
Type:
Vector3

dst or new Vector3 if not provided

(static) orthographic(left, right, bottom, top, near, far, dstopt) → {Matrix4}

Computes a 4-by-4 orthographic projection matrix given the coordinates of the
planes defining the axis-aligned, box-shaped viewing volume. The matrix
generated sends that box to the unit box. Note that although left and right
are x coordinates and bottom and top are y coordinates, near and far
are not z coordinates, but rather they are distances along the negative
z-axis. We assume a unit box extending from -1 to 1 in the x and y
dimensions and from -1 to 1 in the z dimension.

Parameters:
Name Type Attributes Description
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.

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) perspective(fieldOfViewInRadians, aspect, near, far, dstopt) → {Matrix4}

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 -1 to 1 in the z dimension.

Parameters:
Name Type Attributes Description
fieldOfViewInRadians number

field of view in y axis.

aspect number

aspect of viewport (width / height)

near number

near Z clipping plane

far number

far Z clipping plane

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) scale(m, sx, sy, sz, dstopt) → {Matrix4}

Multiply by a scaling matrix

Parameters:
Name Type Attributes Description
m Matrix4

matrix to multiply

sx number

x scale.

sy number

y scale.

sz number

z scale.

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) scaling(sx, sy, sz, dstopt) → {Matrix4}

Makes a scale matrix

Parameters:
Name Type Attributes Description
sx number

x scale.

sy number

y scale.

sz number

z scale.

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) subtractVectors(a, b, dst) → {Vector3}

subtracts 2 vectors3s

Parameters:
Name Type Description
a Vector3

a

b Vector3

b

dst Vector3

optional vector3 to store result

Returns:
Type:
Vector3

dst or new Vector3 if not provided

(static) transformDirection(m, v, dst) → {Vector4}

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:
Name Type Description
m Matrix4

The matrix.

v Vector3

The direction.

dst Vector4

optional vector4 to store result

Returns:
Type:
Vector4

dst or new Vector4 if not provided

(static) transformNormal(m, v, dstopt) → {Vector3}

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:
Name Type Attributes Description
m Matrix4

The matrix.

v Vector3

The normal.

dst Vector3 <optional>

The direction.

Returns:
Type:
Vector3

The transformed direction.

(static) transformPoint(m, v, dst) → {Vector4}

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:
Name Type Description
m Matrix4

The matrix.

v Vector3

The point.

dst Vector4

optional vector4 to store result

Returns:
Type:
Vector4

dst or new Vector4 if not provided

(static) transformVector(m, v, dst) → {Vector4}

Takes a matrix and a vector with 4 entries, transforms that vector by
the matrix, and returns the result as a vector with 4 entries.

Parameters:
Name Type Description
m Matrix4

The matrix.

v Vector4

The point in homogenous coordinates.

dst Vector4

optional vector4 to store result

Returns:
Type:
Vector4

dst or new Vector4 if not provided

(static) translate(m, tx, ty, tz, dstopt) → {Matrix4}

Mutliply by translation matrix.

Parameters:
Name Type Attributes Description
m Matrix4

matrix to multiply

tx number

x translation.

ty number

y translation.

tz number

z translation.

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) translation(tx, ty, tz, dstopt) → {Matrix4}

Makes a translation matrix

Parameters:
Name Type Attributes Description
tx number

x translation.

ty number

y translation.

tz number

z translation.

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) transpose(m, dstopt) → {Matrix4}

Transposes a matrix.

Parameters:
Name Type Attributes Description
m Matrix4

matrix to transpose.

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) xRotate(m, angleInRadians, dstopt) → {Matrix4}

Multiply by an x rotation matrix

Parameters:
Name Type Attributes Description
m Matrix4

matrix to multiply

angleInRadians number

amount to rotate

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) xRotation(angleInRadians, dstopt) → {Matrix4}

Makes an x rotation matrix

Parameters:
Name Type Attributes Description
angleInRadians number

amount to rotate

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) yRotate(m, angleInRadians, dstopt) → {Matrix4}

Multiply by an y rotation matrix

Parameters:
Name Type Attributes Description
m Matrix4

matrix to multiply

angleInRadians number

amount to rotate

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) yRotation(angleInRadians, dstopt) → {Matrix4}

Makes an y rotation matrix

Parameters:
Name Type Attributes Description
angleInRadians number

amount to rotate

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) zRotate(m, angleInRadians, dstopt) → {Matrix4}

Multiply by an z rotation matrix

Parameters:
Name Type Attributes Description
m Matrix4

matrix to multiply

angleInRadians number

amount to rotate

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

(static) zRotation(angleInRadians, dstopt) → {Matrix4}

Makes an z rotation matrix

Parameters:
Name Type Attributes Description
angleInRadians number

amount to rotate

dst Matrix4 <optional>

optional matrix to store result

Returns:
Type:
Matrix4

dst or a new matrix if none provided

Type Definitions

Matrix4

An array or typed array with 16 values.

Type:
  • Array.<number> | TypedArray

Vector3

An array or typed array with 3 values.

Type:
  • Array.<number> | TypedArray

Vector4

An array or typed array with 4 values.

Type:
  • Array.<number> | TypedArray