Animate angles with `dampAngle`
maindampAngle function is a wrapper around damp that uses deltaAngle to ensure the animation takes the shortest path between two angles, preventingrepository·main·Indexed 21 days ago
https://github.com/pmndrs/maathA mathematical utility library providing high-performance random number generators, geometric distributions, and noise functions for graphics and simulations. It includes specialized tools for TypedArray buffer manipulation, physics-based damping for Three.js objects, standard easing functions, and geometry utilities such as RoundedPlaneGeometry and UV mapping (cylindrical, spherical, and box).
dampAngle function is a wrapper around damp that uses deltaAngle to ensure the animation takes the shortest path between two angles, preventingComputes the average position (centroid) of all points in the buffer.
myBuffer: The TypedArray to process.stride: The number of elements per point (2 or 3).Returns a V2 or V3 representing the center.
// Example: Get the center of 3D points
const centerPoint = center(myBuffer, 3);Use dot(a, b) to calculate the dot product of two vectors, returning a scalar. Use cross(a, b) to calculate the cross product of two vectors, returning a new V3 perpendicular to both inputs.
import { dot, cross } from 'maath/vector3';
const a = [1, 0, 0];
const b = [0, 1, 0];
dot(a, b); // 0
cross(a, b); // [0, 0, 1]The vectorEquals function checks if two 3D vectors are approximately equal, accounting for floating-point errors using an epsilon value.
// Returns true if vectors are within Number.EPSILON of each other
vectorEquals(vectorA, vectorB);
// Custom epsilon
vectorEquals(vectorA, vectorB, 0.0001);The inSphere function populates a TypedArray with random 3D coordinates located within the volume of a sphere. You can specify the radius and the center (as an array of 3 numbers).
import { inSphere } from '@maath/core/random';
const buffer = new Float32Array(30);
const sphere = { radius: 2, center: [1, 1, 1] };
inSphere(buffer, sphere);The getMinor function calculates the determinant of a submatrix (minor) derived from a Matrix4 by removing a specific row r and column c.
Note: The function uses 1-based indexing for the row r and column c parameters (e.g., to remove the first row, pass 1).
import { Matrix4 } from "three";
import { getMinor } from "maath/matrix";
const m = new Matrix4();
// ... populate matrix
// Get the determinant of the 3x3 matrix remaining after
// removing row 1 and column 1
const minorDet = getMinor(m, 1, 1);Rotates all points in a buffer using a quaternion. Rotation is performed around a specified center.
buffer: The TypedArray to mutate.rotation: An object containing:q: A three Quaternion representing the rotation.center (optional): An array [x, y, z] representing the pivot point. Defaults to [0, 0, 0].import { Quaternion } from 'three';
// Example: Rotate buffer around the origin
rotate(myBuffer, { q: new Quaternion().setFromAxisAngle(new Vector3(0, 1, 0), Math.PI / 2) });Use these functions for geometric calculations:
dot(a, b): Returns the dot product of two vectors.length(a): Returns the magnitude (length) of the vector. This involves a square root operation.lengthSqr(a): Returns the squared length of the vector. Performance Tip: Use lengthSqr instead of length when comparing the relative lengths of vectors to avoid the computational cost of Math.sqrt.import { dot, length, lengthSqr } from 'maath/vector2';
const a = [3, 4];
const b = [1, 2];
const d = dot(a, b); // 11
const l = length(a); // 5
const l2 = lengthSqr(a); // 25The easing module provides several standard easing functions used to control the rate of change in animations. These functions typically take a value t (usually between 0 and 1) and return a transformed value.
Available easing types:
linear: No change in rate.exp: Exponential easing.rsqw: A custom easing function.sine: Sine-based easing with in, out, and inOut variants.cubic: Cubic-based easing with in, out, and inOut variants.quint: Quintic-based easing with in, out, and inOut variants.circ: Circular-based easing with in, out, and inOut variants.quart: Quartic-based easing with in, out, and inOut variants.expo: Exponential-based easing with in, out, and inOut variants.import { linear, sine, cubic, quint, circ, quart, expo, exp, rsqw } from '@maath/easing';
// Example usage of a sine in-out easing
const easedValue = sine.inOut(0.5); // 0.5A collection of interpolation utilities:
lerp(v0, v1, t): Linear interpolation between v0 and v1 by factor t.inverseLerp(v0, v1, t): Finds the linear parameter t that produces t within the range [v0, v1].fade(t): An ease-in-out function (smoothstep-like) that goes to -Infinite before 0 and Infinite after 1.lerp(0, 10, 0.5); // 5
inverseLerp(0, 10, 5); // 0.5
fade(0.5);Use matrixSum3 to perform element-wise addition of two Matrix3 objects. It returns a new Matrix3 instance representing the sum.
import { Matrix3 } from "three";
import { matrixSum3 } from "maath/matrix";
const m1 = new Matrix3().set(1, 0, 0, 0, 1, 0, 0, 0, 1);
const m2 = new Matrix3().set(2, 2, 2, 2, 2, 2, 2, 2, 2);
const result = matrixSum3(m1, m2);
// result is a Matrix3 with elements:
// | 3 2 2 |
// | 2 3 2 |
// | 2 2 3 |Use isPointInTriangle to determine if a 2D point is contained within the boundaries of a given triangle. The function uses a matrix determinant approach to perform the check.
import { isPointInTriangle } from '@maath/geometry';
const triangle: Triangle = [[0, 0], [1, 0], [0, 1]];
const point = [0.2, 0.2];
const isInside = isPointInTriangle(point, triangle);