4.1.0 • Published 2 months ago

pex-math v4.1.0

Weekly downloads
73
License
MIT
Repository
github
Last release
2 months ago

pex-math

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

Array-based vector, quaternion and matrix math with utils for PEX.

Installation

npm install pex-math

Usage

import { mat4 } from "pex-math";

const identityMatrix = mat4.create();
// => [1, 0, 0, 0,
//     0, 1, 0, 0,
//     0, 0, 1, 0,
//     0, 0, 0, 1]

API

Modules

Typedefs

pex-math

pex-math.mat2x3 : mat2x3

Kind: static property of pex-math

pex-math.mat3 : mat3

Kind: static property of pex-math

pex-math.mat4 : mat4

Kind: static property of pex-math

pex-math.vec2 : vec2

Kind: static property of pex-math

pex-math.vec3 : vec3

Kind: static property of pex-math

pex-math.vec4 : vec4

Kind: static property of pex-math

pex-math.avec2 : avec2

Kind: static property of pex-math

pex-math.avec3 : avec3

Kind: static property of pex-math

pex-math.avec4 : avec4

Kind: static property of pex-math

pex-math.quat : quat

Kind: static property of pex-math

pex-math.euler : euler

Kind: static property of pex-math

pex-math.utils : utils

Kind: static property of pex-math

avec2

avec2.set2(a, i, x, y)

Sets a vector components.

Kind: static method of avec2

ParamType
aavec2
inumber
xnumber
ynumber

avec2.set(a, i, b, j)

Sets a vector to another vector.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber

avec2.equals(a, i, b, j) ⇒ boolean

Compares two vectors.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber

avec2.add(a, i, b, j)

Adds a vector to another.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber

avec2.sub(a, i, b, j)

Subtracts a vector from another.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber

avec2.scale(a, i, s)

Scales a vector by a number.

Kind: static method of avec2

ParamType
aavec2
inumber
snumber

avec2.addScaled(a, i, b, j, s)

Adds two vectors after scaling the second one.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber
snumber

avec2.dot(a, i, b, j) ⇒ number

Calculates the dot product of two vectors.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber

avec2.length(a, i) ⇒ number

Calculates the length of a vector.

Kind: static method of avec2

ParamType
aavec2
inumber

avec2.lengthSq(a, i) ⇒ number

Calculates the squared length of a vector.

Kind: static method of avec2

ParamType
aavec2
inumber

avec2.normalize(a, i)

Normalises a vector.

Kind: static method of avec2

ParamType
aavec2
inumber

avec2.distance(a, i, b, j) ⇒ number

Calculates the distance between two vectors.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber

avec2.distanceSq(a, i, b, j) ⇒ number

Calculates the squared distance between two vectors.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber

avec2.limit(a, i, len)

Limits a vector to a length.

Kind: static method of avec2

ParamType
aavec2
inumber
lennumber

avec2.lerp(a, i, b, j, t)

Linearly interpolates between two vectors.

Kind: static method of avec2

ParamType
aavec2
inumber
bavec2
jnumber
tnumber

avec2.forEach(a, callbackFn)

Executes a function once for each array element.

Kind: static method of avec2

ParamType
aavec2
callbackFniterativeCallback

avec2.map(a, callbackFn) ⇒ avec2

Creates a new array populated with the results of calling a provided function on every element in the calling array.

Kind: static method of avec2

ParamType
aavec2
callbackFniterativeCallback

avec2.toString(a, i, precision) ⇒ string

Prints a vector to a string.

Kind: static method of avec2

ParamTypeDefault
aavec2
inumber
precisionnumber4

avec3

avec3.set3(a, i, x, y, z)

Sets a vector components.

Kind: static method of avec3

ParamType
aavec3
inumber
xnumber
ynumber
znumber

avec3.set(a, i, b, j)

Sets a vector to another vector.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber

avec3.equals(a, i, b, j) ⇒ boolean

Compares two vectors.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber

avec3.add(a, i, b, j)

