@threenative/core 0.3.11 → 0.3.13
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 +1 -1
- package/capabilities.json +1988 -204
- package/dist/{assets-YUbXa3-Y.d.ts → assets-CfMKnOsK.d.ts} +78 -1
- package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
- package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-G7ttVy7y.d.ts} +1 -1
- package/dist/chunk-4SRAQFTU.js +133 -0
- package/dist/chunk-CFGTPFUH.js +231 -0
- package/dist/chunk-DIPIJICH.js +8916 -0
- package/dist/chunk-EEABDLUC.js +2237 -0
- package/dist/chunk-EFWZWLPY.js +1895 -0
- package/dist/chunk-GXY7B3JG.js +46 -0
- package/dist/chunk-GYUZJQ7U.js +280 -0
- package/dist/chunk-H4J6IAJE.js +504 -0
- package/dist/chunk-KIRUKTBQ.js +187 -0
- package/dist/chunk-QWJEMUUT.js +94 -0
- package/dist/chunk-SV4QVUTQ.js +31 -0
- package/dist/chunk-U4YHUKKL.js +615 -0
- package/dist/chunk-VP43GIWW.js +18 -0
- package/dist/chunk-XDTZ4QMO.js +160 -0
- package/dist/default-tone-mapping-GgKPTiBP.d.ts +4 -0
- package/dist/{game-DzMcitDO.d.ts → game-k8K3hZai.d.ts} +85 -9
- package/dist/hot.d.ts +5 -4
- package/dist/hot.js +5 -52
- package/dist/index.d.ts +633 -1351
- package/dist/index.js +12873 -23068
- package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-DPTzeCcD.d.ts} +35 -3
- package/dist/playtest.d.ts +5 -4
- package/dist/playtest.js +25 -75
- package/dist/quality-BYqKGAwo.d.ts +868 -0
- package/dist/react-css.js +1 -215
- package/dist/react.d.ts +3 -2
- package/dist/render-environment.d.ts +170 -0
- package/dist/render-environment.js +352 -0
- package/dist/{renderer-rZm0hVqq.d.ts → renderer-BtOSseYf.d.ts} +42 -35
- package/dist/spells/index.d.ts +643 -0
- package/dist/spells/index.js +1558 -0
- package/dist/spells/ultra/index.d.ts +1600 -0
- package/dist/spells/ultra/index.js +6804 -0
- package/dist/terrain-jobs-worker.js +1 -123
- package/dist/two-sided-foliage-W523J3UM.js +1 -0
- package/dist/ui-layer.js +6 -384
- package/dist/worker.js +13 -43
- package/dist/world.d.ts +988 -268
- package/dist/world.js +17464 -18290
- package/mcp/animation-server.mjs +1 -1
- package/mcp/animation.mjs +2 -3
- package/mcp/engine-server.mjs +245 -52
- package/mcp/servers.mjs +10 -3
- package/mcp/terrain.mjs +8 -0
- package/package.json +19 -7
- package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
- package/patches/three@0.187.0.patch +2004 -407
- package/src/animation-batch.ts +193 -0
- package/src/animation-layers.ts +7 -3
- package/src/animation.ts +209 -29
- package/src/asset-cache.ts +3 -3
- package/src/asset-memo.ts +202 -0
- package/src/assets.ts +437 -125
- package/src/auto-batch.ts +190 -44
- package/src/card-lod.ts +152 -0
- package/src/compressed-texture-preparation.ts +1008 -0
- package/src/compute-driven.ts +1 -1
- package/src/config.ts +8 -0
- package/src/debug.ts +103 -22
- package/src/dev-global.ts +54 -0
- package/src/engine-hooks.ts +37 -0
- package/src/frame-budget.ts +50 -23
- package/src/game-boot-phase.ts +269 -0
- package/src/game-dev-tools.ts +86 -0
- package/src/game-frame-loop.ts +958 -0
- package/src/game-renderer-metrics.ts +77 -0
- package/src/game-startup.ts +1076 -0
- package/src/game-ui.ts +84 -0
- package/src/game-validate.ts +70 -0
- package/src/game.ts +198 -2264
- package/src/gamepad-mappings.ts +269 -0
- package/src/generated/console-markers.ts +13 -0
- package/src/geometry-capture.ts +28 -4
- package/src/gpu-readings.ts +98 -0
- package/src/gpu-scene-bvh.ts +149 -36
- package/src/hot.ts +4 -7
- package/src/index.ts +141 -9
- package/src/input.ts +237 -25
- package/src/instanced-batch-lod.ts +3 -8
- package/src/interpolation.ts +171 -40
- package/src/launch-diagnostics.ts +22 -12
- package/src/look/ao-admission.ts +25 -0
- package/src/look/auto-exposure.ts +5 -16
- package/src/look/default-tone-mapping.ts +41 -5
- package/src/look/filmic-tone-mapping.ts +15 -13
- package/src/look/local-exposure.ts +2 -7
- package/src/look/look-diagnostics.ts +3 -3
- package/src/look/quad-pass.ts +21 -0
- package/src/look/quality.ts +14 -5
- package/src/look/unreal-bloom.ts +4 -13
- package/src/look/world-environment.ts +113 -103
- package/src/loop.ts +29 -0
- package/src/merge-parts.ts +20 -24
- package/src/model-lod.ts +139 -17
- package/src/particle-kinds.ts +515 -0
- package/src/particle-significance.ts +12 -0
- package/src/particles.ts +19 -10
- package/src/picking.ts +17 -3
- package/src/platform.ts +23 -4
- package/src/playtest.ts +27 -1
- package/src/profiling/FrameStages.ts +5 -6
- package/src/profiling/Spans.ts +3 -17
- package/src/profiling/debug-telemetry.ts +2 -6
- package/src/profiling/render-list-validate.ts +6 -15
- package/src/projection-apply.ts +4 -1
- package/src/projection-marker.ts +2 -1
- package/src/projection-plan.ts +9 -6
- package/src/quiet-markers.ts +3 -10
- package/src/render/cpu-bvh.ts +100 -0
- package/src/render/depth-pyramid.ts +6 -14
- package/src/render/environment/environmentContribution.ts +202 -0
- package/src/render/environment/environmentRadiance.ts +44 -0
- package/src/render/environment/environmentSampleCache.ts +56 -0
- package/src/render/environment/environmentSampling.ts +201 -0
- package/src/render/environment/environmentSnapshot.ts +71 -0
- package/src/render/environment/gate.ts +57 -0
- package/src/render/mesh-pool.ts +4 -20
- package/src/render/probe-trace.ts +872 -0
- package/src/render/probe-volume.ts +780 -34
- package/src/render/radiance-cache.ts +17 -0
- package/src/render/resolution-subrect.ts +2 -0
- package/src/render/rough-diffuse.ts +46 -12
- package/src/render/soft-shadow.ts +27 -12
- package/src/render/virtual-shadow-adaptive.ts +55 -0
- package/src/render/virtual-shadow-casters.ts +175 -0
- package/src/render/virtual-shadow-layers.ts +76 -0
- package/src/render/virtual-shadow-level.ts +224 -0
- package/src/render/virtual-shadow-markers.ts +193 -0
- package/src/render/virtual-shadow-options.ts +370 -0
- package/src/render/virtual-shadow-pages.ts +5 -0
- package/src/render/virtual-shadow-partial.ts +13 -0
- package/src/render/virtual-shadow.ts +231 -1008
- package/src/render/world-impostor-surface.ts +402 -40
- package/src/render/world-impostor.ts +5 -2
- package/src/render-camera-cull.ts +98 -3
- package/src/render-environment.ts +41 -0
- package/src/render-pass-budget.ts +34 -7
- package/src/renderProjection.ts +22 -0
- package/src/renderer-config.ts +36 -0
- package/src/renderer-facts.ts +37 -0
- package/src/renderer.ts +144 -65
- package/src/resolution-scaler.ts +3 -2
- package/src/scene-entry-inspectors.ts +61 -0
- package/src/snow-field.ts +2 -1
- package/src/spells/cast-pool.ts +361 -0
- package/src/spells/index.ts +177 -0
- package/src/spells/mechanisms.ts +295 -0
- package/src/spells/reaction-field.ts +415 -0
- package/src/spells/spell-lights.ts +139 -0
- package/src/spells/spell-world.ts +187 -0
- package/src/spells/spell.ts +397 -0
- package/src/spells/spells.ts +283 -0
- package/src/spells/ultra/engine/camera.ts +184 -0
- package/src/spells/ultra/engine/quality.ts +96 -0
- package/src/spells/ultra/index.ts +52 -0
- package/src/spells/ultra/main.ts +305 -0
- package/src/spells/ultra/scene/arenaMaterial.ts +325 -0
- package/src/spells/ultra/spells/cast.ts +233 -0
- package/src/spells/ultra/spells/fire.ts +1267 -0
- package/src/spells/ultra/vfx/FluidVolume.ts +816 -0
- package/src/spells/ultra/vfx/FluidVolume.tsl.ts +855 -0
- package/src/spells/ultra/vfx/FxTarget.ts +61 -0
- package/src/spells/ultra/vfx/GroundField.ts +1399 -0
- package/src/spells/ultra/vfx/Particles.ts +1475 -0
- package/src/spells/ultra/vfx/Ribbons.ts +645 -0
- package/src/spells/ultra/vfx/Shockwaves.ts +286 -0
- package/src/spells/ultra/vfx/Volumes.ts +489 -0
- package/src/spells/ultra/vfx/common.ts +1195 -0
- package/src/spells/ultra/vfx/noise.ts +441 -0
- package/src/startup-offscreen-bytes.ts +78 -0
- package/src/startup-readiness.ts +128 -34
- package/src/static-transform.ts +25 -19
- package/src/streamed-textures.ts +112 -0
- package/src/streaming.ts +68 -4
- package/src/texture-streaming.ts +331 -0
- package/src/version.ts +1 -1
- package/src/warmup.ts +56 -8
- package/src/water-surface.ts +9 -3
- package/src/worker.ts +12 -2
- package/src/world-bundle-safety.ts +446 -0
- package/src/world-cell-batches.ts +1178 -0
- package/src/world-cell-chunks.ts +1128 -0
- package/src/world-cell-data.ts +995 -0
- package/src/world-cell-impostors.ts +1461 -0
- package/src/world-cell-lod-bias.ts +151 -0
- package/src/world-cell-models.ts +1074 -0
- package/src/world-cell-options.ts +177 -0
- package/src/world-cell-placements.ts +798 -0
- package/src/world-cell-prewarm.ts +592 -0
- package/src/world-cell-readiness.ts +357 -0
- package/src/world-cell-records.ts +401 -0
- package/src/world-cell-render-bundles.ts +459 -0
- package/src/world-cell-render-gpu-dress.ts +710 -0
- package/src/world-cell-render-main-cull.ts +540 -0
- package/src/world-cell-render-shadow-claims.ts +198 -0
- package/src/world-cell-render-util.ts +259 -0
- package/src/world-cell-render.ts +1501 -0
- package/src/world-cell-residency.ts +754 -0
- package/src/world-cell-stale.ts +238 -0
- package/src/world-cells.ts +1335 -10418
- package/src/world-chunk-shadow.ts +308 -0
- package/src/world-collar.ts +315 -0
- package/src/world-gpu-flags.ts +131 -0
- package/src/world-gpu-readback.ts +881 -0
- package/src/world-gpu-reference.ts +524 -0
- package/src/world-gpu-scene.ts +256 -2286
- package/src/world-gpu-shadow.ts +203 -0
- package/src/world-gpu-types.ts +409 -0
- package/src/world-gpu-validation.ts +518 -0
- package/src/world-heightfield.ts +802 -0
- package/src/world-load-limiter.ts +111 -0
- package/src/world-model-sections.ts +80 -0
- package/src/world-noise.ts +241 -0
- package/src/world-package.ts +42 -0
- package/src/world-region.ts +1 -1
- package/src/world-river-ribbon.ts +282 -0
- package/src/world-root-frame.ts +39 -0
- package/src/world-shore-field.ts +91 -0
- package/src/world-static-pose.ts +163 -0
- package/src/world-surface-sampler.ts +48 -0
- package/src/world-terrain-splat.ts +361 -153
- package/src/world-tile-budget.ts +249 -0
- package/src/world-tile-geometry.ts +469 -0
- package/src/world-tile-lod.ts +408 -0
- package/src/world-tile-observation.ts +566 -0
- package/src/world-tile-stitch.ts +166 -0
- package/src/world-tiles.ts +247 -1755
- package/src/world-validate.ts +6 -14
- package/src/world-warm.ts +51 -0
- package/src/world-water-bodies.ts +359 -0
- package/src/world.ts +48 -741
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import type { Box3, InstancedBufferAttribute, Matrix4 } from "three";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The instance records of the world's shared batches, written by one engine call a frame.
|
|
5
|
+
*
|
|
6
|
+
* A shared batch used to write, zero, move and regroup its records in a JS copy of each instance
|
|
7
|
+
* buffer and push every change through `needsUpdate`, which on the owned engine is a bridge crossing
|
|
8
|
+
* per range and per attribute: the 2 km forest walk spent 4.2 ms a frame in the admission drain, 63%
|
|
9
|
+
* of it in the bridge glue around those writes. Now a batch only decides where records go. What
|
|
10
|
+
* happens to the bytes is queued here as ops, and `flush` hands the whole frame to the engine in
|
|
11
|
+
* one `InstancedBufferAttribute.__tnRecords` call, which writes the engine's own buffers and their
|
|
12
|
+
* upload ranges: the batch never reads or holds the bytes. The record boxes the batch publishes as
|
|
13
|
+
* mesh bounds come back from the same call, measured from the bytes as written.
|
|
14
|
+
*
|
|
15
|
+
* Upstream three (`engine: "threejs"`) has no engine to call, so the same ops run on each
|
|
16
|
+
* attribute's own array in {@link applyRecords}, the twin of `bindings_geometry.cpp`'s
|
|
17
|
+
* `applyRecords`.
|
|
18
|
+
*
|
|
19
|
+
* Every op starts with its code and the attribute's index in the call's attribute list; element
|
|
20
|
+
* offsets and counts are in floats of that attribute's array:
|
|
21
|
+
* - `WRITE at count payloadAt`: `count` floats of the payload at `at`.
|
|
22
|
+
* - `FILL at count`: zeros.
|
|
23
|
+
* - `MOVE from to count`: `copyWithin(to, from, from + count)`.
|
|
24
|
+
* - `COPY source count`: the first `count` floats of attribute `source` (a grown buffer's old one).
|
|
25
|
+
* - `GATHER stride blocks [from count]...`: the blocks (in records) laid out from record 0 in order.
|
|
26
|
+
* - `UPLOAD from count`: the range the next render uploads; `count` 0 uploads the whole array.
|
|
27
|
+
* - `BOX from count payloadAt`: answers the AABB and largest basis scale of records
|
|
28
|
+
* `[from, from + count)` of a matrix buffer, for a local box whose centre and half size are the
|
|
29
|
+
* six payload floats at `payloadAt`: seven numbers per op, in op order.
|
|
30
|
+
* - `LANES stride lanes values...`: the first `lanes` floats of every record set to `values`.
|
|
31
|
+
*/
|
|
32
|
+
export const RECORD_OP = {
|
|
33
|
+
write: 1,
|
|
34
|
+
fill: 2,
|
|
35
|
+
move: 3,
|
|
36
|
+
copy: 4,
|
|
37
|
+
gather: 5,
|
|
38
|
+
upload: 6,
|
|
39
|
+
box: 7,
|
|
40
|
+
lanes: 8,
|
|
41
|
+
} as const;
|
|
42
|
+
|
|
43
|
+
/** Numbers each `BOX` op answers: min x y z, max x y z, largest basis scale. */
|
|
44
|
+
export const RECORD_BOX_SIZE = 7;
|
|
45
|
+
|
|
46
|
+
/** What a queued batch does around a flush: name its upload ranges, then read its boxes back. */
|
|
47
|
+
export interface IRecordWriter {
|
|
48
|
+
uploadRecords(): void;
|
|
49
|
+
settleRecords(boxes: ArrayLike<number>): void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type IRecordsCall = (
|
|
53
|
+
attributes: InstancedBufferAttribute[],
|
|
54
|
+
ops: Float64Array,
|
|
55
|
+
payload: Float32Array,
|
|
56
|
+
) => ArrayLike<number>;
|
|
57
|
+
|
|
58
|
+
/** The engine's own applier, present only on the owned engine. */
|
|
59
|
+
interface IRecordsEngine {
|
|
60
|
+
__tnRecords?: IRecordsCall;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function grown<T extends Float64Array | Float32Array>(array: T, needed: number): T {
|
|
64
|
+
if (needed <= array.length) return array;
|
|
65
|
+
const next = new (array.constructor as new (length: number) => T)(
|
|
66
|
+
Math.max(needed, array.length * 2),
|
|
67
|
+
);
|
|
68
|
+
next.set(array);
|
|
69
|
+
return next;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class RecordQueue {
|
|
73
|
+
#attributes: InstancedBufferAttribute[] = [];
|
|
74
|
+
readonly #slots = new Map<InstancedBufferAttribute, number>();
|
|
75
|
+
#ops = new Float64Array(1024);
|
|
76
|
+
#opAt = 0;
|
|
77
|
+
#payload = new Float32Array(16384);
|
|
78
|
+
#payloadAt = 0;
|
|
79
|
+
#boxes = 0;
|
|
80
|
+
readonly #writers = new Set<IRecordWriter>();
|
|
81
|
+
|
|
82
|
+
/** The payload buffer; read it after {@link reserve}, which may replace it. */
|
|
83
|
+
get payload(): Float32Array {
|
|
84
|
+
return this.#payload;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Room for `floats` payload floats at a record-aligned offset, which is returned. */
|
|
88
|
+
reserve(floats: number): number {
|
|
89
|
+
const at = Math.ceil(this.#payloadAt / 16) * 16;
|
|
90
|
+
this.#payload = grown(this.#payload, at + floats);
|
|
91
|
+
this.#payloadAt = at + floats;
|
|
92
|
+
return at;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
write(attribute: InstancedBufferAttribute, at: number, count: number, payloadAt: number): void {
|
|
96
|
+
if (count > 0) this.#op(RECORD_OP.write, attribute, at, count, payloadAt);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fill(attribute: InstancedBufferAttribute, at: number, count: number): void {
|
|
100
|
+
if (count > 0) this.#op(RECORD_OP.fill, attribute, at, count);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
move(attribute: InstancedBufferAttribute, from: number, to: number, count: number): void {
|
|
104
|
+
if (count > 0 && from !== to) this.#op(RECORD_OP.move, attribute, from, to, count);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
copy(target: InstancedBufferAttribute, source: InstancedBufferAttribute, count: number): void {
|
|
108
|
+
if (count > 0) this.#op(RECORD_OP.copy, target, this.#slot(source), count);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** `blocks` holds `[fromRecord, records]` pairs, laid out from record 0 in order. */
|
|
112
|
+
gather(attribute: InstancedBufferAttribute, stride: number, blocks: readonly number[]): void {
|
|
113
|
+
if (blocks.length === 0) return;
|
|
114
|
+
this.#op(RECORD_OP.gather, attribute, stride, blocks.length / 2, ...blocks);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
upload(attribute: InstancedBufferAttribute, from: number, count: number): void {
|
|
118
|
+
this.#op(RECORD_OP.upload, attribute, from, count);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Queues a box; the returned index is where its seven numbers start in the flush's answer. */
|
|
122
|
+
box(
|
|
123
|
+
attribute: InstancedBufferAttribute,
|
|
124
|
+
from: number,
|
|
125
|
+
count: number,
|
|
126
|
+
center: { x: number; y: number; z: number },
|
|
127
|
+
half: { x: number; y: number; z: number },
|
|
128
|
+
): number {
|
|
129
|
+
const at = this.reserve(6);
|
|
130
|
+
this.#payload.set([center.x, center.y, center.z, half.x, half.y, half.z], at);
|
|
131
|
+
this.#op(RECORD_OP.box, attribute, from, count, at);
|
|
132
|
+
const index = this.#boxes;
|
|
133
|
+
this.#boxes += RECORD_BOX_SIZE;
|
|
134
|
+
return index;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
lanes(attribute: InstancedBufferAttribute, stride: number, values: readonly number[]): void {
|
|
138
|
+
this.#op(RECORD_OP.lanes, attribute, stride, values.length, ...values);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** `writer` names its upload ranges at the next flush and reads its boxes back after it. */
|
|
142
|
+
touch(writer: IRecordWriter): void {
|
|
143
|
+
this.#writers.add(writer);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Hands the frame's ops to the engine in one call (or to {@link applyRecords} on upstream three),
|
|
148
|
+
* then tells each writer what its boxes measured. A frame that queued nothing calls nothing.
|
|
149
|
+
*/
|
|
150
|
+
flush(): void {
|
|
151
|
+
if (this.#writers.size === 0 && this.#opAt === 0) return;
|
|
152
|
+
const writers = [...this.#writers];
|
|
153
|
+
this.#writers.clear();
|
|
154
|
+
for (const writer of writers) writer.uploadRecords();
|
|
155
|
+
const attributes = this.#attributes;
|
|
156
|
+
const ops = this.#ops.subarray(0, this.#opAt);
|
|
157
|
+
const payload = this.#payload.subarray(0, this.#payloadAt);
|
|
158
|
+
this.#attributes = [];
|
|
159
|
+
this.#slots.clear();
|
|
160
|
+
this.#opAt = 0;
|
|
161
|
+
this.#payloadAt = 0;
|
|
162
|
+
this.#boxes = 0;
|
|
163
|
+
const first = attributes[0] as (InstancedBufferAttribute & IRecordsEngine) | undefined;
|
|
164
|
+
let boxes: ArrayLike<number> = [];
|
|
165
|
+
if (first !== undefined) {
|
|
166
|
+
const engine = first.__tnRecords;
|
|
167
|
+
boxes =
|
|
168
|
+
typeof engine === "function"
|
|
169
|
+
? engine.call(first, attributes, ops, payload)
|
|
170
|
+
: applyRecords(attributes, ops, payload);
|
|
171
|
+
}
|
|
172
|
+
for (const writer of writers) writer.settleRecords(boxes);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#slot(attribute: InstancedBufferAttribute): number {
|
|
176
|
+
let slot = this.#slots.get(attribute);
|
|
177
|
+
if (slot === undefined) {
|
|
178
|
+
slot = this.#attributes.length;
|
|
179
|
+
this.#attributes.push(attribute);
|
|
180
|
+
this.#slots.set(attribute, slot);
|
|
181
|
+
}
|
|
182
|
+
return slot;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
#op(code: number, attribute: InstancedBufferAttribute, ...args: number[]): void {
|
|
186
|
+
const slot = this.#slot(attribute);
|
|
187
|
+
this.#ops = grown(this.#ops, this.#opAt + 2 + args.length);
|
|
188
|
+
this.#ops[this.#opAt] = code;
|
|
189
|
+
this.#ops[this.#opAt + 1] = slot;
|
|
190
|
+
this.#ops.set(args, this.#opAt + 2);
|
|
191
|
+
this.#opAt += 2 + args.length;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The one queue every shared batch writes into, flushed at the end of `WorldCells.update`. */
|
|
196
|
+
export const batchRecords = new RecordQueue();
|
|
197
|
+
|
|
198
|
+
function span(array: ArrayLike<number>, at: number, count: number, what: string): void {
|
|
199
|
+
if (
|
|
200
|
+
!(
|
|
201
|
+
Number.isInteger(at) &&
|
|
202
|
+
Number.isInteger(count) &&
|
|
203
|
+
at >= 0 &&
|
|
204
|
+
count >= 0 &&
|
|
205
|
+
at + count <= array.length
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
throw new RangeError(
|
|
209
|
+
`TN_WORLD_RECORDS: ${what} [${String(at)}, ${String(at + count)}) is outside ${String(array.length)} floats`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** `GATHER stride blocks [from count]...` on one array, read whole before it is written. */
|
|
214
|
+
function gather(array: Float32Array, ops: Float64Array, at: number): void {
|
|
215
|
+
const stride = ops[at + 2] as number;
|
|
216
|
+
const blocks = ops[at + 3] as number;
|
|
217
|
+
let total = 0;
|
|
218
|
+
for (let block = 0; block < blocks; block += 1) total += ops[at + 5 + block * 2] as number;
|
|
219
|
+
const scratch = new Float32Array(total * stride);
|
|
220
|
+
let written = 0;
|
|
221
|
+
for (let block = 0; block < blocks; block += 1) {
|
|
222
|
+
const from = (ops[at + 4 + block * 2] as number) * stride;
|
|
223
|
+
const count = (ops[at + 5 + block * 2] as number) * stride;
|
|
224
|
+
span(array, from, count, "gather");
|
|
225
|
+
scratch.set(array.subarray(from, from + count), written);
|
|
226
|
+
written += count;
|
|
227
|
+
}
|
|
228
|
+
span(array, 0, written, "gather target");
|
|
229
|
+
array.set(scratch);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** `LANES stride lanes values...`: the first `lanes` floats of every record. */
|
|
233
|
+
function lanes(array: Float32Array, ops: Float64Array, at: number): void {
|
|
234
|
+
const stride = ops[at + 2] as number;
|
|
235
|
+
const count = ops[at + 3] as number;
|
|
236
|
+
if (!(count <= stride)) throw new RangeError("TN_WORLD_RECORDS: lanes wider than the record");
|
|
237
|
+
for (let record = 0; record + stride <= array.length; record += stride)
|
|
238
|
+
for (let lane = 0; lane < count; lane += 1) array[record + lane] = ops[at + 4 + lane] as number;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* The ops of one flush on each attribute's own array: the upstream-three twin of the engine's
|
|
243
|
+
* `__tnRecords`, with the same checks. Answers the `BOX` ops' numbers in order.
|
|
244
|
+
*/
|
|
245
|
+
export function applyRecords(
|
|
246
|
+
attributes: readonly InstancedBufferAttribute[],
|
|
247
|
+
ops: Float64Array,
|
|
248
|
+
payload: Float32Array,
|
|
249
|
+
): number[] {
|
|
250
|
+
const boxes: number[] = [];
|
|
251
|
+
for (let at = 0; at < ops.length; ) at = applyOp(attributes, ops, at, payload, boxes);
|
|
252
|
+
return boxes;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** One op of {@link applyRecords}; answers where the next op starts. */
|
|
256
|
+
function applyOp(
|
|
257
|
+
attributes: readonly InstancedBufferAttribute[],
|
|
258
|
+
ops: Float64Array,
|
|
259
|
+
at: number,
|
|
260
|
+
payload: Float32Array,
|
|
261
|
+
boxes: number[],
|
|
262
|
+
): number {
|
|
263
|
+
const attribute = attributes[ops[at + 1] as number];
|
|
264
|
+
if (attribute === undefined) throw new RangeError("TN_WORLD_RECORDS: an op names no attribute");
|
|
265
|
+
const array = attribute.array as Float32Array;
|
|
266
|
+
const a = ops[at + 2] as number;
|
|
267
|
+
const b = ops[at + 3] as number;
|
|
268
|
+
const c = ops[at + 4] as number;
|
|
269
|
+
switch (ops[at]) {
|
|
270
|
+
case RECORD_OP.write:
|
|
271
|
+
span(array, a, b, "write");
|
|
272
|
+
span(payload, c, b, "payload");
|
|
273
|
+
array.set(payload.subarray(c, c + b), a);
|
|
274
|
+
return at + 5;
|
|
275
|
+
case RECORD_OP.fill:
|
|
276
|
+
span(array, a, b, "fill");
|
|
277
|
+
array.fill(0, a, a + b);
|
|
278
|
+
return at + 4;
|
|
279
|
+
case RECORD_OP.move:
|
|
280
|
+
span(array, a, c, "move source");
|
|
281
|
+
span(array, b, c, "move target");
|
|
282
|
+
array.copyWithin(b, a, a + c);
|
|
283
|
+
return at + 5;
|
|
284
|
+
case RECORD_OP.copy: {
|
|
285
|
+
const source = attributes[a]?.array as Float32Array | undefined;
|
|
286
|
+
if (source === undefined) throw new RangeError("TN_WORLD_RECORDS: copy names no source");
|
|
287
|
+
span(array, 0, b, "copy target");
|
|
288
|
+
span(source, 0, b, "copy source");
|
|
289
|
+
array.set(source.subarray(0, b));
|
|
290
|
+
return at + 4;
|
|
291
|
+
}
|
|
292
|
+
case RECORD_OP.gather:
|
|
293
|
+
gather(array, ops, at);
|
|
294
|
+
return at + 4 + b * 2;
|
|
295
|
+
case RECORD_OP.upload:
|
|
296
|
+
span(array, a, b, "upload");
|
|
297
|
+
attribute.clearUpdateRanges();
|
|
298
|
+
if (b > 0) attribute.addUpdateRange(a, b);
|
|
299
|
+
attribute.needsUpdate = true;
|
|
300
|
+
return at + 4;
|
|
301
|
+
case RECORD_OP.box:
|
|
302
|
+
span(array, a * 16, b * 16, "box");
|
|
303
|
+
span(payload, c, 6, "box payload");
|
|
304
|
+
boxes.push(...boxOf(array, a, b, payload, c));
|
|
305
|
+
return at + 5;
|
|
306
|
+
case RECORD_OP.lanes:
|
|
307
|
+
lanes(array, ops, at);
|
|
308
|
+
return at + 4 + b;
|
|
309
|
+
default:
|
|
310
|
+
throw new RangeError(`TN_WORLD_RECORDS: unknown op ${String(ops[at])}`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* The AABB of records `[from, from + count)`: each record's local box centre moved by its matrix,
|
|
316
|
+
* with half extents `|basis| * half`, which is the box a rotated or stretched instance actually
|
|
317
|
+
* covers. The seventh number is the largest basis column, the scale a fern or a trunk is drawn at.
|
|
318
|
+
*/
|
|
319
|
+
function boxOf(
|
|
320
|
+
array: Float32Array,
|
|
321
|
+
from: number,
|
|
322
|
+
count: number,
|
|
323
|
+
local: Float32Array,
|
|
324
|
+
at: number,
|
|
325
|
+
): number[] {
|
|
326
|
+
const cx = local[at] as number;
|
|
327
|
+
const cy = local[at + 1] as number;
|
|
328
|
+
const cz = local[at + 2] as number;
|
|
329
|
+
const hx = local[at + 3] as number;
|
|
330
|
+
const hy = local[at + 4] as number;
|
|
331
|
+
const hz = local[at + 5] as number;
|
|
332
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
333
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
334
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
335
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
336
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
337
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
338
|
+
let scale = 0;
|
|
339
|
+
for (let index = from; index < from + count; index += 1) {
|
|
340
|
+
const m = index * 16;
|
|
341
|
+
const m0 = array[m] as number;
|
|
342
|
+
const m1 = array[m + 1] as number;
|
|
343
|
+
const m2 = array[m + 2] as number;
|
|
344
|
+
const m4 = array[m + 4] as number;
|
|
345
|
+
const m5 = array[m + 5] as number;
|
|
346
|
+
const m6 = array[m + 6] as number;
|
|
347
|
+
const m8 = array[m + 8] as number;
|
|
348
|
+
const m9 = array[m + 9] as number;
|
|
349
|
+
const m10 = array[m + 10] as number;
|
|
350
|
+
const px = m0 * cx + m4 * cy + m8 * cz + (array[m + 12] as number);
|
|
351
|
+
const py = m1 * cx + m5 * cy + m9 * cz + (array[m + 13] as number);
|
|
352
|
+
const pz = m2 * cx + m6 * cy + m10 * cz + (array[m + 14] as number);
|
|
353
|
+
const ex = Math.abs(m0) * hx + Math.abs(m4) * hy + Math.abs(m8) * hz;
|
|
354
|
+
const ey = Math.abs(m1) * hx + Math.abs(m5) * hy + Math.abs(m9) * hz;
|
|
355
|
+
const ez = Math.abs(m2) * hx + Math.abs(m6) * hy + Math.abs(m10) * hz;
|
|
356
|
+
if (px - ex < minX) minX = px - ex;
|
|
357
|
+
if (py - ey < minY) minY = py - ey;
|
|
358
|
+
if (pz - ez < minZ) minZ = pz - ez;
|
|
359
|
+
if (px + ex > maxX) maxX = px + ex;
|
|
360
|
+
if (py + ey > maxY) maxY = py + ey;
|
|
361
|
+
if (pz + ez > maxZ) maxZ = pz + ez;
|
|
362
|
+
const basis = Math.max(Math.hypot(m0, m1, m2), Math.hypot(m4, m5, m6), Math.hypot(m8, m9, m10));
|
|
363
|
+
if (basis > scale) scale = basis;
|
|
364
|
+
}
|
|
365
|
+
return [minX, minY, minZ, maxX, maxY, maxZ, scale];
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* The AABB a local box covers under `matrix`, measured as the engine's `BOX` op measures a record: the
|
|
370
|
+
* centre moved by the matrix, half extents `|basis| * half`. Equal to `Box3.applyMatrix4`'s eight
|
|
371
|
+
* corners for an affine matrix, in twelve multiply-adds; a placement's box was the drain's largest
|
|
372
|
+
* per-record cost on the forest walk (0.12 ms a frame), measured twice per placement. An empty local
|
|
373
|
+
* box is left as authored, as three leaves it.
|
|
374
|
+
*/
|
|
375
|
+
export function placedBox(
|
|
376
|
+
target: Box3,
|
|
377
|
+
min: readonly number[],
|
|
378
|
+
max: readonly number[],
|
|
379
|
+
matrix: Matrix4,
|
|
380
|
+
): Box3 {
|
|
381
|
+
target.min.fromArray(min);
|
|
382
|
+
target.max.fromArray(max);
|
|
383
|
+
if (target.isEmpty()) return target;
|
|
384
|
+
const e = matrix.elements;
|
|
385
|
+
const cx = ((min[0] as number) + (max[0] as number)) / 2;
|
|
386
|
+
const cy = ((min[1] as number) + (max[1] as number)) / 2;
|
|
387
|
+
const cz = ((min[2] as number) + (max[2] as number)) / 2;
|
|
388
|
+
const hx = ((max[0] as number) - (min[0] as number)) / 2;
|
|
389
|
+
const hy = ((max[1] as number) - (min[1] as number)) / 2;
|
|
390
|
+
const hz = ((max[2] as number) - (min[2] as number)) / 2;
|
|
391
|
+
for (let axis = 0; axis < 3; axis += 1) {
|
|
392
|
+
const a = e[axis] as number;
|
|
393
|
+
const b = e[axis + 4] as number;
|
|
394
|
+
const c = e[axis + 8] as number;
|
|
395
|
+
const centre = a * cx + b * cy + c * cz + (e[axis + 12] as number);
|
|
396
|
+
const half = Math.abs(a) * hx + Math.abs(b) * hy + Math.abs(c) * hz;
|
|
397
|
+
target.min.setComponent(axis, centre - half);
|
|
398
|
+
target.max.setComponent(axis, centre + half);
|
|
399
|
+
}
|
|
400
|
+
return target;
|
|
401
|
+
}
|