Math Library

350+ pure functions for vectors, matrices, quaternions, geometric primitives, GPU data packing, and interpolation. All exported via EngineBootstrap.js and MathImports.js.

Design Philosophy

Module Map

ModuleFunctionsPurpose
MathVec3.js~303D vectors: add, sub, scale, dot, cross, normalize, lerp, reflect, project, smoothDamp
MathVec2.js~122D vectors: add, sub, scale, dot, length, normalize, lerp, distance
MathQuat.js~15Quaternions: slerp, fromAxisAngle, fromEuler, lookAt, multiply, inverse
MathMat4.js~154×4 matrices: perspective, orthographic, lookAt, inverse, multiply, fromRotationTranslation
MathScalar.js~8Scalar: clamp, lerp, smoothstep, saturate, inverseLerp, remap
MathRay.js~20Rays: create, intersect (sphere, AABB, plane, triangle, capsule, OBB), transform
MathPlane.js~20Planes: create, distance, project, intersect (ray, segment, plane), transform
MathLine3.js~15Line segments: create, closest point (to point, segment, ray), distance
MathRect2.js~252D rectangles: create, contains, intersects, merge, grow, clamp
MathDualQuat.js~20Dual quaternions: create, multiply, sclerp, transform point, toMat4 (for DQS skinning)
MathPacking.js~20GPU packing: half-float, UNORM/SNORM, RGB9E5, R11G11B10F, octahedral normals
MathGeometry.js~20Geometry helpers: AABB, sphere, frustum, distance calculations
MathColor.js~10Color: HSL↔RGB, temperature→RGB, sRGB↔linear
MathCurves.js~15Curves: bezier, catmull-rom, hermite, arc-length parameterization
MathNoise.js~10Noise: simplex2D/3D, perlin, FBM, curl noise

Quick Examples

Vectors

import { vec3, vec3Add, vec3Normalize, vec3Cross, vec3Dot } from './engine/MathImports.js';

const a = vec3(1, 0, 0);
const b = vec3(0, 1, 0);
const sum = vec3Add(a, b);        // → [1, 1, 0]
const up = vec3Cross(a, b);       // → [0, 0, 1]
const dot = vec3Dot(a, b);        // → 0
const n = vec3Normalize(sum);     // → [0.707, 0.707, 0]

Matrices

import { mat4PerspectiveRadWebGPU, mat4LookAt, mat4Multiply } from './engine/MathImports.js';

// WebGPU projection (Z range [0,1])
const proj = mat4PerspectiveRadWebGPU(Math.PI/4, 16/9, 0.1, 1000);
const view = mat4LookAt([0,5,10], [0,0,0], [0,1,0]);
const vp = mat4Multiply(proj, view);

GPU Packing

import { floatToHalf, packOctNormal, packRGB9E5 } from './engine/core/math/MathPacking.js';

const half = floatToHalf(3.14);            // → uint16
const oct = packOctNormal([0, 1, 0]);     // → [sn8, sn8]
const hdr = packRGB9E5([5.0, 2.0, 0.1]); // → uint32

Ray Intersection

import { rayCreate, rayIntersectSphere, rayIntersectAABB } from './engine/core/math/MathRay.js';

const ray = rayCreate([0,0,5], [0,0,-1]);
const hit = rayIntersectSphere(ray, [0,0,0], 1.0);
// → { t: 4.0, point: [0,0,1], normal: [0,0,1] } or null