@n3rdw1z4rd/core 0.19.1 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -10
- package/dist/graph/doc.d.ts +141 -0
- package/dist/graph/doc.js +173 -0
- package/dist/graph/evaluate.d.ts +114 -0
- package/dist/graph/evaluate.js +319 -0
- package/dist/graph/index.d.ts +9 -0
- package/dist/graph/index.js +9 -0
- package/dist/graph/node.d.ts +153 -0
- package/dist/graph/node.js +94 -0
- package/dist/graph/nodes.d.ts +11 -0
- package/dist/graph/nodes.js +399 -0
- package/dist/graph/script.d.ts +56 -0
- package/dist/graph/script.js +98 -0
- package/dist/graph/sources.d.ts +11 -0
- package/dist/graph/sources.js +263 -0
- package/dist/graph/subgraph.d.ts +36 -0
- package/dist/graph/subgraph.js +280 -0
- package/dist/graph/types.d.ts +56 -0
- package/dist/graph/types.js +67 -0
- package/dist/graph.cjs +1709 -0
- package/dist/graph.d.cts +568 -0
- package/dist/index.cjs +495 -207
- package/dist/index.d.cts +231 -49
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -3
- package/dist/input/_input.d.ts +25 -0
- package/dist/input/_input.js +97 -0
- package/dist/input/action-map.d.ts +42 -7
- package/dist/input/action-map.js +44 -18
- package/dist/input/bindings.d.ts +1 -1
- package/dist/input/bindings.js +1 -1
- package/dist/input/gestures.d.ts +20 -2
- package/dist/input/gestures.js +34 -8
- package/dist/input/index.d.ts +5 -4
- package/dist/input/index.js +3 -2
- package/dist/input/input-action-map.d.ts +327 -0
- package/dist/input/input-action-map.js +689 -0
- package/dist/input/input.d.ts +77 -272
- package/dist/input/input.js +201 -653
- package/dist/rng-Dhqq7owV.cjs +642 -0
- package/dist/{spatial-partition-3d-Ba-VBveM.cjs → spatial-partition-3d-CmSCL87Z.cjs} +19 -660
- package/dist/three/adaptive-grid.d.ts +43 -0
- package/dist/three/adaptive-grid.js +127 -0
- package/dist/three/ecs.d.ts +5 -3
- package/dist/three/ecs.js +5 -3
- package/dist/three/editor-camera.d.ts +93 -0
- package/dist/three/editor-camera.js +174 -0
- package/dist/three/index.d.ts +6 -1
- package/dist/three/index.js +6 -1
- package/dist/three/picking.d.ts +34 -0
- package/dist/three/picking.js +59 -0
- package/dist/three/schema.d.ts +139 -0
- package/dist/three/schema.js +214 -0
- package/dist/three/world.d.ts +192 -0
- package/dist/three/world.js +363 -0
- package/dist/three.cjs +970 -23
- package/dist/three.d.cts +497 -5
- package/dist/vite.cjs +40 -0
- package/dist/vite.d.cts +35 -0
- package/dist/vite.d.ts +35 -0
- package/dist/vite.js +39 -0
- package/package.json +21 -1
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
//#region src/math.ts
|
|
2
|
+
/**
|
|
3
|
+
* π, half a turn in radians.
|
|
4
|
+
* @category Math & Random
|
|
5
|
+
*/
|
|
6
|
+
const PI = Math.PI;
|
|
7
|
+
/**
|
|
8
|
+
* 2π, a full turn in radians.
|
|
9
|
+
* @category Math & Random
|
|
10
|
+
*/
|
|
11
|
+
const TAU = Math.PI * 2;
|
|
12
|
+
/**
|
|
13
|
+
* π / 2, a quarter turn in radians.
|
|
14
|
+
* @category Math & Random
|
|
15
|
+
*/
|
|
16
|
+
const HALF_PI = Math.PI / 2;
|
|
17
|
+
/**
|
|
18
|
+
* π / 4, an eighth of a turn in radians.
|
|
19
|
+
* @category Math & Random
|
|
20
|
+
*/
|
|
21
|
+
const QUARTER_PI = Math.PI / 4;
|
|
22
|
+
/**
|
|
23
|
+
* Euler's number, the base of natural logarithms.
|
|
24
|
+
* @category Math & Random
|
|
25
|
+
*/
|
|
26
|
+
const E = Math.E;
|
|
27
|
+
/**
|
|
28
|
+
* Multiply degrees by this to get radians.
|
|
29
|
+
* @category Math & Random
|
|
30
|
+
*/
|
|
31
|
+
const DEG2RAD = PI / 180;
|
|
32
|
+
/**
|
|
33
|
+
* Multiply radians by this to get degrees.
|
|
34
|
+
* @category Math & Random
|
|
35
|
+
*/
|
|
36
|
+
const RAD2DEG = 180 / PI;
|
|
37
|
+
/**
|
|
38
|
+
* Natural logarithm of 2.
|
|
39
|
+
* @category Math & Random
|
|
40
|
+
*/
|
|
41
|
+
const LN2 = Math.LN2;
|
|
42
|
+
/**
|
|
43
|
+
* Natural logarithm of 10.
|
|
44
|
+
* @category Math & Random
|
|
45
|
+
*/
|
|
46
|
+
const LN10 = Math.LN10;
|
|
47
|
+
/**
|
|
48
|
+
* Base-2 logarithm of e.
|
|
49
|
+
* @category Math & Random
|
|
50
|
+
*/
|
|
51
|
+
const LOG2E = Math.LOG2E;
|
|
52
|
+
/**
|
|
53
|
+
* Base-10 logarithm of e.
|
|
54
|
+
* @category Math & Random
|
|
55
|
+
*/
|
|
56
|
+
const LOG10E = Math.LOG10E;
|
|
57
|
+
/**
|
|
58
|
+
* Square root of 2.
|
|
59
|
+
* @category Math & Random
|
|
60
|
+
*/
|
|
61
|
+
const SQRT2 = Math.SQRT2;
|
|
62
|
+
/**
|
|
63
|
+
* Square root of 1/2.
|
|
64
|
+
* @category Math & Random
|
|
65
|
+
*/
|
|
66
|
+
const SQRT1_2 = Math.SQRT1_2;
|
|
67
|
+
/**
|
|
68
|
+
* `Number.EPSILON` (about 2.2e-16), the default tolerance of {@link nearlyEqual}.
|
|
69
|
+
* @category Math & Random
|
|
70
|
+
*/
|
|
71
|
+
const EPSILON = Number.EPSILON;
|
|
72
|
+
/**
|
|
73
|
+
* Limits `value` to the inclusive range [`minValue`, `maxValue`].
|
|
74
|
+
* @category Math & Random
|
|
75
|
+
*/
|
|
76
|
+
const clamp = (value, minValue, maxValue) => Math.min(Math.max(value, minValue), maxValue);
|
|
77
|
+
/**
|
|
78
|
+
* Clamps `value` to [0, 1].
|
|
79
|
+
* @category Math & Random
|
|
80
|
+
*/
|
|
81
|
+
const saturate = (value) => clamp(value, 0, 1);
|
|
82
|
+
/**
|
|
83
|
+
* Linearly interpolates from `start` (t = 0) to `end` (t = 1). `t` is not clamped, so values
|
|
84
|
+
* outside [0, 1] extrapolate.
|
|
85
|
+
* @category Math & Random
|
|
86
|
+
*/
|
|
87
|
+
const lerp = (start, end, t) => start + (end - start) * t;
|
|
88
|
+
/**
|
|
89
|
+
* The inverse of {@link lerp}: returns the `t` at which `value` lies between `start` and `end`
|
|
90
|
+
* (0 at `start`, 1 at `end`, unclamped). Returns 0 when `start === end`.
|
|
91
|
+
* @category Math & Random
|
|
92
|
+
*/
|
|
93
|
+
const inverseLerp = (start, end, value) => end === start ? 0 : (value - start) / (end - start);
|
|
94
|
+
/**
|
|
95
|
+
* Hermite smoothstep, as in GLSL: 0 at or below `edge0`, 1 at or above `edge1`, with a smooth
|
|
96
|
+
* S-curve (3t² - 2t³) in between. With `edge0 === edge1` it is a step: 0 below the edge, 1 at or above.
|
|
97
|
+
* @category Math & Random
|
|
98
|
+
*/
|
|
99
|
+
const smoothstep = (edge0, edge1, value) => {
|
|
100
|
+
if (edge1 === edge0) return value < edge0 ? 0 : 1;
|
|
101
|
+
const t = saturate((value - edge0) / (edge1 - edge0));
|
|
102
|
+
return t * t * (3 - 2 * t);
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Fractional part of `value`, always in [0, 1) (e.g. `fract(-0.25)` is 0.75).
|
|
106
|
+
* @category Math & Random
|
|
107
|
+
*/
|
|
108
|
+
const fract = (value) => value - Math.floor(value);
|
|
109
|
+
/**
|
|
110
|
+
* Modulo whose result takes the sign of `divisor` (so `mod(-1, 4)` is 3), unlike JavaScript's `%`.
|
|
111
|
+
* @category Math & Random
|
|
112
|
+
*/
|
|
113
|
+
const mod = (value, divisor) => (value % divisor + divisor) % divisor;
|
|
114
|
+
/**
|
|
115
|
+
* Wraps `value` into the half-open range [`minValue`, `maxValue`), e.g. `wrap(370, 0, 360)` is 10.
|
|
116
|
+
* An empty range (`minValue === maxValue`) returns `minValue`.
|
|
117
|
+
* @category Math & Random
|
|
118
|
+
*/
|
|
119
|
+
const wrap = (value, minValue, maxValue) => {
|
|
120
|
+
const range = maxValue - minValue;
|
|
121
|
+
return range === 0 ? minValue : minValue + mod(value - minValue, range);
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Converts degrees to radians.
|
|
125
|
+
* @category Math & Random
|
|
126
|
+
*/
|
|
127
|
+
const radians = (degrees) => degrees * DEG2RAD;
|
|
128
|
+
/**
|
|
129
|
+
* Converts radians to degrees.
|
|
130
|
+
* @category Math & Random
|
|
131
|
+
*/
|
|
132
|
+
const degrees = (radians) => radians * RAD2DEG;
|
|
133
|
+
/**
|
|
134
|
+
* Wraps an angle in radians into [-π, π).
|
|
135
|
+
* @category Math & Random
|
|
136
|
+
*/
|
|
137
|
+
const normalizeRadians = (radians) => wrap(radians, -PI, PI);
|
|
138
|
+
/**
|
|
139
|
+
* Euclidean distance between (`x1`, `y1`) and (`x2`, `y2`).
|
|
140
|
+
* @category Math & Random
|
|
141
|
+
*/
|
|
142
|
+
const distance2d = (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1);
|
|
143
|
+
/**
|
|
144
|
+
* Euclidean distance between (`x1`, `y1`, `z1`) and (`x2`, `y2`, `z2`).
|
|
145
|
+
* @category Math & Random
|
|
146
|
+
*/
|
|
147
|
+
const distance3d = (x1, y1, z1, x2, y2, z2) => Math.hypot(x2 - x1, y2 - y1, z2 - z1);
|
|
148
|
+
/**
|
|
149
|
+
* Squared 2D Euclidean distance; cheaper than {@link distance2d} when only comparing distances.
|
|
150
|
+
* @category Math & Random
|
|
151
|
+
*/
|
|
152
|
+
const squaredDistance = (x1, y1, x2, y2) => {
|
|
153
|
+
const dx = x2 - x1;
|
|
154
|
+
const dy = y2 - y1;
|
|
155
|
+
return dx * dx + dy * dy;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* 2D Manhattan (taxicab) distance: `|dx| + |dy|`.
|
|
159
|
+
* @category Math & Random
|
|
160
|
+
*/
|
|
161
|
+
const manhattanDistance = (x1, y1, x2, y2) => Math.abs(x2 - x1) + Math.abs(y2 - y1);
|
|
162
|
+
/**
|
|
163
|
+
* True when `a` and `b` differ by at most `epsilon` (default {@link EPSILON}), scaled by the larger
|
|
164
|
+
* magnitude once that exceeds 1: absolute near zero, relative for large values.
|
|
165
|
+
* @category Math & Random
|
|
166
|
+
*/
|
|
167
|
+
const nearlyEqual = (a, b, epsilon = EPSILON) => Math.abs(a - b) <= epsilon * Math.max(1, Math.abs(a), Math.abs(b));
|
|
168
|
+
/**
|
|
169
|
+
* Rounds `value` to `digits` decimal places (default 0); negative `digits` round to tens,
|
|
170
|
+
* hundreds, etc.
|
|
171
|
+
* @category Math & Random
|
|
172
|
+
*/
|
|
173
|
+
const roundTo = (value, digits = 0) => {
|
|
174
|
+
const factor = 10 ** digits;
|
|
175
|
+
return Math.round(value * factor) / factor;
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Deterministic 32-bit integer hash (the MurmurHash3 finalizer). `n` is truncated to a 32-bit
|
|
179
|
+
* integer first.
|
|
180
|
+
* @returns an unsigned 32-bit integer in [0, 2³²)
|
|
181
|
+
* @category Math & Random
|
|
182
|
+
*/
|
|
183
|
+
const hash32 = (n) => {
|
|
184
|
+
n |= 0;
|
|
185
|
+
n ^= n >>> 16;
|
|
186
|
+
n = Math.imul(n, 2246822507);
|
|
187
|
+
n ^= n >>> 13;
|
|
188
|
+
n = Math.imul(n, 3266489909);
|
|
189
|
+
n ^= n >>> 16;
|
|
190
|
+
return n >>> 0;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* Deterministically hashes any number of values (each truncated to a 32-bit integer) into one
|
|
194
|
+
* unsigned 32-bit integer by folding them through {@link hash32}. Order matters; no values gives 0.
|
|
195
|
+
* @category Math & Random
|
|
196
|
+
*/
|
|
197
|
+
const hash32mix = (...values) => {
|
|
198
|
+
let n = 0;
|
|
199
|
+
for (const value of values) {
|
|
200
|
+
n ^= value | 0;
|
|
201
|
+
n = hash32(n);
|
|
202
|
+
}
|
|
203
|
+
return n;
|
|
204
|
+
};
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/random.ts
|
|
207
|
+
/**
|
|
208
|
+
* Hash-based random numbers. The static methods are pure functions of their arguments (the same
|
|
209
|
+
* inputs always give the same result), which makes them handy for coordinate-based noise; an
|
|
210
|
+
* instance is a seeded sequence that hashes its seed and then increments it on every call.
|
|
211
|
+
*
|
|
212
|
+
* ```ts
|
|
213
|
+
* Random.float(x, y); // same value for the same (x, y), in [0, 1)
|
|
214
|
+
*
|
|
215
|
+
* const random = new Random(42);
|
|
216
|
+
* random.float(); // deterministic sequence for seed 42
|
|
217
|
+
* ```
|
|
218
|
+
*
|
|
219
|
+
* See {@link Rng} for ranges, picking, shuffling and other helpers built on top of it.
|
|
220
|
+
* @category Math & Random
|
|
221
|
+
*/
|
|
222
|
+
var Random = class Random {
|
|
223
|
+
_seed;
|
|
224
|
+
/** @param seed truncated to a 32-bit integer, default `Date.now()` */
|
|
225
|
+
constructor(seed = Date.now()) {
|
|
226
|
+
this._seed = seed | 0;
|
|
227
|
+
}
|
|
228
|
+
/** the next value to be hashed; truncated to a 32-bit integer when set */
|
|
229
|
+
get seed() {
|
|
230
|
+
return this._seed;
|
|
231
|
+
}
|
|
232
|
+
/** truncated to a 32-bit integer */
|
|
233
|
+
set seed(value) {
|
|
234
|
+
this._seed = value | 0;
|
|
235
|
+
}
|
|
236
|
+
/** Unsigned 32-bit integer in [0, 2³²) derived from `values` (a {@link hash32mix} hash). */
|
|
237
|
+
static uint(...values) {
|
|
238
|
+
return hash32mix(...values);
|
|
239
|
+
}
|
|
240
|
+
/** Float in [0, 1) derived from `values`. */
|
|
241
|
+
static float(...values) {
|
|
242
|
+
return hash32mix(...values) / 4294967296;
|
|
243
|
+
}
|
|
244
|
+
/** Boolean derived from `values`. */
|
|
245
|
+
static bool(...values) {
|
|
246
|
+
return (hash32mix(...values) & 1) !== 0;
|
|
247
|
+
}
|
|
248
|
+
/** -1 or 1 derived from `values`. */
|
|
249
|
+
static sign(...values) {
|
|
250
|
+
return this.bool(...values) ? 1 : -1;
|
|
251
|
+
}
|
|
252
|
+
/** Angle in radians in [0, 2π) derived from `values`. */
|
|
253
|
+
static angle(...values) {
|
|
254
|
+
return this.float(...values) * TAU;
|
|
255
|
+
}
|
|
256
|
+
/** Next unsigned 32-bit integer in [0, 2³²); advances the seed. */
|
|
257
|
+
uint() {
|
|
258
|
+
return Random.uint(this._seed++);
|
|
259
|
+
}
|
|
260
|
+
/** Next float in [0, 1); advances the seed. */
|
|
261
|
+
float() {
|
|
262
|
+
return Random.float(this._seed++);
|
|
263
|
+
}
|
|
264
|
+
/** Next boolean; advances the seed. */
|
|
265
|
+
bool() {
|
|
266
|
+
return Random.bool(this._seed++);
|
|
267
|
+
}
|
|
268
|
+
/** Next -1 or 1; advances the seed. */
|
|
269
|
+
sign() {
|
|
270
|
+
return Random.sign(this._seed++);
|
|
271
|
+
}
|
|
272
|
+
/** Next angle in radians in [0, 2π); advances the seed. */
|
|
273
|
+
angle() {
|
|
274
|
+
return Random.angle(this._seed++);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/rng.ts
|
|
279
|
+
/**
|
|
280
|
+
* Seeded random number generator with helpers for ranges, picking, shuffling, geometric points,
|
|
281
|
+
* Gaussian samples and matrices. Every call advances the underlying {@link Random}, so a given
|
|
282
|
+
* seed always reproduces the same sequence of calls.
|
|
283
|
+
*
|
|
284
|
+
* ```ts
|
|
285
|
+
* const r = new Rng(1234);
|
|
286
|
+
* r.range(1, 7); // integer in [1, 7), like a die roll
|
|
287
|
+
* r.pick(['a', 'b', 'c']);
|
|
288
|
+
* r.shuffle([1, 2, 3, 4]); // new array, input untouched
|
|
289
|
+
* ```
|
|
290
|
+
*
|
|
291
|
+
* A shared, time-seeded instance is exported as {@link rng}.
|
|
292
|
+
* @category Math & Random
|
|
293
|
+
*/
|
|
294
|
+
var Rng = class {
|
|
295
|
+
/** the underlying generator; shared if one was passed to the constructor */
|
|
296
|
+
random;
|
|
297
|
+
/** the underlying {@link Random}'s current seed; setting it restarts the sequence */
|
|
298
|
+
get seed() {
|
|
299
|
+
return this.random.seed;
|
|
300
|
+
}
|
|
301
|
+
/** sets the underlying {@link Random}'s seed */
|
|
302
|
+
set seed(seed) {
|
|
303
|
+
this.random.seed = seed;
|
|
304
|
+
}
|
|
305
|
+
/** @param seed a seed (default `Date.now()`), or an existing {@link Random} to wrap */
|
|
306
|
+
constructor(seed) {
|
|
307
|
+
this.random = seed instanceof Random ? seed : new Random(seed);
|
|
308
|
+
}
|
|
309
|
+
/** Float in [0, 1). */
|
|
310
|
+
nextFloat() {
|
|
311
|
+
return this.random.float();
|
|
312
|
+
}
|
|
313
|
+
/** Unsigned 32-bit integer in [0, 2³²). */
|
|
314
|
+
nextUint() {
|
|
315
|
+
return this.random.uint();
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* random integer in [min, max); range(max) is [0, max). Non-integer bounds are rounded up,
|
|
319
|
+
* so the result is always an integer inside the requested range.
|
|
320
|
+
*/
|
|
321
|
+
range(min, max) {
|
|
322
|
+
if (typeof max !== "number") {
|
|
323
|
+
max = min;
|
|
324
|
+
min = 0;
|
|
325
|
+
}
|
|
326
|
+
min = Math.ceil(min);
|
|
327
|
+
max = Math.ceil(max);
|
|
328
|
+
return min + Math.floor(this.random.float() * (max - min));
|
|
329
|
+
}
|
|
330
|
+
pick(array) {
|
|
331
|
+
return array[this.range(0, array.length)];
|
|
332
|
+
}
|
|
333
|
+
/** Up to `count` distinct elements of `array` in random order (all of them if `count` exceeds its length). */
|
|
334
|
+
sample(array, count) {
|
|
335
|
+
return this.shuffle(array).slice(0, count);
|
|
336
|
+
}
|
|
337
|
+
/** A shuffled copy of `array` (Fisher-Yates); the input is not modified. */
|
|
338
|
+
shuffle(array) {
|
|
339
|
+
const result = [...array];
|
|
340
|
+
for (let i = result.length - 1; i > 0; --i) {
|
|
341
|
+
const j = this.range(0, i + 1);
|
|
342
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
/** True with the given probability (0 = never, 1 = always). */
|
|
347
|
+
chance(probability) {
|
|
348
|
+
return this.random.float() < probability;
|
|
349
|
+
}
|
|
350
|
+
/** Random unit-length 2D direction as `[x, y]`. */
|
|
351
|
+
vector2() {
|
|
352
|
+
const θ = this.random.angle();
|
|
353
|
+
return [Math.cos(θ), Math.sin(θ)];
|
|
354
|
+
}
|
|
355
|
+
/** Uniform point on a circle of `radius` (default 1) centred on the origin, as `[x, y]`. */
|
|
356
|
+
pointOnCircle(radius = 1) {
|
|
357
|
+
const θ = this.random.angle();
|
|
358
|
+
return [radius * Math.cos(θ), radius * Math.sin(θ)];
|
|
359
|
+
}
|
|
360
|
+
/** Uniform point inside a disc of `radius` (default 1) centred on the origin, as `[x, y]`. */
|
|
361
|
+
pointInCircle(radius = 1) {
|
|
362
|
+
const θ = this.random.angle();
|
|
363
|
+
const r = Math.sqrt(this.random.float()) * radius;
|
|
364
|
+
return [r * Math.cos(θ), r * Math.sin(θ)];
|
|
365
|
+
}
|
|
366
|
+
/** Uniform point on the surface of a sphere of `radius` (default 1) centred on the origin, as `[x, y, z]`. */
|
|
367
|
+
pointOnSphere(radius = 1) {
|
|
368
|
+
const u = this.random.float();
|
|
369
|
+
const v = this.random.float();
|
|
370
|
+
const θ = TAU * u;
|
|
371
|
+
const φ = Math.acos(2 * v - 1);
|
|
372
|
+
const s = Math.sin(φ);
|
|
373
|
+
return [
|
|
374
|
+
radius * s * Math.cos(θ),
|
|
375
|
+
radius * s * Math.sin(θ),
|
|
376
|
+
radius * Math.cos(φ)
|
|
377
|
+
];
|
|
378
|
+
}
|
|
379
|
+
/** Uniform point inside a ball of `radius` (default 1) centred on the origin, as `[x, y, z]`. */
|
|
380
|
+
pointInSphere(radius = 1) {
|
|
381
|
+
const u = this.random.float();
|
|
382
|
+
const v = this.random.float();
|
|
383
|
+
const θ = TAU * u;
|
|
384
|
+
const φ = Math.acos(2 * v - 1);
|
|
385
|
+
const r = radius * Math.cbrt(this.random.float());
|
|
386
|
+
const s = Math.sin(φ);
|
|
387
|
+
return [
|
|
388
|
+
r * s * Math.cos(θ),
|
|
389
|
+
r * s * Math.sin(θ),
|
|
390
|
+
r * Math.cos(φ)
|
|
391
|
+
];
|
|
392
|
+
}
|
|
393
|
+
/** Normally distributed sample (Box-Muller), default mean 0 and standard deviation 1. */
|
|
394
|
+
gaussian(mean = 0, standardDeviation = 1) {
|
|
395
|
+
let u = 0;
|
|
396
|
+
let v = 0;
|
|
397
|
+
while (u === 0) u = this.random.float();
|
|
398
|
+
while (v === 0) v = this.random.float();
|
|
399
|
+
return mean + Math.sqrt(-2 * Math.log(u)) * Math.cos(TAU * v) * standardDeviation;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* `rows` x `columns` array of uniform floats in [`min`, `max`) (default [0, 1)); `columns`
|
|
403
|
+
* defaults to `rows`.
|
|
404
|
+
*/
|
|
405
|
+
matrix(rows, columns = rows, min = 0, max = 1) {
|
|
406
|
+
return Array.from({ length: rows }, () => Array.from({ length: columns }, () => min + this.random.float() * (max - min)));
|
|
407
|
+
}
|
|
408
|
+
/** Same as {@link Rng.matrix} with values in [-1, 1). */
|
|
409
|
+
transformMatrix(rows, columns = rows) {
|
|
410
|
+
return this.matrix(rows, columns, -1, 1);
|
|
411
|
+
}
|
|
412
|
+
/** Random color as `[r, g, b]`, each an integer in [0, 255]. */
|
|
413
|
+
rgb() {
|
|
414
|
+
return [
|
|
415
|
+
this.range(256),
|
|
416
|
+
this.range(256),
|
|
417
|
+
this.range(256)
|
|
418
|
+
];
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
/**
|
|
422
|
+
* Shared {@link Rng} seeded from `Date.now()` at module load; set `rng.seed` for reproducible results.
|
|
423
|
+
* @category Math & Random
|
|
424
|
+
*/
|
|
425
|
+
const rng = new Rng();
|
|
426
|
+
//#endregion
|
|
427
|
+
Object.defineProperty(exports, "DEG2RAD", {
|
|
428
|
+
enumerable: true,
|
|
429
|
+
get: function() {
|
|
430
|
+
return DEG2RAD;
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
Object.defineProperty(exports, "E", {
|
|
434
|
+
enumerable: true,
|
|
435
|
+
get: function() {
|
|
436
|
+
return E;
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
Object.defineProperty(exports, "EPSILON", {
|
|
440
|
+
enumerable: true,
|
|
441
|
+
get: function() {
|
|
442
|
+
return EPSILON;
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
Object.defineProperty(exports, "HALF_PI", {
|
|
446
|
+
enumerable: true,
|
|
447
|
+
get: function() {
|
|
448
|
+
return HALF_PI;
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
Object.defineProperty(exports, "LN10", {
|
|
452
|
+
enumerable: true,
|
|
453
|
+
get: function() {
|
|
454
|
+
return LN10;
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
Object.defineProperty(exports, "LN2", {
|
|
458
|
+
enumerable: true,
|
|
459
|
+
get: function() {
|
|
460
|
+
return LN2;
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
Object.defineProperty(exports, "LOG10E", {
|
|
464
|
+
enumerable: true,
|
|
465
|
+
get: function() {
|
|
466
|
+
return LOG10E;
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
Object.defineProperty(exports, "LOG2E", {
|
|
470
|
+
enumerable: true,
|
|
471
|
+
get: function() {
|
|
472
|
+
return LOG2E;
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
Object.defineProperty(exports, "PI", {
|
|
476
|
+
enumerable: true,
|
|
477
|
+
get: function() {
|
|
478
|
+
return PI;
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
Object.defineProperty(exports, "QUARTER_PI", {
|
|
482
|
+
enumerable: true,
|
|
483
|
+
get: function() {
|
|
484
|
+
return QUARTER_PI;
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
Object.defineProperty(exports, "RAD2DEG", {
|
|
488
|
+
enumerable: true,
|
|
489
|
+
get: function() {
|
|
490
|
+
return RAD2DEG;
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
Object.defineProperty(exports, "Random", {
|
|
494
|
+
enumerable: true,
|
|
495
|
+
get: function() {
|
|
496
|
+
return Random;
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
Object.defineProperty(exports, "Rng", {
|
|
500
|
+
enumerable: true,
|
|
501
|
+
get: function() {
|
|
502
|
+
return Rng;
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
Object.defineProperty(exports, "SQRT1_2", {
|
|
506
|
+
enumerable: true,
|
|
507
|
+
get: function() {
|
|
508
|
+
return SQRT1_2;
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
Object.defineProperty(exports, "SQRT2", {
|
|
512
|
+
enumerable: true,
|
|
513
|
+
get: function() {
|
|
514
|
+
return SQRT2;
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
Object.defineProperty(exports, "TAU", {
|
|
518
|
+
enumerable: true,
|
|
519
|
+
get: function() {
|
|
520
|
+
return TAU;
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
Object.defineProperty(exports, "clamp", {
|
|
524
|
+
enumerable: true,
|
|
525
|
+
get: function() {
|
|
526
|
+
return clamp;
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
Object.defineProperty(exports, "degrees", {
|
|
530
|
+
enumerable: true,
|
|
531
|
+
get: function() {
|
|
532
|
+
return degrees;
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
Object.defineProperty(exports, "distance2d", {
|
|
536
|
+
enumerable: true,
|
|
537
|
+
get: function() {
|
|
538
|
+
return distance2d;
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
Object.defineProperty(exports, "distance3d", {
|
|
542
|
+
enumerable: true,
|
|
543
|
+
get: function() {
|
|
544
|
+
return distance3d;
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
Object.defineProperty(exports, "fract", {
|
|
548
|
+
enumerable: true,
|
|
549
|
+
get: function() {
|
|
550
|
+
return fract;
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
Object.defineProperty(exports, "hash32", {
|
|
554
|
+
enumerable: true,
|
|
555
|
+
get: function() {
|
|
556
|
+
return hash32;
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
Object.defineProperty(exports, "hash32mix", {
|
|
560
|
+
enumerable: true,
|
|
561
|
+
get: function() {
|
|
562
|
+
return hash32mix;
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
Object.defineProperty(exports, "inverseLerp", {
|
|
566
|
+
enumerable: true,
|
|
567
|
+
get: function() {
|
|
568
|
+
return inverseLerp;
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
Object.defineProperty(exports, "lerp", {
|
|
572
|
+
enumerable: true,
|
|
573
|
+
get: function() {
|
|
574
|
+
return lerp;
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
Object.defineProperty(exports, "manhattanDistance", {
|
|
578
|
+
enumerable: true,
|
|
579
|
+
get: function() {
|
|
580
|
+
return manhattanDistance;
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
Object.defineProperty(exports, "mod", {
|
|
584
|
+
enumerable: true,
|
|
585
|
+
get: function() {
|
|
586
|
+
return mod;
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
Object.defineProperty(exports, "nearlyEqual", {
|
|
590
|
+
enumerable: true,
|
|
591
|
+
get: function() {
|
|
592
|
+
return nearlyEqual;
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
Object.defineProperty(exports, "normalizeRadians", {
|
|
596
|
+
enumerable: true,
|
|
597
|
+
get: function() {
|
|
598
|
+
return normalizeRadians;
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
Object.defineProperty(exports, "radians", {
|
|
602
|
+
enumerable: true,
|
|
603
|
+
get: function() {
|
|
604
|
+
return radians;
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
Object.defineProperty(exports, "rng", {
|
|
608
|
+
enumerable: true,
|
|
609
|
+
get: function() {
|
|
610
|
+
return rng;
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
Object.defineProperty(exports, "roundTo", {
|
|
614
|
+
enumerable: true,
|
|
615
|
+
get: function() {
|
|
616
|
+
return roundTo;
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
Object.defineProperty(exports, "saturate", {
|
|
620
|
+
enumerable: true,
|
|
621
|
+
get: function() {
|
|
622
|
+
return saturate;
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
Object.defineProperty(exports, "smoothstep", {
|
|
626
|
+
enumerable: true,
|
|
627
|
+
get: function() {
|
|
628
|
+
return smoothstep;
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
Object.defineProperty(exports, "squaredDistance", {
|
|
632
|
+
enumerable: true,
|
|
633
|
+
get: function() {
|
|
634
|
+
return squaredDistance;
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
Object.defineProperty(exports, "wrap", {
|
|
638
|
+
enumerable: true,
|
|
639
|
+
get: function() {
|
|
640
|
+
return wrap;
|
|
641
|
+
}
|
|
642
|
+
});
|