@threenative/core 0.3.4 → 0.3.5

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.
Files changed (176) hide show
  1. package/capabilities.json +1505 -79
  2. package/dist/{audio-7i3Xl0l3.d.ts → audio-DIJE5HTr.d.ts} +43 -3
  3. package/dist/{canvas-layer-DDmC_VVF.d.ts → canvas-layer-C1-5CviZ.d.ts} +5 -1
  4. package/dist/{game-CljaDv4D.d.ts → game-Bj8G1wmD.d.ts} +102 -11
  5. package/dist/hot.d.ts +4 -4
  6. package/dist/index.d.ts +712 -118
  7. package/dist/index.js +12423 -4955
  8. package/dist/{gpu-readback-CqJEfWNQ.d.ts → model-lod-BDAUYgsS.d.ts} +80 -5
  9. package/dist/playtest.d.ts +6 -10
  10. package/dist/playtest.js +47 -24
  11. package/dist/react-css.d.ts +62 -0
  12. package/dist/react-css.js +591 -0
  13. package/dist/react.d.ts +2 -2
  14. package/dist/{renderer-CfsS2hxi.d.ts → renderer-9yEo9hPa.d.ts} +489 -22
  15. package/dist/terrain-jobs-worker.d.ts +2 -0
  16. package/dist/terrain-jobs-worker.js +130 -0
  17. package/dist/ui-layer.js +59 -8
  18. package/dist/world.d.ts +766 -73
  19. package/dist/world.js +9307 -3543
  20. package/gpl/recipes/export_world.py +18 -1
  21. package/mcp/blender-server.mjs +7 -4
  22. package/mcp/blender.mjs +3 -4
  23. package/mcp/engine-server.mjs +2 -2
  24. package/mcp/servers.mjs +4 -4
  25. package/package.json +15 -9
  26. package/patches/three@0.185.1-prd269-upgrade.patch +22418 -0
  27. package/patches/three@0.185.1.patch +6594 -700
  28. package/scripts/apply-three-patch.mjs +62 -4
  29. package/src/animation-blend.ts +264 -0
  30. package/src/animation-composition.ts +362 -0
  31. package/src/animation-layers.ts +260 -0
  32. package/src/animation-root-motion.ts +283 -0
  33. package/src/animation.ts +896 -0
  34. package/src/assets.ts +1303 -0
  35. package/src/atmosphere/index.ts +335 -0
  36. package/src/atmosphere/luts.ts +298 -0
  37. package/src/atmosphere/params.ts +323 -0
  38. package/src/audio.ts +912 -0
  39. package/src/billboard.ts +124 -0
  40. package/src/bone-lengths.ts +157 -0
  41. package/src/camera-shake.ts +116 -0
  42. package/src/canvas-layer.ts +47 -0
  43. package/src/clip-audit.ts +401 -0
  44. package/src/clustered-batch.ts +492 -0
  45. package/src/clustered-mesh.ts +638 -0
  46. package/src/compute-driven.ts +176 -0
  47. package/src/compute-timing.ts +318 -0
  48. package/src/config.ts +593 -0
  49. package/src/debug.ts +82 -0
  50. package/src/engine-render-hook.ts +25 -0
  51. package/src/entities.ts +79 -0
  52. package/src/entity-snapshot.ts +53 -0
  53. package/src/flight.ts +683 -0
  54. package/src/fluid-field.ts +748 -0
  55. package/src/fluid-particles.ts +1253 -0
  56. package/src/frame-budget.ts +1368 -0
  57. package/src/game.ts +2390 -0
  58. package/src/geometry-capture.ts +1098 -0
  59. package/src/gpu-class.ts +160 -0
  60. package/src/gpu-frame-observation.ts +424 -0
  61. package/src/gpu-readback.ts +143 -0
  62. package/src/gpu-scene-bvh.ts +464 -0
  63. package/src/grounding.ts +100 -0
  64. package/src/hot.ts +161 -0
  65. package/src/import-meta.d.ts +13 -0
  66. package/src/index.ts +1536 -0
  67. package/src/input.ts +690 -0
  68. package/src/instanced-batch-lod.ts +283 -0
  69. package/src/instanced-batch.ts +274 -0
  70. package/src/interpolation.ts +129 -0
  71. package/src/launch-diagnostics.ts +161 -0
  72. package/src/loop.ts +564 -0
  73. package/src/matrix-world.ts +245 -0
  74. package/src/merge-parts.ts +812 -0
  75. package/src/model-lod.ts +1315 -0
  76. package/src/net-protocol.ts +333 -0
  77. package/src/net-session.ts +477 -0
  78. package/src/net.ts +80 -0
  79. package/src/ocean/fft.ts +72 -0
  80. package/src/ocean/spectral.ts +728 -0
  81. package/src/particle-significance.ts +102 -0
  82. package/src/particles.ts +648 -0
  83. package/src/path-follow.ts +175 -0
  84. package/src/picking.ts +250 -0
  85. package/src/pipeline-census.ts +1566 -0
  86. package/src/platform.ts +210 -0
  87. package/src/playtest.ts +839 -0
  88. package/src/pointer-events.ts +411 -0
  89. package/src/pose-measure.ts +435 -0
  90. package/src/profiling/FrameCounters.ts +231 -0
  91. package/src/profiling/Spans.ts +548 -0
  92. package/src/profiling/opacity-convention.ts +21 -0
  93. package/src/profiling/render-list-validate.ts +175 -0
  94. package/src/profiling/render-trap-audit.ts +84 -0
  95. package/src/profiling/scene-warning.ts +190 -0
  96. package/src/profiling/span-probes.ts +191 -0
  97. package/src/profiling/unlit-foliage-warning.ts +149 -0
  98. package/src/projection-apply.ts +1529 -0
  99. package/src/projection-marker.ts +142 -0
  100. package/src/projection-plan.ts +1258 -0
  101. package/src/projection-skinned.ts +419 -0
  102. package/src/projection-stability.ts +409 -0
  103. package/src/projection-uniform.ts +328 -0
  104. package/src/random.ts +41 -0
  105. package/src/react-css.ts +604 -0
  106. package/src/react-glyphs.ts +114 -0
  107. package/src/react-host.ts +576 -0
  108. package/src/react-layout.ts +265 -0
  109. package/src/react.ts +60 -0
  110. package/src/render/alpha-antialiasing.ts +159 -0
  111. package/src/render/batched-velocity.ts +126 -0
  112. package/src/render/chain.ts +1021 -0
  113. package/src/render/daylight.ts +189 -0
  114. package/src/render/depth-pyramid.ts +596 -0
  115. package/src/render/foliage-alpha.ts +166 -0
  116. package/src/render/material-key.ts +60 -0
  117. package/src/render/mesh-pool.ts +144 -0
  118. package/src/render/probe-volume.ts +1668 -0
  119. package/src/render/resolution-subrect.ts +253 -0
  120. package/src/render/velocity.ts +442 -0
  121. package/src/render/virtual-shadow-pages.ts +827 -0
  122. package/src/render/virtual-shadow-partial.ts +174 -0
  123. package/src/render/virtual-shadow.ts +3330 -0
  124. package/src/render/world-impostor-surface.ts +722 -0
  125. package/src/render/world-impostor.ts +558 -0
  126. package/src/render-camera-cull.ts +441 -0
  127. package/src/render-pass-budget.ts +501 -0
  128. package/src/renderProjection.ts +758 -0
  129. package/src/renderer-config.ts +127 -0
  130. package/src/renderer.ts +1847 -0
  131. package/src/replay-protocol.ts +85 -0
  132. package/src/replay.ts +205 -0
  133. package/src/resolution-scaler.ts +819 -0
  134. package/src/rig-preparation.ts +271 -0
  135. package/src/ripple-field.ts +432 -0
  136. package/src/scale.ts +109 -0
  137. package/src/scene.ts +239 -0
  138. package/src/schedule.ts +155 -0
  139. package/src/skeletal-mesh.ts +61 -0
  140. package/src/skeleton.ts +100 -0
  141. package/src/snow-field.ts +744 -0
  142. package/src/softbody-topology.ts +269 -0
  143. package/src/softbody.ts +516 -0
  144. package/src/sprite-animator.ts +222 -0
  145. package/src/startup-readiness.ts +386 -0
  146. package/src/state.ts +66 -0
  147. package/src/static-transform.ts +274 -0
  148. package/src/storage-buffer.ts +253 -0
  149. package/src/streaming.ts +308 -0
  150. package/src/target-fps.ts +92 -0
  151. package/src/terrain-jobs-worker.ts +17 -0
  152. package/src/terrain-jobs.ts +353 -0
  153. package/src/textures.ts +39 -0
  154. package/src/tracers.ts +237 -0
  155. package/src/ui-bridge.ts +368 -0
  156. package/src/ui-hit-regions.ts +380 -0
  157. package/src/ui-layer.ts +83 -0
  158. package/src/ui-state.ts +220 -0
  159. package/src/version.ts +2 -0
  160. package/src/viewport.ts +253 -0
  161. package/src/warmup.ts +1357 -0
  162. package/src/water-surface.ts +384 -0
  163. package/src/wave-field.ts +524 -0
  164. package/src/world-capabilities.ts +131 -0
  165. package/src/world-cell-proxy.ts +392 -0
  166. package/src/world-cells.ts +10564 -0
  167. package/src/world-gpu-scene.ts +3354 -0
  168. package/src/world-heightmap.ts +82 -0
  169. package/src/world-package.ts +469 -0
  170. package/src/world-passes.ts +582 -0
  171. package/src/world-region.ts +17 -0
  172. package/src/world-terrain-splat.ts +564 -0
  173. package/src/world-tiles.ts +3826 -0
  174. package/src/world-topology.ts +370 -0
  175. package/src/world-validate.ts +48 -0
  176. package/src/world.ts +805 -0
