Complete reference for every export in math, grouped by module. For an
overview, installation, and examples, see the README.
mathβ Vectors, quaternions, euler angles & matricesmath/shapesβ Shape primitives & spatial queriesmath/geometryβ Geometric algorithmsmath/timeβ Easing & spring animationmath/randomβ Seeded random number generatorsmath/noiseβ Perlin, simplex & worley noise, plus fractal helpersmath/colorβ Color & colorspace utilities
Types
type MutableArrayLike<T> = { [index: number]: T; length: number; }type Vec2 = [ x: number, y: number ]β A 2D vectortype Vec3 = [ x: number, y: number, z: number ]β A 3D vectortype Vec4 = [ x: number, y: number, z: number, w: number ]β A 4D vectortype Euler = [ x: number, y: number, z: number, order?: EulerOrder ]β A Euler in 3D space, with an optional order (default is 'xyz')type EulerOrder = 'xyz' | 'xzy' | 'yxz' | 'yzx' | 'zxy' | 'zyx'β Euler orderstype Quat = [ x: number, y: number, z: number, w: number ]β A quaternion that represents rotationtype Quat2 = [ x: number, y: number, z: number, w: number, x2: number, y2: number, z2: number, w2: number ]β A dual quaternion that represents both rotation and translationtype Mat2 = [ e1: number, e2: number, e3: number, e4: number ]β A 2x2 matrixtype Mat2d = [ e1: number, e2: number, e3: number, e4: number, e5: number, e6: number ]β A 2D affine transform matrixtype Mat3 = [ e1: number, e2: number, e3: number, e4: number, e5: number, e6: number, e7: number, e8: number, e9: number ]β A 3x3 matrixtype Mat4 = [ e1: number, e2: number, e3: number, e4: number, e5: number, e6: number, e7: number, e8: number, e9: number, e10: number, e11: number, e12: number, e13: number, e14: number, e15: number, e16: number ]β A 4x4 matrixtype Spherical = [ r: number, theta: number, phi: number ]β A point in spherical coordinates [r, theta, phi] (Three.js / OpenGL convention)type Polar = [ r: number, theta: number ]β A point in polar coordinates [r, theta]
Operations
EPSILON = 0.000001round(a: number): numberβ Symmetric roundfade(t: number)β Ease-in-out, goes to -Infinite before 0 and Infinite after 1lerp(v0: number, v1: number, t: number)clamp(value: number, min: number, max: number): numberβ Clamp a value between min and maxrepeat(t: number, length: number): numberβ Loopstso that it is never larger thanlengthand never smaller than 0.remap(number: number, inLow: number, inHigh: number, outLow: number, outHigh: number): numberβ Remaps a number from one range to another.remapClamp(value: number, inLow: number, inHigh: number, outLow: number, outHigh: number): numberβ Remaps a number from one range to another, clamping the result to the output range.DEGREES_TO_RADIANSRADIANS_TO_DEGREESdegreesToRadians(degrees: number): numberβ Converts Degrees To RadiansradiansToDegrees(radians: number): numberβ Converts Radians To DegreeswrapAngle(a: number): numberβ Wraps an angle (in radians) into the range (-Ο, Ο].deltaAngle(current: number, target: number): numberβ Calculates the shortest signed difference between two angles (in radians).
Query
equals(a: number, b: number, epsilon = EPSILON): booleanβ Tests whether or not the arguments have approximately the same value, within an absolute
import { vec2 } from 'math';Create
vec2.create(): Vec2β Creates a new, empty vec2vec2.clone(a: Vec2): Vec2β Creates a new vec2 initialized with values from an existing vectorvec2.fromValues(x: number, y: number): Vec2β Creates a new vec2 initialized with the given valuesvec2.copy(out: Vec2, a: Vec2): Vec2β Copy the values from one vec2 to anothervec2.set(out: Vec2, x: number, y: number): Vec2β Set the components of a vec2 to the given valuesvec2.fromBuffer(out: Vec2, buffer: ArrayLike<number>, startIndex: number): Vec2β Sets the components of a vec2 from a buffervec2.toBuffer(outBuffer: MutableArrayLike<number>, vec: Vec2, startIndex: number): MutableArrayLike<number>β Writes the components of a vec2 to a buffervec2.zero(out: Vec2): Vec2β Set the components of a vec2 to zerovec2.str(a: Vec2): stringβ Returns a string representation of a vector
Operations
vec2.add(out: Vec2, a: Vec2, b: Vec2): Vec2β Adds two vec2'svec2.addScalar(out: Vec2, a: Vec2, b: number): Vec2β Adds a scalar value to all components of a vec2vec2.subtract(out: Vec2, a: Vec2, b: Vec2): Vec2β Subtracts vector b from vector avec2.subtractScalar(out: Vec2, a: Vec2, b: number): Vec2β Subtracts a scalar value from all components of a vec2vec2.multiply(out: Vec2, a: Vec2, b: Vec2): Vec2β Multiplies two vec2'svec2.divide(out: Vec2, a: Vec2, b: Vec2): Vec2β Divides two vec2'svec2.ceil(out: Vec2, a: Vec2): Vec2β Math.ceil the components of a vec2vec2.floor(out: Vec2, a: Vec2): Vec2β Math.floor the components of a vec2vec2.min(out: Vec2, a: Vec2, b: Vec2): Vec2β Returns the minimum of two vec2'svec2.max(out: Vec2, a: Vec2, b: Vec2): Vec2β Returns the maximum of two vec2'svec2.round(out: Vec2, a: Vec2): Vec2β symmetric round the components of a vec2vec2.scale(out: Vec2, a: Vec2, b: number): Vec2β Scales a vec2 by a scalar numbervec2.scaleAndAdd(out: Vec2, a: Vec2, b: Vec2, scale: number): Vec2β Adds two vec2's after scaling the second operand by a scalar valuevec2.distance(a: Vec2, b: Vec2): numberβ Calculates the euclidian distance between two vec2'svec2.squaredDistance(a: Vec2, b: Vec2): numberβ Calculates the squared euclidian distance between two vec2'svec2.length(a: Vec2): numberβ Calculates the length of a vec2vec2.squaredLength(a: Vec2): numberβ Calculates the squared length of a vec2vec2.negate(out: Vec2, a: Vec2): Vec2β Negates the components of a vec2vec2.inverse(out: Vec2, a: Vec2): Vec2β Returns the inverse of the components of a vec2vec2.normalize(out: Vec2, a: Vec2): Vec2β Normalize a vec2vec2.dot(a: Vec2, b: Vec2): numberβ Calculates the dot product of two vec2'svec2.cross(out: Vec3, a: Vec2, b: Vec2): Vec3β Computes the cross product of two vec2'svec2.lerp(out: Vec2, a: Vec2, b: Vec2, t: number): Vec2β Performs a linear interpolation between two vec2's
Transform
vec2.transformMat2(out: Vec2, a: Vec2, m: Mat2): Vec2β Transforms the vec2 with a mat2vec2.transformMat2d(out: Vec2, a: Vec2, m: Mat2d): Vec2β Transforms the vec2 with a mat2dvec2.transformMat3(out: Vec2, a: Vec2, m: Mat3): Vec2β Transforms the vec2 with a mat3vec2.transformMat4(out: Vec2, a: Vec2, m: Mat4): Vec2β Transforms the vec2 with a mat4vec2.rotate(out: Vec2, a: Vec2, b: Vec2, rad: number): Vec2β Rotate a 2D vector
Query
vec2.angle(a: Vec2, b: Vec2): numberβ Get the angle between two 2D vectorsvec2.exactEquals(a: Vec2, b: Vec2): booleanβ Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)vec2.equals(a: Vec2, b: Vec2): booleanβ Returns whether or not the vectors have approximately the same elements in the same position.vec2.finite(a: Vec2): booleanβ Returns whether or not the vector is finite
Aliases
vec2.len = lengthβ Alias forlengthvec2.sub = subtractβ Alias forsubtractvec2.mul = multiplyβ Alias formultiplyvec2.div = divideβ Alias fordividevec2.dist = distanceβ Alias fordistancevec2.sqrDist = squaredDistanceβ Alias forsquaredDistancevec2.sqrLen = squaredLengthβ Alias forsquaredLength
import { vec3 } from 'math';Create
vec3.create(): Vec3β Creates a new, empty vec3vec3.clone(a: Vec3): Vec3β Creates a new vec3 initialized with values from an existing vectorvec3.fromValues(x: number, y: number, z: number): Vec3β Creates a new vec3 initialized with the given valuesvec3.copy(out: Vec3, a: Vec3): Vec3β Copy the values from one vec3 to anothervec3.set(out: Vec3, x: number, y: number, z: number): Vec3β Set the components of a vec3 to the given valuesvec3.setScalar(out: Vec3, s: number): Vec3β Sets all components of a vec3 to the given scalar valuevec3.fromBuffer(out: Vec3, buffer: ArrayLike<number>, startIndex: number): Vec3β Sets the components of a vec3 from a buffervec3.toBuffer(outBuffer: MutableArrayLike<number>, vec: Vec3, startIndex: number): MutableArrayLike<number>β Writes the components of a vec3 to a buffervec3.zero(out: Vec3): Vec3β Set the components of a vec3 to zerovec3.str(a: Vec3): stringβ Returns a string representation of a vector
Operations
vec3.length(a: Vec3): numberβ Calculates the length of a vec3vec3.add(out: Vec3, a: Vec3, b: Vec3): Vec3β Adds two vec3'svec3.addScalar(out: Vec3, a: Vec3, b: number): Vec3β Adds a scalar value to all components of a vec3vec3.subtract(out: Vec3, a: Vec3, b: Vec3): Vec3β Subtracts vector b from vector avec3.subtractScalar(out: Vec3, a: Vec3, b: number): Vec3β Subtracts a scalar value from all components of a vec3vec3.multiply(out: Vec3, a: Vec3, b: Vec3): Vec3β Multiplies two vec3'svec3.divide(out: Vec3, a: Vec3, b: Vec3): Vec3β Divides two vec3'svec3.ceil(out: Vec3, a: Vec3): Vec3β Math.ceil the components of a vec3vec3.floor(out: Vec3, a: Vec3): Vec3β Math.floor the components of a vec3vec3.min(out: Vec3, a: Vec3, b: Vec3): Vec3β Returns the minimum of two vec3'svec3.max(out: Vec3, a: Vec3, b: Vec3): Vec3β Returns the maximum of two vec3'svec3.round(out: Vec3, a: Vec3): Vec3β symmetric round the components of a vec3vec3.scale(out: Vec3, a: Vec3, b: number): Vec3β Scales a vec3 by a scalar numbervec3.scaleAndAdd(out: Vec3, a: Vec3, b: Vec3, scale: number): Vec3β Adds two vec3's after scaling the second operand by a scalar valuevec3.distance(a: Vec3, b: Vec3): numberβ Calculates the euclidian distance between two vec3'svec3.squaredDistance(a: Vec3, b: Vec3): numberβ Calculates the squared euclidian distance between two vec3'svec3.squaredLength(a: Vec3): numberβ Calculates the squared length of a vec3vec3.negate(out: Vec3, a: Vec3): Vec3β Negates the components of a vec3vec3.inverse(out: Vec3, a: Vec3): Vec3β Returns the inverse of the components of a vec3vec3.normalize(out: Vec3, a: Vec3): Vec3β Normalize a vec3vec3.dot(a: Vec3, b: Vec3): numberβ Calculates the dot product of two vec3'svec3.cross(out: Vec3, a: Vec3, b: Vec3): Vec3β Computes the cross product of two vec3'svec3.perpendicular(out: Vec3, a: Vec3): Vec3β Calculates a normalized perpendicular vector to the given vector.vec3.lerp(out: Vec3, a: Vec3, b: Vec3, t: number): Vec3β Performs a linear interpolation between two vec3'svec3.slerp(out: Vec3, a: Vec3, b: Vec3, t: number): Vec3β Performs a spherical linear interpolation between two vec3'svec3.hermite(out: Vec3, a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: number): Vec3β Performs a hermite interpolation with two control pointsvec3.bezier(out: Vec3, a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: number): Vec3β Performs a bezier interpolation with two control points
Transform
vec3.transformMat4(out: Vec3, a: Vec3, m: Mat4): Vec3β Transforms the vec3 with a mat4.vec3.transformMat3(out: Vec3, a: Vec3, m: Mat3): Vec3β Transforms the vec3 with a mat3.vec3.transformQuat(out: Vec3, a: Vec3, q: Quat): Vec3β Transforms the vec3 with a quatvec3.rotateX(out: Vec3, a: Vec3, b: Vec3, rad: number): Vec3β Rotate a 3D vector around the x-axisvec3.rotateY(out: Vec3, a: Vec3, b: Vec3, rad: number): Vec3β Rotate a 3D vector around the y-axisvec3.rotateZ(out: Vec3, a: Vec3, b: Vec3, rad: number): Vec3β Rotate a 3D vector around the z-axis
Query
vec3.angle(a: Vec3, b: Vec3): numberβ Get the angle between two 3D vectorsvec3.exactEquals(a: Vec3, b: Vec3): booleanβ Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)vec3.equals(a: Vec3, b: Vec3): booleanβ Returns whether or not the vectors have approximately the same elements in the same position.vec3.finite(a: Vec3): booleanβ Returns whether or not the vector is finitevec3.isScaleInsideOut(scale: Vec3): booleanβ Determines if a scale vector represents an inside-out transformation (reflection)
Aliases
vec3.sub = subtractβ Alias forsubtractvec3.mul = multiplyβ Alias formultiplyvec3.div = divideβ Alias fordividevec3.dist = distanceβ Alias fordistancevec3.sqrDist = squaredDistanceβ Alias forsquaredDistancevec3.len = lengthβ Alias forlengthvec3.sqrLen = squaredLengthβ Alias forsquaredLength
import { vec4 } from 'math';Create
vec4.create(): Vec4β Creates a new, empty vec4vec4.clone(a: Vec4): Vec4β Creates a new vec4 initialized with values from an existing vectorvec4.fromValues(x: number, y: number, z: number, w: number): Vec4β Creates a new vec4 initialized with the given valuesvec4.copy(out: Vec4, a: Vec4): Vec4β Copy the values from one vec4 to anothervec4.set(out: Vec4, x: number, y: number, z: number, w: number): Vec4β Set the components of a vec4 to the given valuesvec4.fromBuffer(out: Vec4, buffer: ArrayLike<number>, startIndex: number): Vec4β Sets the components of a vec4 from a buffervec4.toBuffer(outBuffer: MutableArrayLike<number>, vec: Vec4, startIndex: number): MutableArrayLike<number>β Writes the components of a vec4 to a buffervec4.zero(out: Vec4): Vec4β Set the components of a vec4 to zerovec4.str(a: Vec4): stringβ Returns a string representation of a vector
Operations
vec4.add(out: Vec4, a: Vec4, b: Vec4): Vec4β Adds two vec4'svec4.subtract(out: Vec4, a: Vec4, b: Vec4): Vec4β Subtracts vector b from vector avec4.multiply(out: Vec4, a: Vec4, b: Vec4): Vec4β Multiplies two vec4'svec4.divide(out: Vec4, a: Vec4, b: Vec4): Vec4β Divides two vec4'svec4.ceil(out: Vec4, a: Vec4): Vec4β Math.ceil the components of a vec4vec4.floor(out: Vec4, a: Vec4): Vec4β Math.floor the components of a vec4vec4.min(out: Vec4, a: Vec4, b: Vec4): Vec4β Returns the minimum of two vec4'svec4.max(out: Vec4, a: Vec4, b: Vec4): Vec4β Returns the maximum of two vec4'svec4.round(out: Vec4, a: Vec4): Vec4β symmetric round the components of a vec4vec4.scale(out: Vec4, a: Vec4, b: number): Vec4β Scales a vec4 by a scalar numbervec4.scaleAndAdd(out: Vec4, a: Vec4, b: Vec4, scale: number): Vec4β Adds two vec4's after scaling the second operand by a scalar valuevec4.distance(a: Vec4, b: Vec4): numberβ Calculates the euclidian distance between two vec4'svec4.squaredDistance(a: Vec4, b: Vec4): numberβ Calculates the squared euclidian distance between two vec4'svec4.length(a: Vec4): numberβ Calculates the length of a vec4vec4.squaredLength(a: Vec4): numberβ Calculates the squared length of a vec4vec4.negate(out: Vec4, a: Vec4): Vec4β Negates the components of a vec4vec4.inverse(out: Vec4, a: Vec4): Vec4β Returns the inverse of the components of a vec4vec4.normalize(out: Vec4, a: Vec4): Vec4β Normalize a vec4vec4.dot(a: Vec4, b: Vec4): numberβ Calculates the dot product of two vec4'svec4.cross(out: Vec4, u: Vec4, v: Vec4, w: Vec4): Vec4β Returns the cross-product of three vectors in a 4-dimensional spacevec4.lerp(out: Vec4, a: Vec4, b: Vec4, t: number): Vec4β Performs a linear interpolation between two vec4's
Transform
vec4.transformMat4(out: Vec4, a: Vec4, m: Mat4): Vec4β Transforms the vec4 with a mat4.vec4.transformQuat(out: Vec4, a: Vec4, q: Quat): Vec4β Transforms the vec4 with a quat
Query
vec4.exactEquals(a: Vec4, b: Vec4): booleanβ Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)vec4.equals(a: Vec4, b: Vec4): booleanβ Returns whether or not the vectors have approximately the same elements in the same position.vec4.finite(a: Vec4): booleanβ Returns whether or not the vector is finite
Aliases
vec4.sub = subtractβ Alias forsubtractvec4.mul = multiplyβ Alias formultiplyvec4.div = divideβ Alias fordividevec4.dist = distanceβ Alias fordistancevec4.sqrDist = squaredDistanceβ Alias forsquaredDistancevec4.len = lengthβ Alias forlengthvec4.sqrLen = squaredLengthβ Alias forsquaredLength
import { euler } from 'math';Create
euler.create(): Eulerβ Creates a new Euler with default values (0, 0, 0, 'xyz').euler.fromValues(x: number, y: number, z: number, order: EulerOrder): Eulerβ Creates a new Euler from the given values.euler.set(out: Euler, x: number, y: number, z: number, order: EulerOrder): Eulerβ Sets a given Euler from the given values.euler.fromDegrees(out: Euler, x: number, y: number, z: number, order: EulerOrder): Eulerβ Sets Euler angle radians from given degreeseuler.fromRotationMat4(out: Euler, rotationMatrix: Mat4, order: EulerOrder = out[3] || 'xyz'): Eulerβ Sets the Euler angles from a rotation matrix.euler.fromQuat(out: Euler, q: Quat, order: EulerOrder): Eulerβ Sets the Euler angles from a quaternion.euler.reorder(out: Euler, a: Euler, order: EulerOrder): Eulerβ Reorders the Euler based on the specified order.
Query
euler.exactEquals(a: Euler, b: Euler): booleanβ Returns whether or not the euler angles have exactly the same elements in the same position (when compared with ===)euler.equals(a: Euler, b: Euler): booleanβ Returns whether or not the euler angles have approximately the same elements in the same position.
import { quat } from 'math';Create
quat.create(): Quatβ Creates a new identity quatquat.fromBuffer(out: Quat, buffer: ArrayLike<number>, startIndex: number): Quatβ Sets the components of a quat from a bufferquat.toBuffer(outBuffer: MutableArrayLike<number>, q: Quat, startIndex: number): MutableArrayLike<number>β Writes the components of a quat to a bufferquat.identity(out: Quat): Quatβ Set a quat to the identity quaternionquat.setAxisAngle(out: Quat, axis: Vec3, rad: number): Quatβ Sets a quat from the given angle and rotation axisquat.calculateW(out: Quat, a: Quat): Quatβ Calculates the W component of a quat from the X, Y, and Z components.quat.fromMat3(out: Quat, m: Mat3): Quatβ Creates a quaternion from the given 3x3 rotation matrix.quat.fromMat4(out: Quat, m: Mat4): Quatβ Calculates a quaternion from a 4x4 rotation matrixquat.fromEuler(out: Quat, euler: Euler): Quatβ Creates a quaternion from the given eulerquat.fromDegrees(out: Quat, x: number, y: number, z: number, order: EulerOrder): Quatβ Creates a quaternion from euler angles specified in degrees.quat.str(a: Quat): stringβ Returns a string representation of a quaternionquat.cloneβ Creates a new quat initialized with values from an existing quaternionquat.fromValuesβ Creates a new quat initialized with the given valuesquat.copyβ Copy the values from one quat to anotherquat.setβ Set the components of a quat to the given valuesquat.setAxesβ Sets the specified quaternion with values corresponding to the given
Operations
quat.multiply(out: Quat, a: Quat, b: Quat): Quatβ Multiplies two quat'squat.exp(out: Quat, a: Quat): Quatβ Calculate the exponential of a unit quaternion.quat.ln(out: Quat, a: Quat): Quatβ Calculate the natural logarithm of a unit quaternion.quat.pow(out: Quat, a: Quat, b: number): Quatβ Calculate the scalar power of a unit quaternion.quat.slerp(out: Quat, a: Quat, b: Quat, t: number): Quatβ Performs a spherical linear interpolation between two quatquat.invert(out: Quat, a: Quat): Quatβ Calculates the inverse of a quatquat.conjugate(out: Quat, a: Quat): Quatβ Calculates the conjugate of a quatquat.addβ Adds two quat'squat.scaleβ Scales a quat by a scalar numberquat.dotβ Calculates the dot product of two quat'squat.lerpβ Performs a linear interpolation between two quat'squat.lengthβ Calculates the length of a quatquat.squaredLengthβ Calculates the squared length of a quatquat.normalizeβ Normalize a quatquat.rotationToβ Sets a quaternion to represent the shortest rotation from onequat.sqlerpβ Performs a spherical linear interpolation with two control points
Transform
quat.rotateX(out: Quat, a: Quat, rad: number): Quatβ Rotates a quaternion by the given angle about the X axisquat.rotateY(out: Quat, a: Quat, rad: number): Quatβ Rotates a quaternion by the given angle about the Y axisquat.rotateZ(out: Quat, a: Quat, rad: number): Quatβ Rotates a quaternion by the given angle about the Z axis
Query
quat.getAxisAngle(out_axis: Vec3, q: Quat): numberβ Gets the rotation axis and angle for a givenquat.getAngle(a: Quat, b: Quat): numberβ Gets the angular distance between two unit quaternionsquat.exactEqualsβ Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===)quat.equals(a: Quat, b: Quat): booleanβ Returns whether or not the quaternions have approximately the same elements in the same position.
Aliases
quat.len = lengthβ Alias forlengthquat.sqrLen = squaredLengthβ Alias forsquaredLengthquat.mul = multiplyβ Alias formultiply
import { quat2 } from 'math';Create
quat2.create(): Quat2β Creates a new identity dual quatquat2.clone(a: Quat2): Quat2β Creates a new quat initialized with values from an existing quaternionquat2.fromValues(x1: number, y1: number, z1: number, w1: number, x2: number, y2: number, z2: number, w2: number): Quat2β Creates a new dual quat initialized with the given valuesquat2.fromRotationTranslationValues(x1: number, y1: number, z1: number, w1: number, x2: number, y2: number, z2: number): Quat2β Creates a new dual quat from the given values (quat and translation)quat2.fromRotationTranslation(out: Quat2, q: Quat, t: Vec3): Quat2β Creates a dual quat from a quaternion and a translationquat2.fromTranslation(out: Quat2, t: Vec3): Quat2β Creates a dual quat from a translationquat2.fromRotation(out: Quat2, q: Quat): Quat2β Creates a dual quat from a quaternionquat2.fromMat4(out: Quat2, a: Mat4): Quat2β Creates a new dual quat from a matrix (4x4)quat2.copy(out: Quat2, a: Quat2): Quat2β Copy the values from one dual quat to anotherquat2.identity(out: Quat2): Quat2β Set a dual quat to the identity dual quaternionquat2.set(out: Quat2, x1: number, y1: number, z1: number, w1: number, x2: number, y2: number, z2: number, w2: number): Quat2β Set the components of a dual quat to the given valuesquat2.setReal(out: Quat2, q: Quat): Quat2β Set the real component of a dual quat to the given quaternionquat2.setDual(out: Quat2, q: Quat): Quat2β Set the dual component of a dual quat to the given quaternionquat2.str(a: Quat2): stringβ Returns a string representation of a dual quaternion
Operations
quat2.add(out: Quat2, a: Quat2, b: Quat2): Quat2β Adds two dual quat'squat2.multiply(out: Quat2, a: Quat2, b: Quat2): Quat2β Multiplies two dual quat'squat2.scale(out: Quat2, a: Quat2, b: number): Quat2β Scales a dual quat by a scalar numberquat2.dot(a: Quat2, b: Quat2): numberβ Calculates the dot product of two dual quat's (The dot product of the real parts)quat2.lerp(out: Quat2, a: Quat2, b: Quat2, t: number): Quat2β Performs a linear interpolation between two dual quats'squat2.invert(out: Quat2, a: Quat2): Quat2β Calculates the inverse of a dual quat. If they are normalized, conjugate is cheaperquat2.conjugate(out: Quat2, a: Quat2): Quat2β Calculates the conjugate of a dual quatquat2.length(a: Quat2): numberβ Calculates the length of a dual quat (the length of its real/rotation part)quat2.squaredLength(a: Quat2): numberβ Calculates the squared length of a dual quat (the squared length of its real/rotation part)quat2.normalize(out: Quat2, a: Quat2): Quat2β Normalize a dual quat
Transform
quat2.translate(out: Quat2, a: Quat2, v: Vec3): Quat2β Translates a dual quat by the given vectorquat2.rotateX(out: Quat2, a: Quat2, rad: number): Quat2β Rotates a dual quat around the X axisquat2.rotateY(out: Quat2, a: Quat2, rad: number): Quat2β Rotates a dual quat around the Y axisquat2.rotateZ(out: Quat2, a: Quat2, rad: number): Quat2β Rotates a dual quat around the Z axisquat2.rotateByQuatAppend(out: Quat2, a: Quat2, q: Quat): Quat2β Rotates a dual quat by a given quaternion (a * q)quat2.rotateByQuatPrepend(out: Quat2, q: Quat, a: Quat2): Quat2β Rotates a dual quat by a given quaternion (q * a)quat2.rotateAroundAxis(out: Quat2, a: Quat2, axis: Vec3, rad: number): Quat2β Rotates a dual quat around a given axis. Does the normalisation automatically
Query
quat2.getReal(out: Quat, a: Quat2): Quatβ Gets the real part of a dual quatquat2.getDual(out: Quat, a: Quat2): Quatβ Gets the dual part of a dual quatquat2.getTranslation(out: Vec3, a: Quat2): Vec3β Gets the translation of a normalized dual quatquat2.exactEquals(a: Quat2, b: Quat2): booleanβ Returns whether or not the dual quaternions have exactly the same elements in the same position (when compared with ===)quat2.equals(a: Quat2, b: Quat2): booleanβ Returns whether or not the dual quaternions have approximately the same elements in the same position.
Aliases
quat2.mul = multiplyβ Alias formultiplyquat2.len = lengthβ Alias forlengthquat2.sqrLen = squaredLengthβ Alias forsquaredLength
import { mat2 } from 'math';Create
mat2.create(): Mat2β Creates a new identity mat2mat2.clone(a: Mat2): Mat2β Creates a new mat2 initialized with values from an existing matrixmat2.copy(out: Mat2, a: Mat2): Mat2β Copy the values from one mat2 to anothermat2.identity(out: Mat2): Mat2β Set a mat2 to the identity matrixmat2.fromValues(m00: number, m01: number, m10: number, m11: number): Mat2β Create a new mat2 with the given valuesmat2.set(out: Mat2, m00: number, m01: number, m10: number, m11: number): Mat2β Set the components of a mat2 to the given valuesmat2.fromRotation(out: Mat2, rad: number): Mat2β Creates a matrix from a given anglemat2.fromScaling(out: Mat2, v: Vec2): Mat2β Creates a matrix from a vector scalingmat2.str(a: Mat2): stringβ Returns a string representation of a mat2
Operations
mat2.transpose(out: Mat2, a: Mat2): Mat2β Transpose the values of a mat2mat2.invert(out: Mat2, a: Mat2): Mat2 | nullβ Inverts a mat2mat2.adjoint(out: Mat2, a: Mat2): Mat2β Calculates the adjugate of a mat2mat2.determinant(a: Mat2): numberβ Calculates the determinant of a mat2mat2.multiply(out: Mat2, a: Mat2, b: Mat2): Mat2β Multiplies two mat2'smat2.frob(a: Mat2): numberβ Returns Frobenius norm of a mat2mat2.LDU(L: Mat2, D: Mat2, U: Mat2, a: Mat2): [ Mat2, Mat2, Mat2 ]β Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrixmat2.add(out: Mat2, a: Mat2, b: Mat2): Mat2β Adds two mat2'smat2.subtract(out: Mat2, a: Mat2, b: Mat2): Mat2β Subtracts matrix b from matrix amat2.multiplyScalar(out: Mat2, a: Mat2, b: number): Mat2β Multiply each element of the matrix by a scalar.mat2.multiplyScalarAndAdd(out: Mat2, a: Mat2, b: Mat2, scale: number): Mat2β Adds two mat2's after multiplying each element of the second operand by a scalar value.
Transform
mat2.rotate(out: Mat2, a: Mat2, rad: number): Mat2β Rotates a mat2 by the given anglemat2.scale(out: Mat2, a: Mat2, v: Vec2): Mat2β Scales the mat2 by the dimensions in the given vec2
Query
mat2.exactEquals(a: Mat2, b: Mat2): booleanβ Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)mat2.equals(a: Mat2, b: Mat2): booleanβ Returns whether or not the matrices have approximately the same elements in the same position.
Aliases
mat2.mul = multiplyβ Alias formultiplymat2.sub = subtractβ Alias forsubtract
import { mat2d } from 'math';Create
mat2d.create(): Mat2dβ Creates a new identity mat2dmat2d.clone(a: Mat2d): Mat2dβ Creates a new mat2d initialized with values from an existing matrixmat2d.copy(out: Mat2d, a: Mat2d): Mat2dβ Copy the values from one mat2d to anothermat2d.identity(out: Mat2d): Mat2dβ Set a mat2d to the identity matrixmat2d.fromValues(a: number, b: number, c: number, d: number, tx: number, ty: number): Mat2dβ Create a new mat2d with the given valuesmat2d.set(out: Mat2d, a: number, b: number, c: number, d: number, tx: number, ty: number): Mat2dβ Set the components of a mat2d to the given valuesmat2d.fromRotation(out: Mat2d, rad: number): Mat2dβ Creates a matrix from a given anglemat2d.fromScaling(out: Mat2d, v: Vec2): Mat2dβ Creates a matrix from a vector scalingmat2d.fromTranslation(out: Mat2d, v: Vec2): Mat2dβ Creates a matrix from a vector translationmat2d.str(a: Mat2d): stringβ Returns a string representation of a mat2d
Operations
mat2d.invert(out: Mat2d, a: Mat2d): Mat2d | nullβ Inverts a mat2dmat2d.determinant(a: Mat2d): numberβ Calculates the determinant of a mat2dmat2d.multiply(out: Mat2d, a: Mat2d, b: Mat2d): Mat2dβ Multiplies two mat2d'smat2d.frob(a: Mat2d): numberβ Returns Frobenius norm of a mat2dmat2d.add(out: Mat2d, a: Mat2d, b: Mat2d): Mat2dβ Adds two mat2d'smat2d.subtract(out: Mat2d, a: Mat2d, b: Mat2d): Mat2dβ Subtracts matrix b from matrix amat2d.multiplyScalar(out: Mat2d, a: Mat2d, b: number): Mat2dβ Multiply each element of the matrix by a scalar.mat2d.multiplyScalarAndAdd(out: Mat2d, a: Mat2d, b: Mat2d, scale: number): Mat2dβ Adds two mat2d's after multiplying each element of the second operand by a scalar value.
Transform
mat2d.rotate(out: Mat2d, a: Mat2d, rad: number): Mat2dβ Rotates a mat2d by the given anglemat2d.scale(out: Mat2d, a: Mat2d, v: Vec2): Mat2dβ Scales the mat2d by the dimensions in the given vec2mat2d.translate(out: Mat2d, a: Mat2d, v: Vec2): Mat2dβ Translates the mat2d by the dimensions in the given vec2
Query
mat2d.exactEquals(a: Mat2d, b: Mat2d): booleanβ Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)mat2d.equals(a: Mat2d, b: Mat2d): booleanβ Returns whether or not the matrices have approximately the same elements in the same position.
Aliases
mat2d.mul = multiplyβ Alias formultiplymat2d.sub = subtractβ Alias forsubtract
import { mat3 } from 'math';Create
mat3.create(): Mat3β Creates a new identity mat3mat3.fromMat4(out: Mat3, a: Mat4): Mat3β Copies the upper-left 3x3 values into the given mat3.mat3.clone(a: Mat3): Mat3β Creates a new mat3 initialized with values from an existing matrixmat3.copy(out: Mat3, a: Mat3): Mat3β Copy the values from one mat3 to anothermat3.fromValues(m00: number, m01: number, m02: number, m10: number, m11: number, m12: number, m20: number, m21: number, m22: number): Mat3β Create a new mat3 with the given valuesmat3.set(out: Mat3, m00: number, m01: number, m02: number, m10: number, m11: number, m12: number, m20: number, m21: number, m22: number): Mat3β Set the components of a mat3 to the given valuesmat3.identity(out: Mat3): Mat3β Set a mat3 to the identity matrixmat3.zero(out: Mat3): Mat3β Set a mat3 to the zero matrixmat3.fromTranslation(out: Mat3, v: Vec2): Mat3β Creates a matrix from a vector translationmat3.fromRotation(out: Mat3, rad: number): Mat3β Creates a matrix from a given anglemat3.fromScaling(out: Mat3, v: Vec2): Mat3β Creates a matrix from a vector scalingmat3.fromMat2d(out: Mat3, a: Mat2d): Mat3β Copies the values from a mat2d into a mat3mat3.fromQuat(out: Mat3, q: Quat): Mat3β Calculates a 3x3 matrix from the given quaternionmat3.projection(out: Mat3, width: number, height: number): Mat3β Generates a 2D projection matrix with the given boundsmat3.str(a: Mat3): stringβ Returns a string representation of a mat3
Operations
mat3.transpose(out: Mat3, a: Mat3): Mat3β Transpose the values of a mat3mat3.invert(out: Mat3, a: Mat3): Mat3 | nullβ Inverts a mat3mat3.adjoint(out: Mat3, a: Mat3): Mat3β Calculates the adjugate of a mat3mat3.determinant(a: Mat3): numberβ Calculates the determinant of a mat3mat3.multiply(out: Mat3, a: Mat3, b: Mat3): Mat3β Multiplies two mat3'smat3.normalFromMat4(out: Mat3, a: Mat4): Mat3 | nullβ Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrixmat3.frob(a: Mat3): numberβ Returns Frobenius norm of a mat3mat3.add(out: Mat3, a: Mat3, b: Mat3): Mat3β Adds two mat3'smat3.subtract(out: Mat3, a: Mat3, b: Mat3): Mat3β Subtracts matrix b from matrix amat3.multiplyScalar(out: Mat3, a: Mat3, b: number): Mat3β Multiply each element of the matrix by a scalar.mat3.multiplyScalarAndAdd(out: Mat3, a: Mat3, b: Mat3, scale: number): Mat3β Adds two mat3's after multiplying each element of the second operand by a scalar value.
Transform
mat3.translate(out: Mat3, a: Mat3, v: Vec2): Mat3β Translate a mat3 by the given vectormat3.rotate(out: Mat3, a: Mat3, rad: number): Mat3β Rotates a mat3 by the given anglemat3.scale(out: Mat3, a: Mat3, v: Vec2): Mat3β Scales the mat3 by the dimensions in the given vec2
Query
mat3.exactEquals(a: Mat3, b: Mat3): booleanβ Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)mat3.equals(a: Mat3, b: Mat3): booleanβ Returns whether or not the matrices have approximately the same elements in the same position.
Aliases
mat3.mul = multiplyβ Alias formultiplymat3.sub = subtractβ Alias forsubtract
import { mat4 } from 'math';Create
mat4.create(): Mat4β Creates a new identity mat4mat4.clone(a: Mat4): Mat4β Creates a new mat4 initialized with values from an existing matrixmat4.copy(out: Mat4, a: Mat4): Mat4β Copy the values from one mat4 to anothermat4.fromValues(m00: number, m01: number, m02: number, m03: number, m10: number, m11: number, m12: number, m13: number, m20: number, m21: number, m22: number, m23: number, m30: number, m31: number, m32: number, m33: number): Mat4β Create a new mat4 with the given valuesmat4.set(out: Mat4, m00: number, m01: number, m02: number, m03: number, m10: number, m11: number, m12: number, m13: number, m20: number, m21: number, m22: number, m23: number, m30: number, m31: number, m32: number, m33: number): Mat4β Set the components of a mat4 to the given valuesmat4.identity(out: Mat4): Mat4β Set a mat4 to the identity matrixmat4.zero(out: Mat4): Mat4β Set a mat4 to the zero matrixmat4.fromTranslation(out: Mat4, v: Vec3): Mat4β Creates a matrix from a vector translationmat4.fromScaling(out: Mat4, v: Vec3): Mat4β Creates a matrix from a vector scalingmat4.fromRotation(out: Mat4, rad: number, axis: Vec3): Mat4 | nullβ Creates a matrix from a given angle around a given axismat4.fromXRotation(out: Mat4, rad: number): Mat4β Creates a matrix from the given angle around the X axismat4.fromYRotation(out: Mat4, rad: number): Mat4β Creates a matrix from the given angle around the Y axismat4.fromZRotation(out: Mat4, rad: number): Mat4β Creates a matrix from the given angle around the Z axismat4.fromRotationTranslation(out: Mat4, q: Quat | Quat2, v: Vec3): Mat4β Creates a matrix from a quaternion rotation and vector translationmat4.fromQuat2(out: Mat4, a: Quat2): Mat4β Creates a new mat4 from a dual quat.mat4.fromRotationTranslationScale(out: Mat4, q: Quat, v: Vec3, s: Vec3): Mat4β Creates a matrix from a quaternion rotation, vector translation and vector scalemat4.fromRotationTranslationScaleOrigin(out: Mat4, q: Quat, v: Vec3, s: Vec3, o: Vec3): Mat4β Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given originmat4.fromQuat(out: Mat4, q: Quat): Mat4β Calculates a 4x4 matrix from the given quaternionmat4.frustumNO(out: Mat4, left: number, right: number, bottom: number, top: number, near: number, far: number): Mat4β Generates a frustum matrix with the given bounds.mat4.frustumZO(out: Mat4, left: number, right: number, bottom: number, top: number, near: number, far: number): Mat4β Generates a frustum matrix with the given bounds, suitable for WebGPU.mat4.perspectiveNO(out: Mat4, fovy: number, aspect: number, near: number, far: number): Mat4β Generates a perspective projection matrix with the given bounds.mat4.perspectiveZO(out: Mat4, fovy: number, aspect: number, near: number, far: number): Mat4β Generates a perspective projection matrix suitable for WebGPU with the given bounds.mat4.perspectiveFromFieldOfViewNO(out: Mat4, fov: { upDegrees: number; downDegrees: number; leftDegrees: number; rightDegrees: number; }, near: number, far: number): Mat4β Generates a perspective projection matrix with the given field of view.mat4.perspectiveFromFieldOfViewZO(out: Mat4, fov: { upDegrees: number; downDegrees: number; leftDegrees: number; rightDegrees: number; }, near: number, far: number): Mat4β Generates a perspective projection matrix with the given field of view, suitable for WebGPU.mat4.orthoNO(out: Mat4, left: number, right: number, bottom: number, top: number, near: number, far: number): Mat4β Generates a orthogonal projection matrix with the given bounds.mat4.orthoZO(out: Mat4, left: number, right: number, bottom: number, top: number, near: number, far: number): Mat4β Generates a orthogonal projection matrix with the given bounds.mat4.lookAt(out: Mat4, eye: Vec3, center: Vec3, up: Vec3): Mat4β Generates a look-at matrix with the given eye position, focal point, and up axis.mat4.targetTo(out: Mat4, eye: Vec3, target: Vec3, up: Vec3): Mat4β Generates a matrix that makes something look at something else.mat4.str(a: Mat4): stringβ Returns a string representation of a mat4
Operations
mat4.transpose(out: Mat4, a: Mat4): Mat4β Transpose the values of a mat4mat4.invert(out: Mat4, a: Mat4): Mat4 | nullβ Inverts a mat4mat4.invert3x3(out: Mat4, a: Mat4): Mat4 | nullβ Inverts only the 3x3 rotation part of a mat4.mat4.adjoint(out: Mat4, a: Mat4): Mat4β Calculates the adjugate of a mat4mat4.determinant(a: Mat4): numberβ Calculates the determinant of a mat4mat4.multiply(out: Mat4, a: Mat4, b: Mat4): Mat4β Multiplies two mat4smat4.multiply3x3(out: Mat4, a: Mat4, b: Mat4): Mat4β Multiplies two mat4s treating them as 3x3 rotation matrices.mat4.multiply3x3RightTransposed(out: Mat4, a: Mat4, b: Mat4): Mat4β Multiplies a mat4 by the transpose of another mat4mat4.multiply3x3TransposedVec(out: Vec3, mat: Mat4, vec: Vec3): Vec3β Transform a Vec3 by the transpose of the 3x3 rotation part.mat4.multiply3x3Vec(out: Vec3, mat: Mat4, vec: Vec3): Vec3β Transform a Vec3 by only the 3x3 rotation part of a Mat4.mat4.decompose(out_r: Quat, out_t: Vec3, out_s: Vec3, mat: Mat4): Quatβ Decomposes a transformation matrix into its rotation, translationmat4.frob(a: Mat4): numberβ Returns Frobenius norm of a mat4mat4.add(out: Mat4, a: Mat4, b: Mat4): Mat4β Adds two mat4'smat4.subtract(out: Mat4, a: Mat4, b: Mat4): Mat4β Subtracts matrix b from matrix amat4.multiplyScalar(out: Mat4, a: Mat4, b: number): Mat4β Multiply each element of the matrix by a scalar.mat4.multiplyScalarAndAdd(out: Mat4, a: Mat4, b: Mat4, scale: number): Mat4β Adds two mat4's after multiplying each element of the second operand by a scalar value.
Transform
mat4.crossProductMatrix(out: Mat4, v: Vec3): Mat4β Cross product matrix (skew-symmetric matrix).mat4.translate(out: Mat4, a: Mat4, v: Vec3): Mat4β Translate a mat4 by the given vectormat4.scale(out: Mat4, a: Mat4, v: Vec3): Mat4β Scales the mat4 by the dimensions in the given vec3 not using vectorizationmat4.rotate(out: Mat4, a: Mat4, rad: number, axis: Vec3): Mat4 | nullβ Rotates a mat4 by the given angle around the given axismat4.rotateX(out: Mat4, a: Mat4, rad: number): Mat4β Rotates a matrix by the given angle around the X axismat4.rotateY(out: Mat4, a: Mat4, rad: number): Mat4β Rotates a matrix by the given angle around the Y axismat4.rotateZ(out: Mat4, a: Mat4, rad: number): Mat4β Rotates a matrix by the given angle around the Z axis
Query
mat4.getTranslation(out: Vec3, mat: Mat4): Vec3β Returns the translation vector component of a transformationmat4.getScaling(out: Vec3, mat: Mat4): Vec3β Returns the scaling factor component of a transformationmat4.getRotation(out: Quat, mat: Mat4): Quatβ Returns a quaternion representing the rotational componentmat4.exactEquals(a: Mat4, b: Mat4): booleanβ Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)mat4.equals(a: Mat4, b: Mat4): booleanβ Returns whether or not the matrices have approximately the same elements in the same position.
Aliases
mat4.mul = multiplyβ Alias formultiplymat4.sub = subtractβ Alias forsubtract
import { spherical } from 'math';Create
spherical.create(): Sphericalβ Creates a new spherical coordinate at r=1, theta=0, phi=0spherical.fromValues(r: number, theta: number, phi: number): Sphericalβ Creates a new Spherical initialized with the given valuesspherical.clone(a: Spherical): Sphericalβ Creates a new Spherical initialized with values from an existing onespherical.copy(out: Spherical, a: Spherical): Sphericalβ Copies values from one Spherical to anotherspherical.set(out: Spherical, r: number, theta: number, phi: number): Sphericalβ Sets the components of a Sphericalspherical.setFromVec3(out: Spherical, v: Vec3): Sphericalβ Sets a Spherical from Cartesian Vec3 coordinates (Three.js / OpenGL convention)spherical.makeSafe(out: Spherical, a: Spherical): Sphericalβ Clamps phi to the range [EPSILON, Ο - EPSILON] to avoid coordinatespherical.toVec3(out: Vec3, a: Spherical): Vec3β Converts spherical coordinates to a Cartesian Vec3 (Three.js / OpenGL convention)spherical.fromVec2(out: Spherical, v: Vec2): Sphericalβ Converts a Vec2 (x, z) in the horizontal XZ plane to spherical coordinates.spherical.toVec2(out: Vec2, a: Spherical): Vec2β Projects spherical coordinates onto the XZ plane, returning a Vec2 (x, z).spherical.str(a: Spherical): stringβ Returns a string representation of a Spherical
Operations
spherical.normalize(out: Spherical, a: Spherical): Sphericalβ Sets r=1, preserving the angles. No-op if r is already zero.spherical.scale(out: Spherical, a: Spherical, s: number): Sphericalβ Scales the radial distance r by a scalarspherical.lerp(out: Spherical, a: Spherical, b: Spherical, t: number): Sphericalβ Linearly interpolates between two Spherical coordinates taking the shortest
Query
spherical.equals(a: Spherical, b: Spherical): booleanβ Returns true if two Spherical coordinates are approximately equalspherical.exactEquals(a: Spherical, b: Spherical): booleanβ Returns true if two Spherical coordinates are exactly equal (===).spherical.angleTo(a: Spherical, b: Spherical): numberβ Returns the great-circle angle (in radians) between two spherical coordinates
Aliases
spherical.fromVec3 = setFromVec3β Alias forsetFromVec3
import { polar } from 'math';Create
polar.create(): Polarβ Creates a new polar coordinate at r=1, theta=0polar.fromValues(r: number, theta: number): Polarβ Creates a new Polar initialized with the given valuespolar.clone(a: Polar): Polarβ Creates a new Polar initialized with values from an existing onepolar.copy(out: Polar, a: Polar): Polarβ Copies values from one Polar to anotherpolar.set(out: Polar, r: number, theta: number): Polarβ Sets the components of a Polarpolar.setFromVec2(out: Polar, v: Vec2): Polarβ Sets a Polar from Cartesian Vec2 coordinatespolar.toVec2(out: Vec2, a: Polar): Vec2β Converts polar coordinates to a Cartesian Vec2polar.str(a: Polar): stringβ Returns a string representation of a Polar
Operations
polar.normalize(out: Polar, a: Polar): Polarβ Sets r=1, preserving the angle. No-op on the angle if r is already zero.polar.scale(out: Polar, a: Polar, s: number): Polarβ Scales the radial distance r by a scalarpolar.lerp(out: Polar, a: Polar, b: Polar, t: number): Polarβ Linearly interpolates between two Polar coordinates, taking the shortestpolar.distance(a: Polar, b: Polar): numberβ Returns the straight-line (chord) distance between two polar coordinates
Transform
polar.rotate(out: Polar, a: Polar, rad: number): Polarβ Rotates a Polar by an angle (in radians), wrapping theta into (-pi, pi].
Query
polar.angleTo(a: Polar, b: Polar): numberβ Returns the smallest angle (in radians) between two polar directionspolar.equals(a: Polar, b: Polar): booleanβ Returns true if two Polar coordinates are approximately equalpolar.exactEquals(a: Polar, b: Polar): booleanβ Returns true if two Polar coordinates are exactly equal (===).
Aliases
polar.fromVec2 = setFromVec2β Alias forsetFromVec2
type Box2 = [ minX: number, minY: number, maxX: number, maxY: number ]β An axis-aligned box in 2D space, as [minX, minY, maxX, maxY]type Box3 = [ minX: number, minY: number, minZ: number, maxX: number, maxY: number, maxZ: number ]β A box in 3D spacetype OBB3 = { center: Vec3; halfExtents: Vec3; rotation: Mat3; }β An oriented bounding box in 3D spacetype Plane3 = { normal: Vec3; constant: number; }β A plane in 3D spacetype Sphere = { center: Vec3; radius: number; }β A sphere in 3D spacetype Circle = { center: Vec2; radius: number; }β A circle in 2D spacetype Frustum = [ Plane3, Plane3, Plane3, Plane3, Plane3, Plane3 ]β A view frustum, represented as the six bounding planes of a camera's view volume.type FrustumCorners = [ Vec3, Vec3, Vec3, Vec3, Vec3, Vec3, Vec3, Vec3 ]β The eight corners of a frustum, as returned by corners.
import { box2 } from 'math/shapes';Create
box2.create(): Box2β Create a new empty Box2 with "min" set to positive infinity and "max" set to negative infinitybox2.clone(box: Box2): Box2β Clones a Box2box2.copy(out: Box2, box: Box2): Box2β Copies a Box2 to another Box2box2.set(out: Box2, minX: number, minY: number, maxX: number, maxY: number): Box2β Sets the min and max values of a Box2box2.setFromVectors(out: Box2, min: Vec2, max: Vec2): Box2β Sets the min and max values of a Box2 from Vec2 vectorsbox2.setFromCenterAndSize(out: Box2, center: Vec2, size: Vec2): Box2β Sets the box from a center point and size
Operations
box2.min(out: Vec2, box: Box2): Vec2β Extracts the minimum corner of a Box2box2.max(out: Vec2, box: Box2): Vec2β Extracts the maximum corner of a Box2box2.empty(out: Box2): Box2β Set a Box2 to empty (min to positive infinity, max to negative infinity)box2.expandByPoint(out: Box2, box: Box2, point: Vec2): Box2β Expands a Box2 to include a pointbox2.expandByExtents(out: Box2, box: Box2, vector: Vec2): Box2β Widens a Box2 by a vector on both sidesbox2.expandByMargin(out: Box2, box: Box2, margin: number): Box2β Expands a Box2 uniformly by a scalar margin on all sidesbox2.union(out: Box2, boxA: Box2, boxB: Box2): Box2β Computes the union of two bounding boxesbox2.center(out: Vec2, box: Box2): Vec2β Calculate the center point of a bounding boxbox2.extents(out: Vec2, box: Box2): Vec2β Calculate the extents (half-size) of a bounding boxbox2.size(out: Vec2, box: Box2): Vec2β Calculate the size (dimensions) of a bounding boxbox2.area(box: Box2): numberβ Calculate the area of a bounding boxbox2.scale(out: Box2, box: Box2, scale: Vec2): Box2β Scale a bounding box by a vector, handling non-uniform and negative scaling
Query
box2.exactEquals(a: Box2, b: Box2): booleanβ Returns whether or not the boxes have exactly the same elements in the same position (when compared with ===)box2.equals(a: Box2, b: Box2): booleanβ Returns whether or not the boxes have approximately the same elements in the same positionbox2.containsPoint(box: Box2, point: Vec2): booleanβ Test if a point is contained within the bounding boxbox2.containsBox2(container: Box2, contained: Box2): booleanβ Test if one Box2 completely contains another Box2box2.intersectsBox2(boxA: Box2, boxB: Box2): booleanβ Check whether two bounding boxes intersectbox2.intersectsCircle(box: Box2, circle: Circle): booleanβ Test intersection between an axis-aligned bounding box and a circle.
import { box3 } from 'math/shapes';Create
box3.create(): Box3β Create a new empty Box3 with "min" set to positive infinity and "max" set to negative infinitybox3.clone(box: Box3): Box3β Clones a Box3box3.copy(out: Box3, box: Box3): Box3β Copies a Box3 to another Box3box3.set(out: Box3, minX: number, minY: number, minZ: number, maxX: number, maxY: number, maxZ: number): Box3β Sets the min and max values of a Box3box3.setFromVectors(out: Box3, min: Vec3, max: Vec3): Box3β Sets the min and max values of a Box3 from Vec3 vectorsbox3.setFromCenterAndSize(out: Box3, center: Vec3, size: Vec3): Box3β Sets the box from a center point and size
Operations
box3.min(out: Vec3, box: Box3): Vec3β Extracts the minimum corner of a Box3box3.max(out: Vec3, box: Box3): Vec3β Extracts the maximum corner of a Box3box3.empty(out: Box3): Box3β Set a Box3 to empty (min to positive infinity, max to negative infinity)box3.expandByPoint(out: Box3, box: Box3, point: Vec3): Box3β Expands a Box3 to include a pointbox3.expandByExtents(out: Box3, box: Box3, vector: Vec3): Box3β Widens a Box3 by a vector on both sidesbox3.expandByMargin(out: Box3, box: Box3, margin: number): Box3β Expands a Box3 uniformly by a scalar margin on all sidesbox3.union(out: Box3, boxA: Box3, boxB: Box3): Box3β Computes the union of two bounding boxesbox3.center(out: Vec3, box: Box3): Vec3β Calculate the center point of a bounding boxbox3.extents(out: Vec3, box: Box3): Vec3β Calculate the extents (half-size) of a bounding boxbox3.size(out: Vec3, box: Box3): Vec3β Calculate the size (dimensions) of a bounding boxbox3.surfaceArea(box: Box3): numberβ Calculate the surface area of a bounding boxbox3.scale(out: Box3, box: Box3, scale: Vec3): Box3β Scale a bounding box by a vector, handling non-uniform and negative scaling
Transform
box3.transformMat4(out: Box3, box: Box3, mat: Mat4): Box3β Transform a bounding box by a 4x4 matrix.
Query
box3.exactEquals(a: Box3, b: Box3): booleanβ Returns whether or not the boxes have exactly the same elements in the same position (when compared with ===)box3.equals(a: Box3, b: Box3): booleanβ Returns whether or not the boxes have approximately the same elements in the same positionbox3.containsPoint(box: Box3, point: Vec3): booleanβ Test if a point is contained within the bounding boxbox3.containsBox3(container: Box3, contained: Box3): booleanβ Test if one Box3 completely contains another Box3box3.intersectsBox3(boxA: Box3, boxB: Box3): booleanβ Check whether two bounding boxes intersectbox3.intersectsTriangle3(box: Box3, a: Vec3, b: Vec3, c: Vec3): booleanβ Test whether an axis-aligned bounding box intersects a triangle, via thebox3.intersectsSphere(box: Box3, sphere: Sphere): booleanβ Test intersection between axis-aligned bounding box and a sphere.box3.intersectsPlane3(box: Box3, plane: Plane3): booleanβ Test intersection between axis-aligned bounding box and plane.
import { obb3 } from 'math/shapes';Create
obb3.create(): OBB3obb3.clone(a: OBB3): OBB3obb3.copy(out: OBB3, a: OBB3): OBB3obb3.set(out: OBB3, center: Vec3, halfExtents: Vec3, rotation: Mat3): OBB3β Sets an OBB from center, half extents, and a rotation matrix.obb3.setFromCenterHalfExtentsQuaternion(out: OBB3, center: Vec3, halfExtents: Vec3, q: Quat): OBB3β Sets an OBB from center, half extents, and a quaternion.obb3.setFromBox3(out: OBB3, aabb: Box3): OBB3β Creates an OBB from an axis-aligned bounding box (AABB).
Operations
obb3.clampPoint(out: Vec3, obb: OBB3, point: Vec3): Vec3β Clamps a point to the surface or interior of an OBB.
Transform
obb3.applyMatrix4(out: OBB3, obb: OBB3, matrix: Mat4): OBB3β Applies a 4x4 transformation matrix to an OBB.
Query
obb3.containsPoint(obb: OBB3, point: Vec3): booleanβ Tests whether a point is contained within an OBB.obb3.intersectsOBB3(a: OBB3, b: OBB3, epsilon = EPSILON): booleanβ Tests whether an OBB intersects with another OBB using the Separating Axis Theorem.obb3.intersectsBox3(obb: OBB3, aabb: Box3): booleanβ Tests whether an OBB intersects with an AABB.
import { plane3 } from 'math/shapes';Create
plane3.create(): Plane3β Creates a new plane with normal (0, 1, 0) and constant 0plane3.fromNormalAndConstant(out: Plane3, normal: Vec3, constant: number): Plane3β Creates a plane from a normal and constantplane3.fromNormalAndPoint(out: Plane3, normal: Vec3, point: Vec3): Plane3β Creates a plane from a normal and a point on the planeplane3.fromCoplanarPoints(out: Plane3, a: Vec3, b: Vec3, c: Vec3): Plane3β Creates a plane from three coplanar pointsplane3.clone(plane: Plane3): Plane3β Clones a planeplane3.copy(out: Plane3, plane: Plane3): Plane3β Copies one plane to another
Operations
plane3.normalize(out: Plane3, plane: Plane3): Plane3β Normalizes a plane (ensures the normal vector is unit length)plane3.negate(out: Plane3, plane: Plane3): Plane3β Negates a plane (flips the normal and constant)plane3.offset(out: Plane3, plane: Plane3, distance: number): Plane3β Offsets a plane by a distance along its normalplane3.distanceToPoint(plane: Plane3, point: Vec3): numberβ Calculates the signed distance from a point to the planeplane3.projectPoint(out: Vec3, plane: Plane3, point: Vec3): Vec3β Projects a point onto the planeplane3.intersect(out: Vec3, p1: Plane3, p2: Plane3, p3: Plane3): booleanβ Finds the intersection point of three planes
Transform
plane3.transform(out: Plane3, plane: Plane3, matrix: Mat4): Plane3β Transforms a plane by a 4x4 matrix
Query
plane3.intersectsSphere(plane: Plane3, sphere: Sphere): booleanβ Tests if a sphere intersects the planeplane3.exactEquals(a: Plane3, b: Plane3): booleanβ Tests if two planes are exactly equalplane3.equals(a: Plane3, b: Plane3): booleanβ Tests if two planes are equal
import { sphere } from 'math/shapes';Create
sphere.create(): Sphereβ Creates a new sphere with a default center 0,0,0 and radius 1
Query
sphere.containsPoint(sphere: Sphere, point: Vec3): booleanβ Returns true if a point lies inside (or on the surface of) the sphere.
import { circle } from 'math/shapes';circle.create(): Circle
import { segment2 } from 'math/shapes';segment2.closestPoint(out: Vec2, point: Vec2, a: Vec2, b: Vec2): Vec2β Calculates the closest point on a line segment to a given pointsegment2.intersects(a: Vec2, b: Vec2, c: Vec2, d: Vec2): booleanβ Tests whether the two closed segments a-b and c-d intersect. Collinearsegment2.intersection(out: Vec2, a: Vec2, b: Vec2, c: Vec2, d: Vec2): Vec2 | nullβ Computes the intersection point of the two closed segments a-b and c-d
import { polygon2 } from 'math/shapes';Operations
polygon2.signedArea(vertices: number[], n: number): numberβ Returns the signed area of the polygon using the shoelace formula.polygon2.area(vertices: number[], n: number): numberβ Returns the (non-negative) area of the polygon.polygon2.centroid(out: Vec2, vertices: number[], n: number): Vec2β Computes the area-weighted centroid (center of mass) of the polygon.polygon2.perimeter(vertices: number[], n: number): numberβ Returns the perimeter (sum of edge lengths) of the polygon.polygon2.winding(vertices: number[], n: number): numberβ Returns the winding order of the polygon from the sign of its signed areapolygon2.reverse(out: number[], vertices: number[], n: number): number[]β Reverses the winding order of the polygon, writing the result intoout.polygon2.bounds(out: Box2, vertices: number[], n: number): Box2β Writes the axis-aligned bounding box of the polygon intooutas a Box2polygon2.closestPoint(out: Vec2, vertices: number[], n: number, point: Vec2): Vec2β Finds the point on the polygon's boundary closest topointand writes it topolygon2.signedDistance(vertices: number[], n: number, point: Vec2): numberβ Returns the distance frompointto the polygon's boundary, signed so thatpolygon2.overlapConvex(verticesA: number[], numA: number, verticesB: number[], numB: number): booleanβ Tests whether two convex polygons overlap, using the separating axis theorem.
Query
polygon2.containsPoint(vertices: number[], n: number, point: Vec2): booleanβ Tests whether a point lies inside the polygon. Works for both convex andpolygon2.isConvex(vertices: number[], n: number): booleanβ Tests whether the polygon is convex. Works for both winding orders. Assumes apolygon2.isReflexVertex(vertices: number[], n: number, i: number): booleanβ Tests whether vertexiis a reflex (concave) vertex of the polygon β thepolygon2.intersectsSegment(vertices: number[], n: number, a: Vec2, b: Vec2): booleanβ Tests whether the segmenta-bintersects the polygon, i.e. it has an
import { triangle2 } from 'math/shapes';Operations
triangle2.signedArea(a: Vec2, b: Vec2, c: Vec2): numberβ Returns the signed area of the triangle (a, b, c). The result is positive whentriangle2.area(a: Vec2, b: Vec2, c: Vec2): numberβ Returns the (non-negative) area of the triangle (a, b, c).triangle2.centroid(out: Vec2, a: Vec2, b: Vec2, c: Vec2): Vec2β Computes the centroid of the triangle (a, b, c).triangle2.bounds(out: Box2, a: Vec2, b: Vec2, c: Vec2): Box2β Computes the axis-aligned bounding box of the triangle (a, b, c).
Query
triangle2.containsPoint(a: Vec2, b: Vec2, c: Vec2, point: Vec2): booleanβ Tests whether a point lies inside the triangle (a, b, c). Works for either
import { triangle3 } from 'math/shapes';triangle3.bounds(out: Box3, a: Vec3, b: Vec3, c: Vec3): Box3β Computes the axis-aligned bounding box of a triangle defined by three vertices.triangle3.normal(out: Vec3, a: Vec3, b: Vec3, c: Vec3): Vec3β Computes the normal vector of a triangle defined by three vertices.triangle3.centroid(out: Vec3, a: Vec3, b: Vec3, c: Vec3): Vec3β Computes the centroid of a triangle defined by three vertices.
import { raycast3 } from 'math/shapes';Types
type IntersectsTriangleResult = { fraction: number; hit: boolean; frontFacing: boolean; }β Result of a ray-triangle intersection test
Operations
raycast3.createIntersectsTriangleResult(): IntersectsTriangleResultβ Creates a new IntersectsTriangleResult with default values.
Query
raycast3.intersectsTriangle(out: IntersectsTriangleResult, origin: Vec3, direction: Vec3, length: number, a: Vec3, b: Vec3, c: Vec3, backfaceCulling: boolean): voidβ Ray-triangle intersection test.raycast3.intersectsBox3(origin: Vec3, direction: Vec3, length: number, aabb: Box3): booleanβ Test if a ray intersects an axis-aligned bounding box.
import { frustum } from 'math/shapes';Create
frustum.create(): Frustumβ Creates a new frustum of zeroed planes.frustum.clone(f: Frustum): Frustumβ Clones a frustum.frustum.copy(out: Frustum, f: Frustum): Frustumβ Copies one frustum to another.frustum.setFromViewProjectionMatrixNO(out: Frustum, proj: Mat4, view: Mat4): Frustumβ Extracts the six planes of a view frustum from a projection and view matrix, using thefrustum.setFromViewProjectionMatrixZO(out: Frustum, proj: Mat4, view: Mat4): Frustumβ Extracts the six planes of a view frustum from a projection and view matrix, using thefrustum.setFromViewProjectionMatrixSides(out: Frustum, proj: Mat4, view: Mat4): Frustumβ Extracts only the four lateral planes (left, right, bottom, top) of a view frustum from a
Operations
frustum.sidesIntersectsSphere(f: Frustum, s: Sphere): booleanβ Tests if a sphere intersects the lateral planes of a sides-only frustum, skipping near and far.frustum.sidesIntersectsBox3(f: Frustum, box: Box3): booleanβ Tests if an axis-aligned box intersects the lateral planes of a sides-only frustum, using thefrustum.sidesContainsPoint(f: Frustum, p: Vec3): booleanβ Tests if a point is inside the lateral planes of a sides-only frustum, skipping near and far.frustum.sidesIntersectsRay(f: Frustum, origin: Vec3, direction: Vec3): booleanβ Tests if a ray intersects the lateral planes of a sides-only frustum, using a slab test over thefrustum.corners(out: FrustumCorners, f: Frustum): FrustumCornersβ Computes the eight corners of the frustum by intersecting three planes each.
Query
frustum.intersectsSphere(f: Frustum, s: Sphere): booleanβ Tests if a sphere intersects the frustum.frustum.intersectsBox3(f: Frustum, box: Box3): booleanβ Tests if an axis-aligned box intersects the frustum, using the p-vertex test.frustum.containsPoint(f: Frustum, p: Vec3): booleanβ Tests if a point is inside the frustum.frustum.intersectsRay(f: Frustum, origin: Vec3, direction: Vec3): booleanβ Tests if a ray intersects the frustum, using a slab test over the planes.
circumcircle(out: Circle, a: Vec2, b: Vec2, c: Vec2): Circleβ Calculates the circumcircle of three points and stores the center in the output parameter.decomposePolygon2Quick(vertices: number[], n: number): number[][]β Decomposes a simple polygon into convex sub-polygons using Bayazit's fastdecomposePolygon2Quality(vertices: number[], n: number): number[][]β Decomposes a simple polygon into the (near-)minimum number of convextriangulatePolygon2(out: number[], vertices: number[], n: number): numberβ Triangulates a simple polygon by ear clipping, writing triangle indices intoquickhull2(points: number[]): number[]β Computes the convex hull of a set of 2D points using the QuickHull algorithm.quickhull3(points: number[]): number[]β Computes the convex hull of a set of 3D points using an incremental QuickHull algorithm.
type Spring<T> = { value: T; velocity: T; }β Spring state: avalueand itsvelocity, of matching rank
import { easing } from 'math/time';easing.exp(t: number)easing.linear(t: number)easing.sineIn(x: number)easing.sineOut(x: number)easing.sineInOut(x: number)easing.cubicIn(x: number)easing.cubicOut(x: number)easing.cubicInOut(x: number)easing.quintIn(x: number)easing.quintOut(x: number)easing.quintInOut(x: number)easing.circIn(x: number)easing.circOut(x: number)easing.circInOut(x: number)easing.quartIn(t: number)easing.quartOut(t: number)easing.quartInOut(t: number)easing.expoIn(x: number)easing.expoOut(x: number)easing.expoInOut(x: number)easing.rsqw(t: number, delta = 0.01, a = 1, f = 1 / (2 * Math.PI))
import { spring } from 'math/time';Create
spring.create(value = 0): Spring<number>β Creates a scalar spring atvalue, at rest.spring.fromResponse(response: number): numberβ Converts a SwiftUI-styleresponseβ the spring's natural period, in seconds
Operations
spring.update(state: Spring<number>, target: number, smoothTime: number, dampingRatio: number, delta: number): Spring<number>β Springsstate.valuetowardtarget, mutatingstatein place. Returns it.spring.damp(state: Spring<number>, target: number, smoothTime: number, delta: number): Spring<number>β Critically-damped update (dampingRatio = 1): moves towardtargetasspring.dampAngle(state: Spring<number>, target: number, smoothTime: number, delta: number): Spring<number>β Like damp, but takes the shortest angular path totarget(radians)
import { spring2 } from 'math/time';Create
spring2.create(value: Vec2 = [0, 0]): Spring<Vec2>β Creates a Vec2 spring atvalue(copied), at rest.
Operations
spring2.update(state: Spring<Vec2>, target: Vec2, smoothTime: number, dampingRatio: number, delta: number): Spring<Vec2>β Springsstate.valuetowardtarget, mutatingstatein place. Returns it.spring2.damp(state: Spring<Vec2>, target: Vec2, smoothTime: number, delta: number): Spring<Vec2>β Critically-damped Vec2 spring (dampingRatio = 1). See update.
import { spring3 } from 'math/time';Create
spring3.create(value: Vec3 = [0, 0, 0]): Spring<Vec3>β Creates a Vec3 spring atvalue(copied), at rest.
Operations
spring3.update(state: Spring<Vec3>, target: Vec3, smoothTime: number, dampingRatio: number, delta: number): Spring<Vec3>β Springsstate.valuetowardtarget, mutatingstatein place. Returns it.spring3.damp(state: Spring<Vec3>, target: Vec3, smoothTime: number, delta: number): Spring<Vec3>β Critically-damped Vec3 spring (dampingRatio = 1). See update.
import { spring4 } from 'math/time';Create
spring4.create(value: Vec4 = [0, 0, 0, 0]): Spring<Vec4>β Creates a Vec4 spring atvalue(copied), at rest.
Operations
spring4.update(state: Spring<Vec4>, target: Vec4, smoothTime: number, dampingRatio: number, delta: number): Spring<Vec4>β Springsstate.valuetowardtarget, mutatingstatein place. Returns it.spring4.damp(state: Spring<Vec4>, target: Vec4, smoothTime: number, delta: number): Spring<Vec4>β Critically-damped Vec4 spring (dampingRatio = 1). See update.
type Isaac32 = { m: Uint32Array; r: Uint32Array; a: number; b: number; c: number; i: number; }β State of an ISAAC-32 PRNG: two 256-word arrays plus three accumulators and atype Isaac64 = { mHi: Uint32Array; mLo: Uint32Array; rHi: Uint32Array; rLo: Uint32Array; aHi: number; aLo: number; bHi: number; bLo: number; cHi: number; cLo: number; i: number; }β State of an ISAAC64 PRNG. Create one with create.type Mulberry32 = { a: number; }β State of a Mulberry32 PRNG: a single 32-bit accumulator that sampletype RandomGenerator = () => numberβ A function that returns a random number in the range [0, 1).
import { isaac32 } from 'math/random';Create
isaac32.create(seed = 0): Isaac32β Creates ISAAC-32 PRNG state seeded withseed.
Operations
isaac32.next(state: Isaac32): numberβ Advancesstateand returns the next raw 32-bit unsigned integer.isaac32.sample(state: Isaac32): numberβ Advancesstateand returns the next number in the range [0, 1).isaac32.seed(): numberβ Generates a random 32-bit unsigned integer seed, suitable for use with
import { isaac64 } from 'math/random';Create
isaac64.create(seed: bigint = 0n): Isaac64β Creates ISAAC64 PRNG state seeded withseed.
Operations
isaac64.next(state: Isaac64): bigintβ Advancesstateand returns the next raw 64-bit unsigned integer.isaac64.sample(state: Isaac64): numberβ Advancesstateand returns the next number in the range [0, 1).isaac64.seed(): bigintβ Generates a random 64-bit unsigned integer seed, suitable for use with
import { mulberry32 } from 'math/random';Create
mulberry32.create(seed: number): Mulberry32β Creates Mulberry32 PRNG state seeded withseed.
Operations
mulberry32.next(state: Mulberry32): numberβ Advancesstateand returns the next raw 32-bit unsigned integer.mulberry32.sample(state: Mulberry32): numberβ Advancesstateand returns the next number in the range [0, 1).mulberry32.seed(): numberβ Generates a random 32-bit unsigned integer seed, suitable for use with
import { random } from 'math/random';random.float(random: RandomGenerator, min: number, max: number): numberβ Returns a random float in the range [min, max).random.int(random: RandomGenerator, min: number, max: number): numberβ Returns a random integer in the range [min, max] (inclusive).random.bool(random: RandomGenerator, chance = 0.5): booleanβ Returns a random boolean.random.sign(random: RandomGenerator, plusChance = 0.5): numberβ Returns a random sign, either 1 or -1.random.choice<T>(random: RandomGenerator, items: T[]): Tβ Returns a random item from an array.random.vec2(out: Vec2, random: RandomGenerator): Vec2β Writes a random unit-length Vec2 into out.random.vec3(out: Vec3, random: RandomGenerator): Vec3β Writes a random unit-length Vec3 into out.random.vec4(out: Vec4, random: RandomGenerator): Vec4β Writes a random unit-length Vec4 into out.random.quat(out: Quat, random: RandomGenerator): Quatβ Writes a random unit quaternion into out.
Types
type Permutation = { perm: number[]; gradP: Vec3[]; gradP4: Vec4[]; }β Seeded permutation and gradient tables that back a noise generator.
Operations
fbm(sample: (frequency: number) => number, octaves: number, lacunarity: number, gain: number): numberβ Fractional Brownian motion: sums octaves of a noise source at increasingridged(sample: (frequency: number) => number, octaves: number, lacunarity: number, gain: number): numberβ Ridged multifractal: like fbm, but each octave is folded tobillow(sample: (frequency: number) => number, octaves: number, lacunarity: number, gain: number): numberβ Billow noise: like fbm, but each octave is folded to2*abs(noise) - 1domainWarp2(out: Vec2, sample: (x: number, y: number) => number, x: number, y: number, amount = 1): Vec2β Domain warping (2D): offsets a point by a noise-derived vector, so feeding thedomainWarp3(out: Vec3, sample: (x: number, y: number, z: number) => number, x: number, y: number, z: number, amount = 1): Vec3β Domain warping (3D): offsets a point by a noise-derived vector so a noisecurl2(out: Vec2, sample: (x: number, y: number) => number, x: number, y: number, eps = 1e-4): Vec2β Curl of a 2D scalar noise potential - a divergence-free (incompressible) flowcurl3(out: Vec3, sample: (x: number, y: number, z: number) => number, x: number, y: number, z: number, eps = 1e-4): Vec3β Curl of a 3D noise vector potential - a divergence-free 3D flow field for
import { perlin2d } from 'math/noise';Types
type Perlin2DGenerator = Permutationβ A seeded 2D Perlin noise generator. Create one with create.
Create
perlin2d.create(seed: number): Perlin2DGeneratorβ Creates a 2D Perlin noise generator with the given seed.
Operations
perlin2d.sample({ perm, gradP }: Perlin2DGenerator, x: number, y: number): numberβ Samples 2D Perlin noise.
import { perlin3d } from 'math/noise';Types
type Perlin3DGenerator = Permutationβ A seeded 3D Perlin noise generator. Create one with create.
Create
perlin3d.create(seed: number): Perlin3DGeneratorβ Creates a 3D Perlin noise generator with the given seed.
Operations
perlin3d.sample({ perm, gradP }: Perlin3DGenerator, x: number, y: number, z: number): numberβ Samples 3D Perlin noise.
import { simplex2d } from 'math/noise';Types
type Simplex2DGenerator = Permutationβ A seeded 2D simplex noise generator. Create one with create.
Create
simplex2d.create(seed: number): Simplex2DGeneratorβ Creates a 2D simplex noise generator with the given seed.
Operations
simplex2d.sample({ perm, gradP }: Simplex2DGenerator, x: number, y: number): numberβ Samples 2D simplex noise, returning a value in the interval [-1, 1].
import { simplex3d } from 'math/noise';Types
type Simplex3DGenerator = Permutationβ A seeded 3D simplex noise generator. Create one with create.
Create
simplex3d.create(seed: number): Simplex3DGeneratorβ Creates a 3D simplex noise generator with the given seed.
Operations
simplex3d.sample({ perm, gradP }: Simplex3DGenerator, x: number, y: number, z: number): numberβ Samples 3D simplex noise, returning a value in the interval [-1, 1].
import { simplex4d } from 'math/noise';Types
type Simplex4DGenerator = Permutationβ A seeded 4D simplex noise generator. Create one with create.
Create
simplex4d.create(seed: number): Simplex4DGeneratorβ Creates a 4D simplex noise generator with the given seed.
Operations
simplex4d.sample({ perm, gradP4 }: Simplex4DGenerator, x: number, y: number, z: number, w: number): numberβ Samples 4D simplex noise, returning a value in the interval [-1, 1].
import { worley2d } from 'math/noise';Types
type Worley2DGenerator = Permutationβ A seeded 2D Worley (cellular) noise generator. Create one with create.
Create
worley2d.create(seed: number): Worley2DGeneratorβ Creates a 2D Worley noise generator with the given seed.
Operations
worley2d.sample({ perm }: Worley2DGenerator, x: number, y: number): numberβ Samples 2D Worley (cellular) noise: the Euclidean distance to the nearest of a
import { worley3d } from 'math/noise';Types
type Worley3DGenerator = Permutationβ A seeded 3D Worley (cellular) noise generator. Create one with create.
Create
worley3d.create(seed: number): Worley3DGeneratorβ Creates a 3D Worley noise generator with the given seed.
Operations
worley3d.sample({ perm }: Worley3DGenerator, x: number, y: number, z: number): numberβ Samples 3D Worley (cellular) noise: the Euclidean distance to the nearest of a
type Color = [ r: number, g: number, b: number ]β A linear-sRGB color: [r, g, b] floats in [0, 1].type ColorInput = string | number | [ number, number, number ]β Accepted input types for creating or parsing a Color.type HSL = [ hue: number, saturation: number, lightness: number ]β A hue-saturation-lightness color: [h, s, l], all in [0, 1] (hue wraps).
import { color } from 'math/color';Create
color.create(): Colorβ Create a new Color initialized to black [0, 0, 0].color.fromValues(r: number, g: number, b: number): Colorβ Create a new Color with the given linear r, g, b values.color.clone(c: Color): Colorβ Create a new Color that is a copy ofc.color.copy(out: Color, src: Color): Colorβ Copy the values fromsrcintoout. Returnsout.color.set(out: Color, r: number, g: number, b: number): Colorβ Set the linear r, g, b components ofoutdirectly. Returnsout.color.setScalar(out: Color, s: number): Colorβ Set all three channels ofoutto the same linear values(a gray). Returnsout.color.setFromSRGB(out: Color, srgb: [ number, number, number ]): Colorβ Setoutfrom an sRGB gamma-encoded [r, g, b] array with values in [0, 1].color.fromSRGB(srgb: [ number, number, number ]): Colorβ Create a new Color from an sRGB gamma-encoded [r, g, b] array with values in [0, 1].color.toSRGB(out: [ number, number, number ], c: Color): [ number, number, number ]β Write the sRGB gamma-encoded [r, g, b] of a linear Color intoout(values [0, 1]).color.toCSS(c: Color): stringβ Create a CSSrgb(...)string in sRGB gamma space (for HTML/canvas use).color.toHex(c: Color): numberβ Convert to a 0xRRGGBB integer in sRGB gamma space.color.toHexString(c: Color): stringβ Convert to a 6-digit sRGB hex string without a leading '#', e.g. 'ff8800'.
Operations
color.add(out: Color, a: Color, b: Color): Colorβ Adda + bcomponent-wise intoout. Returnsout.color.addScalar(out: Color, a: Color, s: number): Colorβ Add scalarsto each channel ofaintoout. Returnsout.color.sub(out: Color, a: Color, b: Color): Colorβ Subtracta - bcomponent-wise intoout. Returnsout.color.multiply(out: Color, a: Color, b: Color): Colorβ Multiplya * bcomponent-wise intoout(tinting). Returnsout.color.multiplyScalar(out: Color, a: Color, s: number): Colorβ Scale each channel ofabysintoout(brightness). Returnsout.color.lerp(out: Color, a: Color, b: Color, t: number): Colorβ Linearly interpolate fromatobbytintoout(physically-correct blend). Returnsout.color.clamp(out: Color, c: Color): Colorβ Clamp each channel ofcto [0, 1] intoout. Returnsout.
Query
color.equals(a: Color, b: Color, epsilon = 0): booleanβ Whetheraandbare equal, within an optional per-channelepsilon(default exact).color.luminance(c: Color): numberβ Relative luminance in [0, 1] (Rec. 709 weights, on linear light).
import { colorspace } from 'math/color';colorspace.srgbToLinear(c: number): numberβ Convert a single sRGB gamma-encoded channel [0, 1] to linear light [0, 1].colorspace.linearToSrgb(c: number): numberβ Convert a single linear light channel [0, 1] to sRGB gamma-encoded [0, 1].colorspace.linearSrgbToLinearDisplayP3(out: Color, c: Color): Colorβ Convert a linear-sRGB Color to linear Display-P3 primaries, intoout. Returnsout.colorspace.linearDisplayP3ToLinearSrgb(out: Color, c: Color): Colorβ Convert a linear Display-P3 Color to linear-sRGB primaries, intoout. Returnsout.
import { hsl } from 'math/color';Create
hsl.create(): HSLβ Create a new HSL initialized to [0, 0, 0] (black).hsl.fromValues(h: number, s: number, l: number): HSLβ Create a new HSL with the given h, s, l values (all in [0, 1]).hsl.clone(a: HSL): HSLβ Create a new HSL that is a copy ofa.hsl.copy(out: HSL, src: HSL): HSLβ Copy the values fromsrcintoout. Returnsout.hsl.set(out: HSL, h: number, s: number, l: number): HSLβ Set the h, s, l components ofoutdirectly. Returnsout.hsl.fromColor(out: HSL, c: Color): HSLβ Write the HSL of a linear Color intoout. Returnsout.hsl.toColor(out: Color, a: HSL): Colorβ Write the linear Color of an HSL intoout. Returnsout.
Operations
hsl.lerp(out: HSL, a: HSL, b: HSL, t: number): HSLβ Interpolate fromatobbytintoout, taking the shortest path aroundhsl.offset(out: HSL, a: HSL, dh: number, ds: number, dl: number): HSLβ Offsetaby (dh, ds, dl) intoout: hue wraps into [0, 1), saturation and