Adds a vector to another.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber

avec3.sub(a, i, b, j)

Subtracts a vector from another.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber

avec3.scale(a, i, s)

Scales a vector by a number.

Kind: static method of avec3

ParamType
aavec3
inumber
snumber

avec3.addScaled(a, i, b, j, s)

Adds two vectors after scaling the second one.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber
snumber

avec3.multMat4(a, i, m, j)

Multiplies a vector by a matrix.

Kind: static method of avec3

ParamType
aavec3
inumber
mamat4
jnumber

avec3.multQuat(a, i, q, j)

Multiplies a vector by a quaternion.

Kind: static method of avec3

ParamType
aavec3
inumber
qaquat
jnumber

avec3.dot(a, i, b, j) ⇒ number

Calculates the dot product of two vectors.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber

avec3.cross(a, i, b, j)

Calculates the cross product of two vectors.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber

avec3.length(a, i) ⇒ number

Calculates the length of a vector.

Kind: static method of avec3

ParamType
aavec3
inumber

avec3.lengthSq(a, i) ⇒ number

Calculates the squared length of a vector.

Kind: static method of avec3

ParamType
aavec3
inumber

avec3.normalize(a, i)

Normalises a vector.

Kind: static method of avec3

ParamType
aavec3
inumber

avec3.distance(a, i, b, j) ⇒ number

Calculates the distance between two vectors.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber

avec3.distanceSq(a, i, b, j) ⇒ number

Calculates the squared distance between two vectors.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber

avec3.limit(a, i, len)

Limits a vector to a length.

Kind: static method of avec3

ParamType
aavec3
inumber
lennumber

avec3.lerp(a, i, b, j, t)

Linearly interpolates between two vectors.

Kind: static method of avec3

ParamType
aavec3
inumber
bavec3
jnumber
tnumber

avec3.forEach(a, callbackFn)

Executes a function once for each array element.

Kind: static method of avec3

ParamType
aavec3
callbackFniterativeCallback

avec3.map(a, callbackFn) ⇒ avec3

Creates a new array populated with the results of calling a provided function on every element in the calling array.

Kind: static method of avec3

ParamType
aavec3
callbackFniterativeCallback

avec3.toString(a, i, precision) ⇒ string

Prints a vector to a string.

Kind: static method of avec3

ParamTypeDefault
aavec3
inumber
precisionnumber4

avec4

avec4.set4(a, i, x, y, z, w)

Sets a vector components.

Kind: static method of avec4

ParamType
aavec4
inumber
xnumber
ynumber
znumber
wnumber

avec4.set(a, i, b, j)

Sets a vector to another vector.

Kind: static method of avec4

ParamType
aavec4
inumber
bavec4
jnumber

avec4.equals(a, i, b, j) ⇒ boolean

Compares two vectors.

Kind: static method of avec4

ParamType
aavec4
inumber
bavec4
jnumber

avec4.add(a, i, b, j)

Adds a vector to another.

Kind: static method of avec4

ParamType
aavec4
inumber
bavec4
jnumber

avec4.sub(a, i, b, j)

Subtracts a vector from another.

Kind: static method of avec4

ParamType
aavec4
inumber
bavec4
jnumber

avec4.scale(a, i, s)

Scales a vector by a number.

Kind: static method of avec4

ParamType
aavec4
inumber
snumber

avec4.addScaled(a, i, b, j, s)

Adds two vectors after scaling the second one.

Kind: static method of avec4

ParamType
aavec4
inumber
bavec4
jnumber
snumber

avec4.multMat4(a, i, m, j)

Multiplies a vector with a matrix.

Kind: static method of avec4

ParamType
aavec4
inumber
mamat4
jnumber

avec4.lerp(a, i, b, j, t)

Linearly interpolates between two vectors.

Kind: static method of avec4

ParamType
aavec4
inumber
bavec4
jnumber
tnumber

avec4.forEach(a, callbackFn)

Executes a function once for each array element.

Kind: static method of avec4

ParamType
aavec4
callbackFniterativeCallback