@@ -0,0 +1,328 @@
1
+ import type { Color, Material } from "three";
2
+
3
+ import { tintableMaterial } from "./projection-skinned.js";
4
+
5
+ /**
6
+ * The fingerprint a uniform batch is keyed on, and the per-frame proof a member still matches it.
7
+ *
8
+ * A group of meshes that share a geometry and a material differing **only in base colour** draws as
9
+ * one instanced call with the colour carried per instance, so the draw's only job is to know which
10
+ * materials are interchangeable. That question has two halves, and both are affordable every frame
11
+ * for a scene of four thousand materials:
12
+ *
13
+ * - **At classification**, a material's own properties become a signature string, and the values
14
+ * behind it are cached by position. Two materials with the same signature differ in nothing a
15
+ * shader can see except the colour.
16
+ * - **Per frame**, the material's own values are read back in one `Object.values` call and compared
17
+ * against that cache. Everything is compared by identity, which is what a swap is, except the
18
+ * small-object types three clones per material — a `Color`, a `Vector2`, an `Euler` — which a game
19
+ * mutates in place, so their components are read instead. A property the group cannot carry
20
+ * (`color` travels per instance, `defines` is walked shallowly beside this, and the identity
21
+ * fields are no draw's business) is skipped rather than watched, and the number of own properties
22
+ * is compared, because a game that attaches a draw-affecting one after the fact would otherwise be
23
+ * invisible here and the group would keep drawing it with a shared material that never had it.
24
+ *
25
+ * The comparison is deliberately a value compare and nothing else. `material.version` is not part of
26
+ * it, because `material.needsUpdate = true` bumps it without changing a pixel, and a material that
27
+ * set it every frame would then be reclassified every frame for nothing. Every change that *can*
28
+ * move a pixel is a change to one of the values read here, so the values are the whole truth and
29
+ * the version is not.
30
+ */
31
+
32
+ /** Own properties no draw reads: the per-instance colour, and identity or bookkeeping. */
33
+ const UNREAD_PROPERTIES: ReadonlySet<string> = new Set([
34
+ "color",
35
+ "defines",
36
+ "id",
37
+ "name",
38
+ "userData",
39
+ "uuid",
40
+ "version",
41
+ ]);
42
+
43
+ /** How a component-compared value's own numeric fields are read back per frame. */
44
+ const READ_KEYED = 0;
45
+ const READ_RGB = 1;
46
+ const READ_XY = 2;
47
+ const READ_UNDERSCORE_XYZ = 3;
48
+
49
+ interface IUniformRecord {
50
+ /** The values as of the last classification, by the position they were enumerated at. */
51
+ values: unknown[];
52
+ /** Flat `[start, end)` pairs covering the positions judged by identity, skips excluded. */
53
+ identityRanges: Int32Array;
54
+ /** Positions of the component-compared values, in enumeration order. */
55
+ vectorSlots: number[];
56
+ /** How each of those values is read back, and how many fields of it there are. */
57
+ vectorReads: Uint8Array;
58
+ vectorWidths: Uint8Array;
59
+ /** The own numeric field names of every component-compared value, flattened. */
60
+ vectorFields: string[];
61
+ components: number[];
62
+ /** `defines` keys, in enumeration order, and the value each held. */
63
+ defineKeys: string[];
64
+ defineValues: unknown[];
65
+ signature: string;
66
+ }
67
+
68
+ const records = new WeakMap<Material, IUniformRecord>();
69
+ /**
70
+ * A number per opaque value — anything whose components say nothing, from a `ShaderMaterial`'s
71
+ * `uniforms` to a game's own `onBeforeCompile`. Two materials holding the *same* one are as
72
+ * interchangeable as two holding the same texture; two holding different ones are not, and every
73
+ * one of them stringifies to `[object Object]`, so identity has to be spoken for numerically.
74
+ */
75
+ const opaqueIds = new WeakMap<object, number>();
76
+ let nextOpaqueId = 1;
77
+
78
+ function isTextureValue(value: unknown): boolean {
79
+ return value !== null && (value as { isTexture?: boolean }).isTexture === true;
80
+ }
81
+
82
+ /**
83
+ * The own **numeric** field names of a value three clones per material and a game then mutates in
84
+ * place — a `Color`, a `Vector2`, an `Euler` — or `undefined` when the value is not one of those.
85
+ *
86
+ * Recognised structurally, by "every own value is a primitive and at least one is a number", rather
87
+ * than by naming each class: that is what a clone of a `MeshStandardMaterial` actually holds, it
88
+ * covers the classes a future three adds, and it refuses anything it cannot read — a `Texture`,
89
+ * which is compared by identity, and anything else, which is opaque. The *numbers* are what come
90
+ * back, because a boolean or a string field is not something the compare could ever see move: a
91
+ * `Color`'s `isColor` reads as `0` before and after, so recording it would cost a read per frame to
92
+ * compare a value against itself. The *names* are recorded rather than the objects so the per-frame
93
+ * read is a handful of field reads: a material animating `normalScale` or an `envMapRotation` is
94
+ * caught, and one that set `needsUpdate` is not mistaken for one that did.
95
+ */
96
+ function componentFieldsOf(value: unknown): string[] | undefined {
97
+ if (value === null || typeof value !== "object" || isTextureValue(value)) return undefined;
98
+ const fields = Object.keys(value);
99
+ if (fields.length === 0) return undefined;
100
+ const numbers: string[] = [];
101
+ for (const field of fields) {
102
+ const kind = typeof (value as Record<string, unknown>)[field];
103
+ if (kind === "number") numbers.push(field);
104
+ else if (kind !== "boolean" && kind !== "string") return undefined;
105
+ }
106
+ return numbers.length === 0 ? undefined : numbers;
107
+ }
108
+
109
+ /**
110
+ * How the recorded fields of one component-compared value are read back each frame.
111
+ *
112
+ * A field read by name through a variable — `value[name]` — is a dictionary lookup the engine
113
+ * cannot fold, and it is the single most expensive thing a settled frame did: four small objects
114
+ * per material, and every one of them looked up field by field. Naming the field instead makes it
115
+ * a fixed offset, so the three shapes three actually clones (`Color`, `Vector2`, `Euler`) are read
116
+ * directly and anything a future material brings falls back to the general path rather than being
117
+ * assumed a shape it is not.
118
+ */
119
+ function readKindOf(fields: string[]): number {
120
+ if (fields.length === 3) {
121
+ if (fields[0] === "r" && fields[1] === "g" && fields[2] === "b") return READ_RGB;
122
+ if (fields[0] === "_x" && fields[1] === "_y" && fields[2] === "_z") return READ_UNDERSCORE_XYZ;
123
+ return READ_KEYED;
124
+ }
125
+ if (fields.length === 2 && fields[0] === "x" && fields[1] === "y") return READ_XY;
126
+ return READ_KEYED;
127
+ }
128
+
129
+ /** The base colour a tinted group carries per instance, or `undefined` when the material has none. */
130
+ export function baseColorOf(material: Material): Color | undefined {
131
+ const color = (material as { color?: Color }).color;
132
+ return color !== undefined && typeof color.r === "number" ? color : undefined;
133
+ }
134
+
135
+ /** Whether a material may be the shared half of a uniform group at all. */
136
+ export function uniformEligible(material: Material): boolean {
137
+ return tintableMaterial(material) && baseColorOf(material) !== undefined;
138
+ }
139
+
140
+ function opaqueIdOf(value: object): number {
141
+ const existing = opaqueIds.get(value);
142
+ if (existing !== undefined) return existing;
143
+ const id = nextOpaqueId;
144
+ nextOpaqueId += 1;
145
+ opaqueIds.set(value, id);
146
+ return id;
147
+ }
148
+
149
+ /** How a value speaks for itself in a signature, which is built once per classification. */
150
+ function signatureTextOf(
151
+ value: unknown,
152
+ fields: string[] | undefined,
153
+ components: number[],
154
+ ): string {
155
+ if (fields !== undefined) return `v${components.slice(-fields.length).join(",")}`;
156
+ if (isTextureValue(value)) return `t${(value as { uuid?: string }).uuid ?? ""}`;
157
+ if (value !== null && typeof value === "object") return `o${opaqueIdOf(value)}`;
158
+ return `${typeof value}:${String(value)}`;
159
+ }
160
+
161
+ /**
162
+ * The signature two materials must share to draw in one uniform group, or `undefined` when this
163
+ * material may not be in one. Also refreshes the material's cached values, so the per-frame proof
164
+ * below always compares against what this classification saw.
165
+ */
166
+ export function uniformSignatureOf(material: Material): string | undefined {
167
+ if (!uniformEligible(material)) return undefined;
168
+ const keys = Object.keys(material);
169
+ const values = Object.values(material);
170
+ let record = records.get(material);
171
+ if (record === undefined) {
172
+ record = {
173
+ values: [],
174
+ identityRanges: new Int32Array(0),
175
+ vectorSlots: [],
176
+ vectorReads: new Uint8Array(0),
177
+ vectorWidths: new Uint8Array(0),
178
+ vectorFields: [],
179
+ components: [],
180
+ defineKeys: [],
181
+ defineValues: [],
182
+ signature: "",
183
+ };
184
+ records.set(material, record);
185
+ }
186
+ const { vectorSlots, vectorFields, components, defineKeys, defineValues } = record;
187
+ const reads: number[] = [];
188
+ const widths: number[] = [];
189
+ vectorSlots.length = 0;
190
+ vectorFields.length = 0;
191
+ components.length = 0;
192
+ const ranges: number[] = [];
193
+ let open = -1;
194
+ let signature = "";
195
+ for (let index = 0; index < keys.length; index += 1) {
196
+ const key = keys[index] as string;
197
+ const value = values[index];
198
+ const fields = UNREAD_PROPERTIES.has(key) ? undefined : componentFieldsOf(value);
199
+ if (fields === undefined) {
200
+ // The unread and the component-compared are both left out of the identity walk, and either
201
+ // one ends a run of it: the run is only worth having while the positions are consecutive.
202
+ if (UNREAD_PROPERTIES.has(key)) continue;
203
+ if (open === -1) open = index;
204
+ } else {
205
+ if (open !== -1) {
206
+ ranges.push(open, index);
207
+ open = -1;
208
+ }
209
+ vectorSlots.push(index);
210
+ reads.push(readKindOf(fields));
211
+ widths.push(fields.length);
212
+ for (const field of fields) {
213
+ vectorFields.push(field);
214
+ components.push((value as Record<string, number>)[field] as number);
215
+ }
216
+ }
217
+ signature += `${key}=${signatureTextOf(value, fields, components)}|`;
218
+ }
219
+ if (open !== -1) ranges.push(open, keys.length);
220
+ record.values = values;
221
+ record.identityRanges = Int32Array.from(ranges);
222
+ record.vectorReads = Uint8Array.from(reads);
223
+ record.vectorWidths = Uint8Array.from(widths);
224
+ defineKeys.length = 0;
225
+ defineValues.length = 0;
226
+ const defines = material.defines as Record<string, unknown> | undefined;
227
+ if (defines !== null && defines !== undefined) {
228
+ for (const key in defines) {
229
+ defineKeys.push(key);
230
+ defineValues.push(defines[key]);
231
+ signature += `defines.${key}=${signatureTextOf(defines[key], undefined, [])}|`;
232
+ }
233
+ }
234
+ record.signature = signature;
235
+ return signature;
236
+ }
237
+
238
+ /** The three shapes three clones per material, read by name rather than through a variable. */
239
+ interface IComponents {
240
+ r: number;
241
+ g: number;
242
+ b: number;
243
+ x: number;
244
+ y: number;
245
+ _x: number;
246
+ _y: number;
247
+ _z: number;
248
+ }
249
+
250
+ /**
251
+ * Whether the material still holds every value its group's shared draw was built from.
252
+ *
253
+ * False is the safe answer: the member leaves the group and is drawn exactly, and the frame's
254
+ * classification is re-derived. True is a claim about the material's whole own value list, read
255
+ * whether or not any of it moved.
256
+ */
257
+ export function uniformUnchanged(material: Material): boolean {
258
+ const record = records.get(material);
259
+ // No fingerprint means no group was ever derived from this material, so nothing to have drifted
260
+ // from. Re-deriving the classification is what establishes one.
261
+ if (record === undefined) return true;
262
+ // One read of the material's own values, and the count that says whether a property appeared or
263
+ // disappeared comes with it. Reading each value by name instead is what this replaced, and it is
264
+ // several times slower: an engine cannot fold a lookup through a variable, so eighty-one of
265
+ // them per material is eighty-one dictionary probes, against one copy of eighty-one slots.
266
+ const current = Object.values(material);
267
+ const {
268
+ values,
269
+ identityRanges,
270
+ vectorSlots,
271
+ vectorReads,
272
+ vectorWidths,
273
+ vectorFields,
274
+ components,
275
+ } = record;
276
+ if (current.length !== values.length) return false;
277
+ for (let range = 0; range < identityRanges.length; range += 2) {
278
+ const end = identityRanges[range + 1] as number;
279
+ for (let index = identityRanges[range] as number; index < end; index += 1) {
280
+ if (current[index] !== values[index]) return false;
281
+ }
282
+ }
283
+ let read = 0;
284
+ for (let slot = 0; slot < vectorSlots.length; slot += 1) {
285
+ const value = current[vectorSlots[slot] as number];
286
+ if (value === null || typeof value !== "object") return false;
287
+ const parts = value as IComponents;
288
+ switch (vectorReads[slot]) {
289
+ case READ_RGB:
290
+ if (
291
+ parts.r !== components[read] ||
292
+ parts.g !== components[read + 1] ||
293
+ parts.b !== components[read + 2]
294
+ )
295
+ return false;
296
+ break;
297
+ case READ_XY:
298
+ if (parts.x !== components[read] || parts.y !== components[read + 1]) return false;
299
+ break;
300
+ case READ_UNDERSCORE_XYZ:
301
+ if (
302
+ parts._x !== components[read] ||
303
+ parts._y !== components[read + 1] ||
304
+ parts._z !== components[read + 2]
305
+ )
306
+ return false;
307
+ break;
308
+ default: {
309
+ const fields = value as Record<string, number>;
310
+ const width = vectorWidths[slot] as number;
311
+ for (let part = 0; part < width; part += 1) {
312
+ if (fields[vectorFields[read + part] as string] !== components[read + part]) return false;
313
+ }
314
+ }
315
+ }
316
+ read += vectorWidths[slot] as number;
317
+ }
318
+ const defines = material.defines as Record<string, unknown> | undefined;
319
+ const defineKeys = record.defineKeys;
320
+ let count = 0;
321
+ if (defines !== null && defines !== undefined) {
322
+ for (const key in defines) {
323
+ if (key !== defineKeys[count] || defines[key] !== record.defineValues[count]) return false;
324
+ count += 1;
325
+ }
326
+ }
327
+ return count === defineKeys.length;
328
+ }
package/src/random.ts ADDED
@@ -0,0 +1,41 @@
1
+ export interface IRandom {
2
+ (): number;
3
+ pick<T>(items: readonly T[]): T;
4
+ range(min: number, max: number): number;
5
+ state: number;
6
+ }
7
+
8
+ export function createRandom(seed?: number): IRandom {
9
+ if (seed !== undefined && !Number.isFinite(seed)) throw new TypeError("seed must be finite.");
10
+ let state = seed === undefined ? 0 : seed >>> 0;
11
+ const deterministic = seed !== undefined;
12
+ const random = (() => {
13
+ if (!deterministic) return Math.random();
14
+ state = (Math.imul(1_664_525, state) + 1_013_904_223) >>> 0;
15
+ return state / 4_294_967_296;
16
+ }) as IRandom;
17
+ random.range = (min, max) => {
18
+ if (!Number.isFinite(min) || !Number.isFinite(max) || max <= min) {
19
+ throw new RangeError("range requires finite bounds with max greater than min.");
20
+ }
21
+ return min + random() * (max - min);
22
+ };
23
+ random.pick = <T>(items: readonly T[]) => {
24
+ if (items.length === 0) throw new RangeError("pick requires a non-empty list.");
25
+ return items[Math.floor(random() * items.length)] as T;
26
+ };
27
+ Object.defineProperty(random, "state", {
28
+ get: () => {
29
+ if (!deterministic) throw new Error("state is unavailable on an unseeded random.");
30
+ return state;
31
+ },
32
+ set: (value: number) => {
33
+ if (!deterministic) throw new Error("state is unavailable on an unseeded random.");
34
+ if (!Number.isFinite(value) || !Number.isInteger(value)) {
35
+ throw new TypeError("state must be a finite integer.");
36
+ }
37
+ state = value >>> 0;
38
+ },
39
+ });
40
+ return random;
41
+ }