@adatechnology/game-destruction-three 0.25.0 → 0.26.1

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 CHANGED
@@ -151,8 +151,15 @@ pane shatters and a thrown chair can lose a leg. `Piece.localCenter` is the cent
151
151
 
152
152
  - Particles are two fixed-size ring buffers (additive 40 %, normal 60 %), one draw call each; dead particles cost a loop iteration, nothing else.
153
153
  - Fragments share one box geometry, six shard geometries, six of each wood shape (sliver, broken board, MDF lump, plywood veneer; 20 to 100 triangles each) and one material per source material id and colour; they shrink away instead of fading.
154
+ - The fragments in flight draw instanced: one `InstancedMesh` (named `fragment-instances`) per shared geometry and material, and one shadow draw, instead of a draw and a shadow draw
155
+ each (a blast's ~200 fragments were ~400 draw calls). Each fragment keeps its own mesh in the scene (name `fragment`, origin, transform), moved to camera layer 29
156
+ (`INSTANCED_FRAGMENT_LAYER`) while an instance draws it; glass (transparent, sorted) and a fragment carrying bullet holes draw on their own. At rest a fragment draws on its own
157
+ again and the settled batches take it (a batched one woken waits for the batch to give it back its layers); an emptied instanced mesh leaves the scene and its instance
158
+ buffer is freed. The instanced meshes answer no ray: a raycaster that enables layer 29 finds the fragments' own meshes. The resting ones look under them only off the ground: one lying
159
+ on the terrain casts no ray (`surfaceAt`), and the box a fragment lands on is kept until that fragment moves.
154
160
  - Decals share one disc geometry and one material; a mesh keeps at most 48 and the renderer 600.
155
161
  - Heat, char and melt are only read for "active" parts (hot, burning, melting) plus a scan of all parts every 0.4 s.
162
+ - The first renderer runs one blast on a small wall off screen, once per engine (`jitWarmUp`, on by default, about 0.4 s at load), so the first real explosion does not run cold (it cost about twice the later ones). A host on an engine passes the engine it made its world from, `jitWarmUp: { engine }`, or only the kit's own solver and the shared code are warmed; `false` turns it off. A warm-up that fails is skipped with one warning: the game starts without it.
156
163
  - The explosion light is created on the first explosion (one light in the scene afterwards, intensity 0 at rest) so plan for the one-time shader recompile.
157
164
  - Melt assumes upright meshes (it scales the mesh along its own Y).
158
165
  - Debris at rest can cost no simulation and few draws; both are opt-in, so a host that sets nothing simulates and draws exactly as before. Over `maxAwakePieces`
@@ -164,6 +171,83 @@ pane shatters and a thrown chair can lose a leg. `Piece.localCenter` is the cent
164
171
  vertices draw on their own. `settledBatchStats()` reports the batches. The test room turns the batches on; its awake budget stays off unless
165
172
  `debris.maxAwakePieces` / `maxAwakeFragments` are given (`SUGGESTED_MAX_AWAKE_PIECES`, `SUGGESTED_MAX_AWAKE_FRAGMENTS`).
166
173
 
174
+ ### The explosion's work in a frame budget (`workBudget`, opt-in)
175
+
176
+ Left out, the work a blow leaves (loose groups to cut into pieces, fragments to spawn) is cut by the clock (`eventBudgetMs`, or a shared frame budget's deadline), as before.
177
+ With `workBudget: {}` it is cut by a budget of **work units**, never of time (spec 008, `design/orcamento-da-explosao.md`, evidence T3.8aa): one arbiter (`WorkBudget` of
178
+ `game-physics`) that the events, the splits, the fragments and the blast's wake queue draw from, in that order. The events still apply in their frame (flash, dust, cracks,
179
+ the part hidden), and each is charged `perEvent`. Each loose group joins a queue (`WorkQueue`, data only), ranked by its distance to `focus` (read once, when it came loose),
180
+ then by its smallest part id, and charged by the size of its work (`splitWorkUnits`: as if none of its boxes were fitted ahead, so the schedule never depends on a cache).
181
+ Until its turn the group stays in its object, drawn and solid where it stood; none stands loose more than `maxWaitFrames` frames (a group loosened in frame F is a piece by
182
+ F + 12), and the queue moves one a frame at least. The first split an event makes in a frame always runs in it, over the budget if need be. No clock is read: the same session
183
+ cuts the same pieces in the same frames on every machine of the same `tier`. With the blast's wake queue on, set `debrisCost: { source: 'fixed' }` too, or its batches still
184
+ follow the measured step.
185
+
186
+ **Registering with a frame budget brings the clock back.** When `deferredQueue()` is registered with a game-runtime `FrameBudget`, that budget's clock decides in which frames
187
+ the work step runs at all, so the frames the pieces are cut in follow the machine again: determinism holds only with the queue left unregistered (the renderer drains it in
188
+ its own `update`).
189
+
190
+ The split queue is not part of a save. After a load, an object bound (`bind`) or drawn again (`redraw`) with more than one group has its loose groups queued in the next work
191
+ step, so none stays loose in its object; a queued group whose parts were shattered meanwhile still splits by the parts it has left, and one whose parts are no longer loose
192
+ gives its place to the object's loose groups as they are then. `unbind`, `release` and `releasePieces` drop the object's waiting groups. Without `workBudget` the same holds:
193
+ an object bound or drawn again with loose groups (a house built with loose poles, a mailbox, a door whose wall was taken out) has them split into pieces in the next `update`
194
+ or drain, where they stood, before any event touches it (`autoSplit: false` leaves them in the object). Every option must be a finite number,
195
+ or `WorkBudgetOptionError` (`WORK_BUDGET_OPTION`) is thrown.
196
+
197
+ | Option | Default | What it does |
198
+ |---|---|---|
199
+ | `budgetMs` | 4 (`DEFAULT_WORK_BUDGET_MS`) | Milliseconds of deferred work a frame may take on the reference machine |
200
+ | `unitsPerMs` | 100 (`DEFAULT_WORK_UNITS_PER_MS`) | Work units in a millisecond on the reference machine (the calibration of the costs below) |
201
+ | `tier` | 1 | How much slower than the reference this machine runs the same work, picked once per session: the frame holds `budgetMs × unitsPerMs / tier` units |
202
+ | `maxWaitFrames` | 12 (`DEFAULT_MAX_WAIT_FRAMES`, 0,2 s) | Frames a loose group stands loose at most, whatever the budget: it splits by the `maxWaitFrames`-th work step after it came loose |
203
+ | `costs` | `WORK_UNIT_DEFAULTS`: `perSplit` 120, `perVertex` 0.05, `perHostVertex` 0.01, `perObjectPart` 0.3, `perFragment` 4, `perEvent` 40, `perWake` 2 | Units per unit of work; the first calibration (T3.8aa), to be measured again on top of the fits ahead, the batched bodies and the ground budget |
204
+
205
+ ### Debris of a blast (`blastDebris`, opt-in)
206
+
207
+ Left out, a blast shoves, wakes and loosens everything in its own frame, as before. With `blastDebris: {}` (or any of the numbers) the renderer keeps the cost of a blast
208
+ bounded. **Every number is an option** (per renderer in `RendererOptions.blastDebris`; per blast in `ExplodeOptions.debris`, which wins, for what belongs to one blast:
209
+ `maxWakeDelay`, `minPhysicsPieces`, `maxPhysicsPieces`, `physicsCostTargetMs`, `chipSize`; `BLAST_DEBRIS_DEFAULTS` is the default), measured on "Intel Core Ultra 9 275HX, 24 cores, Node 20" (spec 008, `evidence.md` T3.6d). Nothing adapts by GPU vendor, only by the cost measured on the machine
210
+ (`PhysicsFrame.stepMs` over the world's awake bodies, smoothed).
211
+
212
+ | Option | Default | What it does |
213
+ |---|---|---|
214
+ | `debrisCost` (renderer option) | `{ source: 'measured' }` | Where the step cost above comes from. `measured` reads the loop's wall-clock step time (`PhysicsFrame.stepMs`): batch sizes and piece counts then depend on the machine, so two machines (or a replay) diverge; `record` collects each frame's sample. `{ source: 'fixed' }` never reads the clock: batches and counts come only from the numbers in this table, the same on every machine (tests, lockstep). `{ source: 'replay', samples }` plays a recorded list frame by frame instead of the clock. |
215
+ | `wakeCostTargetMs` | 4 | The physics step may reach this as debris wakes: each frame wakes as many (nearest `focus` first, else the blast's centre) as the cost of the frames before leaves room for. Nothing is timed per body |
216
+ | `maxWakeDelay` | 0.08 s | A piece waits at most this to wake, whatever the cost |
217
+ | `maxQueuedWakes` | 2048 | Pieces waiting to wake at most; the farthest over it wake at once |
218
+ | `minWakesPerFrame` | 8 | Woken every frame at least, so a queue behind a heavy frame drains |
219
+ | `minPhysicsPieces` / `maxPhysicsPieces` | 24 / 300 | Pieces of one blast that enter physics: `physicsCostTargetMs / wake cost per body - bodies already awake`, clamped between them (250 on the measured machine); the big ones first, nearest the player first |
220
+ | `physicsCostTargetMs` | 5 | Milliseconds of step a blast's debris may add in the frame it wakes (sets the count above, at the wake cost per body) |
221
+ | `wakeCostPerBodyMs` | 0.02 | Cost of one body in the frame a pile wakes in until measured (a woken island costs ~9 times what it costs settled) |
222
+ | `physicsCostPerBodyMs` | 0.0022 | Cost of one awake body until the world has been measured |
223
+ | `chipSize` | 0.15 m | Longest side under which a loosened object is a chip: it goes after every big one when the count is full. A piece left out stays where it stands (drawn, solid: nothing invisible collides) and the blast throws visual chips (`vfx`, no collision) there |
224
+ | `maxLivePieces` | 600 | Pieces alive at once (asleep or not): over it the far and old ones at rest fade first, then the oldest |
225
+ | `far.distance` / `far.age` | 40 m / 30 s | A piece at rest farther than this from `focus` and older than this is far and old |
226
+ | `far.action` | `'sleep'` | `'sleep'` or `'fade'`. `'fade'` removes the body and collider and shrinks the root over `far.fadeSeconds` (0.5), then `onDebrisFaded(id, root)` hands it to the host to dispose. Merged drawing of debris at rest is `settledBatches` |
227
+
228
+ ### Workers (CLAUDE.md §14)
229
+
230
+ Two kinds of the worker coordinator (`@adatechnology/game-workers`) live here, in the three-free entry `@adatechnology/game-destruction-three/worker-kinds`
231
+ that a game's worker file imports (the kit's preset, `createKitWorkers`, already hosts both):
232
+
233
+ - `groundTilesKind` (`destruction.ground-tiles`, lane `sim`): the engine's ground tiles under a crater. `groundWorker: createCoordinatorGroundPort(workers)`
234
+ hands them to the coordinator; each tile is applied at its fixed update, from the worker's answer or made here from the same job. With `terrainPatchAt`
235
+ (`Terrain.heightPatch`, next to `terrainGridAt`) a tile inside the terrain is asked for with the vertices under it and its ring (~26 x 26 numbers) instead of
236
+ its samples and band (~60 000); the samples are made from them with the terrain's own blend (`heightPatchGrid`), the same bits, and a change is told by
237
+ comparing patches (the whole patch: a change the host did not mark is caught too). A tile across the terrain's bounds is read as before.
238
+ - `staticCollidersKind` (`destruction.static-colliders`, lane `build`): the fixed colliders measured from what is drawn (a house at load, a part measured
239
+ again). `workers` in the renderer's options hands each part to the coordinator; its object waits for the answer as a part out of turns waits for its update,
240
+ and gets the same boxes (`measureStaticPart` is the kernel on both sides). Opt-in: measured in the test room (a house, `single_storey_a`) the main thread
241
+ spent more with it (262 ms against 192 ms over 90 updates, worst update 110 ms against 30 ms), since reading and copying a part's vertices costs what
242
+ measuring them does. It pays where a part is large and the main thread is the bottleneck; measure before turning it on.
243
+
244
+ ```ts
245
+ const workers = createKitWorkers()
246
+ createDestructionRenderer(THREE, { ...options, groundWorker: createCoordinatorGroundPort(workers) /* , workers: opt-in, see above */ })
247
+ ```
248
+
249
+ The world's own solver passes are `WorldDesc.velocityIterations` (default 32 in `KIT_WORLD`, the least that keeps a 20:1 mass stack from making energy).
250
+
167
251
  ## API
168
252
 
169
253
  ```ts
@@ -172,7 +256,7 @@ specFromObject3D(THREE, root, options?: SpecFromObjectOptions): { spec, parts }
172
256
  heatColor(temperatureC: number): number // 0xRRGGBB, 0 below ~450 C, dark red ~500, orange ~800, yellow ~1100, white from 1400
173
257
  ```
174
258
 
175
- `RendererOptions`: `scene`, `world`, `quality`, `maxFragments`, `maxAwakePieces`, `maxAwakeFragments`, `settledBatches`, `maxParticles`, `fragmentLife`, `groundAt`, `gravity`, `autoSplit`, `onPiece`, `onSound`,
259
+ `RendererOptions`: `scene`, `world`, `quality`, `maxFragments`, `maxAwakePieces`, `maxAwakeFragments`, `settledBatches`, `blastDebris`, `workBudget`, `focus`, `onDebrisFaded`, `maxParticles`, `fragmentLife`, `groundAt`, `terrainAt` (the terrain alone, what an engine stands pieces on: every drawn fixed thing is a body of its own there), `terrainRollingAt` (the rolling resistance of the terrain's material at a point, `TerrainMaterial.rolling`: a round piece on soil or sand stops far sooner than on asphalt; read at the middle of each 2 m tile, the same in both solvers), `gravity`, `autoSplit`, `onPiece`, `onSound`,
176
260
  `vfx` (shared effects world), `camera` (of the own world, when `vfx` is not given), `pointScale` (point size in pixels per metre at 1 m; about `viewportHeight / (2 tan(fov / 2))`, default 700).
177
261
 
178
262
  How long debris stays: `fragmentLife` is the seconds a resting fragment stays before it shrinks away (default 12, `0` = forever), or a `FragmentLifeRule`