diff --git a/packages/scene/src/math.ts b/packages/scene/src/math.ts index affe7a7d..861026e6 100644 --- a/packages/scene/src/math.ts +++ b/packages/scene/src/math.ts @@ -970,4 +970,76 @@ export const MathUtils = { clamp: (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v)), euclideanModulo: (n: number, m: number) => ((n % m) + m) % m, lerp: (a: number, b: number, t: number) => a + (b - a) * t, + /** Maps `x` from the range [a1, a2] to [b1, b2]. */ + mapLinear: (x: number, a1: number, a2: number, b1: number, b2: number) => b1 + ((x - a1) * (b2 - b1)) / (a2 - a1), + /** The fraction of `value` between `x` and `y`; 0 when the range is empty. */ + inverseLerp: (x: number, y: number, value: number) => (x !== y ? (value - x) / (y - x) : 0), + /** Frame-rate independent damping towards `y` (`lambda` per second, `dt` in seconds). */ + damp: (x: number, y: number, lambda: number, dt: number) => x + (y - x) * (1 - Math.exp(-lambda * dt)), + pingpong: (x: number, length = 1) => length - Math.abs((((x % (length * 2)) + length * 2) % (length * 2)) - length), + smoothstep(x: number, min: number, max: number): number { + if (x <= min) return 0; + if (x >= max) return 1; + x = (x - min) / (max - min); + return x * x * (3 - 2 * x); + }, + smootherstep(x: number, min: number, max: number): number { + if (x <= min) return 0; + if (x >= max) return 1; + x = (x - min) / (max - min); + return x * x * x * (x * (x * 6 - 15) + 10); + }, + /** A random integer in [low, high]. */ + randInt: (low: number, high: number) => low + Math.floor(Math.random() * (high - low + 1)), + /** A random float in [low, high). */ + randFloat: (low: number, high: number) => low + Math.random() * (high - low), + /** A random float in (-range / 2, range / 2]. */ + randFloatSpread: (range: number) => range * (0.5 - Math.random()), + /** Deterministic pseudo-random float in [0, 1) (Mulberry32); passing `s` reseeds. */ + seededRandom(s?: number): number { + if (s !== undefined) seed = s; + let t = (seed += 0x6d2b79f5); + t = Math.imul(t ^ (t >>> 15), t | 1); + t ^= t + Math.imul(t ^ (t >>> 7), t | 61); + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; + }, + /** An RFC 4122 version 4 UUID, lowercase. */ + generateUUID(): string { + const d0 = (Math.random() * 0xffffffff) | 0, d1 = (Math.random() * 0xffffffff) | 0; + const d2 = (Math.random() * 0xffffffff) | 0, d3 = (Math.random() * 0xffffffff) | 0; + const h = (v: number) => HEX[v & 0xff]; + return ( + h(d0) + h(d0 >> 8) + h(d0 >> 16) + h(d0 >> 24) + "-" + h(d1) + h(d1 >> 8) + "-" + + h(((d1 >> 16) & 0x0f) | 0x40) + h(d1 >> 24) + "-" + h((d2 & 0x3f) | 0x80) + h(d2 >> 8) + "-" + + h(d2 >> 16) + h(d2 >> 24) + h(d3) + h(d3 >> 8) + h(d3 >> 16) + h(d3 >> 24) + ); + }, + isPowerOfTwo: (value: number) => (value & (value - 1)) === 0 && value !== 0, + ceilPowerOfTwo: (value: number) => Math.pow(2, Math.ceil(Math.log(value) / Math.LN2)), + floorPowerOfTwo: (value: number) => Math.pow(2, Math.floor(Math.log(value) / Math.LN2)), + /** A float in [0, 1] (or [-1, 1]) as the normalized integer `array`'s component type stores. */ + normalize(value: number, array: ArrayLike): number { + if (array.constructor === Float32Array) return value; + return Math.round(value * normalizationScale(array)); + }, + /** A normalized integer from `array`'s component type, as a float in [0, 1] (or [-1, 1] when signed). */ + denormalize(value: number, array: ArrayLike): number { + if (array.constructor === Float32Array) return value; + return Math.max(value / normalizationScale(array), -1); + }, }; + +function normalizationScale(array: ArrayLike): number { + switch (array.constructor) { + case Uint32Array: return 4294967295; + case Uint16Array: return 65535; + case Uint8Array: case Uint8ClampedArray: return 255; + case Int32Array: return 2147483647; + case Int16Array: return 32767; + case Int8Array: return 127; + } + throw new Error("THREE.MathUtils: Invalid component type."); +} + +let seed = 1234567; +const HEX = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0")); diff --git a/packages/scene/test/three-compat-mathutils.test.ts b/packages/scene/test/three-compat-mathutils.test.ts index d1ba37ec..937a3564 100644 --- a/packages/scene/test/three-compat-mathutils.test.ts +++ b/packages/scene/test/three-compat-mathutils.test.ts @@ -9,3 +9,86 @@ test("MathUtils.euclideanModulo: negative dividends return Euclidean residues", assert.equal(THREE.MathUtils.euclideanModulo(12, 5), 2); assert.equal(THREE.MathUtils.euclideanModulo(10, 5), 0); }); + +// three.js r187 src/math/MathUtils.js: the scalar helpers three.js code reaches for. +test("MathUtils: mapLinear, inverseLerp, damp and pingpong match three.js", () => { + const M = THREE.MathUtils; + assert.equal(M.mapLinear(5, 0, 10, 100, 200), 150); + assert.equal(M.mapLinear(-5, 0, 10, 0, 1), -0.5, "extrapolates outside the range"); + assert.equal(M.inverseLerp(2, 4, 3), 0.5); + assert.equal(M.inverseLerp(2, 2, 3), 0, "empty range gives 0, not NaN"); + assert.equal(M.damp(0, 10, 0, 1), 0, "lambda 0 does not move"); + assert.ok(Math.abs(M.damp(0, 10, 1, 1) - 10 * (1 - Math.exp(-1))) < 1e-12); + assert.equal(M.pingpong(0.25), 0.25); + assert.equal(M.pingpong(1.25), 0.75); + assert.equal(M.pingpong(-0.25), 0.25); + assert.equal(M.pingpong(5, 2), 1); +}); + +test("MathUtils: smoothstep and smootherstep clamp and ease, as three.js", () => { + const M = THREE.MathUtils; + assert.equal(M.smoothstep(-1, 0, 1), 0); + assert.equal(M.smoothstep(2, 0, 1), 1); + assert.equal(M.smoothstep(0.5, 0, 1), 0.5); + assert.equal(M.smoothstep(0.25, 0, 1), 0.15625); + assert.equal(M.smootherstep(0.5, 0, 1), 0.5); + assert.equal(M.smootherstep(0.25, 0, 1), 0.103515625); + assert.equal(M.smootherstep(3, 0, 2), 1); +}); + +test("MathUtils: random helpers stay in range; seededRandom is three.js's Mulberry32", () => { + const M = THREE.MathUtils; + for (let k = 0; k < 200; k++) { + const i = M.randInt(-2, 2); + assert.ok(Number.isInteger(i) && i >= -2 && i <= 2); + const f = M.randFloat(1, 3); + assert.ok(f >= 1 && f < 3); + const s = M.randFloatSpread(4); + assert.ok(s > -2 && s <= 2); + } + const a = [M.seededRandom(42), M.seededRandom(), M.seededRandom()]; + const b = [M.seededRandom(42), M.seededRandom(), M.seededRandom()]; + assert.deepEqual(a, b, "reseeding repeats the sequence"); + assert.ok(a.every((v) => v >= 0 && v < 1)); + // Mulberry32 with state 42: first output, computed from the reference algorithm. + let t = (42 + 0x6d2b79f5) | 0; + t = Math.imul(t ^ (t >>> 15), t | 1); + t ^= t + Math.imul(t ^ (t >>> 7), t | 61); + assert.equal(a[0], ((t ^ (t >>> 14)) >>> 0) / 4294967296); +}); + +test("MathUtils: generateUUID gives lowercase version-4 UUIDs", () => { + const ids = new Set(); + for (let k = 0; k < 50; k++) { + const id = THREE.MathUtils.generateUUID(); + assert.match(id, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); + ids.add(id); + } + assert.equal(ids.size, 50); +}); + +test("MathUtils: power-of-two helpers, as three.js", () => { + const M = THREE.MathUtils; + assert.equal(M.isPowerOfTwo(1), true); + assert.equal(M.isPowerOfTwo(1024), true); + assert.equal(M.isPowerOfTwo(0), false); + assert.equal(M.isPowerOfTwo(6), false); + assert.equal(M.ceilPowerOfTwo(5), 8); + assert.equal(M.ceilPowerOfTwo(8), 8); + assert.equal(M.floorPowerOfTwo(5), 4); + assert.equal(M.floorPowerOfTwo(8), 8); +}); + +test("MathUtils: normalize and denormalize follow the array's component type", () => { + const M = THREE.MathUtils; + assert.equal(M.normalize(0.3, new Float32Array(1)), 0.3, "float arrays pass through"); + assert.equal(M.normalize(1, new Uint8Array(1)), 255); + assert.equal(M.normalize(0.5, new Uint16Array(1)), 32768); + assert.equal(M.normalize(-1, new Int8Array(1)), -127); + assert.equal(M.denormalize(255, new Uint8Array(1)), 1); + assert.equal(M.denormalize(65535, new Uint16Array(1)), 1); + assert.equal(M.denormalize(-128, new Int8Array(1)), -1, "the most negative integer clamps to -1"); + assert.equal(M.denormalize(-32767, new Int16Array(1)), -1); + assert.equal(M.denormalize(0.3, new Float32Array(1)), 0.3); + assert.throws(() => M.normalize(1, new Float64Array(1)), "unsupported component types throw"); +});