avec4.map(a, callbackFn) ⇒ avec4

Creates a new array populated with the results of calling a provided function on every element in the calling array.

Kind: static method of avec4

ParamType
aavec4
callbackFniterativeCallback

avec4.toString(a, i, precision) ⇒ string

Prints a vector to a string.

Kind: static method of avec4

ParamTypeDefault
aavec4
inumber
precisionnumber4

euler

euler.create() ⇒ euler

Create a new euler angles 0, 0, 0: vec3 array of x, y, z rotation yaw, pitch, roll in radians.

Kind: static method of euler

euler.fromQuat(a, q) ⇒ euler

Creates euler angles from quaternion. Assumes XYZ order of rotations.

Kind: static method of euler

ParamType
aeuler
qquat

mat2x3

mat2x3.create() ⇒ mat2x3

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

Kind: static method of mat2x3

mat2x3.identity(a) ⇒ mat2x3

Sets a matrix to the identity matrix.

Kind: static method of mat2x3

ParamType
amat2x3

mat2x3.copy(a) ⇒ mat2x3

Returns a copy of a matrix.

Kind: static method of mat2x3

ParamType
amat2x3

mat2x3.set(a, b) ⇒ mat2x3

Sets a matrix from another matrix.

Kind: static method of mat2x3

ParamType
amat2x3
bmat2x3

mat2x3.equals(a, b) ⇒ boolean

Compares two matrices.

Kind: static method of mat2x3

ParamType
amat2x3
bmat2x3

mat2x3.mult(a, b) ⇒ mat2x3

Multiplies two matrices.

Kind: static method of mat2x3

ParamType
amat2x3
bmat2x3

mat2x3.translate(a, v) ⇒ mat2x3

Translates a matrix by a vector.

Kind: static method of mat2x3

ParamType
amat2x3
vvec2

mat2x3.rotate(a, rad) ⇒ mat2x3

Rotates a matrix by an angle.

Kind: static method of mat2x3

ParamType
amat2x3
radRadians

mat2x3.scale(a, v) ⇒ mat2x3

Scales a matrix by a vector.

Kind: static method of mat2x3

ParamType
amat2x3
vvec2

mat3

mat3.create() ⇒ mat3

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

Kind: static method of mat3

mat3.identity(a) ⇒ mat3

Sets a matrix to the identity matrix.

Kind: static method of mat3

ParamType
amat3

mat3.copy(a) ⇒ mat3

Returns a copy of a matrix.

Kind: static method of mat3

ParamType
amat3

mat3.set(a, b) ⇒ mat3

Sets a matrix from another matrix.

Kind: static method of mat3

ParamType
amat3
bmat3

mat3.equals(a, b) ⇒ boolean

Compares two matrices.

Kind: static method of mat3

ParamType
amat3
bmat3

mat3.mult(a, b) ⇒ mat3

Multiplies two matrices.

Kind: static method of mat3

ParamType
amat3
bmat3

mat3.transpose(a) ⇒ mat3

Transposes a matrix.

Kind: static method of mat3

ParamType
amat3

mat3.fromQuat(a, q) ⇒ mat3

Sets matrix to a quaternion.

Kind: static method of mat3

ParamType
amat3
qquat

mat3.fromMat2x3(a, b) ⇒ mat3

Sets a 3x3 matrix from a 2x3 matrix.

Kind: static method of mat3

ParamType
amat3
bmat2x3

mat3.fromMat4(a, b) ⇒ mat3

Sets a 3x3 matrix to a 4x4 matrix.

Kind: static method of mat3

ParamType
amat3
bmat4

mat4

mat4.create() ⇒ mat4

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

Kind: static method of mat4

mat4.identity(a) ⇒ mat4

Sets a matrix to the identity matrix.

Kind: static method of mat4

ParamType
amat4

mat4.copy(a) ⇒ mat4

Returns a copy of a matrix.

Kind: static method of mat4

ParamType
amat4

mat4.set(a, b) ⇒ mat4

