Capítulo 796 de 859

Chapter 796: Global Utility Functions

Core Idea

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.

Key Concepts

  • clamp/lerp/damp: the three interpolation primitives you reach for constantly in animation/camera code.
  • degToRad/radToDeg: angle conversion, since three.js APIs expect radians.
  • randFloat/randInt/randFloatSpread: seeded-free random helpers for scattering objects, jitter, etc.
  • isPowerOfTwo/ceilPowerOfTwo/floorPowerOfTwo: texture-dimension validation, since POT textures have broader mipmap/wrapping support.
  • generateUUID: used internally for object identity; safe to reuse for your own IDs.

Reference Tables

FunctionSignatureDescription
clampclamp(value, min, max) : numberClamps a value between min and max.
lerplerp(x, y, t) : numberLinear interpolation; t=0x, t=1y.
dampdamp(x, y, lambda, dt) : numberFrame-rate-independent spring-like interpolation toward y.
inverseLerpinverseLerp(x, y, value) : numberInverse of lerp — returns the [0,1] fraction of value between x and y.
mapLinearmapLinear(x, a1, a2, b1, b2) : numberRemaps x from range [a1,a2] to [b1,b2].
pingpongpingpong(x, length=1) : numberAlternates a value between 0 and length.
euclideanModuloeuclideanModulo(n, m) : numberModulo that stays positive (unlike JS % for negative n).
degToRad / radToDeg(value) : numberAngle unit conversion.
randFloatrandFloat(low, high) : numberRandom float in [low, high].
randFloatSpreadrandFloatSpread(range) : numberRandom float in [-range/2, range/2].
randIntrandInt(low, high) : numberRandom integer in [low, high].
seededRandomseededRandom(seed) : numberDeterministic pseudo-random float in [0,1].
isPowerOfTwoisPowerOfTwo(value) : booleanChecks if a number is a power of two.
ceilPowerOfTwo / floorPowerOfTwo(value) : numberNearest power-of-two, rounded up/down.
generateUUIDgenerateUUID() : stringRFC-4122-style UUID.
normalize / denormalize(value, array : TypedArray) : numberConverts between a typed array's integer range and [0,1] float.
isTypedArrayisTypedArray(array) : booleanType check for any TypedArray.
convertArrayconvertArray(array, type) : TypedArrayConverts an array to a specific TypedArray type.
fromHalfFloatfromHalfFloat(val) : numberFP16 → FP32 conversion.
setQuaternionFromProperEuler(q, a, b, c, order)Sets a quaternion from intrinsic proper Euler angles (rarely needed directly — most code uses Quaternion.setFromEuler).

Anti-patterns

  • Reaching for a full library (lodash, etc.) for clamp/lerp: these ship built-in and are already used internally by three.js, so behavior matches the rest of the engine.
  • Assuming randFloat/randInt are seeded/deterministic: only seededRandom is. Use it for reproducible procedural generation.

Key Takeaways

  1. These functions live at module/global scope (import from 'three' or three/src/math/MathUtils.js depending on version), not as static methods you need to look up per-class.
  2. damp is the frame-rate-independent alternative to naive lerp(a, b, 0.1) per-frame smoothing — prefer it for camera/object following.
  3. Power-of-two helpers matter for texture compatibility across older WebGL contexts and certain compression formats.
  4. Highly example-specific helpers (procedural building/forest/road generators, bitonic-sort GPU utilities, shadow-filter TSL functions) live in the source but are out of scope for a general reference — see the relevant addon's own chapter instead.

Connects To

  • MathUtils (classic API alias for most of these): see also Vector/Matrix/Quaternion chapters in Curves & Math.
  • Texture: isPowerOfTwo/ceilPowerOfTwo relate directly to texture dimension constraints.
  • AnimationMixer: damp/lerp/pingpong are common building blocks for custom animation logic layered on top of clips.