Array-based vector, quaternion and matrix math for PEX
// require any of the modules
var mat4 = require('pex-math/mat4')
// all of them are namespaces of functions
var identityMatrix = mat4.create()
// => [1, 0, 0, 0,
// 0, 1, 0, 0,
// 0, 0, 1, 0,
// 0, 0, 0, 1]
var vec2 = require('pex-math/vec2')
vec2.create()
Returns a new vec2 at 0, 0, 0.
vec2.set(v, w)
Sets a vector to another vector.
v
: vec2 - the vector being setw
: vec2 - the vector used as templateReturns v
.
vec2.copy(v)
Returns a copy of a vector.
v
: vec2Returns a new instance of v
.
vec2.equals(v, w)
Compares two vectors.
v
: vec2w
: vec2Returns a bool
.
vec2.add(v, w)
Add a vector with another.
v
: vec2w
: vec2Returns v
after addition.
vec2.sub(v, w)
Subtracts a vector with another.
v
: vec2w
: vec2Returns v
after subtraction.
vec2.scale(v, s)
Scales a vector by a number.
v
: vec2s
: NumberReturns v
scaled.
vec2.dot(v, w)
Calculates the dot product of two vectors.
v
: vec2w
: vec2Returns v
after calculation.
vec2.length(v)
Calculates the length of a vector.
v
: vec2Returns the length of v
.
vec2.lengthSq(v)
Calculates the squared length of a vector.
v
: vec2Returns the squared length of v
.
vec2.normalize(v)
Normalises a vector.
v
: vec2Returns v
normalised.
vec2.distance(v, w)
Calculates the distance between two vectors.
v
: vec2w
: vec2Returns the distance between v
and w
.
vec2.distanceSq(v, w)
Calculates the squared distance between two vectors.
v
: vec2w
: vec2Returns the squared distance between v
and w
.
vec2.limit(v, s)
Limits a vector to a length.
v
: vec2s
: NumberReturns v
scaled.
vec2.lerp(v, w, t)
Linearly interpolates between two vectors.
v
: vec2w
: vec2t
: Number - lerp valueReturns v
lerped.
vec2.toString(v, precision)
Prints a vector to a string.
v
: vec2precision
: numberReturns a String
.
var vec3 = require('pex-math/vec3')
vec3.create()
Returns a new vec3 at 0, 0, 0.
vec3.set(v, w)
Sets a vector to another vector.
v
: vec3 - the vector being setw
: vec3 - the vector used as templateReturns v
.
vec3.copy(v)
Returns a copy of a vector.
v
: vec3Returns a new instance of v
.
vec3.equals(v, w)
Compares two vectors.
v
: vec3w
: vec3Returns a bool
.
vec3.add(v, w)
Add a vector with another.
v
: vec3w
: vec3Returns v
after addition.
vec3.addScaled(v, w, s)
Add a vector with another scaled vector.
v
: vec3w
: vec3s
: NumberReturns v
after addition.
vec3.sub(v, w)
Subtracts a vector with another.
v
: vec3w
: vec3Returns v
after subtraction.
vec3.scale(v, s)
Scales a vector by a number.
v
: vec3s
: NumberReturns v
scaled.
vec3.multMat4(v, m)
Multiplies a vector by a matrix.
v
: vec3m
: mat4Returns v
after multiplication.
vec3.multQuat(v, q)
Multiplies a vector by a quaternion.
v
: vec3q
: quatReturns v
after multiplication.
vec3.dot(v, w)
Calculates the dot product of two vectors.
v
: vec3w
: vec3Returns v
after calculation.
vec3.cross(v, w)
Calculates the cross product of two vectors.
v
: vec3w
: vec3Returns v
after calculation.
vec3.length(v)
Calculates the length of a vector.
v
: vec3Returns the length of v
.
vec3.lengthSq(v)
Calculates the squared length of a vector.
v
: vec3Returns the squared length of v
.
vec3.normalize(v)
Normalises a vector.
v
: vec3Returns v
normalised.
vec3.distance(v, w)
Calculates the distance between two vectors.
v
: vec3w
: vec3Returns the distance between v
and w
.
vec3.distanceSq(v, w)
Calculates the squared distance between two vectors.
v
: vec3w
: vec3Returns the squared distance between v
and w
.
vec3.limit(v, s)
Limits a vector to a length.
v
: vec3s
: NumberReturns v
scaled.
vec3.lerp(v, w, t)
Linearly interpolates between two vectors.
v
: vec3w
: vec3t
: Number - lerp valueReturns v
lerped.
vec3.toString(v, p)
Prints a vector to a string.
v
: vec3p
: Number - precisionReturns a String
.
var vec4 = require('pex-math/vec4')
vec4.create()
Returns a new vec4 at 0, 0, 0.
vec4.equals(v, w)
Compares two vectors.
v
: vec4w
: vec4Returns a bool
.
vec4.set(v, w)
Sets a vector to another vector.
v
: vec4 - the vector being setw
: vec4 - the vector used as templateReturns v
.
vec4.fromVec3(v, v3)
Create a vec4 from vec3.
v
: vec4v3
: vec3Returns v4
.
vec4.multMat4(v, m)
Multiplies a vector with a matrix.
v
: vec4m
: mat4Returns v
.
vec4.copy(v)
Returns a copy of a vector.
v
: vec4Returns a new instance of v
.
vec4.scale(v, s)
Scales a vector by a number.
v
: vec4s
: NumberReturns v
scaled.
var mat2x3 = require('pex-math/mat2x3')
mat2x3.create()
Returns a 2x3 identity matrix, a short form for a 3x3 matrix with the last row ignored.
Row major memory layout:
0 1 2
3 4 5
Equivalent to the column major OpenGL spec:
0 3
1 4
2 5
m00 m10
m01 m11
m02 m12
Returns a new mat2x3
.
mat2x3.set(m, n)
Sets a matrix from another matrix.
m
: mat2x3 - the matrix being setn
: mat2x3 - the matrix used as templateReturns m
.
mat2x3.identity(m)
Sets a matrix to the identity matrix.
m
: mat2x3Returns m
as its identity.
mat2x3.mult(m, n)
Multiplies two matrices.
m
: mat2x3n
: mat2x3Returns m
multiplied by n
.
mat2x3.translate(m, v)
Translates a matrix by a vector.
m
: mat2x3v
: vec2Returns m
translated.
mat2x3.rotate(m, r)
Rotates a matrix by an angle.
m
: mat2x3r
: Number - the angle of rotationReturns m
rotated.
mat2x3.scale(m, v)
Scales a matrix by a vector.
m
: mat2x3v
: vec2Returns m
scaled.
var mat3 = require('pex-math/mat3')
mat3.create()
Returns a 3x3 identity matrix.
Row major memory layout:
0 1 2
3 4 5
6 7 8
Equivalent to the column major OpenGL spec:
0 3 6
1 4 7
2 5 8
m00 m10 m20
m01 m11 m21
m02 m12 m22
Returns a new mat3
.
mat3.set(m, n)
Sets a matrix from another matrix.
m
: mat3 - the matrix being setn
: mat3 - the matrix used as templateReturns m
.
mat3.identity(m)
Sets a matrix to the identity matrix.
m
: mat3Returns m
as its identity.
mat3.mult(m, n)
Multiplies two matrices.
m
: mat3n
: mat3Returns m
multiplied by n
.
mat3.equals(m, n)
Compares two matrices.
m
: mat3n
: mat3Returns a bool
.
mat3.fromMat2x3(m, m2x3)
Sets a 3x3 matrix from a 2x3 matrix.
m
: mat3m2x3
: mat2x3Returns m3
.
mat3.fromMat4(m, m4)
Sets a 3x3 matrix to a 4x4 matrix.
m
: mat3m4
: mat4Returns m3
.
mat3.fromQuat(m, q)
Sets matrix to a quaternion.
m
: mat3q
: quatReturns m
.
var mat4 = require('pex-math/mat4')
mat4.create()
Returns a 4x4 identity matrix.
Row major memory layout:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Equivalent to the column major OpenGL spec:
0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15
m00 m10 m20 m30
m01 m11 m21 m31
m02 m12 m22 m32
m03 m13 m23 m33
Returns a new mat4
.
mat4.set(m, n)
Sets a matrix from another matrix.
m
: mat4 - the matrix being setn
: mat4 - the matrix used as templateReturns the newly set a
.
mat4.equals(m, n)
Compares two matrices.
m
: mat4n
: mat4Returns a bool
.
mat4.copy(m)
Returns a copy of a matrix.
m
: mat4Returns a new instance of m
.
mat4.mult(m, n)
Multiplies two matrices.
m
: mat4n
: mat4Returns m
multiplied by n
.
mat4.invert(m)
Inverts a matrix.
m
: mat4Returns m
inverted.
mat4.tranpose(m)
Transposes a matrix.
m
: mat4Returns m
transposed.
mat4.identity(m)
Sets a matrix to the identity matrix.
m
: mat4Returns m
as its identity.
mat4.scale(m, v)
Scales a matrix by a vector.
m
: mat4v
: vec3Returns m
scaled.
mat4.translate(m, v)
Translates a matrix by a vector.
m
: mat4v
: vec3Returns m
translated.
mat4.rotate(m, r, v)
Rotates a matrix by an angle at an axis.
m
: mat4r
: Number - the angle of rotationv
: vec3 - the axis of rotationReturns m
rotated.
mat4.frustum(m, left, right, bottom, top, near, far)
Create a frustum matrix.
m
: mat4left
: Number - left boundright
: Number - right boundbottom
: Number - bottom boundtop
: Number - top boundnear
: Number - near clipping planefar
: Number - far clipping planeReturns m
.
mat4.perspective(m, fovy, aspectRatio, near, far)
Create a perspective matrix.
m
: mat4 - out matrixfovy
: Number - field of view in radiansaspectRatio
: Number - aspect rationear
: Number - near clipping planefar
: Number - far clipping planeReturns m
.
mat4.ortho(m, left, right, bottom, top, near, far)
Create a orthographic matrix.
m
: mat4left
: Number - left boundright
: Number - right boundbottom
: Number - bottom boundtop
: Number - top boundnear
: Number - near clipping planefar
: Number - far clipping planeReturns m
.
mat4.lookAt(m, from, to, up)
Calculates a lookAt matrix from a position, target and up vectors.
m
: mat4from
: vec3 - position vectorto
: vec3 - target vectorup
: vec3 - up vectorReturns m
.
mat4.fromQuat(m, q)
Sets matrix to a quaternion.
m
: mat4q
: quatReturns m
.
mat4.fromMat3(m, m3)
Sets a 4x4 matrix to a 3x3 matrix.
m
: mat4m3
: mat3Returns m
.
mat4.fromTranslationRotationScale(m, t, r, s)
Sets matrix to the TRS matrix.
m
: mat4t
: vec3r
: quats
: vec3Returns m
.
var quat = require('pex-math/quat')
quat.create()
Create a new quaternion.
quat.equals(q, p)
Compares two quaternions.
q
: quatp
: quatReturns bool
.
quat.identity(q)
Sets a quaternion to the identity quaternion.
q
: quatReturns q
.
quat.copy(q)
Returns a copy of a quaternion.
q
: quatReturns a new instance of q
.
quat.set(q, p)
Sets one quaternion to another.
q
: quatp
: quatReturns q
.
quat.mult(q, p)
Multiplies one quaternion by another.
q
: quatp
: quatReturns q
after multiplication.
quat.invert(q)
Inverts a quaternion.
q
: quatReturns q
inverted.
quat.conjugate(q)
Conjugates a quaternion.
q
: quatReturns q
conjugates.
quat.length(q)
Calculates the length of a quaternion.
q
: quatReturns the length of q
.
quat.normalize(q)
Normalizes a quaternion.
q
: quatReturns q
normalized.
quat.dot(q, p)
Calculates the dot product of two quaternions.
q
: quatp
: quatReturns the dot product of q
and p
.
quat.fromEuler(q, euler)
Set euler angles to a quaternion. Assumes XYZ rotation order.
q
: quateuler
: vec3/euler [x/yaw, y/pitch, z/roll]Returns q
.
quat.fromAxisAngle(q, axis, angle)
Set the angle at an axis of a quaternion.
q
: quataxis
: vec3angle
: NumberReturns q
.
quat.fromAxes(q, xAxis, yAxis, zAxis)
Sets quaternion from orthonormal base xyz.
q
: quatxAxis
: vec3zAxis
: vec3yAxis
: vec3Returns q
.
quat.fromMat3(q, m)
Sets a quaternion to a 3x3 matrix.
q
: quatm
: mat3Returns q
.
quat.fromMat4(q, m)
Sets a quaternion to a 4x4 matrix.
q
: quatm
: mat4Returns q
.
quat.slerp(q, p, t)
Spherical linear interpolation between two quaternions.
q
: quatp
: quatt
: NumberReturns q
.
quat.fromTo(q, from, to, up)
TODO
Returns q
.
vec3 array of [x, y, z] rotation [yaw, pitch, roll] in radians.
var euler = require('pex-math/euler')
euler.create()
Create a new euler angles [0, 0, 0].
euler.fromQuat(e, q)
Creates euler angles from quaterion. Assumes XYZ order of rotations.
e
: euler/vec3q
: quatReturns e
.
utils.lerp(a, b, t)
Linear interpolation between two numbers.
a
: Numberb
: Numbert
: NumberReturns a Number
.
utils.clamp(a, min, max)
Clamps a number between two numbers.
a
: Numbermin
: Numbermax
: NumberReturns a Number
.
utils.smoothstep(a, min, max)
a
: Numbermin
: Numbermax
: NumberReturns a Number
.
utils.map(a, inStart, inEnd, outStart, outEnd)
Maps a number from one range to another.
a
: NumberinStart
: NumberinEnd
: NumberoutStart
: NumberoutEnd
: NumberReturns a Number
.
toRadians(degrees)
Transforms degrees into radians.
degrees
- NumberReturns a Number
.
toDegrees(radians)
Transforms radians into degrees.
radians
- NumberReturns a Number
.
fraction(a)
Returns the fractional part of a number.
a
- NumberReturns a Number
.
sign(a)
Returns the sign of a number.
a
- NumberReturns a Number
.
sign(a)
Returns the sign of a number.
a
- NumberReturns a Number
.
isPowerOfTwo(a)
Returns the sign of a number.
a
- NumberReturns a Number
.
nextPowerOfTwo(a)
Returns the sign of a number.
a
- NumberReturns a Number
.