MathUtil
Back to documentation index.
### new MathUtil()
A collection of math functions for working
with vectors, matrices, quaternions, and other
mathematical objects of interest to three-dimensional graphics programming.
## Vectors
A vector is a line segment pointing in a certain direction in space and
having a certain length and an unspecified starting point.
A particular vector can instead be treated as describing a position
(by pointing to that position from an origin (0,0,0)), or a color.
In MathUtil
, vectors are stored in arrays of numbers (usually
three or four numbers), and functions dealing with vectors begin
with “vec”.
If a 4-element vector describes a position or direction, the elements
are given as X, Y, Z, and W, in that order.
If a 4-element vector describes a color, the elements are given as red, green,
blue, and alpha, in that order (where each element ranges from 0-1).
If a 3D direction is used in a 4-element vector function (one beginning with “vec4”),
use 0 as the fourth element. If a 3D position (point) is used in a 4-element vector
function, the fourth element is generally 1. (If the
fourth element is anything other than 0, the vector is in homogeneous
coordinates , where the 3D position equals the first three elements divided
by the fourth.)
A unit vector is a vector with a length of 1. (A vector’s length , or norm , is the square root
of the sum of the squares of its components.) A vector can be “normalized” to
a unit vector by dividing each of its components by its length (doing so won’t change
the vector’s direction). The following functions normalize vectors and find their length: MathUtil.vec3normalize converts a 3-element vector to a unit vector; MathUtil.vec4normalize converts a 4-element vector to a unit vector; MathUtil.vec3length finds a 3-element vector’s length; MathUtil.vec4length finds a 4-element vector’s length. Note that due to rounding error, normalizing a vector with a MathUtil
method might not necessarily result in a vector with a length of 1.
## Matrices
A matrix is a rectangular array that can describe a
transformation from one coordinate system to another. Transformations
that matrices can describe include translation (shifting), scaling, and rotation.
Functions dealing with matrices begin with “mat”.
A 3 × 3 or 4 × 4 matrix has 9 or 16 elements, respectively. In mathematical publications,
matrices are often notated in column-major order, in which each
element of the matrix is placed in columns as opposed to rows, as in the following example:
matrix[0]
matrix[4]
matrix[8]
matrix[12]
matrix[1]
matrix[5]
matrix[9]
matrix[13]
matrix[2]
matrix[6]
matrix[10]
matrix[14]
matrix[3]
matrix[7]
matrix[11]
matrix[15]
The numbers in brackets in the matrix above are the zero-based indices
into the matrix arrays passed to MathUtil
’s matrix methods.
For 3 × 3 matrices, the elements are arranged in the following order:
matrix[0]
matrix[3]
matrix[6]
matrix[1]
matrix[4]
matrix[7]
matrix[2]
matrix[5]
matrix[8]
A Matrix Transforms Between Coordinate Systems: A transformed 3D coordinate system is made up of an x-, y-, and z-axes, and a center of the coordinate system. These are four 3-element vectors that describe how the three axes and the center map to the new coordinate system in relation to the old coordinate system.
The following depiction of a 4 × 4 matrix illustrates the meaning of each of its elements. To keep things
simple, this matrix’s transformation is one that keeps straight lines straight and parallel lines parallel.
[0] x-axis X
[4] y-axis X
[8] z-axis X
[12] Center X
[1] x-axis Y
[5] y-axis Y
[9] z-axis Y
[13] Center Y
[2] x-axis Z
[6] y-axis Z
[10] z-axis Z
[14] Center Z
[3] 0
[7] 0
[11] 0
[15] 1
The following is an example of a transformation matrix.
1
0
0
2
0
0.5
-0.866025</mtd>
3
</mtr>
0
0.866025</mtd>
0.5</mtd>
4
</mtr>
0
0
0
1
</mtable>
</mfenced>
</math>
Here, the first column shows an x-axis vector at (1, 0, 0),
the second column shows a y-axis vector at (0, 0.5, 0.866025),
the third column shows a z-axis vector at (0, -0.866025, 0.5),
and the fourth column centers the coordinate system at (2, 3, 4).
Provided the matrix can be inverted (see the documentation for mat4invert), the three axis vectors are
_basis vectors_ of the coordinate system.
\*Why a 4 × 4 matrix?\*\* A matrix can describe _linear transformations_ from one vector in space
to another. These transformations, which include [\*\*scaling\*\*](#Scaling),
[\*\*rotation\*\*](#Rotation), and shearing, can change where a vector points _at_,
but not where it points _from_. It's enough to use a 3 × 3 matrix to describe
linear transformations in 3D space.
But certain other transformations, such as [\*\*translation\*\*](#Translation) and
[\*\*perspective\*\*](#Projective_Transformations), are common in 3D computer graphics.
To describe translation and perspective in 3D, the 3 × 3 matrix must be
augmented by an additional row and column, turning it into a 4 × 4 matrix.
A 4 × 4 matrix can describe linear transformations in 4D space and
transform 4-element vectors. A 4-element vector has four components:
X, Y, Z, and W. If a 4-element vector represents a 3D point, these
components are the point's _homogeneous coordinates_ (unless the
vector's W is 0). To convert these coordinates back to 3D, divide
X, Y, and Z by W. This is usually only required, however, if the
matrix describes a perspective projection (see
[\*\*"Projective Transformations"\*\*](#Projective_Transformations)).
A similar situation applies in 2D between 2 × 2 and 3 × 3 matrices as it does
in 3D between 3 × 3 and 4 × 4 matrices.
\*Transforming points.\*\* The transformation formula multiplies a matrix by a 3D point to change that point's
position:
\*\*a′\*\*_x_ = matrix[0] ⋅ \*\*a\*\*_x_ + matrix[4] ⋅ \*\*a\*\*_y_ + matrix[8] ⋅ \*\*a\*\*_z_ + matrix[12] ⋅ \*\*a\*\*_w_
\*\*a′\*\*_y_ = matrix[1] ⋅ \*\*a\*\*_x_ + matrix[5] ⋅ \*\*a\*\*_y_ + matrix[9] ⋅ \*\*a\*\*_z_ + matrix[13] ⋅ \*\*a\*\*_w_
\*\*a′\*\*_z_ = matrix[2] ⋅ \*\*a\*\*_x_ + matrix[6] ⋅ \*\*a\*\*_y_ + matrix[10] ⋅ \*\*a\*\*_z_ + matrix[14] ⋅ \*\*a\*\*_w_
\*\*a′\*\*_w_ = matrix[3] ⋅ \*\*a\*\*_x_ + matrix[7] ⋅ \*\*a\*\*_y_ + matrix[11] ⋅ \*\*a\*\*_z_ + matrix[15] ⋅ \*\*a\*\*_w_
For more on why \*\*a′\*\*_w_ appears here, see \*\*"Why a 4 × 4 Matrix?"\*\*, earlier. In each formula that follows, \*\*a\*\*_w_ is assumed to be 1 (indicating a conventional 3D point).
\* Scaling.\*\* Scaling changes an object's size.
To create a scaling matrix, use MathUtil.mat4scaled() ,
and specify the scaling factors for the x-, y-, and z-axes. Each point is multiplied by the scaling
factors to change the object's size. For example, a Y-factor of 2 doubles an object's height.
To multiply an existing matrix by a scaling, use
MathUtil.mat4scale() . This will put the scaling
before the other transformations.
Scaling uses the 1st, 6th, and 11th elements of the matrix as seen here:
sx
0
0
0
0
sy
0
0
0
0
sz
0
0
0
0
1
where the x-coordinate is multiplied by `sx`, the y-coordinate is multiplied by `sy`, and
the z-coordinate is multiplied by `sz`.
The scaling formula would look like:
\*\*a′\*\*_x_ = sx ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + 0
\*\*a′\*\*_y_ = 0 ⋅ \*\*a\*\*_x_ + sy ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + 0
\*\*a′\*\*_z_ = 0 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + sz ⋅ \*\*a\*\*_z_ + 0
\*\*a′\*\*_w_ = 0 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + 1 = 1
For example, we multiply the input x by `sx` to get the output x. If `sx` is 1, x
remains unchanged. Likewise for y (`sy`) and z (`sz`).
If `sx`, `sy`, or `sz` is -1, that coordinate is _reflected_ along the corresponding axis.
If `sx`, `sy`, and `sz` are all 1, we have an _identity matrix_, where the input vector
is equal to the output vector.
When the transformed X, Y, or z-axis has a length other than 1, the coordinate
system will be scaled up or down along that axis. The scalings given
here will scale the lengths of the corresponding axes. For example,
if `sx` is 2, the x-axis will be (2, 0, 0) and thus have a length of 2.
\*\*Translation.\*\* A translation is a shifting of an object's position.
To create a translation matrix, use MathUtil.mat4translated() ,
and specify the X-offset, the Y-offset, and the Z-offset. For example, an X-offset of 1 moves
an object 1 unit to the right, and a Y offset of -1 moves it 1 unit down.
To multiply an existing matrix by a translation, use
MathUtil.mat4translate() . This will put the translation
before the other transformations. In a transformation matrix,
translation effectively occurs after all other transformations such as scaling and rotation.
It uses the 13th, 14th, and 15th elements of the matrix as seen here:
1
0
0
tx
0
1
0
ty
0
0
1
tz
0
0
0
1
where `tx` is added to the x-coordinate, `ty` is added to the y-coordinate, and
`tz` is added to the z-coordinate. The transformation formulas would look like:
\*\*a′\*\*_x_ = 1 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + tx
\*\*a′\*\*_y_ = 0 ⋅ \*\*a\*\*_x_ + 1 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + ty
\*\*a′\*\*_z_ = 0 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 1 ⋅ \*\*a\*\*_z_ + tz
\*\*a′\*\*_w_ = 0 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + 1 = 1
For example, we add the input x and `tx` to get the output x. If `tx` is 0, x
remains unchanged. Likewise for y (`ty`) and z (`tz`).
\*Rotation.\*\* Rotation changes an object's orientation.
To create a rotation matrix, use MathUtil.mat4rotated() ,
and specify the angle (in degrees) to rotate, and the [\*\*axis of rotation\*\*](#Axis_of_Rotation). For example:
Specifying `(45, [1, 0, 0])` means a 45-degree rotation of the point around the x-axis.
Specifying `(80, [0, 2, 3])` means a 45-degree rotation of the point around the axis that
starts at the origin (0, 0, 0) and points toward the point (0, 2, 3).
When describing an axis of rotation, [1, 0, 0]
is the x-axis,
[0, 1, 0]
is the y-axis, and [0, 0, 1]
is the z-axis.
To multiply an existing matrix by a rotation, use
MathUtil.mat4rotate() . This will put the rotation
before the other transformations.
Given an angle of rotation, θ,
the transformation matrix for rotating 3D points is as follows. (For a list of common
sines and cosines, see the end of this section.)
1
0
0
0
0
cos θ
- sin θ
0
0
sin θ
cos θ
0
0
0
0
1
Rotation about the x-axis.
cos θ
0
sin θ
0
0
1
0
0
- sin θ
0
cos θ
0
0
0
0
1
Rotation about the y-axis.
cos θ
- sin θ
0
0
sin θ
cos θ
0
0
0
0
1
0
0
0
0
1
Rotation about the z-axis.
Note that:
When we rotate a point about the x-axis, the x-coordinate is unchanged
and the y- and z-coordinates are adjusted in the rotation. For rotations about the
y-axis or the z-axis, the Y or z-coordinate, respectively, is likewise unchanged.
If the axis of rotation points backward from the "eye", positive rotations mean
counterclockwise rotation in right-handed coordinate systems. For example,
60 degrees about the axis means
60 degrees counterclockwise, and negative 60 degrees means 60 degrees
clockwise.
Rotating a point around an arbitrary axis of rotation is more complicated to describe.
When describing an axis of rotation, [1, 0, 0]
is the x-axis,
[0, 1, 0]
is the y-axis, and [0, 0, 1]
is the z-axis.
See [\*\*"Rotation example"\*\*](#Rotation_Example) for an illustration of a rotation
transformation.
Related functions:
MathUtil.mat4rotated() -
Returns a rotation matrix
MathUtil.mat4rotate() -
Multiplies a matrix by a translation.
A list of common sines and cosines follows. Values
shown with three decimal places are approximate.
| | 0°| 22.5°| 30°| 45°| 60°| 67.5°| 90°| 112.5°| 120°| 135°| 150°| 157.5°| 180°|
-------|---|------|----|----|----|------|----|------|-----|-----|-----|-------|-----|
| sin | 0 | 0.383 | 0.5 | 0.707 | 0.866 | 0.924 | 1 | 0.924 | 0.866 | 0.707 | 0.5 | 0.383 | 0 |
| cos | 1 | 0.924 | 0.866 | 0.707 | 0.5 | 0.383 | 0 | -0.383 | -0.5 | -0.707 | -0.866 | -0.924 | -1 |
| | 180°| 202.5°| 210°| 225°| 240°| 247.5°| 270°| 292.5°| 300°| 315°| 330°| 337.5°| 360°|
-------|---|------|----|----|----|------|----|------|-----|-----|-----|-------|-----|
| sin | 0 | -0.383 | -0.5 | -0.707 | -0.866 | -0.924 | -1 | -0.924 | -0.866 | -0.707 | -0.5 | -0.383 | 0 |
| cos | -1 | -0.924 | -0.866 | -0.707 | -0.5 | -0.383 | 0 | 0.383 | 0.5 | 0.707 | 0.866 | 0.924 | 1 |
### Matrix Multiplication
The order in which you do transforms is important. In general, scaling then translating is
not the same as translating then scaling. Assuming your geometry is centered at the origin
(0, 0, 0), you should create a transformation in this order:
Call `MathUtil.mat4identity()` , creating a matrix without a transformation.
Do your translations if needed, using `mat4translate()` .
Do your rotations if needed, using `mat4rotate()` .
Do your scalings if needed, using `mat4scale()` .
This way, the scalings and rotations will affect the object while it's still centered, and
before the translations (shifts) take place.
You can also multiply transforms using MathUtil.mat4multiply() .
This takes two matrices and returns one combined matrix. The combined matrix will have the effect
of doing the second matrix's transform, then the first matrix's transform.
When two matrices are multiplied, the combined matrix will be such
that the transformations they describe happen in reverse
order. For example, if the first matrix (input matrix) describes a translation and the second
matrix describes a scaling, the multiplied matrix will
describe the effect of scaling then translation.
Matrix multiplication is not commutative; the order of multiplying matrices is important.
To get an insight of how matrix multiplication works, treat the second matrix as a group
of column vectors (with the same number of rows as the number of columns in the
first matrix). Multiplying the two matrices transforms these vectors to new ones in the
same way as if the column vectors were transformed individually. (This also explains why there can
be one or more column vectors in the second matrix and not just four in the case of a 4 × 4 matrix,
and also why transforming a 4-element column vector is the same as multiplying a 4 × 4 matrix by a
matrix with one column and four rows.\*)
This insight reveals a practical use of matrix multiplication: transforming four 4-element
vectors at once using a single matrix operation involving two 4 × 4 matrices. After the
matrix multiplication, each of the transformed vectors will be contained in one of the four columns
of the output matrix.
The methods `mat4multiply`, `mat4scale`, `mat4scaleInPlace`, `mat4translate`, and
mat4rotate involve multiplying 4 × 4 matrices.
Related functions:
MathUtil.mat4multiply() -
Multiplies two matrices
\\* Reading the [\*\*tutorial by Dmitry Sokolov\*\*](https://github.com/ssloy/tinyrenderer/wiki/Lesson-4:-Perspective-projection) led me to this useful insight.
\*Projective transformations.\*\* In all the transformations described earlier, the last row in the transformation matrix is
(0, 0, 0, 1). (Such transformations are called _affine transformations_, those that
keep straight lines straight and parallel lines parallel.) However, this is not the case for
some transformations in the `MathUtil` class.
Transformations that don't necessarily preserve parallelism of lines are called _projective transformations_.
An NxN matrix can describe certain projective transformations if it has one more row and one more column
than the number of dimensions. For example, a 4 × 4 matrix can describe 3D projective transformations
in the form of linear transformations on homogeneous coordinates (see
[\*\*"Why a 4 × 4 Matrix?"\*\*](#Why_a_4x4_Matrix)). For a 3D projective transformation, the last row
in the matrix is not necessarily (0, 0, 0, 1).
One example of a projective transformation is found in a _perspective projection_ matrix,
as returned by MathUtil.mat4perspective or MathUtil.mat4frustum . When a 4-element vector is transformed with this matrix, its W component is generated by setting it to the negative z-coordinate in _eye space_, or more specifically, as follows:
\*\*a′\*\*_w_ = 0 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + -1 ⋅ \*\*a\*\*_z_ + 0
For more on perspective projections, see _The "Camera" and Geometric Transforms_ .
Related functions:
MathUtil.mat4frustum() -
Returns a frustum matrix
MathUtil.mat4perspective() -
Returns a field-of-view perspective matrix
## Rotation Example
As an example, say we rotate 60 degrees about the x-axis (`mat4rotated(60, 1, 0, 0)`,
θ = 60°). First, we find the rotation formula for the x-axis:
\*\*a′\*\*_x_ = 1 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + 0
\*\*a′\*\*_y_ = 0 ⋅ \*\*a\*\*_x_ + (cos θ) ⋅ \*\*a\*\*_y_ + -(sin θ) ⋅ \*\*a\*\*_z_ + 0
\*\*a′\*\*_z_ = 0 ⋅ \*\*a\*\*_x_ + (sin θ) ⋅ \*\*a\*\*_y_ + (cos θ) ⋅ \*\*a\*\*_z_ + 0
\*\*a′\*\*_w_ = 0 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + 1 = 1
We calculate cos θ as 0.5 and sin θ as about 0.866025.
We plug those numbers into the rotation formula to get a formula for rotating a
point 60 degrees about the x-axis.
\*\*a′\*\*_x_ = 1 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + 0 = \*\*a\*\*_x_
\*\*a′\*\*_y_ ~= 0 ⋅ \*\*a\*\*_x_ + 0.5 ⋅ \*\*a\*\*_y_ + -0.866025 ⋅ \*\*a\*\*_z_ + 0
\*\*a′\*\*_z_ ~= 0 ⋅ \*\*a\*\*_x_ + 0.866025 ⋅ \*\*a\*\*_y_ + 0.5 ⋅ \*\*a\*\*_z_ + 0
\*\*a′\*\*_w_ = 0 ⋅ \*\*a\*\*_x_ + 0 ⋅ \*\*a\*\*_y_ + 0 ⋅ \*\*a\*\*_z_ + 1 = 1
If a point is located at (10, 20, 30), the rotated point would now be:
\*\*a′\*\*_x_ = 1 ⋅ 10 + 0 ⋅ 20 + 0 ⋅ 30 + 0
= 1 ⋅ 10
= 10
\*\*a′\*\*_y_ ~= 0 ⋅ 10 + 0.5 ⋅ 20 + -0.866025 ⋅ 30 + 0
~= 0.5 ⋅ 20 + -0.866025 ⋅ 30
~= 10 + -25.98075
~= -15.98075
\*\*a′\*\*_z_ ~= 0 ⋅ 10 + 0.866025 ⋅ 20 + 0.5 ⋅ 30 + 0
~= 0.866025 ⋅ 20 + 0.5 ⋅ 30
~= 17.3205 + 15
~= 32.3205
\*\*a′\*\*_w_ = 0 ⋅ 10 + 0 ⋅ 20 + 0 ⋅ 30 + 1
= 1
So the rotated point would be at about (10, -15.98075, 32.3205).
## Describing Rotations
Rotations in 3D space can be described in many ways, including
quaternions, Tait-Bryan angles, and an angle and axis.
### Axis of Rotation
A rotation of vectors or points can be described using an _angle_
and an _axis of rotation_, for example, in the MathUtil.mat4rotate method.
An axis of rotation is a vector pointing in a certain direction. When a point (or vector)
is rotated at any angle around this axis, the new point (or vector) will lie
on the same plane as the previous point. The axis of rotation describes
a vector that is perpendicular to that plane's surface (the plane's _normal_).
Here are examples of an axis of rotation.
The x-axis of rotation (upward or downward turn) is (1, 0, 0).
The y-axis of rotation (leftward or rightward turn) is (0, 1, 0).
The z-axis of rotation (side-by-side sway) is (0, 0, 1).
While the axis of rotation points backward from the "eye", if the angle's value
is positive and the [\*\*coordinate system\*\*](#Coordinate_Systems) is...
...right handed, then the angle runs counterclockwise.
...left handed, then the angle runs clockwise.
While the axis of rotation points backward from the "eye", if the angle's value
is negative, then the angle runs in the opposite direction.
Vectors that point in the same direction (for example, vectors (1, 0, 0) and (2, 0, 0))
describe the same axis of rotation.
Unless stated otherwise, an axis of rotation passed to a `MathUtil`
method need not be a [\*\*unit vector\*\*](#Unit_Vectors).
### Quaternions
A quaternion is a 4-element vector that can describe a 3D rotation. Functions dealing with quaternions begin with "quat". Functions that generate quaternions include: MathUtil.quatIdentity , which generates a quaternion describing an
absence of rotations; MathUtil.quatFromVectors , which generates a quaternion describing
a rotation from one vector to another; MathUtil.quatFromMat4 , which generates a quaternion from a [\*\*4 × 4 matrix\*\*](#Matrices); MathUtil.quatFromAxisAngle , which generates a quaternion from an angle and [\*\*axis of rotation\*\*](#Axis_of_Rotation); MathUtil.quatFromTaitBryan, which generates a quaternion from Tait-Bryan angles.
#### Using Quaternions
For best results when using quaternions:
- Store the rotation of each object as a single quaternion.
- As rotations occur each frame, convert the rotation (which may be
in pitch/yaw/roll or another form, depending on the input device) to a quaternion
(see [\*\*"Generating Quaternions"\*\*](#Generating_Quaternions)
and multiply that quaternion by the current
quaternion to get the object's new rotation.
- Normalize the rotation quaternion (using `quatNormalize()`
or `quatNormalizeInPlace()` )
every few frames. (Quaternions that describe a 3D rotation should be [\*\*unit vectors\*\*](#Unit_Vectors).)
#### Multiplying Quaternions
When two quaternions are multiplied (for example, with {@MathUtil.quatMultiply}),
the result is a combined rotation in which the second rotation happens
before the first rotation (when applied in the global coordinate frame).
Like matrix multiplication, the order in which you multiply quaternions is important.
### Tait-Bryan angles
Pitch-yaw-roll angles (also called Tait-Bryan angles) describe three different rotations
of the same vector around three different axes, called the pitch, yaw, and roll axes
(or the X, Y, z-axes, respectively), which occur one after the other. However:
There are multiple conventions for pitch-yaw-roll angles, including the order of
rotations (for example: pitch-roll-yaw, roll-pitch-yaw), and whether the rotations occur
around the object's original axes ("extrinsic") or its new axes ("intrinsic").
Rotations are multiplied like in quaternions and matrices, so the order the rotations
occur is important. For example, a 30-degree pitch followed by a 20-degree
roll is not the same as a 20-degree pitch followed by a 30-degree roll.
Pitch-yaw-roll angles can cause a problem called "gimbal lock", in which a rotation along
one axis (say, a pitch) can cause a vector to be parallel to another axis (say, the roll
axis), so that a rotation along that axis will do nothing.
Related functions:
MathUtil.quatFromTaitBryan() -
Converts from Tait-Bryan angles to a quaternion
<a id=4 × 4_Matrices></a>
### 4 × 4 Matrices
A 4 × 4 matrix can describe a 3D vector rotation; see [\*\*"Rotation", earlier\*\*](#Rotation).
## Planes
A 4-element array can describe a plane in the following manner:
The 4 elements, labeled A, B, C, and D in that order, describe a plane
whose points satisfy the equation—
Ax + By + Cz + D = 0
where x, y, and z are the
coordinates of any point lying on the plane.
A, B, and C are
the X, Y, and Z components of the plane's normal vector.
D is the signed distance from the plane to the origin (0,0,0).
It's positive if the plane's normal points toward the origin, and
negative if it points away from the origin.
D is the negative dot product of the
plane's normal and any point on the plane.
There is one method that deals with planes:
MathUtil.planeNormalizeInPlace - Converts the plane to a form in which
its normal has a length of 1.
## Boxes
An array of six numbers can describe an axis-aligned bounding box (AABB).
If it does, the first three numbers are the box's minimum x-, y-, and z-coordinates,
and the last three numbers are the box's maximum x-, y-, and z-coordinates.
If a minimum coordinate is greater than a maximum coordinate, then the
box is considered empty.
Methods that deal with boxes include:
MathUtil.boxCenter - Finds a box's center.
MathUtil.boxDimensions - Finds a box's dimensions.
MathUtil.boxIsEmpty - Determines whether a box is empty.
## Coordinate Systems
There are two conventions of 3D coordinate systems, left-handed and
right-handed:
In a _left-handed_ coordinate system, the positive z-axis points _forward from the "eye"_ whenever the positive x-axis points to the right and the positive y-axis points up.
In a _right-handed_ coordinate system, the positive z-axis points _backward from the "eye"_ whenever the positive x-axis points to the right and the positive y-axis points up.
To show this more visually, point one hand's thumb to your right and
its index finger up, and bend the other three fingers halfway down. In a
coordinate system named after that hand (left-handed or
right-handed), if the positive x-axis points in the thumb's
direction and the positive y-axis points in the index finger's direction, the z-axis will
point in the direction the other three fingers point.
As used here, the z-axis is the cross product
of two perpendicular axes, namely the x-axis and the y-axis, in that order.
Which of the X, Y, or z-axes is the right, up, or forward axis is
arbitrary; for example, some conventions may have the z-axis, rather than Y,
be the up axis. Therefore, these three axes are defined here to avoid
confusion.
### Differences in Behavior
#### Projection and view matrices
The difference between a left-handed and right-handed coordinate system
affects how 3D points are transformed, mainly in the projection and view
matrices. The projection and view matrices returned by Math
matrix methods are designed for a right-handed coordinate system. Their
documentation describes how to adjust them for a left-handed coordinate system.
#### Rotation angles (such as used in `mat4rotate` and `quatRotate`)
While the [\*\*axis of rotation\*\*](#Axis_of_Rotation) points backward from the "eye", if the angle's value
is positive and the [\*\*coordinate system\*\*](#Coordinate_Systems) is...
...right handed, then the angle runs counterclockwise.
...left handed, then the angle runs clockwise.
While the axis of rotation points backward from the "eye", if the angle's value
is negative, then the angle runs in the opposite direction.
#### Cross product (`vec3cross`) and normals
Given a triangle formed by...
- points (A minus C), (B minus C), and C, in that order, or
- points A, B, and (0, 0, 0), in that order,
the cross product of the first point with the second,
in that order, is a _normal_ of that triangle (a vector that's perpendicular to the triangle's surface).
While this particular normal points backward from the "eye", the triangle's vertices
run in a counterclockwise path for right-handed coordinate systems, or a clockwise path
for left-handed systems. (In general, there are two possible choices for normals, which each
point in opposite directions.)
### Winding and face classification
A two-dimensional triangle has counterclockwise _winding_ if its vertices are ordered in a counterclockwise path from the first to second to third to first vertex. Otherwise, it has clockwise winding. If the triangle is in 3D space, it's first transformed into 2D _window coordinates_ before its winding is found. (Window coordinates roughly correspond to screen pixels.)
By default, in the WebGL pipeline, triangles with counterclockwise winding are _front faces_, and
other triangles are _back faces_.
#### Finding a triangle's winding
To find a triangle's winding, do the following calculation (X1, X2, X3 and Y1, Y2, Y3 are the window coordinates of its vertices). Note that half of the result will be the triangle's signed area.
(X3 - X1) \* (Y3 - Y2) - (X3 - X2) \* (Y3 - Y1)
If the result is positive, and the window space x-axis points right and the positive y-axis points...
...up (which is the case in WebGL), then the triangle
has counterclockwise winding.
...down, then the triangle has clockwise winding.
If the result is negative, then the triangle has the opposite winding.
### Members
* [GlobalPitchRollYaw](#MathUtil.GlobalPitchRollYaw) Indicates that a vector's rotation occurs as a pitch, then roll, then yaw (each rotation around the original axes).
* [GlobalPitchYawRoll](#MathUtil.GlobalPitchYawRoll) Indicates that a vector's rotation occurs as a pitch, then yaw, then roll (each rotation around the original axes).
* [GlobalRollPitchYaw](#MathUtil.GlobalRollPitchYaw) Indicates that a vector's rotation occurs as a roll, then pitch, then yaw (each rotation around the original axes).
* [GlobalRollYawPitch](#MathUtil.GlobalRollYawPitch) Indicates that a vector's rotation occurs as a roll, then yaw, then pitch (each rotation around the original axes).
* [GlobalYawPitchRoll](#MathUtil.GlobalYawPitchRoll) Indicates that a vector's rotation occurs as a yaw, then pitch, then roll (each rotation around the original axes).
* [GlobalYawRollPitch](#MathUtil.GlobalYawRollPitch) Indicates that a vector's rotation occurs as a yaw, then roll, then pitch (each rotation around the original axes).
* [HalfPi](#MathUtil.HalfPi) Closest approximation to pi divided by 2, or a 90-degree turn in radians.
* [LocalPitchRollYaw](#MathUtil.LocalPitchRollYaw) Indicates that a vector's rotation occurs as a pitch, then roll, then yaw, where the roll and yaw
occur around the rotated object's new axes and not necessarily the original axes.
* [LocalPitchYawRoll](#MathUtil.LocalPitchYawRoll) Indicates that a vector's rotation occurs as a pitch, then yaw, then roll, where the yaw and roll
occur around the rotated object's new axes and not necessarily the original axes.
* [LocalRollPitchYaw](#MathUtil.LocalRollPitchYaw) Indicates that a vector's rotation occurs as a roll, then pitch, then yaw, where the pitch and yaw
occur around the rotated object's new axes and not necessarily the original axes.
* [LocalRollYawPitch](#MathUtil.LocalRollYawPitch) Indicates that a vector's rotation occurs as a roll, then yaw, then pitch, where the yaw and pitch
occur around the rotated object's new axes and not necessarily the original axes.
* [LocalYawPitchRoll](#MathUtil.LocalYawPitchRoll) Indicates that a vector's rotation occurs as a yaw, then pitch, then roll, where the pitch and roll
occur around the rotated object's new axes and not necessarily the original axes.
* [LocalYawRollPitch](#MathUtil.LocalYawRollPitch) Indicates that a vector's rotation occurs as a yaw, then roll, then pitch, where the roll and pitch
occur around the rotated object's new axes and not necessarily the original axes.
* [Num180DividedByPi](#MathUtil.Num180DividedByPi) Closest approximation to 180 divided by pi, or the number of
degrees in a radian.
* [PiDividedBy180](#MathUtil.PiDividedBy180) Closest approximation to pi divided by 180, or the number
of radians in a degree.
* [PiTimes2](#MathUtil.PiTimes2) Closest approximation to pi times 2, or a 360-degree turn in radians.
* [ToDegrees](#MathUtil.ToDegrees) Closest approximation to 180 divided by pi, or the number of
degrees in a radian.
* [ToRadians](#MathUtil.ToRadians) Closest approximation to pi divided by 180, or the number
of radians in a degree.
### Methods
* [boxCenter](#MathUtil.boxCenter) Finds the center of a 3D bounding box.
* [boxDimensions](#MathUtil.boxDimensions) Finds the dimensions of a 3D bounding box.
* [boxIsEmpty](#MathUtil.boxIsEmpty) Determines whether a 3D bounding box is empty.
* [colorToLinear](#MathUtil.colorToLinear) Converts a color from encoded sRGB to linear sRGB using the sRGB transfer function, and returns
a new vector with the result.
* [colorTosRGB](#MathUtil.colorTosRGB) Converts a color from linear sRGB to encoded sRGB using the sRGB transfer function, and returns
a new vector with the result.
* [frustumHasBox](#MathUtil.frustumHasBox) Determines whether an axis-aligned bounding box
is at least partially inside a view frustum.
* [frustumHasPoint](#MathUtil.frustumHasPoint) Determines whether a point is
outside or inside a view frustum.
* [frustumHasSphere](#MathUtil.frustumHasSphere) Determines whether a sphere is at least
partially inside a view frustum.
* [interpCubicBezier](#MathUtil.interpCubicBezier) An interpolation timing function based on the path of a
cubic Bézier
curve with end points (0, 0) and (1, 1) and with two
configurable control points.
* [mat3copy](#MathUtil.mat3copy) Returns a copy of a 3 × 3 matrix.
* [mat3identity](#MathUtil.mat3identity) Returns the identity 3 × 3 matrix (a matrix that keeps
vectors unchanged when they are transformed with this matrix).
* [mat3invert](#MathUtil.mat3invert) Finds the inverse of a 3 × 3 matrix, describing a transformation that undoes the given transformation.
* [mat3multiply](#MathUtil.mat3multiply) Multiplies two 3 × 3 matrices.
* [mat3transform](#MathUtil.mat3transform) Transforms a 3-element vector with a 3 × 3 matrix and returns
the transformed vector.
* [mat3transpose](#MathUtil.mat3transpose) Returns the transpose of a 3 × 3 matrix.
* [mat3transposeInPlace](#MathUtil.mat3transposeInPlace) Transposes a 3 × 3 matrix in place without creating
a new matrix.
* [mat4copy](#MathUtil.mat4copy) Returns a copy of a 4 × 4 matrix.
* [mat4frustum](#MathUtil.mat4frustum) Returns a 4 × 4 matrix representing a perspective projection
in the form of a view frustum, or the limits in the "eye"'s view.
* [mat4identity](#MathUtil.mat4identity) Returns the identity 4 × 4 matrix (a matrix that keeps
vectors unchanged when they are transformed with this matrix).
* [mat4inverseTranspose3](#MathUtil.mat4inverseTranspose3) Returns the transposed result of the inverted 3 × 3 upper-left corner of
the given 4 × 4 matrix.
* [mat4invert](#MathUtil.mat4invert) Finds the inverse of a 4 × 4 matrix.
* [mat4isIdentity](#MathUtil.mat4isIdentity) Returns whether a 4 × 4 matrix is the identity matrix.
* [mat4lookat](#MathUtil.mat4lookat) Returns a 4 × 4 matrix that represents a "camera" view,
transforming world space coordinates, shared by every object in a scene, to coordinates in eye space
(also called camera space or view space ).
* [mat4multiply](#MathUtil.mat4multiply) Multiplies two 4 × 4 matrices.
* [mat4oblique](#MathUtil.mat4oblique) Returns a 4 × 4 view matrix representing an oblique "eye" view,
when used in conjunction with an orthographic projection .
* [mat4ortho](#MathUtil.mat4ortho) Returns a 4 × 4 matrix representing an orthographic projection .
* [mat4ortho2d](#MathUtil.mat4ortho2d) Returns a 4 × 4 matrix representing a 2D orthographic projection .
* [mat4ortho2dAspect](#MathUtil.mat4ortho2dAspect) Returns a 4 × 4 matrix representing a 2D orthographic projection ,
retaining the view rectangle's aspect ratio.
* [mat4orthoAspect](#MathUtil.mat4orthoAspect) Returns a 4 × 4 matrix representing an orthographic projection ,
retaining the view rectangle's aspect ratio.
* [mat4perspective](#MathUtil.mat4perspective) Returns a 4 × 4 matrix representing a perspective projection .
* [mat4perspectiveHorizontal](#MathUtil.mat4perspectiveHorizontal) Returns a 4 × 4 matrix representing a perspective projection ,
given an x-axis field of view.
* [mat4pickMatrix](#MathUtil.mat4pickMatrix) Returns a 4 × 4 matrix that transforms the view to the center of the viewport.
* [mat4projectVec3](#MathUtil.mat4projectVec3) Transforms a 3-element vector with a 4 × 4 matrix and returns
a perspective-correct version of the vector as a 3D point.
* [mat4rotate](#MathUtil.mat4rotate) Multiplies a 4 × 4 matrix by a rotation transformation that rotates vectors
by the given rotation angle and around the given glmath,
and returns a new matrix.
* [mat4rotated](#MathUtil.mat4rotated) Returns a 4 × 4 matrix representing a rotation transformation that rotates vectors
by the given rotation angle and around the given glmath.
* [mat4scale](#MathUtil.mat4scale) Multiplies a 4 × 4 matrix by a scaling transformation.
* [mat4scaleInPlace](#MathUtil.mat4scaleInPlace) Modifies a 4 × 4 matrix by multiplying it by a
scaling transformation.
* [mat4scaled](#MathUtil.mat4scaled) Returns a 4 × 4 matrix representing a scaling transformation.
* [mat4toFrustumPlanes](#MathUtil.mat4toFrustumPlanes) Finds the six clipping planes of a view frustum defined
by a 4 × 4 matrix.
* [mat4toMat3](#MathUtil.mat4toMat3) Returns the upper-left part of a 4 × 4 matrix as a new
3 × 3 matrix.
* [mat4transform](#MathUtil.mat4transform) Transforms a 4-element vector with a 4 × 4 matrix and returns
the transformed vector.
* [mat4transformVec3](#MathUtil.mat4transformVec3) Transforms a 3-element vector with a 4 × 4 matrix as though it were
an affine transformation matrix (without perspective) and returns the transformed vector.
* [mat4translate](#MathUtil.mat4translate) Multiplies a 4 × 4 matrix by a translation transformation.
* [mat4translated](#MathUtil.mat4translated) Returns a 4 × 4 matrix representing a translation.
* [mat4transpose](#MathUtil.mat4transpose) Returns the transpose of a 4 × 4 matrix.
* [mat4transposeInPlace](#MathUtil.mat4transposeInPlace) Transposes a 4 × 4 matrix in place without creating
a new matrix.
* [planeFromNormalAndPoint](#MathUtil.planeFromNormalAndPoint) Creates a plane from a normal vector and a point on the plane.
* [planeNormalize](#MathUtil.planeNormalize) Normalizes this plane so that its normal is a unit vector,
unless all the normal's components are 0, and returns a new plane with the result.
* [planeNormalizeInPlace](#MathUtil.planeNormalizeInPlace) Normalizes this plane so that its normal is a unit vector,
unless all the normal's components are 0, and sets this plane to the result.
* [quatConjugate](#MathUtil.quatConjugate) Returns a quaternion that describes a rotation that undoes the given rotation (an "inverted" rotation); this is done by reversing the sign of the X, Y, and Z components (which describe the quaternion's glmath).
* [quatCopy](#MathUtil.quatCopy) Returns a copy of a quaternion.
* [quatDot](#MathUtil.quatDot) Finds the dot product of two quaternions.
* [quatFromAxisAngle](#MathUtil.quatFromAxisAngle) Generates a quaternion from a rotation transformation that rotates vectors
by the given rotation angle and around the given glmath,
* [quatFromMat4](#MathUtil.quatFromMat4) Generates a quaternion from the vector rotation described in a 4 × 4 matrix.
* [quatFromVectors](#MathUtil.quatFromVectors) Generates a quaternion describing a rotation between
two 3-element vectors.
* [quatIdentity](#MathUtil.quatIdentity) Returns the identity quaternion of multiplication, (0, 0, 0, 1).
* [quatInvert](#MathUtil.quatInvert) Returns a quaternion that describes a rotation that undoes the given rotation (an "inverted" rotation) and is converted to a unit vector.
* [quatIsIdentity](#MathUtil.quatIsIdentity) Returns whether this quaternion is the identity quaternion, (0, 0, 0, 1).
* [quatLength](#MathUtil.quatLength) Returns the distance of this quaternion from the origin.
* [quatMultiply](#MathUtil.quatMultiply) Multiplies two quaternions, creating a composite rotation.
* [quatNlerp](#MathUtil.quatNlerp) Returns a quaternion that lies along the shortest path between the
given two quaternion rotations, using a linear interpolation function, and converts
it to a unit vector.
* [quatNormalize](#MathUtil.quatNormalize) Converts a quaternion to a unit vector; returns a new quaternion.
* [quatNormalizeInPlace](#MathUtil.quatNormalizeInPlace) Converts a quaternion to a unit vector.
* [quatRotate](#MathUtil.quatRotate) Multiplies a quaternion by a rotation transformation that rotates vectors
by the given rotation angle and around the given glmath.
* [quatScale](#MathUtil.quatScale) Multiplies each element of a quaternion by a factor
and returns the result as a new quaternion.
* [quatScaleInPlace](#MathUtil.quatScaleInPlace) Multiplies each element of a quaternion by a factor
and stores the result in that quaternion.
* [quatSlerp](#MathUtil.quatSlerp) Returns a quaternion that lies along the shortest path between the
given two quaternion rotations, using a spherical interpolation function.
* [quatToAxisAngle](#MathUtil.quatToAxisAngle) Calculates the vector rotation for this quaternion in the form
of the angle to rotate the vector by and an glmath to
rotate that vector around.
* [quatToMat4](#MathUtil.quatToMat4) Generates a 4 × 4 matrix describing the rotation
described by this quaternion.
* [quatTransform](#MathUtil.quatTransform) Transforms a 3- or 4-element vector using a
quaternion's vector rotation.
* [vec2abs](#MathUtil.vec2abs) Returns a new 2-element
vector with the absolute value of each of its components.
* [vec2absInPlace](#MathUtil.vec2absInPlace) Sets each component of the given 2-element
vector to its absolute value.
* [vec2add](#MathUtil.vec2add) Adds two 2-element vectors and returns a new
vector with the result.
* [vec2addInPlace](#MathUtil.vec2addInPlace) Adds two 2-element vectors and stores
the result in the first vector.
* [vec2assign](#MathUtil.vec2assign) Assigns the values of a 2-element vector into another
2-element vector.
* [vec2clamp](#MathUtil.vec2clamp) Returns a 2-element vector in which each element of the given 2-element vector is clamped
so it's not less than one value or greater than another value.
* [vec2clampInPlace](#MathUtil.vec2clampInPlace) Clamps each element of the given 2-element vector
so it's not less than one value or greater than another value.
* [vec2copy](#MathUtil.vec2copy) Returns a copy of a 2-element vector.
* [vec2dist](#MathUtil.vec2dist) Finds the straight-line distance from one three-element vector
to another, treating both as 3D points.
* [vec2dot](#MathUtil.vec2dot) Finds the dot product of two 2-element vectors.
* [vec2length](#MathUtil.vec2length) Returns the distance of this 2-element vector from the origin,
also known as its length or magnitude .
* [vec2lerp](#MathUtil.vec2lerp) Does a linear interpolation between two 2-element vectors;
returns a new vector.
* [vec2mul](#MathUtil.vec2mul) Multiplies each of the components of two 2-element vectors and returns a new
vector with the result.
* [vec2mulInPlace](#MathUtil.vec2mulInPlace) Multiplies each of the components of two 2-element vectors and stores
the result in the first vector.
* [vec2negate](#MathUtil.vec2negate) Negates a 2-element vector and returns a new
vector with the result, which is generally a vector with
the same length but opposite direction.
* [vec2negateInPlace](#MathUtil.vec2negateInPlace) Negates a 2-element vector in place, generally resulting in a vector with
the same length but opposite direction.
* [vec2normalize](#MathUtil.vec2normalize) Converts a 2-element vector to a unit vector; returns a new vector.
* [vec2normalizeInPlace](#MathUtil.vec2normalizeInPlace) Converts a 2-element vector to a unit vector.
* [vec2perp](#MathUtil.vec2perp) Returns an arbitrary 2-element vector that is perpendicular
(orthogonal) to the given 2-element vector.
* [vec2proj](#MathUtil.vec2proj) Returns the projection of a 2-element vector on the given
reference vector.
* [vec2reflect](#MathUtil.vec2reflect) Returns a vector that reflects off a surface.
* [vec2scale](#MathUtil.vec2scale) Multiplies each element of a 2-element vector by a factor.
* [vec2scaleInPlace](#MathUtil.vec2scaleInPlace) Multiplies each element of a 2-element vector by a factor, so
that the vector is parallel to the old vector
but its length is multiplied by the given factor.
* [vec2sub](#MathUtil.vec2sub) Subtracts the second vector from the first vector and returns a new
vector with the result.
* [vec2subInPlace](#MathUtil.vec2subInPlace) Subtracts the second vector from the first vector and stores
the result in the first vector.
* [vec3abs](#MathUtil.vec3abs) Returns a new 3-element
vector with the absolute value of each of its components.
* [vec3absInPlace](#MathUtil.vec3absInPlace) Sets each component of the given 3-element
vector to its absolute value.
* [vec3add](#MathUtil.vec3add) Adds two 3-element vectors and returns a new
vector with the result.
* [vec3addInPlace](#MathUtil.vec3addInPlace) Adds two 3-element vectors and stores
the result in the first vector.
* [vec3assign](#MathUtil.vec3assign) Assigns the values of a 3-element vector into another
3-element vector.
* [vec3clamp](#MathUtil.vec3clamp) Returns a 3-element vector in which each element of the given 3-element vector is clamped
so it's not less than one value or greater than another value.
* [vec3clampInPlace](#MathUtil.vec3clampInPlace) Clamps each element of the given 3-element vector
so it's not less than one value or greater than another value.
* [vec3copy](#MathUtil.vec3copy) Returns a copy of a 3-element vector.
* [vec3cross](#MathUtil.vec3cross) Finds the cross product of two 3-element vectors (called A and B).
* [vec3dist](#MathUtil.vec3dist) Finds the straight-line distance from one three-element vector
to another, treating both as 3D points.
* [vec3dot](#MathUtil.vec3dot) Finds the dot product of two 3-element vectors.
* [vec3fromWindowPoint](#MathUtil.vec3fromWindowPoint) Unprojects the window coordinates given in a
3-element vector,
using the given transformation matrix and viewport
rectangle.
* [vec3length](#MathUtil.vec3length) Returns the distance of this 3-element vector from the origin,
also known as its length or magnitude .
* [vec3lerp](#MathUtil.vec3lerp) Does a linear interpolation between two 3-element vectors;
returns a new vector.
* [vec3mul](#MathUtil.vec3mul) Multiplies each of the components of two 3-element vectors and returns a new
vector with the result.
* [vec3mulInPlace](#MathUtil.vec3mulInPlace) Multiplies each of the components of two 3-element vectors and stores
the result in the first vector.
* [vec3negate](#MathUtil.vec3negate) Negates a 3-element vector and returns a new
vector with the result, which is generally a vector with
the same length but opposite direction.
* [vec3negateInPlace](#MathUtil.vec3negateInPlace) Negates a 3-element vector in place, generally resulting in a vector with
the same length but opposite direction.
* [vec3normalize](#MathUtil.vec3normalize) Converts a 3-element vector to a unit vector; returns a new vector.
* [vec3normalizeInPlace](#MathUtil.vec3normalizeInPlace) Converts a 3-element vector to a unit vector.
* [vec3perp](#MathUtil.vec3perp) Returns an arbitrary 3-element vector that is perpendicular
(orthogonal) to the given 3-element vector.
* [vec3proj](#MathUtil.vec3proj) Returns the projection of a 3-element vector on the given
reference vector.
* [vec3reflect](#MathUtil.vec3reflect) Returns a vector that reflects off a surface.
* [vec3scale](#MathUtil.vec3scale) Multiplies each element of a 3-element vector by a factor.
* [vec3scaleInPlace](#MathUtil.vec3scaleInPlace) Multiplies each element of a 3-element vector by a factor, so
that the vector is parallel to the old vector
but its length is multiplied by the given factor.
* [vec3sub](#MathUtil.vec3sub) Subtracts the second vector from the first vector and returns a new
vector with the result.
* [vec3subInPlace](#MathUtil.vec3subInPlace) Subtracts the second vector from the first vector and stores
the result in the first vector.
* [vec3toWindowPoint](#MathUtil.vec3toWindowPoint) Transforms the 3D point specified in this 3-element vector to its
window coordinates
using the given transformation matrix and viewport rectangle.
* [vec3triple](#MathUtil.vec3triple) Finds the scalar triple product of three vectors (A, B, and C).
* [vec4abs](#MathUtil.vec4abs) Returns a new 4-element
vector with the absolute value of each of its components.
* [vec4absInPlace](#MathUtil.vec4absInPlace) Sets each component of the given 4-element
vector to its absolute value.
* [vec4add](#MathUtil.vec4add) Adds two 4-element vectors and returns a new
vector with the result.
* [vec4addInPlace](#MathUtil.vec4addInPlace) Adds two 4-element vectors and stores
the result in the first vector.
* [vec4assign](#MathUtil.vec4assign) Assigns the values of a 4-element vector into another
4-element vector.
* [vec4clamp](#MathUtil.vec4clamp) Returns a 4-element vector in which each element of the given 4-element vector is clamped
* [vec4clampInPlace](#MathUtil.vec4clampInPlace) Clamps each element of the given 4-element vector
so it's not less than one value or greater than another value.
* [vec4copy](#MathUtil.vec4copy) Returns a copy of a 4-element vector.
* [vec4dot](#MathUtil.vec4dot) Finds the dot product of two 4-element vectors.
* [vec4length](#MathUtil.vec4length) Returns the distance of this 4-element vector from the origin,
also known as its length or magnitude .
* [vec4lerp](#MathUtil.vec4lerp) Does a linear interpolation between two 4-element vectors;
returns a new vector.
* [vec4negate](#MathUtil.vec4negate) Negates a 4-element vector and returns a new
vector with the result, which is generally a vector with
the same length but opposite direction.
* [vec4negateInPlace](#MathUtil.vec4negateInPlace) Negates a 4-element vector in place, generally resulting in a vector with
the same length but opposite direction.
* [vec4normalize](#MathUtil.vec4normalize) Converts a 4-element vector to a unit vector; returns a new vector.
* [vec4normalizeInPlace](#MathUtil.vec4normalizeInPlace) Converts a 4-element vector to a unit vector.
* [vec4proj](#MathUtil.vec4proj) Returns the projection of a 4-element vector on the given
reference vector.
* [vec4scale](#MathUtil.vec4scale) Multiplies each element of a 4-element vector by a factor, returning
a new vector that is parallel to the old vector
but with its length multiplied by the given factor.
* [vec4scaleInPlace](#MathUtil.vec4scaleInPlace) Multiplies each element of a 4-element vector by a factor, so
that the vector is parallel to the old vector
but its length is multiplied by the given factor.
* [vec4sub](#MathUtil.vec4sub) Subtracts the second vector from the first vector and returns a new
vector with the result.
* [vec4subInPlace](#MathUtil.vec4subInPlace) Subtracts the second vector from the first vector and stores
the result in the first vector.
### MathUtil.GlobalPitchRollYaw (constant)
Indicates that a vector's rotation occurs as a pitch, then roll, then yaw (each rotation around the original axes).
### MathUtil.GlobalPitchYawRoll (constant)
Indicates that a vector's rotation occurs as a pitch, then yaw, then roll (each rotation around the original axes).
### MathUtil.GlobalRollPitchYaw (constant)
Indicates that a vector's rotation occurs as a roll, then pitch, then yaw (each rotation around the original axes).
### MathUtil.GlobalRollYawPitch (constant)
Indicates that a vector's rotation occurs as a roll, then yaw, then pitch (each rotation around the original axes).
### MathUtil.GlobalYawPitchRoll (constant)
Indicates that a vector's rotation occurs as a yaw, then pitch, then roll (each rotation around the original axes).
### MathUtil.GlobalYawRollPitch (constant)
Indicates that a vector's rotation occurs as a yaw, then roll, then pitch (each rotation around the original axes).
### MathUtil.HalfPi (constant)
Closest approximation to pi divided by 2, or a 90-degree turn in radians.
Default Value: `1.5707963267948966`
### MathUtil.LocalPitchRollYaw (constant)
Indicates that a vector's rotation occurs as a pitch, then roll, then yaw, where the roll and yaw
occur around the rotated object's new axes and not necessarily the original axes.
### MathUtil.LocalPitchYawRoll (constant)
Indicates that a vector's rotation occurs as a pitch, then yaw, then roll, where the yaw and roll
occur around the rotated object's new axes and not necessarily the original axes.
### MathUtil.LocalRollPitchYaw (constant)
Indicates that a vector's rotation occurs as a roll, then pitch, then yaw, where the pitch and yaw
occur around the rotated object's new axes and not necessarily the original axes.
### MathUtil.LocalRollYawPitch (constant)
Indicates that a vector's rotation occurs as a roll, then yaw, then pitch, where the yaw and pitch
occur around the rotated object's new axes and not necessarily the original axes.
### MathUtil.LocalYawPitchRoll (constant)
Indicates that a vector's rotation occurs as a yaw, then pitch, then roll, where the pitch and roll
occur around the rotated object's new axes and not necessarily the original axes.
### MathUtil.LocalYawRollPitch (constant)
Indicates that a vector's rotation occurs as a yaw, then roll, then pitch, where the roll and pitch
occur around the rotated object's new axes and not necessarily the original axes.
### MathUtil.Num180DividedByPi (constant)
Closest approximation to 180 divided by pi, or the number of
degrees in a radian. Multiply by this number to convert radians to degrees.
Default Value: `57.29577951308232`
### MathUtil.PiDividedBy180 (constant)
Closest approximation to pi divided by 180, or the number
of radians in a degree. Multiply by this number to convert degrees to radians.
Default Value: `0.017453292519943295`
### MathUtil.PiTimes2 (constant)
Closest approximation to pi times 2, or a 360-degree turn in radians.
Default Value: `6.283185307179586`
### MathUtil.ToDegrees (constant)
Closest approximation to 180 divided by pi, or the number of
degrees in a radian. Multiply by this number to convert radians to degrees.
### MathUtil.ToRadians (constant)
Closest approximation to pi divided by 180, or the number
of radians in a degree. Multiply by this number to convert degrees to radians.
### (static) MathUtil.boxCenter(box)
Finds the center of a 3D bounding box.
#### Parameters
* `box` (Type: Array.<number>) An axis-aligned bounding box, which is an array of six values. The first three values are the smallest x-, y-, and z-coordinates, and the last three values are the largest x-, y-, and z-coordinates.
#### Return Value
A 3-element array containing the
x-, y-, and z-coordinates, respectively, of the bounding box's
center. (Type: Array.<number>)
### (static) MathUtil.boxDimensions(box)
Finds the dimensions of a 3D bounding box. This is done by subtracting
the first three values of the given array with its last three values.
#### Parameters
* `box` (Type: Array.<number>) An axis-aligned bounding box, which is an array of six values. The first three values are the smallest x-, y-, and z-coordinates, and the last three values are the largest x-, y-, and z-coordinates.
#### Return Value
A 3-element array containing the
width, height, and depth of the bounding box, respectively. If
at least one of the minimum coordinates is greater than its
corresponding maximum coordinate, the array can contain
negative values. (Type: Array.<number>)
### (static) MathUtil.boxIsEmpty(box)
Determines whether a 3D bounding box is empty.
This is determined if the minimum coordinate
is larger than the corresponding maximum coordinate.
#### Parameters
* `box` (Type: Array.<number>) An axis-aligned bounding box, which is an array of six values. The first three values are the smallest x-, y-, and z-coordinates, and the last three values are the largest x-, y-, and z-coordinates.
#### Return Value
true
if at least one
of the minimum coordinates is greater than its
corresponding maximum coordinate; otherwise, false
. (Type: boolean)
### (static) MathUtil.colorToLinear(srgb)
Converts a color from encoded sRGB to linear sRGB using the sRGB transfer function, and returns
a new vector with the result.
Linear RGB is linear because of its linear relationship of light emitted
by a surface of the given color.
#### Parameters
* `srgb` (Type: Array.<number>) A three- or four-element vector giving the red, green, and blue components, in that order, of an sRGB color. The alpha component is either the fourth element in the case of a four-element vector, or 1.0 in the case of a three-element vector. Each element in the vector ranges from 0 through 1.
#### Return Value
A three-element vector giving
the red, green, and blue components, in that order, of the given color
in linear sRGB. The alpha component will be as specified
in the "srgb" parameter. (Type: Array.<number>)
### (static) MathUtil.colorTosRGB(lin)
Converts a color from linear sRGB to encoded sRGB using the sRGB transfer function, and returns
a new vector with the result.
Linear RGB is linear because of its linear relationship of light emitted
by a surface of the given color.
#### Parameters
* `lin` (Type: Array.<number>) A three- or four-element vector giving the red, green, and blue components, in that order, of a linear RGB color. The alpha component is either the fourth element in the case of a four-element vector, or 1.0 in the case of a three-element vector. Each element in the vector ranges from 0 through 1.
#### Return Value
lin A four-element vector giving
the red, green, blue, and alpha components, in that order, of the given color
in encoded sRGB. The alpha component will be as specified
in the "lin" parameter. (Type: Array.<number>)
### (static) MathUtil.frustumHasBox(frustum, box)
Determines whether an axis-aligned bounding box
is at least partially inside a view frustum.
#### Parameters
* `frustum` (Type: Array.<Array.<number>>) An array of six 4-element arrays representing the six clipping planes of the view frustum. In order, they are the left, right, top, bottom, near, and far clipping planes.
* `box` (Type: Array.<number>) An axis-aligned bounding box in world space, which is an array of six values. The first three values are the smallest x-, y-, and z-coordinates, and the last three values are the largest x-, y-, and z-coordinates.
#### Return Value
true
if the box
may be partially or totally
inside the frustum; false
if the box is
definitely outside the frustum, or if the box is empty
(see "boxIsEmpty"). (Type: boolean)
### (static) MathUtil.frustumHasPoint(frustum, x, y, z)
Determines whether a point is
outside or inside a view frustum.
#### Parameters
* `frustum` (Type: Array.<Array.<number>>) An array of six 4-element arrays representing the six clipping planes of the view frustum. In order, they are the left, right, top, bottom, near, and far clipping planes.
* `x` (Type: number) The x-coordinate of a point in world space.
* `y` (Type: number) The y-coordinate of a point in world space.
* `z` (Type: number) The z-coordinate of a point in world space.
#### Return Value
true if the point is inside;
otherwise false; (Type: boolean)
### (static) MathUtil.frustumHasSphere(frustum, x, y, z, radius)
Determines whether a sphere is at least
partially inside a view frustum.
#### Parameters
* `frustum` (Type: Array.<Array.<number>>) An array of six 4-element arrays representing the six clipping planes of the view frustum. In order, they are the left, right, top, bottom, near, and far clipping planes.
* `x` (Type: number) The x-coordinate of the sphere's center in world space.
* `y` (Type: number) The y-coordinate of the sphere's center in world space.
* `z` (Type: number) The z-coordinate of the sphere's center in world space.
* `radius` (Type: number) Radius of the sphere in world-space units.
#### Return Value
true
if the sphere
is partially or totally
inside the frustum; false
otherwise. (Type: boolean)
### (static) MathUtil.interpCubicBezier(a, b, c, d, t)
An interpolation timing function based on the path of a
cubic Bézier
curve with end points (0, 0) and (1, 1) and with two
configurable control points. The x-coordinates of the
curve indicate time, and the y-coordinates of the curve
indicate how far the interpolation has reached.
#### Parameters
* `a` (Type: number) The x-coordinate of the first configurable control point of the curve. Should be within the range 0 through 1.
* `b` (Type: number) The y-coordinate of the first configurable control point of the curve. Should be within the range 0 through 1, but may exceed this range.
* `c` (Type: number) The x-coordinate of the second configurable control point of the curve. Should be within the range 0 through 1.
* `d` (Type: number) The y-coordinate of the second configurable control point of the curve. Should be within the range 0 through 1, but may exceed this range.
* `t` (Type: number) Number ranging from 0 through 1 that indicates time.
#### Return Value
Number ranging from 0 through 1 that indicates
how far the interpolation has reached. Returns 0 if t
is 0 or less, and 1 if t
is 1 or greater. (Type: number)
### (static) MathUtil.mat3copy(mat)
Returns a copy of a 3 × 3 matrix.
#### Parameters
* `mat` (Type: Array.<number>) A 3 × 3atrix.
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.mat3identity()
Returns the identity 3 × 3 matrix (a matrix that keeps
vectors unchanged when they are transformed with this matrix).
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.mat3invert(m)
Finds the inverse of a 3 × 3 matrix, describing a transformation that undoes the given transformation.
#### Parameters
* `m` (Type: Array.<number>) A 3 × 3 matrix.
#### Return Value
The resulting 3 × 3 matrix.
Returns the identity matrix if this matrix's determinant, or overall scaling factor, is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.mat3multiply(a, b)
Multiplies two 3 × 3 matrices. A new matrix is returned.
The matrices are multiplied such that the transformations
they describe happen in reverse order. For example, if the first
matrix (input matrix) describes a translation and the second
matrix describes a scaling, the multiplied matrix will describe
the effect of scaling then translation.
The matrix multiplication is effectively done by breaking up matrix b
into three 3-element vectors (the first 3 elements make up the first vector, and so on),
transforming each vector with
matrix a
, and putting the vectors back together into a new matrix.
#### Parameters
* `a` (Type: Array.<number>) The first matrix.
* `b` (Type: Array.<number>) The second matrix.
#### Return Value
The resulting 3 × 3 matrix. (Type: Array.<number>)
### (static) MathUtil.mat3transform(mat, v, [vy], [vz])
Transforms a 3-element vector with a 3 × 3 matrix and returns
the transformed vector.
Transforming a vector (v
) with a matrix (mat
)
is effectively done by breaking up mat
into three 3-element vectors
(the first 3 elements make up the first vector, and so on), multiplying
each vector in mat
by the corresponding component in
v
, and adding up the resulting vectors (except v
) to
get the transformed vector.
#### Parameters
* `mat` (Type: Array.<number>) A 3 × 3 matrix.
* `v` (Type: Array.<number> | number) x-coordinate. If "vy", and "vz" are omitted, this value can instead be a 4-element array giving the x-, y-, and z-coordinates.
* `vy` (Type: number) (optional) The y-coordinate.
* `vz` (Type: number) (optional) The z-coordinate. To transform a 2D point, set Z to 1, and divide the result's X and Y by the result's Z.
#### Return Value
The transformed vector. (Type: Array.<number>)
### (static) MathUtil.mat3transpose(m3)
Returns the transpose of a 3 × 3 matrix. (A transpose is a
matrix whose rows are converted to columns and vice versa.)
#### Parameters
* `m3` (Type: Array.<number>) A 3 × 3 matrix.
#### Return Value
The resulting 3 × 3 matrix. (Type: Array.<number>)
### (static) MathUtil.mat3transposeInPlace(mat)
Transposes a 3 × 3 matrix in place without creating
a new matrix. (A transpose is a matrix whose rows
are converted to columns and vice versa.)
#### Parameters
* `mat` (Type: Array.<number>) A 3 × 3 matrix.
#### Return Value
The parameter "mat". (Type: Array.<number>)
### (static) MathUtil.mat4copy(mat)
Returns a copy of a 4 × 4 matrix.
#### Parameters
* `mat` (Type: Array.<number>) A 4 × 4 matrix.
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.mat4frustum(l, r, b, t, near, far)
Returns a 4 × 4 matrix representing a perspective projection
in the form of a view frustum, or the limits in the "eye"'s view.
When just this matrix is used to transform vertices, transformed coordinates in the view volume will have the following meanings:
x-coordinates range from -W to W (where W is the fourth component of the transformed vertex) and increase from "l" to "r".
y-coordinates range from -W to W and increase from "b" to "t" (or from "t" to "b" by default in Vulkan).
z-coordinates range from -W to W and increase from "near" to "far". (For view volume z-coordinates ranging from 0 to W, to accommodate how DirectX, Metal, and Vulkan handle such coordinates by default, divide the 15th element of the result, or zero-based index 14, by 2.)
The transformed coordinates have the meanings given earlier assuming that the eye space (untransformed) x-, y-, and z-coordinates increase rightward, upward, and from the "eye" backward, respectively (so that the eye space is a glmath). To adjust this method's result for the opposite "hand", reverse the sign of the result's:
1st to 4th elements (zero-based indices 0 to 3), to reverse the direction in which x-coordinates increase, or
5th to 8th elements (zero-based indices 4 to 7), to reverse the direction in which y-coordinates increase (for example, to make those coordinates increase upward in Vulkan rather than downward), or
9th to 12th elements (zero-based indices 8 to 11), to reverse the direction in which z-coordinates increase.</ul>
#### Parameters
* `l` (Type: number) The x-coordinate of the point in eye space where the left clipping plane meets the near clipping plane.
* `r` (Type: number) The x-coordinate of the point in eye space where the right clipping plane meets the near clipping plane. ("l" is usually less than "r", so that x-coordinates increase from left to right when just this matrix is used to transform vertices. If "l" is greater than "r", x-coordinates increase in the opposite direction.)
* `b` (Type: number) The y-coordinate of the point in eye space where the bottom clipping plane meets the near clipping plane.
* `t` (Type: number) The y-coordinate of the point in eye space where the top clipping plane meets the near clipping plane. ("b" is usually less than "t", so that, in WebGL and OpenGL by default, y-coordinates increase upward when just this matrix is used to transform vertices. If "b" is greater than "t", y-coordinates increase in the opposite direction.)
* `near` (Type: number) The distance, in eye space, from the "eye" to the near clipping plane. Objects closer than this distance won't be seen. This value should be greater than 0, and should be set to the highest distance from the "eye" that the application can afford to clip out for being too close, for example, 0.5, 1, or higher.
* `far` (Type: number) The distance, in eye space, from the "eye" to the far clipping plane. Objects farther than this distance won't be seen. This value should be greater than 0 and should be set so that the absolute ratio of "far" to "near" is as small as the application can accept. ("near" is usually less than "far", so that, in WebGL and most other graphics pipelines by default, z-coordinates increase from the "eye" backward when just this matrix is used to transform vertices. If "near" is greater than "far", z-coordinates increase in the opposite direction.) In the usual case that "far" is greater than "near", depth buffer values will be more concentrated around the near plane than around the far plane due to the perspective projection. The greater the ratio of "far" to "near", the more concentrated the values will be around the near plane, and the more likely two objects close to the far plane will have identical depth values. (Most WebGL implementations support 24-bit depth buffers, meaning they support 16,777,216 possible values per pixel.)
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4identity()
Returns the identity 4 × 4 matrix (a matrix that keeps
vectors unchanged when they are transformed with this matrix).
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.mat4inverseTranspose3(m4)
Returns the transposed result of the inverted 3 × 3 upper-left corner of
the given 4 × 4 matrix.
This is usually used to convert a model-view matrix (view matrix multiplied by model or world matrix) to a matrix
for transforming surface normals in order to keep them perpendicular
to a surface transformed by the model-view matrix. Normals are then
transformed by this matrix and then converted to unit vectors. But if the
input matrix uses only rotations, translations, and/or uniform scaling
(same scaling in X, Y, and Z), the 3 × 3 upper left of the input matrix can
be used instead of the inverse-transpose matrix to transform the normals.
#### Parameters
* `m4` (Type: Array.<number>) A 4 × 4 matrix.
#### Return Value
The resulting 3 × 3 matrix. If the matrix
can't be inverted, returns the identity 3 × 3 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4invert(m)
Finds the inverse of a 4 × 4 matrix.
An inverted matrix can describe a transformation in three-dimensional space that undoes another transformation. For
example, if a scaling enlarges an object, the inverted matrix reduces the object to its original
size.
To invert a \*\*translation\*\*, reverse the sign of the translation elements (given earlier as `tx`, `ty`, and `tz`)
and generate a new translation matrix with the new translation elements. For example,
to invert the translation (5, 2, -3), use the translation (-5, -2, 3).
To invert a \*\*scaling\*\*, use 1 divided by each of the scaling elements (given earlier as `sx`, `sy`, and `sz`)
and generate a new scaling matrix with those elements.
For example, to invert the scaling (2, 3, 4), use the scaling (1/2, 1/3, 1/4).
To invert a \*\*rotation\*\* for a 4 × 4 matrix, swap the 2nd and 5th elements of the matrix,
the 3rd and 9th elements, and the 7th and 10th elements of the matrix (zero-based
elements 1, 4, 2, 8, 6, and 9 respectively). The effect is like reversing the angle of the
rotation to reset an object to its previous orientation. In general, to invert an NxN
rotation matrix, _transpose_ that matrix (so that its rows become its columns and vice versa).
\*Inverting a general NxN matrix.\*\* Matrices that use some combination of translation, scaling, and rotation as well as other kinds of matrices are more complicated to invert. In fact, some matrices can't be inverted at all. Many 4 × 4 or 3 × 3 matrices can be inverted using the MathUtil.mat4invert() or MathUtil.mat3invert() methods, respectively.
To describe how inverting a matrix works, we will need to define some terms:
A matrix cell's _row index_ and _column index_ tell where that cell appears in the matrix. For example, a cell on the first row has row index 0 and a cell on the second column has column index 1.
A matrix's _determinant_ is its overall
scaling factor. Only an NxN matrix with a determinant other
than 0 can be inverted. To find a matrix's determinant:
1. For each cell in the first row (or first column), find the matrix's _minor_ at that cell (that is, the determinant of a matrix generated by eliminating the row and column where that cell appears in the original matrix).
2. Label the minors (A, B, C, D, ...), in that order.
3. The matrix's determinant is (A - B + C - D + ...).
A 1 × 1 matrix's determinant is simply the value of its only cell.
To invert an NxN matrix:
1. Create a new NxN matrix.
2. For each cell in the original matrix, find its minor at that cell (that is, the determinant of a matrix generated by eliminating the row and column where that cell appears in the original matrix), and set the corresponding cell
in the new matrix to that value.
3. In the new matrix, reverse the sign of each cell whose row index
plus column index is odd. (These cells will alternate in a checkerboard
pattern in the matrix.)
4. Transpose the new matrix (convert its rows to columns
and vice versa).
5. Find the original matrix's determinant and divide each
cell in the new matrix by that value.
6. The new matrix will be the inverted form of the original NxN matrix.
#### Parameters
* `m` (Type: Array.<number>) A 4 × 4 matrix.
#### Return Value
The resulting 4 × 4 matrix.
Returns the identity matrix if this matrix's determinant, or overall scaling factor, is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.mat4isIdentity(mat)
Returns whether a 4 × 4 matrix is the identity matrix.
#### Parameters
* `mat` (Type: Array.<number>) A 4 × 4 matrix.
#### Return Value
Return value. (Type: boolean)
### (static) MathUtil.mat4lookat(cameraPos, [lookingAt], [up])
Returns a 4 × 4 matrix that represents a "camera" view,
transforming world space coordinates, shared by every object in a scene, to coordinates in eye space
(also called camera space or view space ). This essentially rotates a "camera"
and moves it to somewhere in the scene. In eye space, when just this matrix is used to transform vertices:
The "camera" is located at the origin (0,0,0), or
at cameraPos
in world space,
and points forward to the lookingAt
position in world space. This generally
puts lookingAt
at the center of the view.
The x-axis points rightward from the "camera"'s viewpoint.
The y-axis points upward from the center of the "camera" to its top. The
up
vector guides this direction.
The z-axis points away from the lookingAt
point toward the "camera", so that the eye space is a glmath.</ul>
To adjust the result of this method for a left-handed coordinate system (so that the z-axis points in the opposite direction), reverse the sign of the 1st, 3rd, 5th, 7th, 9th, 11th, 13th, and 15th elements of the result (zero-based indices 0, 2, 4, 6, 8, 10, 12, and 14).
#### Parameters
* `cameraPos` (Type: Array.<number>) A 3-element vector specifying the "camera" position in world space. When used in conjunction with an orthographic projection , set this parameter to the value of lookingAt
plus a unit vector (for example, using MathUtil.vec3add ) to form an axonometric projection (if the unit vector is [sqrt(1/3),sqrt(1/3),sqrt(1/3)]
, the result is an isometric projection ). See the following examples.
* `lookingAt` (Type: Array.<number>) (optional) A 3-element vector specifying the point in world space that the "camera" is looking at. May be null or omitted, in which case the default is the coordinates (0,0,0).
* `up` (Type: Array.<number>) (optional) A 3-element vector specifying the direction from the center of the "camera" to its top. This parameter may be null or omitted, in which case the default is the vector (0, 1, 0), the vector that results when the "camera" is held upright. This vector must not be parallel to the view direction (the direction from "cameraPos" to "lookingAt"). (See the example for one way to ensure this.)
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
#### Examples
The following example calls this method with an
up vector of (0, 1, 0) except if the view direction is parallel to that
vector or nearly so.
var upVector=[0,1,0]; // y-axis
var viewdir=MathUtil.vec3sub(lookingAt, cameraPos);
var par=MathUtil.vec3length(MathUtil.vec3cross(viewdir,upVector));
if(par<0.00001)upVector=[0,0,1]; // view is almost parallel, so use z-axis
var matrix=MathUtil.mat4lookat(cameraPos,lookingAt,upVector);
The following example creates an
isometric projection for a right-handed coordinate system. The y-axis will point up, the z-axis toward the lower left, and the x-axis toward
the lower right.
// Assumes an orthographic projection matrix is used. Example:
// var projectionMatrix=MathUtil.mat4ortho(-10,10,-10,10,-50,50);
// "Camera" will be at (1,1,1) -- actually (sqrt(1/3),sqrt(1/3),sqrt(1/3)) --
// and point toward [0,0,0]
var lookPoint=[0,0,0];
var cameraPoint=MathUtil.vec3normalize([1,1,1]);
cameraPoint=MathUtil.vec3add(cameraPoint,lookPoint);
var matrix=MathUtil.mat4lookat(cameraPoint,lookPoint);
The following example is like the previous
example, but with the z-axis pointing up.
var lookPoint=[0,0,0];
var cameraPoint=MathUtil.vec3normalize([1,1,1]);
cameraPoint=MathUtil.vec3add(cameraPoint,lookPoint);
// Positive z-axis is the up vector
var matrix=MathUtil.mat4lookat(cameraPoint,lookPoint,[0,0,1]);
The following example creates a "camera" view matrix using the
viewer position, the viewing direction, and the up vector (a "look-to" matrix):
var viewDirection=[0,0,1]
var cameraPos=[0,0,0]
var upVector=[0,1,0]
var lookingAt=MathUtil.vec3add(cameraPos,viewDirection);
var matrix=MathUtil.mat4lookat(cameraPos,lookingAt,upVector);
### (static) MathUtil.mat4multiply(a, b)
Multiplies two 4 × 4 matrices. A new matrix is returned.
The matrices are multiplied such that the transformations
they describe happen in reverse order. For example, if the first
matrix (input matrix) describes a translation and the second
matrix describes a scaling, the multiplied matrix will describe
the effect of scaling then translation.
The matrix multiplication is effectively done by breaking up matrix b
into four 4-element vectors (the first 4 elements make up the first vector, and so on),
transforming each vector with
matrix a
, and putting the vectors back together into a new matrix.
#### Parameters
* `a` (Type: Array.<number>) The first matrix.
* `b` (Type: Array.<number>) The second matrix.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4oblique(alpha, phi)
Returns a 4 × 4 view matrix representing an oblique "eye" view,
when used in conjunction with an orthographic projection .
This method works the same way in right-handed and left-handed
coordinate systems.
#### Parameters
* `alpha` (Type: number) Controls how much the z-axis is stretched. In degrees. A value of 45 (arctan(1)
) indicates a cabinet projection, and a value of 63.435 (arctan(2)
) indicates a cavalier projection.
* `phi` (Type: number) Controls the apparent angle of the negative z-axis in relation to the positive x-axis. In degrees. 0 means the negative z-axis appears to point in the same direction as the positive x-axis, and 90, in the same direction as the positive y-axis.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4ortho(l, r, b, t, n, f)
Returns a 4 × 4 matrix representing an orthographic projection .
In this projection, the left clipping plane is parallel to the right clipping
plane and the top to the bottom.
The projection returned by this method only scales and/or shifts the view, so that
objects with the same size won't appear smaller as they get more distant from the "camera".
When just this matrix is used to transform vertices, transformed coordinates in the view volume will have the following meanings:
x-coordinates range from -1 to 1 and increase from "l" to "r".
y-coordinates range from -1 to 1 and increase from "b" to "t" (or from "t" to "b" by default in Vulkan).
z-coordinates range from -1 to 1 and increase from "n" to "f". (For view volume z-coordinates ranging from 0 to 1, to accommodate how DirectX, Metal, and Vulkan handle such coordinates by default, divide the 11th and 15th elements of the result, or zero-based indices 10 and 14, by 2, then add 0.5 to the 15th element.)
The transformed coordinates have the meanings given earlier assuming that the eye space (untransformed) x-, y-, and z-coordinates increase rightward, upward, and from the "eye" backward, respectively (so that the eye space is a glmath). To adjust this method's result for the opposite "hand", reverse the sign of the result's:
1st to 4th elements (zero-based indices 0 to 3), to reverse the direction in which x-coordinates increase, or
5th to 8th elements (zero-based indices 4 to 7), to reverse the direction in which y-coordinates increase (for example, to make those coordinates increase upward in Vulkan rather than downward), or
9th to 12th elements (zero-based indices 8 to 11), to reverse the direction in which z-coordinates increase.</ul>
#### Parameters
* `l` (Type: number) Leftmost coordinate of the orthographic view.
* `r` (Type: number) Rightmost coordinate of the orthographic view. ("l" is usually less than "r", so that x-coordinates increase from left to right when just this matrix is used to transform vertices. If "l" is greater than "r", x-coordinates increase in the opposite direction.)
* `b` (Type: number) Bottommost coordinate of the orthographic view.
* `t` (Type: number) Topmost coordinate of the orthographic view. ("b" is usually less than "t", so that, in WebGL and OpenGL by default, y-coordinates increase upward when just this matrix is used to transform vertices. If "b" is greater than "t", y-coordinates increase in the opposite direction.)
* `n` (Type: number) The z-coordinate, in eye space, of the near clipping plane. If z-coordinates in eye space increase from the "eye" forward, a positive value for "n" means the plane is in front of the "eye".
* `f` (Type: number) The z-coordinate, in eye space, of the far clipping plane. If z-coordinates in eye space increase from the "eye" forward, a positive value for "f" means the plane is in front of the "eye". ("n" is usually less than "f", so that, in WebGL and most other graphics pipelines by default, z-coordinates increase from the "eye" backward when just this matrix is used to transform vertices. If "n" is greater than "f", z-coordinates increase in the opposite direction.) The absolute difference between n and f should be as small as the application can accept.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4ortho2d(l, r, b, t)
Returns a 4 × 4 matrix representing a 2D orthographic projection .
This is the same as mat4ortho() with the near clipping plane
set to -1 and the far clipping plane set to 1.
See mat4ortho() for information on the meaning of coordinates
when using this matrix and on adjusting the matrix for different coordinate systems.
#### Parameters
* `l` (Type: number) Leftmost coordinate of the orthographic view.
* `r` (Type: number) Rightmost coordinate of the orthographic view. See mat4ortho() for more information on the relationship between the "l" and "r" parameters.
* `b` (Type: number) Bottommost coordinate of the orthographic view.
* `t` (Type: number) Topmost coordinate of the orthographic view. See mat4ortho() for more information on the relationship between the "b" and "t" parameters.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4ortho2dAspect(l, r, b, t, aspect)
Returns a 4 × 4 matrix representing a 2D orthographic projection ,
retaining the view rectangle's aspect ratio.
If the view rectangle's aspect ratio doesn't match the desired aspect
ratio, the view rectangle will be centered on the viewport
or otherwise moved and scaled so as to keep the entire view rectangle visible without stretching
or squishing it; the projection matrix generated will then use a view volume based on the new view rectangle.
This is the same as mat4orthoAspect() with the near clipping plane
set to -1 and the far clipping plane set to 1.
See mat4ortho() for information on the meaning
of coordinates when using this matrix and on adjusting the matrix for different coordinate systems.
#### Parameters
* `l` (Type: number) Leftmost coordinate of the orthographic view.
* `r` (Type: number) Rightmost coordinate of the orthographic view. See mat4ortho() for more information on the relationship between the "l" and "r" parameters.
* `b` (Type: number) Bottommost coordinate of the orthographic view.
* `t` (Type: number) Topmost coordinate of the orthographic view. See mat4ortho() for more information on the relationship between the "b" and "t" parameters.
* `aspect` (Type: number) The ratio of width to height of the viewport, usually the scene's aspect ratio.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
#### Examples
The following example generates an orthographic
projection matrix with a square view rectangle and an aspect ratio
retrieved from the HTML DOM.
var matrix=MathUtil.mat4ortho2dAspect(0,100,0,100,
window.innerWidth/Math.max(1,window.innerHeight));
### (static) MathUtil.mat4orthoAspect(l, r, b, t, n, f, aspect)
Returns a 4 × 4 matrix representing an orthographic projection ,
retaining the view rectangle's aspect ratio.
If the view rectangle's aspect ratio doesn't match the desired aspect
ratio, the view rectangle will be centered on the viewport
or otherwise moved and scaled so as to keep the entire view rectangle visible without stretching
or squishing it; the projection matrix generated will then use a view volume based on the new view rectangle.
The projection returned by this method only scales and/or shifts the view, so that
objects with the same size won't appear smaller as they get more distant from the "eye".
See mat4ortho() for information on the meaning of coordinates
when using this matrix and on adjusting the matrix for different coordinate systems.
#### Parameters
* `l` (Type: number) Leftmost coordinate of the orthographic view.
* `r` (Type: number) Rightmost coordinate of the orthographic view. See mat4ortho() for more information on the relationship between the "l" and "r" parameters.
* `b` (Type: number) Bottommost coordinate of the orthographic view.
* `t` (Type: number) Topmost coordinate of the orthographic view. See mat4ortho() for more information on the relationship between the "b" and "t" parameters.
* `n` (Type: number) Distance, in eye space, from the "eye" to the near clipping plane.
* `f` (Type: number) Distance, in eye space, from the "eye" to the far clipping plane. See mat4ortho() for more information on the "n" and "f" parameters and the relationship between them.
* `aspect` (Type: number) The ratio of width to height of the viewport, usually the scene's aspect ratio.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
#### Examples
The following example generates an orthographic
projection matrix with a square view rectangle, a Z range of 0 to 100, and an aspect ratio
retrieved from the HTML DOM.
var matrix=MathUtil.mat4orthoAspect(0,100,0,100,
0, 100,
window.innerWidth/Math.max(1,window.innerHeight));
### (static) MathUtil.mat4perspective(fovY, aspectRatio, near, far)
Returns a 4 × 4 matrix representing a perspective projection .
When just this matrix is used to transform vertices, transformed coordinates in the view volume will have the following meanings:
x-coordinates range from -W to W (where W is the fourth component of the transformed vertex) and increase from left to right.
y-coordinates range from -W to W and increase upward (or downward by default in Vulkan).
z-coordinates range from -W to W and increase from "near" to "far". (For view volume z-coordinates ranging from 0 to W, to accommodate how DirectX, Metal, and Vulkan handle such coordinates by default, divide the 15th element of the result, or zero-based index 14, by 2.)
The transformed coordinates have the meanings given earlier assuming that the eye space (untransformed) x-, y-, and z-coordinates increase rightward, upward, and from the "eye" backward, respectively (so that the eye space is a glmath). To adjust this method's result for the opposite "hand", reverse the sign of the result's:
1st to 4th elements (zero-based indices 0 to 3), to reverse the direction in which x-coordinates increase, or
5th to 8th elements (zero-based indices 4 to 7), to reverse the direction in which y-coordinates increase (for example, to make those coordinates increase upward in Vulkan rather than downward), or
9th to 12th elements (zero-based indices 8 to 11), to reverse the direction in which z-coordinates increase.</ul>
#### Parameters
* `fovY` (Type: number) The y-axis field of view, in degrees, that is, the shortest angle between the top and bottom clipping planes. Should be less than 180 degrees. (The smaller this number, the bigger close objects appear to be. As a result, zooming out can be implemented by raising this value, and zooming in by lowering it.)
* `aspectRatio` (Type: number) The ratio of width to height of the viewport, usually the scene's aspect ratio.
* `near` (Type: number) The distance, in eye space, from the "eye" to the near clipping plane. Objects closer than this distance won't be seen. This value should be greater than 0, and should be set to the highest distance from the "eye" that the application can afford to clip out for being too close, for example, 0.5, 1, or higher.
* `far` (Type: number) The distance, in eye space, from the "eye" to the far clipping plane. Objects farther than this distance won't be seen. This value should be greater than 0 and should be set so that the absolute ratio of "far" to "near" is as small as the application can accept. ("near" is usually less than "far", so that, in WebGL and most other graphics pipelines by default, z-coordinates increase from the "eye" backward when just this matrix is used to transform vertices. If "near" is greater than "far", z-coordinates increase in the opposite direction.) In the usual case that "far" is greater than "near", depth buffer values will be more concentrated around the near plane than around the far plane due to the perspective projection. The greater the ratio of "far" to "near", the more concentrated the values will be around the near plane, and the more likely two objects close to the far plane will have identical depth values. (Most WebGL implementations support 24-bit depth buffers, meaning they support 16,777,216 possible values per pixel.)
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
#### Examples
The following example generates a perspective
projection matrix with a 55 degree field of view and an aspect ratio
retrieved from the HTML DOM.
var matrix=MathUtil.mat4perspective(55,
window.innerWidth/Math.max(1,window.innerHeight),
0.01,100);
### (static) MathUtil.mat4perspectiveHorizontal(fovX, aspectRatio, near, far)
Returns a 4 × 4 matrix representing a perspective projection ,
given an x-axis field of view.
See mat4perspective() for information on the meaning of coordinates
when using this matrix and on adjusting the matrix for different coordinate systems.
#### Parameters
* `fovX` (Type: number) The x-axis field of view, in degrees, that is, the shortest angle between the left and right clipping planes. Should be less than 180 degrees. (The smaller this number, the bigger close objects appear to be. As a result, zooming out can be implemented by raising this value, and zooming in by lowering it.)
* `aspectRatio` (Type: number) The ratio of width to height of the viewport, usually the scene's aspect ratio.
* `near` (Type: number) The distance, in eye space, from the "eye" to the near clipping plane. Objects closer than this distance won't be seen. See mat4perspective() for further information on this parameter.
* `far` (Type: number) The distance, in eye space, from the "eye" to the far clipping plane. Objects farther than this distance won't be seen. See mat4perspective() for further information on this parameter.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
#### Examples
The following example generates a perspective
projection matrix with a 120 degree horizontal field of view and an aspect ratio
retrieved from the HTML DOM.
var matrix=MathUtil.mat4perspectiveHorizontal(120,
window.innerWidth/Math.max(1,window.innerHeight),
0.01,100);
### (static) MathUtil.mat4pickMatrix(wx, wy, ww, wh, vp)
Returns a 4 × 4 matrix that transforms the view to the center of the viewport. The resulting matrix should be multiplied by a projection matrix (such as that returned by MathUtil.mat4perspective ), a projection-view matrix (projection matrix multiplied
by the view matrix, in that order),
or a model-view-projection matrix, that is, (projection-view matrix multiplied
by the model [world] matrix, in that order).
#### Parameters
* `wx` (Type: number) The x-coordinate of the center of the desired viewport portion, in window coordinates.
* `wy` (Type: number) The y-coordinate of the center of the desired viewport portion, in window coordinates.
* `ww` (Type: number) Width of the desired viewport portion.
* `wh` (Type: number) Height of the desired viewport portion.
* `vp` (Type: Array.<number>) A 4-element array giving the x- and y-coordinates of the current viewport's origin followed by the width and height of a rectangle indicating the current viewport. If the return value of this method will be multiplied by another matrix (such as that returned by MathUtil.mat4ortho , MathUtil.mat4perspective , or similar MathUtil methods), the viewport's origin is the lower-left corner if x- and y-coordinates within the view volume increase rightward and upward, respectively, or the upper-left corner if x- and y-coordinates within the view volume increase rightward and downward, respectively.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4projectVec3(mat, v, [vy], [vz])
Transforms a 3-element vector with a 4 × 4 matrix and returns
a perspective-correct version of the vector as a 3D point.
The transformation involves transforming a 4-element vector with the same x-, y-, and z-coordinates, but with a W coordinate equal to 1, with the 4 × 4 matrix, and
then dividing X, Y, and Z of the transformed 4-element
vector by that vector's W (a perspective divide ),
then returning that vector's new X, Y, and Z.
#### Parameters
* `mat` (Type: Array.<number>) A 4 × 4 matrix to use to transform the vector. This will generally be a projection-view matrix (projection matrix multiplied by the view matrix, in that order), if the vector to transform is in world space , or a model-view-projection matrix, that is, (projection-view matrix multiplied by the model [world] matrix, in that order), if the vector is in model (object) space . If the matrix includes a projection transform returned by MathUtil.mat4ortho , MathUtil.mat4perspective , or similar MathUtil methods, the x-, y-, and z-coordinates within the view volume, before the perspective divide, will have the range specified in those methods' documentation and increase in the direction given in that documentation, and those coordinates, after the perspective divide will range from -1 to 1 (or 0 to 1) if they previously ranged from -W to W (or 0 to W, respectively).
* `v` (Type: Array.<number> | number) x-coordinate of a 3D point to transform. If "vy" and "vz" are omitted, this value can instead be a 3-element array giving the x-, y-, and z-coordinates.
* `vy` (Type: number) (optional) The y-coordinate.
* `vz` (Type: number) (optional) The z-coordinate. To transform a 2D point, set Z to 0.
#### Return Value
The transformed 3-element vector.
The elements, in order, are
the transformed vector's x-, y-, and z-coordinates. (Type: Array.<number>)
### (static) MathUtil.mat4rotate(mat, angle, v, vy, vz)
Multiplies a 4 × 4 matrix by a rotation transformation that rotates vectors
by the given rotation angle and around the given glmath,
and returns a new matrix.
The effect will be that the rotation transformation will
happen before the transformation described in the given matrix,
when applied in the global coordinate frame.
#### Parameters
* `mat` (Type: Array.<number>) A 4 × 4 matrix to multiply.
* `angle` (Type: Array.<number> | number) The desired angle to rotate in degrees. If "v", "vy", and "vz" are omitted, this can instead be a 4-element array giving the glmath as the first three elements, followed by the angle in degrees as the fourth element.
* `v` (Type: Array.<number> | number) X-component of the point lying on the axis of rotation. If "vy" and "vz" are omitted, this can instead be a 3-element array giving the axis of rotation.
* `vy` (Type: number) Y-component of the point lying on the axis of rotation.
* `vz` (Type: number) Z-component of the point lying on the axis of rotation.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4rotated(angle, v, vy, vz)
Returns a 4 × 4 matrix representing a rotation transformation that rotates vectors
by the given rotation angle and around the given glmath.
#### Parameters
* `angle` (Type: Array.<number> | number) The desired angle to rotate in degrees. If "v", "vy", and "vz" are omitted, this can instead be a 4-element array giving the axis of rotation as the first three elements, followed by the angle in degrees as the fourth element.
* `v` (Type: Array.<number> | number) X-component of the point lying on the axis of rotation. If "vy" and "vz" are omitted, this can instead be a 3-element array giving the axis of rotation.
* `vy` (Type: number) Y-component of the point lying on the axis of rotation.
* `vz` (Type: number) Z-component of the point lying on the axis of rotation.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
#### Examples
The following example rotates a vector,
"vec", about the z-axis by the given angle, "angle".
var newVector = MathUtil.mat4projectVec3(
MathUtil.mat4rotated(angle, 0, 0, 1), vec);
### (static) MathUtil.mat4scale(mat, v3, v3y, v3z)
Multiplies a 4 × 4 matrix by a scaling transformation.
#### Parameters
* `mat` (Type: Array.<number>) 4 × 4 matrix to multiply.
* `v3` (Type: Array.<number> | number) Scale factor along the x-axis. A scale factor can be negative, in which case the transformation also causes reflection about the corresponding axis. If "v3y" and "v3z" are omitted, this value can instead be a 3-element array giving the scale factors along the x-, y-, and z-axes.
* `v3y` (Type: number) Scale factor along the y-axis.
* `v3z` (Type: number) Scale factor along the z-axis.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4scaleInPlace(mat, v3, [v3y], [v3z])
Modifies a 4 × 4 matrix by multiplying it by a
scaling transformation.
#### Parameters
* `mat` (Type: Array.<number>) A 4 × 4 matrix.
* `v3` (Type: Array.<number> | number) Scale factor along the x-axis. A scale factor can be negative, in which case the transformation also causes reflection about the corresponding axis. If "v3y" and "v3z" are omitted, this value can instead be a 3-element array giving the scale factors along the x-, y-, and z-axes.
* `v3y` (Type: number) (optional) Scale factor along the y-axis.
* `v3z` (Type: number) (optional) Scale factor along the z-axis.
#### Return Value
The same parameter as "mat". (Type: Array.<number>)
### (static) MathUtil.mat4scaled(v3, v3y, v3z)
Returns a 4 × 4 matrix representing a scaling transformation.
#### Parameters
* `v3` (Type: Array.<number> | number) Scale factor along the x-axis. A scale factor can be negative, in which case the transformation also causes reflection about the corresponding axis. If "v3y" and "v3z" are omitted, this value can instead be a 3-element array giving the scale factors along the x-, y-, and z-axes.
* `v3y` (Type: number) Scale factor along the y-axis.
* `v3z` (Type: number) Scale factor along the z-axis.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4toFrustumPlanes(matrix)
Finds the six clipping planes of a view frustum defined
by a 4 × 4 matrix. These six planes together form the
shape of a "chopped-off" pyramid (or frustum).
In this model, the "eye" is placed at the top
of the pyramid (before being chopped off), four planes are placed at the pyramid's
sides, one plane (the far plane) forms its base, and a
final plane (the near plane) is the pyramid's chopped
off top.
#### Parameters
* `matrix` (Type: Array.<number>) A 4 × 4 matrix. This will usually be a projection-view matrix (projection matrix multiplied by view matrix, in that order).
#### Return Value
An array of six
4-element arrays representing the six clipping planes of the
view frustum. In order, they are the left, right, top,
bottom, near, and far clipping planes. All six planes
will be normalized (see MathUtil.planeNormalizeInPlace ). (Type: Array.<Array.<number>>)
### (static) MathUtil.mat4toMat3(m4)
Returns the upper-left part of a 4 × 4 matrix as a new
3 × 3 matrix.
#### Parameters
* `m4` (Type: Array.<number>) A 4 × 4 matrix.
#### Return Value
The resulting 3 × 3 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4transform(mat, v, [vy], [vz], [vw])
Transforms a 4-element vector with a 4 × 4 matrix and returns
the transformed vector.
Transforming a vector (v
) with a matrix (mat
)
is effectively done by breaking up mat
into four 4-element vectors
(the first 4 elements make up the first vector, and so on), multiplying
each vector in mat
by the corresponding component in
v
, and adding up the resulting vectors (except v
) to
get the transformed vector.
#### Parameters
* `mat` (Type: Array.<number>) A 4 × 4 matrix.
* `v` (Type: Array.<number> | number) x-coordinate. If "vy", "vz", and "vw" are omitted, this value can instead be a 4-element array giving the X, Y, Z, and W coordinates.
* `vy` (Type: number) (optional) The y-coordinate.
* `vz` (Type: number) (optional) The z-coordinate.
* `vw` (Type: number) (optional) W coordinate. To transform a 3D point, set W to 1 and divide the result's X, Y, and Z by the result's W. To transform a 2D point, set Z to 0 and W to 1 and divide the result's X and Y by the result's W.
#### Return Value
The transformed vector. (Type: Array.<number>)
### (static) MathUtil.mat4transformVec3(mat, v, [vy], [vz])
Transforms a 3-element vector with a 4 × 4 matrix as though it were
an affine transformation matrix (without perspective) and returns the transformed vector.
The effect is as though elements
3, 7, 11, and 15 (counting from 0) of the matrix
were assumed to be (0, 0, 0, 1) instead of their actual values and as though the 3-element
vector had a fourth element valued at 1.
For most purposes, use
the MathUtil.mat4projectVec3 method instead, which supports
4 × 4 matrices that may be in a perspective
projection (whose last row is not necessarily (0, 0, 0, 1)).
#### Parameters
* `mat` (Type: Array.<number>) A 4 × 4 matrix.
* `v` (Type: Array.<number> | number) x-coordinate. If "vy" and "vz" are omitted, this value can instead be a 4-element array giving the x-, y-, and z-coordinates.
* `vy` (Type: number) (optional) The y-coordinate.
* `vz` (Type: number) (optional) The z-coordinate. To transform a 2D point, set Z to 0.
#### Return Value
The transformed 3-element vector. (Type: Array.<number>)
### (static) MathUtil.mat4translate(mat, v3, v3y, v3z)
Multiplies a 4 × 4 matrix by a translation transformation.
#### Parameters
* `mat` (Type: Array.<number>) The matrix to multiply.
* `v3` (Type: Array.<number> | number) Translation along the x-axis. If "v3y" and "v3z" are omitted, this value can instead be a 3-element array giving the translations along the x-, y-, and z-axes.
* `v3y` (Type: number) Translation along the y-axis.
* `v3z` (Type: number) Translation along the z-axis.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4translated(v3, v3y, v3z)
Returns a 4 × 4 matrix representing a translation.
#### Parameters
* `v3` (Type: Array.<number> | number) Translation along the x-axis. If "v3y" and "v3z" are omitted, this value can instead be a 3-element array giving the translations along the x-, y-, and z-axes.
* `v3y` (Type: number) Translation along the y-axis.
* `v3z` (Type: number) Translation along the z-axis.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4transpose(m4)
Returns the transpose of a 4 × 4 matrix. (A transpose is a
matrix whose rows are converted to columns and vice versa.)
#### Parameters
* `m4` (Type: Array.<number>) A 4 × 4 matrix.
#### Return Value
The resulting 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.mat4transposeInPlace(mat)
Transposes a 4 × 4 matrix in place without creating
a new matrix. (A transpose is a matrix whose rows
are converted to columns and vice versa.)
#### Parameters
* `mat` (Type: Array.<number>) A 4 × 4 matrix.
#### Return Value
The parameter "mat". (Type: Array.<number>)
### (static) MathUtil.planeFromNormalAndPoint(normal, point)
Creates a plane from a normal vector and a point on the plane.
#### Parameters
* `normal` (Type: Array.<number>) A three-element array identifying the plane's normal vector.
* `point` (Type: Array.<number>) A three-element array identifying a point on the plane.
#### Return Value
A four-element array describing the plane. (Type: Array.<number>)
### (static) MathUtil.planeNormalize(plane)
Normalizes this plane so that its normal is a unit vector,
unless all the normal's components are 0, and returns a new plane with the result.
The plane's distance will be divided by the
normal's length. Returns a new plane.
#### Parameters
* `plane` (Type: Array.<number>) A four-element array defining the plane. The first three elements of the array are the X, Y, and Z components of the plane's normal vector, and the fourth element is the shortest distance from the plane to the origin, or if negative, from the origin to the plane, divided by the normal's length.
#### Return Value
A normalized version of
the plane.
Note that due to rounding error, the length of the plane's normal might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.planeNormalizeInPlace(plane)
Normalizes this plane so that its normal is a unit vector,
unless all the normal's components are 0, and sets this plane to the result.
The plane's distance will be divided by the
current normal's length.
#### Parameters
* `plane` (Type: Array.<number>) A four-element array defining the plane. The first three elements of the array are the X, Y, and Z components of the plane's normal vector, and the fourth element is the shortest distance from the plane to the origin, or if negative, from the origin to the plane, divided by the normal's length.
#### Return Value
The parameter "plane". (Type: Array.<number>)
### (static) MathUtil.quatConjugate(quat)
Returns a quaternion that describes a rotation that undoes the given rotation (an "inverted" rotation); this is done by reversing the sign of the X, Y, and Z components (which describe the quaternion's glmath). The return value won't necessarily be a unit vector.
#### Parameters
* `quat` (Type: Array.<number>) A quaternion, containing four elements.
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.quatCopy(a)
Returns a copy of a quaternion.
#### Parameters
* `a` (Type: Array.<number>) A quaternion.
#### Return Value
Return value. (Type: Array.<number>)
#### See Also
MathUtil.vec4copy
### (static) MathUtil.quatDot(a, b)
Finds the dot product of two quaternions.
It's equal to the sum of the products of
their components (for example, a 's X times b 's X).
#### Parameters
* `a` (Type: Array.<number>) The first quaternion.
* `b` (Type: Array.<number>) The second quaternion.
#### Return Value
Return value. (Type: number)
#### See Also
MathUtil.vec4dot
### (static) MathUtil.quatFromAxisAngle(angle, v, vy, vz)
Generates a quaternion from a rotation transformation that rotates vectors
by the given rotation angle and around the given glmath,
#### Parameters
* `angle` (Type: Array.<number> | number) The desired angle to rotate in degrees. If "v", "vy", and "vz" are omitted, this can instead be a 4-element array giving the axis of rotation as the first three elements, followed by the angle in degrees as the fourth element.
* `v` (Type: Array.<number> | number) X-component of the point lying on the axis of rotation. If "vy" and "vz" are omitted, this can instead be a 3-element array giving the axis of rotation.
* `vy` (Type: number) Y-component of the point lying on the axis of rotation.
* `vz` (Type: number) Z-component of the point lying on the axis of rotation.
#### Return Value
The generated quaternion.
A quaternion's first three elements (X, Y, Z) describe an
glmath whose length is the sine of half of "angle",
and its fourth element (W) is the cosine of half of "angle". (Type: Array.<number>)
### (static) MathUtil.quatFromMat4(m)
Generates a quaternion from the vector rotation described in a 4 × 4 matrix.
The upper 3 × 3 portion of the matrix is used for this calculation.
The results are undefined if the matrix includes any transformation
other than rotation.
#### Parameters
* `m` (Type: Array.<number>) A 4 × 4 matrix.
#### Return Value
The resulting quaternion. (Type: Array.<number>)
### (static) MathUtil.quatFromVectors(vec1, vec2)
Generates a quaternion describing a rotation between
two 3-element vectors. The quaternion
will describe the rotation required to rotate
the ray described in the first vector toward the ray described
in the second vector. The vectors need not be unit vectors.
#### Parameters
* `vec1` (Type: Array.<number>) The first 3-element vector.
* `vec2` (Type: Array.<number>) The second 3-element vector.
#### Return Value
The generated quaternion, which
will be a unit vector. (Type: Array.<number>)
### (static) MathUtil.quatIdentity()
Returns the identity quaternion of multiplication, (0, 0, 0, 1).
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.quatInvert(quat)
Returns a quaternion that describes a rotation that undoes the given rotation (an "inverted" rotation) and is converted to a unit vector.
#### Parameters
* `quat` (Type: Array.<number>) A quaternion, containing four elements.
#### Return Value
Return value. (Type: Array.<number>)
#### See Also
MathUtil.quatConjugate
### (static) MathUtil.quatIsIdentity(quat)
Returns whether this quaternion is the identity quaternion, (0, 0, 0, 1).
#### Parameters
* `quat` (Type: Array.<number>) A quaternion, containing four elements.
#### Return Value
Return value. (Type: boolean)
### (static) MathUtil.quatLength(quat)
Returns the distance of this quaternion from the origin.
It's the same as the square root of the sum of the squares
of its components.
#### Parameters
* `quat` (Type: Array.<number>) The quaternion.
#### Return Value
Return value. (Type: number)
#### See Also
MathUtil.vec4length
### (static) MathUtil.quatMultiply(a, b)
Multiplies two quaternions, creating a composite rotation.
The quaternions are multiplied such that the second quaternion's
rotation happens before the first quaternion's rotation when applied
in the global coordinate frame.
If both quaternions are unit vectors, the resulting
quaternion will also be a unit vector. However, for best results, you should
normalize the quaternions every few multiplications (using
MathUtil.quatNormalize or MathUtil.quatNormalizeInPlace ), since successive
multiplications can cause rounding errors.
Quaternion multiplication is not commutative except in the last component
of the resulting quaternion, since the definition of quaternion multiplication
includes taking a cross product of a
's and b
's first three components,
in that order, and the cross product is not commutative (see also MathUtil.vec3cross ).
#### Parameters
* `a` (Type: Array.<number>) The first quaternion.
* `b` (Type: Array.<number>) The second quaternion.
#### Return Value
The resulting quaternion. (Type: Array.<number>)
### (static) MathUtil.quatNlerp(q1, q2, factor)
Returns a quaternion that lies along the shortest path between the
given two quaternion rotations, using a linear interpolation function, and converts
it to a unit vector.
This is called a normalized linear interpolation, or "nlerp".
Because the shortest path is curved, not straight, this method
will generally not interpolate at constant velocity;
however, the difference in this velocity when interpolating is
rarely noticeable and this method is generally faster than
the MathUtil.quatSlerp method.
#### Parameters
* `q1` (Type: Array.<number>) The first quaternion. Must be a unit vector.
* `q2` (Type: Array.<number>) The second quaternion. Must be a unit vector.
* `factor` (Type: number) A value that usually ranges from 0 through 1. Closer to 0 means closer to q1, and closer to 1 means closer to q2.
#### Return Value
The interpolated quaternion,
which will be a unit vector. (Type: Array.<number>)
### (static) MathUtil.quatNormalize(quat)
Converts a quaternion to a unit vector; returns a new quaternion.
When a quaternion is normalized, the distance from the origin
to that quaternion becomes 1 (unless all its components are 0).
A quaternion is normalized by dividing each of its components
by its length .
#### Parameters
* `quat` (Type: Array.<number>) A quaternion, containing four elements.
#### Return Value
The normalized quaternion.
Note that due to rounding error, the vector's length might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
#### See Also
MathUtil.vec4normalize
### (static) MathUtil.quatNormalizeInPlace(quat)
Converts a quaternion to a unit vector.
When a quaternion is normalized, it describes the same rotation but the distance from the origin
to that quaternion becomes 1 (unless all its components are 0).
A quaternion is normalized by dividing each of its components
by its length .
#### Parameters
* `quat` (Type: Array.<number>) A quaternion, containing four elements.
#### Return Value
The parameter "quat".
Note that due to rounding error, the vector's length might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
#### See Also
MathUtil.vec4normalizeInPlace
### (static) MathUtil.quatRotate(quat, angle, v, vy, vz)
Multiplies a quaternion by a rotation transformation that rotates vectors
by the given rotation angle and around the given glmath.
The result is such that the angle-axis
rotation happens before the quaternion's rotation when applied
in the global coordinate frame.
This method is equivalent to the following (see also MathUtil.quatMultiply ):
return quatMultiply(quat,quatFromAxisAngle(angle,v,vy,vz));
#### Parameters
* `quat` (Type: Array.<number>) Quaternion to rotate.
* `angle` (Type: Array.<number> | number) The desired angle to rotate in degrees. If "v", "vy", and "vz" are omitted, this can instead be a 4-element array giving the axis of rotation as the first three elements, followed by the angle in degrees as the fourth element.
* `v` (Type: Array.<number> | number) X-component of the point lying on the axis of rotation. If "vy" and "vz" are omitted, this can instead be a 3-element array giving the axis of rotation.
* `vy` (Type: number) Y-component of the point lying on the axis of rotation.
* `vz` (Type: number) Z-component of the point lying on the axis of rotation.
#### Return Value
The resulting quaternion. (Type: Array.<number>)
### (static) MathUtil.quatScale(a, scalar)
Multiplies each element of a quaternion by a factor
and returns the result as a new quaternion.
#### Parameters
* `a` (Type: Array.<number>) A quaternion.
* `scalar` (Type: number) A factor to multiply.
#### Return Value
The resulting quaternion. (Type: Array.<number>)
#### See Also
MathUtil.vec4scaleInPlace
### (static) MathUtil.quatScaleInPlace(a, scalar)
Multiplies each element of a quaternion by a factor
and stores the result in that quaternion.
#### Parameters
* `a` (Type: Array.<number>) A quaternion.
* `scalar` (Type: number) A factor to multiply.
#### Return Value
The parameter "a". (Type: Array.<number>)
#### See Also
MathUtil.vec4scaleInPlace
### (static) MathUtil.quatSlerp(q1, q2, factor)
Returns a quaternion that lies along the shortest path between the
given two quaternion rotations, using a spherical interpolation function.
This is called spherical linear interpolation, or "slerp". (A spherical
interpolation finds the shortest angle between the two quaternions -- which
are treated as 4D vectors -- and then finds a vector with a smaller angle
between it and the first quaternion. The "factor" parameter specifies
how small the new angle will be relative to the original angle.)
This method will generally interpolate at constant velocity; however,
this method is not commutative (that is, the order in which the quaternions are given
matters), unlike quatNlerp , making it
unsuitable for blending multiple quaternion rotations,
and this method is generally more computationally expensive
than the quatNlerp method.
#### Parameters
* `q1` (Type: Array.<number>) The first quaternion. Must be a unit vector.
* `q2` (Type: Array.<number>) The second quaternion. Must be a unit vector.
* `factor` (Type: number) A value that usually ranges from 0 through 1. Closer to 0 means closer to q1, and closer to 1 means closer to q2.
#### Return Value
The interpolated quaternion. (Type: Array.<number>)
#### See Also
["Understanding Slerp, Then Not Using It", Jonathan Blow](http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/),
for additional background
### (static) MathUtil.quatToAxisAngle(a)
Calculates the vector rotation for this quaternion in the form
of the angle to rotate the vector by and an glmath to
rotate that vector around.
#### Parameters
* `a` (Type: Array.<number>) A quaternion. Must be a unit vector.
#### Return Value
A 4-element array giving the axis
of rotation as the first three elements, followed by the angle
in degrees as the fourth element. If "a" is a unit vector, the axis
of rotation will be a unit vector. (Type: Array.<number>)
### (static) MathUtil.quatToMat4(quat)
Generates a 4 × 4 matrix describing the rotation
described by this quaternion.
#### Parameters
* `quat` (Type: Array.<number>) A quaternion, containing four elements.
#### Return Value
The generated 4 × 4 matrix. (Type: Array.<number>)
### (static) MathUtil.quatTransform(q, v)
Transforms a 3- or 4-element vector using a
quaternion's vector rotation.
#### Parameters
* `q` (Type: Array.<number>) A quaternion describing the rotation.
* `v` (Type: Array.<number>) A 3- or 4-element vector to transform. The fourth element, if any, is ignored.
#### Return Value
A 4-element vector representing
the transformed vector. The fourth element will be 1.0.
If the input vector has 3 elements, a 3-element vector will
be returned instead. (Type: Array.<number>)
### (static) MathUtil.vec2abs(a)
Returns a new 2-element
vector with the absolute value of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 2-element vector.
#### Return Value
The resulting 2-element vector. (Type: Array.<number>)
### (static) MathUtil.vec2absInPlace(a)
Sets each component of the given 2-element
vector to its absolute value.
#### Parameters
* `a` (Type: Array.<number>) A 2-element vector.
#### Return Value
The vector "a". (Type: Array.<number>)
### (static) MathUtil.vec2add(a, b)
Adds two 2-element vectors and returns a new
vector with the result. Adding two vectors
is the same as adding each of their components.
The resulting vector:
describes a straight-line path for the
combined paths described by the given vectors, in either order, and
will come "between" the two vectors given (at their shortest angle) if all three start
at the same position.</ul>
#### Parameters
* `a` (Type: Array.<number>) The first 2-element vector.
* `b` (Type: Array.<number>) The second 2-element vector.
#### Return Value
The resulting 2-element vector. (Type: Array.<number>)
### (static) MathUtil.vec2addInPlace(a, b)
Adds two 2-element vectors and stores
the result in the first vector. Adding two vectors
is the same as adding each of their components.
The resulting vector:
describes a straight-line path for the
combined paths described by the given vectors, in either order, and
will come "between" the two vectors given (at their shortest angle) if all three start
at the same position.</ul>
#### Parameters
* `a` (Type: Array.<number>) The first 2-element vector.
* `b` (Type: Array.<number>) The second 2-element vector.
#### Return Value
The parameter "a" (Type: Array.<number>)
### (static) MathUtil.vec2assign(dst, src)
Assigns the values of a 2-element vector into another
2-element vector.
#### Parameters
* `dst` (Type: Array.<number>) The 2-element vector to assign to.
* `src` (Type: Array.<number>) The 2-element vector whose values will be copied.
#### Return Value
The parameter "dst" (Type: Array.<number>)
### (static) MathUtil.vec2clamp(a, min, max)
Returns a 2-element vector in which each element of the given 2-element vector is clamped
so it's not less than one value or greater than another value.
#### Parameters
* `a` (Type: Array.<number>) The vector to clamp.
* `min` (Type: number) Lowest possible value. Should not be greater than "max".
* `max` (Type: number) Highest possible value. Should not be less than "min".
#### Return Value
The resulting vector. (Type: Array.<number>)
### (static) MathUtil.vec2clampInPlace(a, min, max)
Clamps each element of the given 2-element vector
so it's not less than one value or greater than another value.
#### Parameters
* `a` (Type: Array.<number>) The vector to clamp.
* `min` (Type: number) Lowest possible value. Should not be greater than "max".
* `max` (Type: number) Highest possible value. Should not be less than "min".
#### Return Value
The resulting vector. (Type: Array.<number>)
### (static) MathUtil.vec2copy(vec)
Returns a copy of a 2-element vector.
#### Parameters
* `vec` (Type: Array.<number>) A 2-element vector.
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.vec2dist(vecFrom, vecTo)
Finds the straight-line distance from one three-element vector
to another, treating both as 3D points.
#### Parameters
* `vecFrom` (Type: Array.<number>) The first 2-element vector.
* `vecTo` (Type: Array.<number>) The second 2-element vector.
#### Return Value
The distance between the two vectors. (Type: number)
### (static) MathUtil.vec2dot(a, b)
Finds the dot product of two 2-element vectors. It's the
sum of the products of their components (for example, a 's X times
b 's X).
For properties of the dot product, see MathUtil.vec3dot .
#### Parameters
* `a` (Type: Array.<number>) The first 2-element vector.
* `b` (Type: Array.<number>) The second 2-element vector.
#### Return Value
A number representing the dot product. (Type: number)
#### Examples
The following shows a fast way to compare
a vector's length using the dot product.
// Check if the vector's length squared is less than 20 units squared
if(MathUtil.vec2dot(vector, vector)<20*20) {
// The vector's length is shorter than 20 units
}
### (static) MathUtil.vec2length(a)
Returns the distance of this 2-element vector from the origin,
also known as its length or magnitude .
It's the same as the square root of the sum of the squares
of its components.
Note that if vectors are merely sorted or compared by their lengths (and
those lengths are not added or multiplied together or the like),
it's faster to sort or compare them by the squares of their lengths (to find
the square of a 2-element vector's length, call MathUtil.vec2dot
passing the same vector as both of its arguments).
#### Parameters
* `a` (Type: Array.<number>) A 2-element vector.
#### Return Value
Return value. (Type: number)
### (static) MathUtil.vec2lerp(v1, v2, factor)
Does a linear interpolation between two 2-element vectors;
returns a new vector.
#### Parameters
* `v1` (Type: Array.<number>) The first vector to interpolate. The interpolation will occur on each component of this vector and v2.
* `v2` (Type: Array.<number>) The second vector to interpolate.
* `factor` (Type: number) A value that usually ranges from 0 through 1. Closer to 0 means closer to v1, and closer to 1 means closer to v2. For a nonlinear interpolation, define a function that takes a value that usually ranges from 0 through 1 and returns a value generally ranging from 0 through 1, and pass the result of that function to this method. For examples, see MathUtil.vec3lerp .
#### Return Value
The interpolated vector. (Type: Array.<number>)
### (static) MathUtil.vec2mul(a, b)
Multiplies each of the components of two 2-element vectors and returns a new
vector with the result.
#### Parameters
* `a` (Type: Array.<number>) The first 2-element vector.
* `b` (Type: Array.<number>) The second 2-element vector.
#### Return Value
The resulting 2-element vector. (Type: Array.<number>)
### (static) MathUtil.vec2mulInPlace(a, b)
Multiplies each of the components of two 2-element vectors and stores
the result in the first vector.
#### Parameters
* `a` (Type: Array.<number>) The first 2-element vector.
* `b` (Type: Array.<number>) The second 2-element vector.
#### Return Value
The parameter "a" (Type: Array.<number>)
### (static) MathUtil.vec2negate(a)
Negates a 2-element vector and returns a new
vector with the result, which is generally a vector with
the same length but opposite direction. Negating a vector
is the same as reversing the sign of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 2-element vector.
#### Return Value
The resulting 2-element vector. (Type: Array.<number>)
### (static) MathUtil.vec2negateInPlace(a)
Negates a 2-element vector in place, generally resulting in a vector with
the same length but opposite direction.
Negating a vector
is the same as reversing the sign of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 2-element vector.
#### Return Value
The parameter "a". (Type: Array.<number>)
### (static) MathUtil.vec2normalize(vec)
Converts a 2-element vector to a unit vector; returns a new vector.
When a vector is normalized, its direction remains the same but the distance from the origin
to that vector becomes 1 (unless all its components are 0).
A vector is normalized by dividing each of its components
by its length .
#### Parameters
* `vec` (Type: Array.<number>) A 2-element vector.
#### Return Value
The resulting vector.
Note that due to rounding error, the vector's length might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
#### Examples
The following example changes the
length of a line segment.
var startPt=[x1,y1]; // Line segment's start
var endPt=[x2,y2]; // Line segment's end
// Find difference between endPt and startPt
var delta=MathUtil.vec2sub(endPt,startPt);
// Normalize delta to a unit vector
var deltaNorm=MathUtil.vec2normalize(delta);
// Rescale to the desired length, here, 10
MathUtil.vec2scaleInPlace(deltaNorm,10);
// Find the new endpoint
endPt=MathUtil.vec2add(startPt,deltaNorm);
### (static) MathUtil.vec2normalizeInPlace(vec)
Converts a 2-element vector to a unit vector.
When a vector is normalized, its direction remains the same but the distance from the origin
to that vector becomes 1 (unless all its components are 0).
A vector is normalized by dividing each of its components
by its length .
#### Parameters
* `vec` (Type: Array.<number>) A 2-element vector.
#### Return Value
The parameter "vec".
Note that due to rounding error, the vector's length might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.vec2perp(vec)
Returns an arbitrary 2-element vector that is perpendicular
(orthogonal) to the given 2-element vector. The return value
will not be converted to a unit vector.
#### Parameters
* `vec` (Type: Array.<number>) A 2-element vector.
#### Return Value
A perpendicular 2-element
vector. Returns (0,0) if "vec" is (0,0). (Type: Array.<number>)
### (static) MathUtil.vec2proj(vec, refVec)
Returns the projection of a 2-element vector on the given
reference vector. Assuming both vectors
start at the same point, the resulting vector
will be parallel to the
reference vector but will make the closest
approach possible to the projected vector's
endpoint. The difference between the projected
vector and the return value will be perpendicular
to the reference vector.
#### Parameters
* `vec` (Type: Array.<number>) The vector to project.
* `refVec` (Type: Array.<number>) The reference vector whose length will be adjusted.
#### Return Value
The projection of
"vec" on "refVec". Returns (0,0,0) if "refVec"'s
length is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.vec2reflect(incident, normal)
Returns a vector that reflects off a surface.
#### Parameters
* `incident` (Type: Array.<number>) Incident vector, or a vector headed in the direction of the surface, as a 2-element vector.
* `normal` (Type: Array.<number>) Surface normal vector, or a vector that's perpendicular to the surface, as a 2-element vector. Should be a unit vector.
#### Return Value
A vector that has the same length
as "incident" but is reflected away from the surface. (Type: Array.<number>)
### (static) MathUtil.vec2scale(a, scalar)
Multiplies each element of a 2-element vector by a factor. Returns
a new vector that is parallel to the old vector
but with its length multiplied by the given factor. If the factor
is positive, the vector will point in the same direction; if negative,
in the opposite direction; if zero, the vector's components will all be 0.
#### Parameters
* `a` (Type: Array.<number>) A 2-element vector.
* `scalar` (Type: number) A factor to multiply. To divide a vector by a number, the factor will be 1 divided by that number.
#### Return Value
The parameter "a". (Type: Array.<number>)
### (static) MathUtil.vec2scaleInPlace(a, scalar)
Multiplies each element of a 2-element vector by a factor, so
that the vector is parallel to the old vector
but its length is multiplied by the given factor. If the factor
is positive, the vector will point in the same direction; if negative,
in the opposite direction; if zero, the vector's components will all be 0.
#### Parameters
* `a` (Type: Array.<number>) A 2-element vector.
* `scalar` (Type: number) A factor to multiply. To divide a vector by a number, the factor will be 1 divided by that number.
#### Return Value
The parameter "a". (Type: Array.<number>)
### (static) MathUtil.vec2sub(a, b)
Subtracts the second vector from the first vector and returns a new
vector with the result. Subtracting two vectors
is the same as subtracting each of their components.
#### Parameters
* `a` (Type: Array.<number>) The first 2-element vector.
* `b` (Type: Array.<number>) The second 2-element vector.
#### Return Value
The resulting 2-element vector.
This is the vector to a
from b
. (Type: Array.<number>)
### (static) MathUtil.vec2subInPlace(a, b)
Subtracts the second vector from the first vector and stores
the result in the first vector. Subtracting two vectors
is the same as subtracting each of their components.
#### Parameters
* `a` (Type: Array.<number>) The first 2-element vector.
* `b` (Type: Array.<number>) The second 2-element vector.
#### Return Value
The parameter "a".
This is the vector to the previous a
from b
. (Type: Array.<number>)
### (static) MathUtil.vec3abs(a)
Returns a new 3-element
vector with the absolute value of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 3-element vector.
#### Return Value
The resulting 3-element vector. (Type: Array.<number>)
### (static) MathUtil.vec3absInPlace(a)
Sets each component of the given 3-element
vector to its absolute value.
#### Parameters
* `a` (Type: Array.<number>) A 3-element vector.
#### Return Value
The vector "a". (Type: Array.<number>)
### (static) MathUtil.vec3add(a, b)
Adds two 3-element vectors and returns a new
vector with the result. Adding two vectors
is the same as adding each of their components.
The resulting vector:
describes a straight-line path for the
combined paths described by the given vectors, in either order, and
will come "between" the two vectors given (at their shortest angle) if all three start
at the same position.</ul>
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector.
#### Return Value
The resulting 3-element vector. (Type: Array.<number>)
### (static) MathUtil.vec3addInPlace(a, b)
Adds two 3-element vectors and stores
the result in the first vector. Adding two vectors
is the same as adding each of their components.
The resulting vector:
describes a straight-line path for the
combined paths described by the given vectors, in either order, and
will come "between" the two vectors given (at their shortest angle) if all three start
at the same position.</ul>
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector.
#### Return Value
The parameter "a" (Type: Array.<number>)
### (static) MathUtil.vec3assign(dst, src)
Assigns the values of a 3-element vector into another
3-element vector.
#### Parameters
* `dst` (Type: Array.<number>) The 3-element vector to assign to.
* `src` (Type: Array.<number>) The 3-element vector whose values will be copied.
#### Return Value
The parameter "dst" (Type: Array.<number>)
### (static) MathUtil.vec3clamp(a, min, max)
Returns a 3-element vector in which each element of the given 3-element vector is clamped
so it's not less than one value or greater than another value.
#### Parameters
* `a` (Type: Array.<number>) The vector to clamp.
* `min` (Type: number) Lowest possible value. Should not be greater than "max".
* `max` (Type: number) Highest possible value. Should not be less than "min".
#### Return Value
The resulting vector. (Type: Array.<number>)
### (static) MathUtil.vec3clampInPlace(a, min, max)
Clamps each element of the given 3-element vector
so it's not less than one value or greater than another value.
#### Parameters
* `a` (Type: Array.<number>) The vector to clamp.
* `min` (Type: number) Lowest possible value. Should not be greater than "max".
* `max` (Type: number) Highest possible value. Should not be less than "min".
#### Return Value
The resulting vector. (Type: Array.<number>)
### (static) MathUtil.vec3copy(vec)
Returns a copy of a 3-element vector.
#### Parameters
* `vec` (Type: Array.<number>) A 3-element vector.
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.vec3cross(a, b)
Finds the cross product of two 3-element vectors (called A and B).
The following are properties of the cross product:
The cross product will be a vector that is orthogonal (perpendicular) to both A and B.
Switching the order of A and B results in a cross product
vector with the same length but opposite direction. (Thus, the cross product is not commutative ,
but it is anticommutative .)
Let there be a triangle formed by point A, point B, and the point (0,0,0) in that order.
While the cross product of A and B points backward from the "eye",
the triangle's vertices are oriented counterclockwise for glmath,
or clockwise for left-handed systems. The triangle's area is half of the cross product's length.
The length of the cross
product equals |a | * |b | * |sin θ|
where |x | is the length of vector x , and
θ is the shortest angle between a and b .
It follows that:
If the length is 0, then A and B are parallel vectors (0 or 180 degrees apart).
If A and B are unit vectors, the length equals the absolute value
of the sine of the shortest angle between A and B.
If A and B are unit vectors, the cross product will be a unit vector only if A is perpendicular
to B (the shortest angle between A and B will be 90 degrees, since sin 90° = 1).
</ul></ul>
The cross product (c ) of vectors a and b is found as
follows:
c .x = a .y * b .z - a .z * b .y
c .y = a .z * b .x - a .x * b .z
c .z = a .x * b .y - a .y * b .x
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector.
#### Return Value
A 3-element vector containing the cross product. (Type: Array.<number>)
#### Examples
The following example uses the cross product to
calculate a triangle's normal vector and its area.
var a=triangle[0];
var b=triangle[1];
var c=triangle[2];
// Find vector from C to A
var da=MathUtil.vec3sub(a,c);
// Find vector from C to B
var db=MathUtil.vec3sub(b,c);
// The triangle's normal is the cross product of da and db
var normal=MathUtil.vec3cross(da,db);
// Find the triangle's area
var area=MathUtil.vec3length(normal)*0.5;
The following example finds the cosine and sine of
the angle between two unit vectors and the orthogonal unit vector of both.
var cr=MathUtil.vec3cross(unitA,unitB);
// Cosine of the angle. Will be positive or negative depending on
// the shortest angle between the vectors.
var cosine=MathUtil.vec3dot(unitA,unitB);
// Sine of the angle. Note that the sine will always be 0 or greater because
// the shortest angle between them is positive or 0 degrees.
var sine=MathUtil.vec3length(cr);
### (static) MathUtil.vec3dist(vecFrom, vecTo)
Finds the straight-line distance from one three-element vector
to another, treating both as 3D points.
#### Parameters
* `vecFrom` (Type: Array.<number>) The first 3-element vector.
* `vecTo` (Type: Array.<number>) The second 3-element vector.
#### Return Value
The distance between the two vectors. (Type: number)
### (static) MathUtil.vec3dot(a, b)
Finds the dot product of two 3-element vectors. It's the
sum of the products of their components (for example, a 's X times
b 's X).
The following are properties of the dot product:
The dot
product equals |a | * |b | * cos θ
where |x | is the length of vector x , and
θ is the shortest angle between a and b .
It follows that:
A dot product of 0 indicates that the vectors are 90
degrees apart, making them orthogonal
(perpendicular to each other).
A dot product greater than 0 means less than 90 degrees apart.
A dot product less than 0 means greater than 90 degrees apart.
If both vectors are unit vectors, the cosine
of the shortest angle between them is equal to their dot product.
However, Math.acos
won't return a negative angle
from that cosine, so the dot product can't
be used to determine if one vector is "ahead of" or "behind" another
vector.
If both vectors are unit vectors, a dot product of 1 or -1 indicates
that the two vectors are parallel (and the vectors are 0 or
180 degrees apart, respectively.)
If one of the vectors is a unit vector, the dot product's absolute
value will be the length that vector must have to make the closest
approach to the other vector's endpoint. If the dot product is negative,
the unit vector must also be in the opposite direction to approach the
other vector's endpoint.
</ul>
If the two vectors are the same, the return value indicates
the vector's length squared. This is illustrated in the example.
Switching the order of the two vectors results in the
same dot product. (Thus, the dot product is commutative .)
</ul>
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector.
#### Return Value
A number representing the dot product. (Type: number)
#### Examples
The following shows a fast way to compare
a vector's length using the dot product.
// Check if the vector's length squared is less than 20 units squared
if(MathUtil.vec3dot(vector, vector)<20*20) {
// The vector's length is shorter than 20 units
}
### (static) MathUtil.vec3fromWindowPoint(vector, matrix, viewport, [yUp])
Unprojects the window coordinates given in a
3-element vector,
using the given transformation matrix and viewport
rectangle.
In the window coordinate space, x-coordinates increase
rightward and y-coordinates increase upward
or downward depending on the "yUp" parameter, and
z-coordinates within the view volume range from 0 to 1 (assuming
MathUtil.mat4projectVec3 treated view volume z-coordinates as ranging from
-1 to 1) and increase from front to back.
#### Parameters
* `vector` (Type: Array.<number>) A 3-element vector giving the x-, y-, and z-coordinates of the 3D point to transform, in window coordinates.
* `matrix` (Type: Array.<number>) A 4 × 4 matrix. After undoing the transformation to window coordinates, the vector will be transformed by the inverse of this matrix according to the MathUtil.mat4projectVec3 method. To convert to world space, this parameter will generally be a projection-view matrix (projection matrix multiplied by the view matrix, in that order). To convert to object (model) space, this parameter will generally be a model-view-projection matrix (projection-view matrix multiplied by the world [model] matrix, in that order). See MathUtil.vec3toWindowPoint for the meaning of window coordinates with respect to the "matrix" and "yUp" parameters.
* `viewport` (Type: Array.<number>) A 4-element array specifying the starting position and size of the viewport in window units (such as pixels). In order, the four elements are the starting position's x-coordinate, its y-coordinate, the viewport's width, and the viewport's height. Throws an error if the width or height is less than 0.
* `yUp` (Type: boolean) (optional) If false, null, undefined, or omitted, reverses the sign of the y-coordinate returned by the MathUtil.mat4projectVec3 method before converting it to window coordinates. If true, the y-coordinate will remain unchanged. If window y-coordinates increase upward, the viewport's starting position is at the lower-left corner. If those coordinates increase downward, the viewport's starting position is at the upper-left corner.
#### Return Value
A 3-element array giving the coordinates
of the unprojected point, in that order. (Type: Array.<number>)
### (static) MathUtil.vec3length(a)
Returns the distance of this 3-element vector from the origin,
also known as its length or magnitude .
It's the same as the square root of the sum of the squares
of its components.
Note that if vectors are merely sorted or compared by their lengths (and
those lengths are not added or multiplied together or the like),
it's faster to sort or compare them by the squares of their lengths (to find
the square of a 3-element vector's length, call MathUtil.vec3dot
passing the same vector as both of its arguments).
#### Parameters
* `a` (Type: Array.<number>) A 3-element vector.
#### Return Value
Return value. (Type: number)
### (static) MathUtil.vec3lerp(v1, v2, factor)
Does a linear interpolation between two 3-element vectors;
returns a new vector.
#### Parameters
* `v1` (Type: Array.<number>) The first vector to interpolate. The interpolation will occur on each component of this vector and v2.
* `v2` (Type: Array.<number>) The second vector to interpolate.
* `factor` (Type: number) A value that usually ranges from 0 through 1. Closer to 0 means closer to v1, and closer to 1 means closer to v2. For a nonlinear interpolation, define a function that takes a value that usually ranges from 0 through 1 and returns a value generally ranging from 0 through 1, and pass the result of that function to this method. The following are examples of interpolation functions. See also the code examples following this list. Linear: factor
. Constant speed. Powers: Math.pow(factor, N)
, where N > 0. For example, N=2 means a square, N=3 means cube, N=1/2 means square root, and N=1/3 means cube root. If N > 1, this function starts slow and ends fast. If N < 1, this function starts fast and ends slow. Sine: Math.sin(Math.PI\*0.5\*factor)
. This function starts fast and ends slow. Smoothstep: (3.0-2.0\*factor)\*factor\*factor
. This function starts and ends slow, and speeds up in the middle. Perlin's "Smootherstep": (10+factor\*(factor\*6-15))\*factor\*factor\*factor
. This function starts and ends slow, and speeds up in the middle. Discrete-step timing, where N is a number of steps greater than 0: Position start: factor < 0 ? 0 : Math.max(1.0,(1.0+Math.floor(factor\*N))/N)
. Position end: Math.floor(factor\*N)/N
. Inverted interpolation: 1.0-INTF(1.0-factor)
, where INTF(x)
is another interpolation function. This function reverses the speed behavior; for example, a function that started fast now starts slow. Ease: factor < 0.5 ? INTF(factor\*2)\*0.5 : 1.0-(INTF((1.0-factor)\*2)\*0.5)
, where INTF(x)
is another interpolation function. Depending on the underlying function, this function eases in, then eases out, or vice versa. </ul>
#### Return Value
The interpolated vector. (Type: Array.<number>)
#### Examples
The following code does a nonlinear
interpolation of two vectors that uses the cube of "factor" rather than
"factor". Rather than at a constant speed, the vectors are interpolated
slowly then very fast.
factor = factor*factor*factor; // cube the interpolation factor
var newVector = MathUtil.vec3lerp(vector1, vector2, factor);
The following code does an inverted cubic
interpolation. This time, vectors are interpolated fast then very slowly.
factor = 1 - factor; // Invert the factor
factor = factor*factor*factor; // cube the interpolation factor
factor = 1 - factor; // Invert the result
var newVector = MathUtil.vec3lerp(vector1, vector2, factor);
The following code does the nonlinear
interpolation called "smoothstep". It slows down at the beginning
and end, and speeds up in the middle.
factor = (3.0-2.0*factor)*factor*factor; // smoothstep interpolation
var newVector = MathUtil.vec3lerp(vector1, vector2, factor);
### (static) MathUtil.vec3mul(a, b)
Multiplies each of the components of two 3-element vectors and returns a new
vector with the result.
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector.
#### Return Value
The resulting 3-element vector. (Type: Array.<number>)
### (static) MathUtil.vec3mulInPlace(a, b)
Multiplies each of the components of two 3-element vectors and stores
the result in the first vector.
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector.
#### Return Value
The parameter "a" (Type: Array.<number>)
### (static) MathUtil.vec3negate(a)
Negates a 3-element vector and returns a new
vector with the result, which is generally a vector with
the same length but opposite direction. Negating a vector
is the same as reversing the sign of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 3-element vector.
#### Return Value
The resulting 3-element vector. (Type: Array.<number>)
### (static) MathUtil.vec3negateInPlace(a)
Negates a 3-element vector in place, generally resulting in a vector with
the same length but opposite direction.
Negating a vector
is the same as reversing the sign of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 3-element vector.
#### Return Value
The parameter "a". (Type: Array.<number>)
### (static) MathUtil.vec3normalize(vec)
Converts a 3-element vector to a unit vector; returns a new vector.
When a vector is normalized, its direction remains the same but the distance from the origin
to that vector becomes 1 (unless all its components are 0).
A vector is normalized by dividing each of its components
by its length .
#### Parameters
* `vec` (Type: Array.<number>) A 3-element vector.
#### Return Value
The resulting vector.
Note that due to rounding error, the vector's length might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
#### Examples
The following example changes the
length of a line segment.
var startPt=[x1,y1,z1]; // Line segment's start
var endPt=[x2,y2,z2]; // Line segment's end
// Find difference between endPt and startPt
var delta=MathUtil.vec3sub(endPt,startPt);
// Normalize delta to a unit vector
var deltaNorm=MathUtil.vec3normalize(delta);
// Rescale to the desired length, here, 10
MathUtil.vec3scaleInPlace(deltaNorm,10);
// Find the new endpoint
endPt=MathUtil.vec3add(startPt,deltaNorm);
### (static) MathUtil.vec3normalizeInPlace(vec)
Converts a 3-element vector to a unit vector.
When a vector is normalized, its direction remains the same but the distance from the origin
to that vector becomes 1 (unless all its components are 0).
A vector is normalized by dividing each of its components
by its length .
#### Parameters
* `vec` (Type: Array.<number>) A 3-element vector.
#### Return Value
The parameter "vec".
Note that due to rounding error, the vector's length might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.vec3perp(vec)
Returns an arbitrary 3-element vector that is perpendicular
(orthogonal) to the given 3-element vector. The return value
will not be converted to a unit vector.
#### Parameters
* `vec` (Type: Array.<number>) A 3-element vector.
#### Return Value
A perpendicular 3-element
vector. Returns (0,0,0) if "vec" is (0,0,0). (Type: Array.<number>)
### (static) MathUtil.vec3proj(vec, refVec)
Returns the projection of a 3-element vector on the given
reference vector. Assuming both vectors
start at the same point, the resulting vector
will be parallel to the
reference vector but will make the closest
approach possible to the projected vector's
endpoint. The difference between the projected
vector and the return value will be perpendicular
to the reference vector.
#### Parameters
* `vec` (Type: Array.<number>) The vector to project.
* `refVec` (Type: Array.<number>) The reference vector whose length will be adjusted.
#### Return Value
The projection of
"vec" on "refVec". Returns (0,0,0) if "refVec"'s
length is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.vec3reflect(incident, normal)
Returns a vector that reflects off a surface.
#### Parameters
* `incident` (Type: Array.<number>) Incident vector, or a vector headed in the direction of the surface, as a 3-element vector.
* `normal` (Type: Array.<number>) Surface normal vector, or a vector that's perpendicular to the surface, as a 3-element vector. Should be a unit vector.
#### Return Value
A vector that has the same length
as "incident" but is reflected away from the surface. (Type: Array.<number>)
### (static) MathUtil.vec3scale(a, scalar)
Multiplies each element of a 3-element vector by a factor. Returns
a new vector that is parallel to the old vector
but with its length multiplied by the given factor. If the factor
is positive, the vector will point in the same direction; if negative,
in the opposite direction; if zero, the vector's components will all be 0.
#### Parameters
* `a` (Type: Array.<number>) A 3-element vector.
* `scalar` (Type: number) A factor to multiply. To divide a vector by a number, the factor will be 1 divided by that number.
#### Return Value
The parameter "a". (Type: Array.<number>)
### (static) MathUtil.vec3scaleInPlace(a, scalar)
Multiplies each element of a 3-element vector by a factor, so
that the vector is parallel to the old vector
but its length is multiplied by the given factor. If the factor
is positive, the vector will point in the same direction; if negative,
in the opposite direction; if zero, the vector's components will all be 0.
#### Parameters
* `a` (Type: Array.<number>) A 3-element vector.
* `scalar` (Type: number) A factor to multiply. To divide a vector by a number, the factor will be 1 divided by that number.
#### Return Value
The parameter "a". (Type: Array.<number>)
### (static) MathUtil.vec3sub(a, b)
Subtracts the second vector from the first vector and returns a new
vector with the result. Subtracting two vectors
is the same as subtracting each of their components.
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector.
#### Return Value
The resulting 3-element vector.
This is the vector to a
from b
. (Type: Array.<number>)
### (static) MathUtil.vec3subInPlace(a, b)
Subtracts the second vector from the first vector and stores
the result in the first vector. Subtracting two vectors
is the same as subtracting each of their components.
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector.
#### Return Value
The parameter "a".
This is the vector to the previous a
from b
. (Type: Array.<number>)
### (static) MathUtil.vec3toWindowPoint(vector, matrix, viewport, [yUp])
Transforms the 3D point specified in this 3-element vector to its
window coordinates
using the given transformation matrix and viewport rectangle.
#### Parameters
* `vector` (Type: Array.<number>) A 3-element vector giving the x-, y-, and z-coordinates of the 3D point to transform.
* `matrix` (Type: Array.<number>) A 4 × 4 matrix to use to transform the vector according to the MathUtil.mat4projectVec3 method, before the transformed vector is converted to window coordinates. This parameter will generally be a projection-view matrix (projection matrix multiplied by the view matrix, in that order), if the vector to transform is in world space , or a model-view-projection matrix, that is, (projection-view matrix multiplied by the model [world] matrix, in that order), if the vector is in model (object) space . If the matrix includes a projection transform returned by MathUtil.mat4ortho , MathUtil.mat4perspective , or similar MathUtil methods, the x-, y-, and z-coordinates within the view volume will increase in the direction given in those methods' documentation (except that if "yUp" is false, null, undefined, or omitted, y-coordinates increase in the opposite direction), and z-coordinates in the view volume will range from 0 to 1 in window coordinates , assuming MathUtil.mat4projectVec3 made such z-coordinates range from -1 to 1.
* `viewport` (Type: Array.<number>) A 4-element array specifying the starting position and size of the viewport in window units (such as pixels). In order, the four elements are the starting position's x-coordinate, its y-coordinate, the viewport's width, and the viewport's height. Throws an error if the width or height is less than 0.
* `yUp` (Type: boolean) (optional) If false, null, undefined, or omitted, reverses the sign of the y-coordinate returned by the MathUtil.mat4projectVec3 method before converting it to window coordinates. If true, the y-coordinate will remain unchanged. If window y-coordinates increase upward, the viewport's starting position is at the lower-left corner. If those coordinates increase downward, the viewport's starting position is at the upper-left corner.
#### Return Value
A 3-element array giving the window
coordinates, in that order. (Type: Array.<number>)
### (static) MathUtil.vec3triple(a, b, c)
Finds the scalar triple product of three vectors (A, B, and C). The triple
product is the dot product of both A and the
cross product
of B and C. The following are properties of the scalar triple product
(called triple product in what follows):
Switching the order of B and C, A and C, or A and B results in a triple product
with its sign reversed. Moving all three parameters to different positions, though,
results in the same triple product.
The triple product's absolute value is the volume of a parallelepiped (skewed
box) where three of its sides having a vertex in common are
defined by A, B, and C, in any order.
The triple product's absolute value divided by 6 is the volume of a tetrahedron,
where three of its sides having a vertex in common are
defined by A, B, and C, in any order.
If the triple product is 0, all three vectors lie on the same plane (are coplanar ).
The triple product is the same as the determinant (overall scaling factor)
of a 3 × 3 matrix whose rows or columns are the vectors A, B, and C, in that order.
Assume A is perpendicular to vectors B and C. If the triple product is positive,
then A points in the same direction as the cross product of
B and C -- which will be perpendicular -- and the angle from B to C, when rotated
about vector A, is positive. If the triple product is negative, then A points in the
opposite direction from that cross product, and that angle is negative.
(See the following example.)
</ul>
#### Parameters
* `a` (Type: Array.<number>) The first 3-element vector.
* `b` (Type: Array.<number>) The second 3-element vector, or the first parameter to the cross product.
* `c` (Type: Array.<number>) The third 3-element vector, or the second parameter to the cross product.
#### Return Value
A number giving the triple product. (Type: number)
### (static) MathUtil.vec4abs(a)
Returns a new 4-element
vector with the absolute value of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 4-element vector.
#### Return Value
The resulting 4-element vector. (Type: Array.<number>)
### (static) MathUtil.vec4absInPlace(a)
Sets each component of the given 4-element
vector to its absolute value.
#### Parameters
* `a` (Type: Array.<number>) A 4-element vector.
#### Return Value
The vector "a". (Type: Array.<number>)
### (static) MathUtil.vec4add(a, b)
Adds two 4-element vectors and returns a new
vector with the result. Adding two vectors
is the same as adding each of their components.
The resulting vector:
describes a straight-line path for the
combined paths described by the given vectors, in either order, and
will come "between" the two vectors given (at their shortest angle) if all three start
at the same position.</ul>
#### Parameters
* `a` (Type: Array.<number>) The first 4-element vector.
* `b` (Type: Array.<number>) The second 4-element vector.
#### Return Value
The resulting 4-element vector. (Type: Array.<number>)
### (static) MathUtil.vec4addInPlace(a, b)
Adds two 4-element vectors and stores
the result in the first vector. Adding two vectors
is the same as adding each of their components.
The resulting vector:
describes a straight-line path for the
combined paths described by the given vectors, in either order, and
will come "between" the two vectors given (at their shortest angle) if all three start
at the same position.</ul>
#### Parameters
* `a` (Type: Array.<number>) The first 4-element vector.
* `b` (Type: Array.<number>) The second 4-element vector.
#### Return Value
The parameter "a".
This is the vector to the previous a
from b
. (Type: Array.<number>)
### (static) MathUtil.vec4assign(dst, src)
Assigns the values of a 4-element vector into another
4-element vector.
#### Parameters
* `dst` (Type: Array.<number>) The 4-element vector to copy the source values to.
* `src` (Type: Array.<number>) The 4-element vector whose values will be copied.
#### Return Value
The parameter "dst". (Type: Array.<number>)
### (static) MathUtil.vec4clamp(a, min, max)
Returns a 4-element vector in which each element of the given 4-element vector is clamped
#### Parameters
* `a` (Type: Array.<number>) The vector to clamp.
* `min` (Type: number) Lowest possible value. Should not be greater than "max".
* `max` (Type: number) Highest possible value. Should not be less than "min".
#### Return Value
The resulting vector. (Type: Array.<number>)
### (static) MathUtil.vec4clampInPlace(a, min, max)
Clamps each element of the given 4-element vector
so it's not less than one value or greater than another value.
#### Parameters
* `a` (Type: Array.<number>) The vector to clamp.
* `min` (Type: number) Lowest possible value. Should not be greater than "max".
* `max` (Type: number) Highest possible value. Should not be less than "min".
#### Return Value
The resulting vector. (Type: Array.<number>)
### (static) MathUtil.vec4copy(vec)
Returns a copy of a 4-element vector.
#### Parameters
* `vec` (Type: Array.<number>) A 4-element vector.
#### Return Value
Return value. (Type: Array.<number>)
### (static) MathUtil.vec4dot(a, b)
Finds the dot product of two 4-element vectors. It's the
sum of the products of their components (for example, a 's X times b 's X).
For properties of the dot product, see MathUtil.vec3dot .
#### Parameters
* `a` (Type: Array.<number>) The first 4-element vector.
* `b` (Type: Array.<number>) The second 4-element vector.
#### Return Value
Return value. (Type: number)
### (static) MathUtil.vec4length(a)
Returns the distance of this 4-element vector from the origin,
also known as its length or magnitude .
It's the same as the square root of the sum of the squares
of its components.
Note that if vectors are merely sorted or compared by their lengths,
it's faster to sort or compare them by the squares of their lengths (to find
the square of a 4-element vector's length, call MathUtil.vec4dot
passing the same vector as both of its arguments).
#### Parameters
* `a` (Type: Array.<number>) A 4-element vector.
#### Return Value
Return value. (Type: number)
### (static) MathUtil.vec4lerp(v1, v2, factor)
Does a linear interpolation between two 4-element vectors;
returns a new vector.
#### Parameters
* `v1` (Type: Array.<number>) The first vector to interpolate. The interpolation will occur on each component of this vector and v2.
* `v2` (Type: Array.<number>) The second vector to interpolate.
* `factor` (Type: number) A value that usually ranges from 0 through 1. Closer to 0 means closer to v1, and closer to 1 means closer to v2. For a nonlinear interpolation, define a function that takes a value that usually ranges from 0 through 1 and generally returns A value that usually ranges from 0 through 1, and pass the result of that function to this method. See the documentation for MathUtil.vec3lerp for examples of interpolation functions.
#### Return Value
The interpolated vector. (Type: Array.<number>)
### (static) MathUtil.vec4negate(a)
Negates a 4-element vector and returns a new
vector with the result, which is generally a vector with
the same length but opposite direction. Negating a vector
is the same as reversing the sign of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 4-element vector.
#### Return Value
The resulting 4-element vector. (Type: Array.<number>)
### (static) MathUtil.vec4negateInPlace(a)
Negates a 4-element vector in place, generally resulting in a vector with
the same length but opposite direction.
Negating a vector
is the same as reversing the sign of each of its components.
#### Parameters
* `a` (Type: Array.<number>) A 4-element vector.
#### Return Value
The parameter "a". (Type: Array.<number>)
### (static) MathUtil.vec4normalize(vec)
Converts a 4-element vector to a unit vector; returns a new vector.
When a vector is normalized, its direction remains the same but the distance from the origin
to that vector becomes 1 (unless all its components are 0).
A vector is normalized by dividing each of its components
by its length .
#### Parameters
* `vec` (Type: Array.<number>) A 4-element vector.
#### Return Value
The resulting vector.
Note that due to rounding error, the vector's length might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.vec4normalizeInPlace(vec)
Converts a 4-element vector to a unit vector.
When a vector is normalized, its direction remains the same but the distance from the origin
to that vector becomes 1 (unless all its components are 0).
A vector is normalized by dividing each of its components
by its length .
#### Parameters
* `vec` (Type: Array.<number>) A 4-element vector.
#### Return Value
The parameter "vec".
Note that due to rounding error, the vector's length might not be exactly equal to 1, and that the vector will remain unchanged if its length is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.vec4proj(vec, refVec)
Returns the projection of a 4-element vector on the given
reference vector. Assuming both vectors
start at the same point, the resulting vector
will be parallel to the
reference vector but will make the closest
approach possible to the projected vector's
endpoint. The difference between the projected
vector and the return value will be perpendicular
to the reference vector.
#### Parameters
* `vec` (Type: Array.<number>) The vector to project.
* `refVec` (Type: Array.<number>) The reference vector whose length will be adjusted.
#### Return Value
The projection of
"vec" on "refVec". Returns (0,0,0,0) if "refVec"'s
length is 0 or extremely close to 0. (Type: Array.<number>)
### (static) MathUtil.vec4scale(a, scalar)
Multiplies each element of a 4-element vector by a factor, returning
a new vector that is parallel to the old vector
but with its length multiplied by the given factor. If the factor
is positive, the vector will point in the same direction; if negative,
in the opposite direction; if zero, the vector's components will all be 0.
#### Parameters
* `a` (Type: Array.<number>) A 4-element vector.
* `scalar` (Type: number) A factor to multiply. To divide a vector by a number, the factor will be 1 divided by that number.
#### Return Value
The resulting 4-element vector. (Type: Array.<number>)
### (static) MathUtil.vec4scaleInPlace(a, scalar)
Multiplies each element of a 4-element vector by a factor, so
that the vector is parallel to the old vector
but its length is multiplied by the given factor. If the factor
is positive, the vector will point in the same direction; if negative,
in the opposite direction; if zero, the vector's components will all be 0.
#### Parameters
* `a` (Type: Array.<number>) A 4-element vector.
* `scalar` (Type: number) A factor to multiply. To divide a vector by a number, the factor will be 1 divided by that number.
#### Return Value
The parameter "a". (Type: Array.<number>)
### (static) MathUtil.vec4sub(a, b)
Subtracts the second vector from the first vector and returns a new
vector with the result. Subtracting two vectors
is the same as subtracting each of their components.
#### Parameters
* `a` (Type: Array.<number>) The first 4-element vector.
* `b` (Type: Array.<number>) The second 4-element vector.
#### Return Value
The resulting 4-element vector.
This is the vector to a
from b
. (Type: Array.<number>)
### (static) MathUtil.vec4subInPlace(a, b)
Subtracts the second vector from the first vector and stores
the result in the first vector. Subtracting two vectors
is the same as subtracting each of their components.
#### Parameters
* `a` (Type: Array.<number>) The first 4-element vector.
* `b` (Type: Array.<number>) The second 4-element vector.
#### Return Value
The parameter "a" (Type: Array.<number>)
[Back to documentation index.](index.html)