Sets a matrix from another matrix.

Kind: static method of mat4

ParamType
amat4
bmat4

mat4.equals(a, b) ⇒ boolean

Compares two matrices.

Kind: static method of mat4

ParamType
amat4
bmat4

mat4.mult(a, b) ⇒ mat4

Multiplies two matrices.

Kind: static method of mat4

ParamType
amat4
bmat4

mat4.invert(a) ⇒ mat4

Inverts a matrix.

Kind: static method of mat4

ParamType
amat4

mat4.transpose(a) ⇒ mat4

Transposes a matrix.

Kind: static method of mat4

ParamType
amat4

mat4.translate(a, v) ⇒ mat4

Translates a matrix by a vector.

Kind: static method of mat4

ParamType
amat4
vvec3

mat4.rotate(a, r, v) ⇒ mat4

Rotates a matrix by an angle at an axis.

Kind: static method of mat4

ParamType
amat4
rRadians
vvec3

mat4.scale(a, v) ⇒ mat4

Scales a matrix by a vector.

Kind: static method of mat4

ParamType
amat4
vvec3

mat4.fromQuat(a, q) ⇒ mat4

Sets a matrix to a quaternion.

Kind: static method of mat4

ParamType
amat4
qquat

mat4.fromTranslationRotationScale(a, v, q, s) ⇒ mat4

Sets a matrix to the TRS matrix.

Kind: static method of mat4

ParamType
amat4
vvec3
qquat
svec3

mat4.fromMat3(a, b) ⇒ mat4

Sets a 4x4 matrix to a 3x3 matrix.

Kind: static method of mat4

ParamType
amat4
bmat3

mat4.frustum(a, left, right, bottom, top, near, far) ⇒ mat4

Creates a frustum matrix.

Kind: static method of mat4

ParamType
amat4
leftnumber
rightnumber
bottomnumber
topnumber
nearnumber
farnumber

mat4.perspective(a, fovy, aspectRatio, near, far) ⇒ mat4

Creates a perspective matrix.

Kind: static method of mat4

ParamType
amat4
fovyRadians
aspectRationumber
nearnumber
farnumber

mat4.ortho(a, left, right, bottom, top, near, far) ⇒ mat4

Creates an orthographic matrix.

Kind: static method of mat4

ParamType
amat4
leftnumber
rightnumber
bottomnumber
topnumber
nearnumber
farnumber

mat4.lookAt(a, from, to, up) ⇒ mat4

Calculates a lookAt matrix from position, target and up vectors.

Kind: static method of mat4

ParamTypeDefault
amat4
fromvec3
tovec3
upvec3Y_UP

mat4.fromDirection(a, direction, up) ⇒ mat4

Sets a matrix from a direction. Note: we assume +Z facing models.

Kind: static method of mat4

ParamTypeDefault
amat4
directionvec3
upvec3Y_UP

mat4.fromPointToPoint(a, from, to, up) ⇒ mat4

Sets a matrix from a point to another.

Kind: static method of mat4

ParamTypeDefault
amat4
fromvec3
tovec3
upvec3Y_UP

quat

quat.set ⇒ quat

Sets a quaternion to another quaternion.

Kind: static constant of quat

ParamType
aquat
bquat

quat.equals ⇒ boolean

Compares two quaternions.

Kind: static constant of quat

ParamType
aquat
bquat

quat.toString ⇒ quat

Prints a quaternion to a string.

Kind: static constant of quat

ParamType
aquat
precisionnumber

quat.create() ⇒ quat

Returns a new quat at 0, 0, 0, 1.

Kind: static method of quat

quat.identity(a) ⇒ quat

Sets a quaternion to the identity quaternion.

Kind: static method of quat

ParamType
aquat

quat.copy(a) ⇒ quat

Returns a copy of a quaternion.

Kind: static method of quat

ParamType
aquat

quat.mult(a, b) ⇒ quat

Multiplies one quaternion by another.

Kind: static method of quat

ParamType
aquat
bquat

quat.invert(a) ⇒ quat

