# Module: webgl-3d-math

## webgl-3d-math

Various 3d math functions.

### Methods

#### 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:

dst or new Vector3 if not provided

Type
Vector3

#### makeAxisRotation(axis, angleInRadians, dst) → {Matrix4}

Makes an rotation matrix around an arbitrary axis

##### Parameters:
Name Type Argument Description
`axis` Vector3

axis to rotate around

`angleInRadians` number

amount to rotate

`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

#### makeFrustum(left, right, bottom, top, near, far, dst) → {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 Argument 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:

dst or a new matrix of none provided

Type
Matrix4

#### makeIdentity(dst) → {Matrix4}

Makes an identity matrix.

##### Parameters:
Name Type Argument Description
`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

#### makeInverse(m, dst) → {Matrix4}

Computes the inverse of a matrix.

##### Parameters:
Name Type Argument Description
`m` Matrix4

matrix to compute inverse of

`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

#### makeLookAt(cameraPosition, target, up, dst) → {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 Argument 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:

dst or a new matrix of none provided

Type
Matrix4

#### makeOrthographic(left, right, bottom, top, near, far, dst) → {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 Argument 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:

dst or a new matrix of none provided

Type
Matrix4

#### makePerspective(fieldOfViewInRadians, aspect, near, far, dst) → {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 Argument 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:

dst or a new matrix of none provided

Type
Matrix4

#### makeScale(sx, sy, sz, dst) → {Matrix4}

Makes a scale matrix

##### Parameters:
Name Type Argument Description
`sx` number

x scale.

`sy` number

y scale.

`sz` number

z scale.

`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

#### makeTranslation(tx, ty, tz, dst) → {Matrix4}

Makes a translation matrix

##### Parameters:
Name Type Argument Description
`tx` number

x translation.

`ty` number

y translation.

`tz` number

z translation.

`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

#### makeTranspose(m, dst) → {Matrix4}

Transposes a matrix.

##### Parameters:
Name Type Argument Description
`m` Matrix4

matrix to transpose.

`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

Makes an x rotation matrix

##### Parameters:
Name Type Argument Description
`angleInRadians` number

amount to rotate

`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

Makes an y rotation matrix

##### Parameters:
Name Type Argument Description
`angleInRadians` number

amount to rotate

`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

Makes an z rotation matrix

##### Parameters:
Name Type Argument Description
`angleInRadians` number

amount to rotate

`dst` Matrix4 <optional>

optional matrix to store result

##### Returns:

dst or a new matrix of none provided

Type
Matrix4

#### matrixVectorMultiply(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:

dst or new Vector4 if not provided

Type
Vector4

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

normalizes a vector.

##### Parameters:
Name Type Description
`v` Vector3

vector to normalzie

`dst` Vector3

optional vector3 to store result

##### Returns:

dst or new Vector3 if not provided

Type
Vector3

#### 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:

dst or new Vector3 if not provided

Type
Vector3

#### 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:

dst or new Vector4 if not provided

Type
Vector4

#### transformNormal(m, v, dst) → {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 Argument Description
`m` Matrix4

The matrix.

`v` Vector3

The normal.

`dst` Vector3 <optional>

The direction.

##### Returns:

The transformed direction.

Type
Vector3

#### 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:

dst or new Vector4 if not provided

Type
Vector4

### 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