Capítulo 796 de 859
Three.js ships a set of general-purpose math/data utility functions available globally (mirroring THREE.MathUtils in the classic API). This chapter curates the genuinely reusable ones; the source doc also bundles dozens of one-off helpers written for specific official examples (e.g. procedural city/forest generator demos) — those are excluded here as non-general-purpose.
| Function | Signature | Description |
|---|---|---|
clamp | clamp(value, min, max) : number | Clamps a value between min and max. |
lerp | lerp(x, y, t) : number | Linear interpolation; t=0 → x, t=1 → y. |
damp | damp(x, y, lambda, dt) : number | Frame-rate-independent spring-like interpolation toward y. |
inverseLerp | inverseLerp(x, y, value) : number | Inverse of lerp — returns the [0,1] fraction of value between x and y. |
mapLinear | mapLinear(x, a1, a2, b1, b2) : number | Remaps x from range [a1,a2] to [b1,b2]. |
pingpong | pingpong(x, length=1) : number | Alternates a value between 0 and length. |
euclideanModulo | euclideanModulo(n, m) : number | Modulo that stays positive (unlike JS % for negative n). |
degToRad / radToDeg | (value) : number | Angle unit conversion. |
randFloat | randFloat(low, high) : number | Random float in [low, high]. |
randFloatSpread | randFloatSpread(range) : number | Random float in [-range/2, range/2]. |
randInt | randInt(low, high) : number | Random integer in [low, high]. |
seededRandom | seededRandom(seed) : number | Deterministic pseudo-random float in [0,1]. |
isPowerOfTwo | isPowerOfTwo(value) : boolean | Checks if a number is a power of two. |
ceilPowerOfTwo / floorPowerOfTwo | (value) : number | Nearest power-of-two, rounded up/down. |
generateUUID | generateUUID() : string | RFC-4122-style UUID. |
normalize / denormalize | (value, array : TypedArray) : number | Converts between a typed array's integer range and [0,1] float. |
isTypedArray | isTypedArray(array) : boolean | Type check for any TypedArray. |
convertArray | convertArray(array, type) : TypedArray | Converts an array to a specific TypedArray type. |
fromHalfFloat | fromHalfFloat(val) : number | FP16 → FP32 conversion. |
setQuaternionFromProperEuler | (q, a, b, c, order) | Sets a quaternion from intrinsic proper Euler angles (rarely needed directly — most code uses Quaternion.setFromEuler). |
clamp/lerp: these ship built-in and are already used internally by three.js, so behavior matches the rest of the engine.randFloat/randInt are seeded/deterministic: only seededRandom is. Use it for reproducible procedural generation.'three' or three/src/math/MathUtils.js depending on version), not as static methods you need to look up per-class.damp is the frame-rate-independent alternative to naive lerp(a, b, 0.1) per-frame smoothing — prefer it for camera/object following.isPowerOfTwo/ceilPowerOfTwo relate directly to texture dimension constraints.damp/lerp/pingpong are common building blocks for custom animation logic layered on top of clips.