Inverts a quaternion.

Kind: static method of quat

ParamType
aquat

quat.conjugate(a) ⇒ quat

Conjugates a quaternion.

Kind: static method of quat

ParamType
aquat

quat.length(a) ⇒ quat

Calculates the length of a quaternion.

Kind: static method of quat

ParamType
aquat

quat.normalize(a) ⇒ quat

Normalizes a quaternion.

Kind: static method of quat

ParamType
aquat

quat.dot(a, b) ⇒ quat

Calculates the dot product of two quaternions.

Kind: static method of quat

ParamType
aquat
bquat

quat.fromEuler(a, e) ⇒ quat

Set euler angles to a quaternion. Assumes XYZ rotation order.

Kind: static method of quat

ParamType
aquat
eeuler

quat.fromAxisAngle(a, v, r) ⇒ quat

Set the angle at an axis of a quaternion.

Kind: static method of quat

ParamType
aquat
vvec3
rRadians

quat.fromAxes(a, x, y, z) ⇒ quat

Sets a quaternion from orthonormal base xyz.

Kind: static method of quat

ParamType
aquat
xvec3
yvec3
zvec3

quat.fromMat3(a, m) ⇒ quat

Sets a quaternion to a 3x3 matrix.

Kind: static method of quat

ParamType
aquat
mmat3

quat.fromMat4(a, m) ⇒ quat

Sets a quaternion to a 4x4 matrix.

Kind: static method of quat

ParamType
aquat
mmat4

quat.fromDirection(a, direction, up) ⇒ quat

Sets a quaternion from a direction Note: we assume +Z facing models.

Kind: static method of quat

ParamTypeDefault
aquat
directionvec3
upvec3Y_UP

quat.fromPointToPoint(a, from, to, up) ⇒ quat

Sets a quaternion from a point to another.

Kind: static method of quat

ParamTypeDefault
aquat
fromvec3
tovec3
upvec3Y_UP

quat.slerp(a, b, t) ⇒ quat

Spherical linear interpolates between two quaternions.

Kind: static method of quat

ParamType
aquat
bquat
tnumber

utils

utils.EPSILON : number

Kind: static constant of utils

utils.Y_UP : vec3

Kind: static constant of utils

utils.lerp(a, b, t) ⇒ number

Linear interpolation between two numbers.

Kind: static method of utils

ParamType
anumber
bnumber
tnumber

utils.clamp(n, min, max) ⇒ number

Clamps a number between two numbers.

Kind: static method of utils

ParamType
nnumber
minnumber
maxnumber

utils.smoothstep(n, min, max) ⇒ number

Smooth Hermite interpolation between 0 and 1

Kind: static method of utils

ParamType
nnumber
minnumber
maxnumber

utils.remap(n, inStart, inEnd, outStart, outEnd) ⇒ number

Maps a number from one range to another.

Kind: static method of utils

ParamType
nnumber
inStartnumber
inEndnumber
outStartnumber
outEndnumber

utils.toRadians(degrees) ⇒ Radians

Transforms degrees into radians.

Kind: static method of utils

ParamType
degreesDegrees

utils.toDegrees(radians) ⇒ Degrees

Transforms radians into degrees.

Kind: static method of utils

ParamType
radiansRadians

utils.isPowerOfTwo(a) ⇒ boolean

Check if a number is a power of two

Kind: static method of utils

ParamType
anumber

utils.nextPowerOfTwo(n) ⇒ number

Returns the next highest power of two.

Kind: static method of utils

ParamType
nnumber

utils.prevPowerOfTwo(n) ⇒ number

Returns the previous power of two.

Kind: static method of utils

ParamType
nnumber

vec2

vec2.create() ⇒ vec2

Returns a new vec2 at 0, 0.

Kind: static method of vec2

vec2.copy(a) ⇒ vec2

Returns a copy of a vector.

Kind: static method of vec2

ParamType
avec2

vec2.set(a, b) ⇒ vec2

Sets a vector to another vector.

Kind: static method of vec2

| Param | Type