@threenative/core 0.3.10 → 0.3.12

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 (158) hide show
  1. package/capabilities.json +1096 -26
  2. package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
  3. package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-CWuFRT7G.d.ts} +1 -1
  4. package/dist/chunk-2Z44XSI2.js +35 -0
  5. package/dist/chunk-5O73CDMR.js +1849 -0
  6. package/dist/chunk-6KLNISRJ.js +153 -0
  7. package/dist/chunk-7UUT3TY5.js +133 -0
  8. package/dist/chunk-CFGTPFUH.js +231 -0
  9. package/dist/chunk-DBELTTTM.js +615 -0
  10. package/dist/chunk-GMHATRPW.js +7701 -0
  11. package/dist/chunk-GXY7B3JG.js +46 -0
  12. package/dist/chunk-KIRUKTBQ.js +187 -0
  13. package/dist/chunk-N5LRP4BR.js +220 -0
  14. package/dist/chunk-OKXUCASC.js +2238 -0
  15. package/dist/chunk-QWJEMUUT.js +94 -0
  16. package/dist/chunk-VAZM36TV.js +497 -0
  17. package/dist/{game-DzMcitDO.d.ts → game-D7pAzZUY.d.ts} +15 -3
  18. package/dist/hot.d.ts +3 -3
  19. package/dist/hot.js +2 -46
  20. package/dist/index.d.ts +329 -302
  21. package/dist/index.js +10565 -22533
  22. package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-B6BMqS1r.d.ts} +33 -3
  23. package/dist/playtest.d.ts +3 -3
  24. package/dist/playtest.js +24 -75
  25. package/dist/react-css.js +1 -215
  26. package/dist/react.d.ts +2 -2
  27. package/dist/{renderer-rZm0hVqq.d.ts → renderer-DvFVoAgj.d.ts} +13 -4
  28. package/dist/spells/index.d.ts +643 -0
  29. package/dist/spells/index.js +1558 -0
  30. package/dist/spells/ultra/index.d.ts +1600 -0
  31. package/dist/spells/ultra/index.js +6804 -0
  32. package/dist/terrain-jobs-worker.js +1 -123
  33. package/dist/two-sided-foliage-W523J3UM.js +1 -0
  34. package/dist/ui-layer.js +5 -384
  35. package/dist/worker.js +4 -41
  36. package/dist/world.d.ts +725 -259
  37. package/dist/world.js +16714 -18284
  38. package/mcp/animation-server.mjs +1 -1
  39. package/mcp/animation.mjs +2 -3
  40. package/mcp/engine-server.mjs +142 -47
  41. package/mcp/servers.mjs +9 -2
  42. package/mcp/terrain.mjs +8 -0
  43. package/package.json +14 -6
  44. package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
  45. package/patches/three@0.187.0.patch +2004 -407
  46. package/src/assets.ts +9 -2
  47. package/src/card-lod.ts +152 -0
  48. package/src/compressed-texture-preparation.ts +1008 -0
  49. package/src/frame-budget.ts +31 -20
  50. package/src/game-dev-tools.ts +93 -0
  51. package/src/game-frame-loop.ts +995 -0
  52. package/src/game-renderer-metrics.ts +77 -0
  53. package/src/game-startup.ts +1001 -0
  54. package/src/game-validate.ts +70 -0
  55. package/src/game.ts +253 -1935
  56. package/src/index.ts +27 -2
  57. package/src/launch-diagnostics.ts +22 -12
  58. package/src/look/ao-admission.ts +24 -0
  59. package/src/look/filmic-tone-mapping.ts +15 -13
  60. package/src/look/look-diagnostics.ts +3 -3
  61. package/src/look/quality.ts +10 -5
  62. package/src/look/world-environment.ts +76 -9
  63. package/src/loop.ts +29 -0
  64. package/src/model-lod.ts +42 -3
  65. package/src/particle-kinds.ts +515 -0
  66. package/src/picking.ts +17 -3
  67. package/src/playtest.ts +27 -1
  68. package/src/projection-apply.ts +4 -1
  69. package/src/projection-plan.ts +9 -6
  70. package/src/render/mesh-pool.ts +1 -1
  71. package/src/render/resolution-subrect.ts +2 -0
  72. package/src/render/rough-diffuse.ts +46 -12
  73. package/src/render/soft-shadow.ts +27 -12
  74. package/src/render/virtual-shadow-adaptive.ts +55 -0
  75. package/src/render/virtual-shadow-casters.ts +175 -0
  76. package/src/render/virtual-shadow-layers.ts +76 -0
  77. package/src/render/virtual-shadow-level.ts +224 -0
  78. package/src/render/virtual-shadow-markers.ts +193 -0
  79. package/src/render/virtual-shadow-options.ts +370 -0
  80. package/src/render/virtual-shadow-pages.ts +5 -0
  81. package/src/render/virtual-shadow.ts +213 -992
  82. package/src/render/world-impostor-surface.ts +293 -34
  83. package/src/render-camera-cull.ts +32 -1
  84. package/src/render-pass-budget.ts +34 -7
  85. package/src/renderProjection.ts +22 -0
  86. package/src/renderer.ts +92 -37
  87. package/src/snow-field.ts +1 -1
  88. package/src/spells/cast-pool.ts +361 -0
  89. package/src/spells/index.ts +177 -0
  90. package/src/spells/mechanisms.ts +295 -0
  91. package/src/spells/reaction-field.ts +415 -0
  92. package/src/spells/spell-lights.ts +139 -0
  93. package/src/spells/spell-world.ts +187 -0
  94. package/src/spells/spell.ts +397 -0
  95. package/src/spells/spells.ts +283 -0
  96. package/src/spells/ultra/engine/camera.ts +184 -0
  97. package/src/spells/ultra/engine/quality.ts +96 -0
  98. package/src/spells/ultra/index.ts +52 -0
  99. package/src/spells/ultra/main.ts +305 -0
  100. package/src/spells/ultra/scene/arenaMaterial.ts +325 -0
  101. package/src/spells/ultra/spells/cast.ts +233 -0
  102. package/src/spells/ultra/spells/fire.ts +1267 -0
  103. package/src/spells/ultra/vfx/FluidVolume.ts +816 -0
  104. package/src/spells/ultra/vfx/FluidVolume.tsl.ts +855 -0
  105. package/src/spells/ultra/vfx/FxTarget.ts +61 -0
  106. package/src/spells/ultra/vfx/GroundField.ts +1399 -0
  107. package/src/spells/ultra/vfx/Particles.ts +1475 -0
  108. package/src/spells/ultra/vfx/Ribbons.ts +645 -0
  109. package/src/spells/ultra/vfx/Shockwaves.ts +286 -0
  110. package/src/spells/ultra/vfx/Volumes.ts +489 -0
  111. package/src/spells/ultra/vfx/common.ts +1195 -0
  112. package/src/spells/ultra/vfx/noise.ts +441 -0
  113. package/src/startup-readiness.ts +128 -34
  114. package/src/version.ts +1 -1
  115. package/src/warmup.ts +56 -8
  116. package/src/water-surface.ts +9 -3
  117. package/src/world-bundle-safety.ts +401 -0
  118. package/src/world-cell-batches.ts +1216 -0
  119. package/src/world-cell-chunks.ts +1132 -0
  120. package/src/world-cell-data.ts +995 -0
  121. package/src/world-cell-impostors.ts +1449 -0
  122. package/src/world-cell-lod-bias.ts +151 -0
  123. package/src/world-cell-models.ts +1074 -0
  124. package/src/world-cell-options.ts +189 -0
  125. package/src/world-cell-placements.ts +694 -0
  126. package/src/world-cell-prewarm.ts +557 -0
  127. package/src/world-cell-readiness.ts +357 -0
  128. package/src/world-cell-render-bundles.ts +459 -0
  129. package/src/world-cell-render-gpu-dress.ts +714 -0
  130. package/src/world-cell-render-main-cull.ts +484 -0
  131. package/src/world-cell-render-shadow-claims.ts +198 -0
  132. package/src/world-cell-render-util.ts +246 -0
  133. package/src/world-cell-render.ts +1493 -0
  134. package/src/world-cell-residency.ts +745 -0
  135. package/src/world-cells.ts +1318 -10418
  136. package/src/world-chunk-shadow.ts +308 -0
  137. package/src/world-collar.ts +315 -0
  138. package/src/world-gpu-flags.ts +163 -0
  139. package/src/world-gpu-readback.ts +881 -0
  140. package/src/world-gpu-reference.ts +510 -0
  141. package/src/world-gpu-scene.ts +214 -2264
  142. package/src/world-gpu-shadow.ts +190 -0
  143. package/src/world-gpu-types.ts +381 -0
  144. package/src/world-gpu-validation.ts +518 -0
  145. package/src/world-heightfield.ts +802 -0
  146. package/src/world-load-limiter.ts +106 -0
  147. package/src/world-noise.ts +241 -0
  148. package/src/world-package.ts +42 -0
  149. package/src/world-region.ts +1 -1
  150. package/src/world-terrain-splat.ts +365 -149
  151. package/src/world-tile-budget.ts +256 -0
  152. package/src/world-tile-geometry.ts +469 -0
  153. package/src/world-tile-lod.ts +408 -0
  154. package/src/world-tile-observation.ts +566 -0
  155. package/src/world-tile-stitch.ts +166 -0
  156. package/src/world-tiles.ts +170 -1745
  157. package/src/world-water-bodies.ts +359 -0
  158. package/src/world.ts +35 -741
@@ -0,0 +1,246 @@
1
+ import { BufferGeometry, InstancedMesh, type Material, type Mesh, type Object3D } from "three";
2
+ import { isStatic, markStatic } from "./static-transform.js";
3
+ import type { SharedBatch } from "./world-cell-batches.js";
4
+ import { clusterOf } from "./world-cell-data.js";
5
+
6
+ /**
7
+ * The pieces of world-cell rendering that hold no world: the constants the cull, the shadow claims
8
+ * and the bundles all read, and the small mesh and hash helpers every one of them calls.
9
+ *
10
+ * Nothing here reads a cell, a batch or a scene, so any of the modules PRD-798 split `CellRender`
11
+ * into can use it without a host of its own.
12
+ */
13
+
14
+ /**
15
+ * Instance-matrix bytes a world keeps in released shared batches, oldest dropped first. A batch is
16
+ * sized to its asset's largest cell times the resident cells, so on a 2 km package one runs 80 kB
17
+ * median and 0.9 MB worst, and every key in the world at once is 26 MB. The budget holds the walk's
18
+ * own back-traffic — a batch leaves when the last cell holding its asset leaves, and is wanted
19
+ * again a few hundred metres back — and lets the rest go, so a session that crosses the whole map
20
+ * does not carry the whole map.
21
+ */
22
+ export const RETIRED_SHARED_BYTES = 8_000_000;
23
+
24
+ /**
25
+ * How far past the frustum a world-grid square is kept inside the main pass's draw window, in metres.
26
+ *
27
+ * The window is decided once per draw, from the camera that draw uses, and a square that has just
28
+ * left the frustum is dropped from it — so a square at the frustum's edge would be drawn in one
29
+ * frame and not the next, and a tree on that edge pops in at the edge of the screen. Growing each
30
+ * square's box by this margin keeps a band of it in the window while it is still off screen, so a
31
+ * tree crosses into view already there.
32
+ *
33
+ * An eighth of the default 64 m square is deliberately small. It has to exceed what a camera
34
+ * travels between two draws — 8 m a frame is 480 m/s at 60 fps, so no playable camera reaches it —
35
+ * and nothing more, because every metre of it is a metre of geometry the vertex stage still shades.
36
+ */
37
+ export const MAIN_CULL_PAD_METRES = 8;
38
+ /**
39
+ * Fade band, as a share of each gate, for an undithered LOD switch: 1 mm at a 10 m gate and 15 mm at
40
+ * 150 m, so every pixel of a placement takes the same level. Nonzero, so the mask never divides zero
41
+ * by zero exactly at the gate.
42
+ */
43
+ export const UNDITHERED_LOD_WIDTH = 1e-4;
44
+
45
+ const slotFree = new WeakSet<Material>();
46
+ /**
47
+ * Refuse a part material whose node graph reads `instanceIndex`. WorldCells repacks instance slots on
48
+ * every refilter, main-pass cull and LOD switch, so a value hashed from the slot moves to another
49
+ * placement as the viewer walks: Strata's wind phase snapped each crown's top about every 0.7 s.
50
+ * ponytail: walks the material's `*Node` slots as built; an index read hidden inside an unbuilt `Fn`
51
+ * body is not seen until a builder runs it.
52
+ */
53
+ export function assertSlotFree(material: Material, assetId: string): void {
54
+ if (slotFree.has(material)) return;
55
+ const seen = new Set<object>();
56
+ const reads = (value: unknown): boolean => {
57
+ if (typeof value !== "object" || value === null || seen.has(value)) return false;
58
+ seen.add(value);
59
+ const node = value as { isNode?: boolean; isIndexNode?: boolean; scope?: string };
60
+ if (node.isIndexNode === true && node.scope === "instance") return true;
61
+ for (const child of Object.values(value))
62
+ if (
63
+ Array.isArray(child)
64
+ ? child.some(reads)
65
+ : (child as { isNode?: boolean } | null)?.isNode === true && reads(child)
66
+ )
67
+ return true;
68
+ return false;
69
+ };
70
+ for (const [key, value] of Object.entries(material))
71
+ if (
72
+ key.endsWith("Node") &&
73
+ (value as { isNode?: boolean } | null)?.isNode === true &&
74
+ reads(value)
75
+ )
76
+ throw new Error(
77
+ `WorldCells asset "${assetId}" material "${material.name}" reads instanceIndex in ${key}. WorldCells repacks instance slots as the viewer moves, so a per-instance value from the slot jumps between placements; use instanceRandom() from @threenative/core/world.`,
78
+ );
79
+ slotFree.add(material);
80
+ }
81
+
82
+ /**
83
+ * Side of the main cull's own visibility cell, in metres, when it divides the package's `cellSize`.
84
+ *
85
+ * This is the finest grid the main pass can cull on, and it is not a free parameter: a shared
86
+ * batch's block is one *world* cell's records and placements never move, so a world cell cannot be
87
+ * spread across two cells of a finer grid without giving every world cell its own block — one more
88
+ * main mesh per key, which is the cost this whole cull exists to avoid. So a package whose
89
+ * `cellSize` is not a whole multiple of 32 m falls back to its own `cellSize`, which is the square
90
+ * its placements are already cut on, and the rule below is the whole of it.
91
+ *
92
+ * A visibility cell is a sub-square of the shadow caster's world-grid square, and it is deliberately
93
+ * finer than that square: `clusterSize` is sized for how many meshes a shadow level submits, while
94
+ * this is sized for how little geometry the vertex stage is handed. It is also the one volume the
95
+ * cull tests, so everything in it — a 64 m cell's XZ and the Y its own placements and asset bounds
96
+ * reach — is what a draw of that cell costs. See {@link WorldCells.#cullMainPass}.
97
+ */
98
+
99
+ export const MAIN_CULL_SQUARE_METRES = 32;
100
+
101
+ /**
102
+ * How often `TN_WORLD_MAIN_CULL` is printed. Five seconds is a line a playtest capture can hold and
103
+ * a walk long enough to see the window move; every frame is a wall of text over the marker it would
104
+ * report on.
105
+ */
106
+ export const MAIN_CULL_MARKER_MS = 5e3;
107
+
108
+ /** Separate shadow regions one tell carries per kind before the last one absorbs the rest. */
109
+ export const SHADOW_REGION_CAP = 64;
110
+
111
+ /**
112
+ * The name half a shadow key is minted under: `asset:level:part@gpu`, beside the main key it twins
113
+ * rather than in place of it, so both meshes exist — one for the main pass's compaction buffer and
114
+ * one for the shadow twin's, which is the whole difference between them.
115
+ */
116
+ export const GPU_KEY_SUFFIX = "@gpu";
117
+
118
+ /**
119
+ * How many groups the main record is split into. A record walks every draw in its group, so a mint
120
+ * on a streaming walk costs the draws of every shard it touches: Machinefall's ~200 recorded main
121
+ * draws re-recorded as one group put ~290 traversed draws in a frame. Eight shards still read 145,
122
+ * because one asset mints several keys (levels, parts) at once and they land in two or three shards;
123
+ * thirty-two keep a frame that touches several to a few dozen draws. A replay is one
124
+ * `executeBundles` per group, so a shard costs far less than the draws it stops re-walking.
125
+ */
126
+ const MAIN_BUNDLE_SHARDS = 32;
127
+
128
+ /** The shard a key lands in, from its name (FNV-1a), so a key keeps its group for its whole life. */
129
+ export function mainShard(key: string): number {
130
+ let hash = 0x811c9dc5;
131
+ for (let index = 0; index < key.length; index += 1) {
132
+ hash ^= key.charCodeAt(index);
133
+ hash = Math.imul(hash, 0x01000193);
134
+ }
135
+ return (hash >>> 0) % MAIN_BUNDLE_SHARDS;
136
+ }
137
+
138
+ /** Every mesh a prepared chunk draws through the main pass, in traversal order. */
139
+ export function chunkDraws(chunk: Object3D): Mesh[] {
140
+ const draws: Mesh[] = [];
141
+ chunk.traverse((node) => {
142
+ const mesh = node as Mesh;
143
+ if (mesh.isMesh === true && node.layers.isEnabled(0)) draws.push(mesh);
144
+ });
145
+ return draws;
146
+ }
147
+
148
+ /**
149
+ * A geometry that draws `source`'s shape through an indirect record: its own object, so the record is
150
+ * this mesh's, over the same attributes and index, so no vertex buffer is copied.
151
+ */
152
+ export function indirectView(
153
+ source: BufferGeometry,
154
+ args: Parameters<BufferGeometry["setIndirect"]>[0],
155
+ offset: number,
156
+ ): BufferGeometry {
157
+ const view = new BufferGeometry();
158
+ for (const [name, attribute] of Object.entries(source.attributes))
159
+ view.setAttribute(name, attribute);
160
+ view.setIndex(source.index);
161
+ view.morphAttributes = source.morphAttributes;
162
+ view.morphTargetsRelative = source.morphTargetsRelative;
163
+ for (const group of source.groups) view.addGroup(group.start, group.count, group.materialIndex);
164
+ view.boundingBox = source.boundingBox;
165
+ view.boundingSphere = source.boundingSphere;
166
+ view.setIndirect(args, offset);
167
+ return view;
168
+ }
169
+
170
+ /** The materials `redressMaterial` gave a mesh, disposed when the mesh is or when it is re-dressed. */
171
+ const redressed = new WeakMap<InstancedMesh, readonly Material[]>();
172
+
173
+ /**
174
+ * A new object drawing `geometry`, carrying everything `old` carried.
175
+ *
176
+ * Three builds an `InstancedMesh`'s instancing node when the object is first compiled, from the
177
+ * `instanceMatrix` it holds then, and keeps that node for the object's whole life — so a mesh the
178
+ * prewarm already compiled is not pointed at the GPU scene's storage buffer by writing a field onto
179
+ * it. Measured on machinefall: 3,724 indirect draws submitted in 2 s, over indirect records and
180
+ * drawn matrices that were correct at draw time, and a dressed forest that drew nothing at all,
181
+ * until every dressed mesh was a fresh object. A fresh object costs a uuid and a node build, which
182
+ * is what the prewarm paid for a mesh that was about to be replaced anyway; see `#dressGpu`.
183
+ */
184
+ export function freshMeshFor(
185
+ old: InstancedMesh,
186
+ geometry: BufferGeometry,
187
+ count: number,
188
+ ): InstancedMesh {
189
+ const next = new InstancedMesh(geometry, old.material, count);
190
+ next.name = old.name;
191
+ next.layers.mask = old.layers.mask;
192
+ next.castShadow = old.castShadow;
193
+ next.receiveShadow = old.receiveShadow;
194
+ next.renderOrder = old.renderOrder;
195
+ next.userData = old.userData;
196
+ next.matrix.copy(old.matrix);
197
+ next.matrixAutoUpdate = old.matrixAutoUpdate;
198
+ next.matrixWorld.copy(old.matrixWorld);
199
+ next.matrixWorldAutoUpdate = old.matrixWorldAutoUpdate;
200
+ // A dressed batch is frozen, and the freeze is keyed by the object: a replacement that did not
201
+ // re-arm it would compose its own world matrix every walk forever.
202
+ if (isStatic(old)) markStatic(next);
203
+ return next;
204
+ }
205
+
206
+ /**
207
+ * Give a mesh a material of its own, so the node three builds for it is its own rather than a shared
208
+ * one's, and dispose those materials with the mesh. The clones share every texture; only the
209
+ * material objects are new. See `#dressGpu`.
210
+ */
211
+ /**
212
+ * The world-grid square the follow point is in, as the `@x,z` half of a cluster key: the square a
213
+ * cell's caster cluster is named for, and the one a prewarm mints first.
214
+ */
215
+ export function followCluster(
216
+ follow: { readonly position: { x: number; z: number } },
217
+ minX: number,
218
+ minZ: number,
219
+ cellSize: number,
220
+ cellsPerCluster: number,
221
+ ): string {
222
+ return clusterOf(
223
+ Math.floor((follow.position.x - minX) / cellSize),
224
+ Math.floor((follow.position.z - minZ) / cellSize),
225
+ cellsPerCluster,
226
+ );
227
+ }
228
+
229
+ /** What a released batch costs to keep: its instance buffer, which is all of it. */
230
+ export function heldBytes(shared: SharedBatch): number {
231
+ return (shared.mesh.instanceMatrix.array as Float32Array).byteLength;
232
+ }
233
+
234
+ export function redressMaterial(mesh: InstancedMesh): void {
235
+ for (const material of redressed.get(mesh) ?? []) material.dispose();
236
+ const clones: Material[] = [];
237
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material])
238
+ clones.push(material.clone());
239
+ mesh.material = Array.isArray(mesh.material) ? clones : (clones[0] as Material);
240
+ if (!redressed.has(mesh))
241
+ mesh.addEventListener("dispose", () => {
242
+ for (const material of redressed.get(mesh) ?? []) material.dispose();
243
+ redressed.delete(mesh);
244
+ });
245
+ redressed.set(mesh, clones);
246
+ }