@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,504 @@
|
|
|
1
|
+
import { GPUReadback } from './chunk-QWJEMUUT.js';
|
|
2
|
+
import { Box3, Matrix4, Frustum, Sphere, Vector3, Sprite } from 'three';
|
|
3
|
+
import { instancedArray, deltaTime, time, Fn, vec3, Loop, instanceIndex, min, max, vec4, uniform, If, uint } from 'three/tsl';
|
|
4
|
+
|
|
5
|
+
var CameraShake = class {
|
|
6
|
+
amplitude;
|
|
7
|
+
rotationAmplitude;
|
|
8
|
+
frequency;
|
|
9
|
+
decay;
|
|
10
|
+
curve;
|
|
11
|
+
offset = {
|
|
12
|
+
position: new Vector3(),
|
|
13
|
+
rotation: new Vector3()
|
|
14
|
+
};
|
|
15
|
+
#active = false;
|
|
16
|
+
#elapsed = 0;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
if (options === void 0 || options === null || options.amplitude === void 0)
|
|
19
|
+
throw new Error("CameraShake.amplitude is required.");
|
|
20
|
+
if (options.rotationAmplitude === void 0)
|
|
21
|
+
throw new Error("CameraShake.rotationAmplitude is required.");
|
|
22
|
+
validateVector(options.amplitude, "CameraShake.amplitude");
|
|
23
|
+
validateVector(options.rotationAmplitude, "CameraShake.rotationAmplitude");
|
|
24
|
+
if (!Number.isFinite(options.frequency) || options.frequency <= 0)
|
|
25
|
+
throw new Error("CameraShake.frequency must be positive and finite.");
|
|
26
|
+
if (!Number.isFinite(options.decay) || options.decay <= 0)
|
|
27
|
+
throw new Error("CameraShake.decay must be positive and finite.");
|
|
28
|
+
if (typeof options.curve !== "function") throw new Error("CameraShake.curve is required.");
|
|
29
|
+
this.amplitude = options.amplitude.clone();
|
|
30
|
+
this.rotationAmplitude = options.rotationAmplitude.clone();
|
|
31
|
+
this.frequency = options.frequency;
|
|
32
|
+
this.decay = options.decay;
|
|
33
|
+
this.curve = options.curve;
|
|
34
|
+
}
|
|
35
|
+
get active() {
|
|
36
|
+
return this.#active;
|
|
37
|
+
}
|
|
38
|
+
get elapsed() {
|
|
39
|
+
return this.#elapsed;
|
|
40
|
+
}
|
|
41
|
+
/** Start or restart the authored shake waveform. */
|
|
42
|
+
trigger() {
|
|
43
|
+
this.#elapsed = 0;
|
|
44
|
+
this.#active = true;
|
|
45
|
+
this.#clearOffset();
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
/** Stop the effect and clear the offset. */
|
|
49
|
+
stop() {
|
|
50
|
+
this.#active = false;
|
|
51
|
+
this.#elapsed = 0;
|
|
52
|
+
this.#clearOffset();
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
/** Evaluate the current offset and advance by one caller-supplied fixed-step delta. */
|
|
56
|
+
update(dt) {
|
|
57
|
+
if (!Number.isFinite(dt) || dt < 0)
|
|
58
|
+
throw new Error("CameraShake.update delta must be finite and non-negative.");
|
|
59
|
+
if (!this.#active) {
|
|
60
|
+
this.#clearOffset();
|
|
61
|
+
return this.offset;
|
|
62
|
+
}
|
|
63
|
+
const envelope = Math.exp(-this.decay * this.#elapsed);
|
|
64
|
+
const phase = this.#elapsed * this.frequency * Math.PI * 2;
|
|
65
|
+
const sample = this.curve(phase);
|
|
66
|
+
if (!Number.isFinite(sample)) throw new Error("CameraShake.curve must return a finite number.");
|
|
67
|
+
this.offset.position.copy(this.amplitude).multiplyScalar(sample * envelope);
|
|
68
|
+
this.offset.rotation.copy(this.rotationAmplitude).multiplyScalar(sample * envelope);
|
|
69
|
+
this.#elapsed += dt;
|
|
70
|
+
if (!Number.isFinite(this.#elapsed)) throw new Error("CameraShake elapsed time overflowed.");
|
|
71
|
+
if (Math.exp(-this.decay * this.#elapsed) <= Number.EPSILON) this.#active = false;
|
|
72
|
+
return this.offset;
|
|
73
|
+
}
|
|
74
|
+
#clearOffset() {
|
|
75
|
+
this.offset.position.set(0, 0, 0);
|
|
76
|
+
this.offset.rotation.set(0, 0, 0);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
function validateVector(value, name) {
|
|
80
|
+
if (value === void 0 || value === null || ![value.x, value.y, value.z].every(Number.isFinite))
|
|
81
|
+
throw new Error(`${name} must contain finite components.`);
|
|
82
|
+
}
|
|
83
|
+
var DEAD_BIRTH = -1e6;
|
|
84
|
+
var LANES = 256;
|
|
85
|
+
var worldBox = new Box3();
|
|
86
|
+
var viewProjection = new Matrix4();
|
|
87
|
+
var frustum = new Frustum();
|
|
88
|
+
var sphere = new Sphere();
|
|
89
|
+
var cameraPosition = new Vector3();
|
|
90
|
+
function foldBounds(data) {
|
|
91
|
+
if (data.length === 0 || data.length % 8 !== 0 || !data.every(Number.isFinite)) return void 0;
|
|
92
|
+
const lanes = data.length / 8;
|
|
93
|
+
const box = new Box3();
|
|
94
|
+
for (let lane = 0; lane < lanes; lane += 1) {
|
|
95
|
+
const lo = lane * 4;
|
|
96
|
+
const hi = (lane + lanes) * 4;
|
|
97
|
+
const lowX = data[lo];
|
|
98
|
+
const highX = data[hi];
|
|
99
|
+
if (lowX === void 0 || highX === void 0) return void 0;
|
|
100
|
+
if (lowX > highX) continue;
|
|
101
|
+
for (let axis = 0; axis < 3; axis += 1) {
|
|
102
|
+
const low = data[lo + axis];
|
|
103
|
+
const high = data[hi + axis];
|
|
104
|
+
if (low === void 0 || high === void 0) return void 0;
|
|
105
|
+
box.min.setComponent(axis, Math.min(box.min.getComponent(axis), low));
|
|
106
|
+
box.max.setComponent(axis, Math.max(box.max.getComponent(axis), high));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return box.isEmpty() ? void 0 : box;
|
|
110
|
+
}
|
|
111
|
+
function computeNode(name, value) {
|
|
112
|
+
if (value === null || typeof value !== "object" || value.isComputeNode !== true) {
|
|
113
|
+
throw new Error(`GPUParticles3D.${name} must return a TSL compute node.`);
|
|
114
|
+
}
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
function finite(name, value) {
|
|
118
|
+
if (!Number.isFinite(value)) throw new Error(`GPUParticles3D.${name} must be a finite number.`);
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
function setVector(uniformNode, name, v) {
|
|
122
|
+
const target = uniformNode.value;
|
|
123
|
+
if (v === void 0) return void target.set(0, 0, 0);
|
|
124
|
+
target.set(finite(`${name}.x`, v.x), finite(`${name}.y`, v.y), finite(`${name}.z`, v.z));
|
|
125
|
+
}
|
|
126
|
+
var GPUParticles3D = class extends Sprite {
|
|
127
|
+
amount;
|
|
128
|
+
buffers;
|
|
129
|
+
processCadence = "render";
|
|
130
|
+
warmupNodes;
|
|
131
|
+
localCoords;
|
|
132
|
+
emitting = true;
|
|
133
|
+
#start;
|
|
134
|
+
#process;
|
|
135
|
+
#spawn;
|
|
136
|
+
#renderer;
|
|
137
|
+
#released = false;
|
|
138
|
+
#lifetime;
|
|
139
|
+
#maxPerFrame;
|
|
140
|
+
#cursor = 0;
|
|
141
|
+
#spawnedThisFrame = 0;
|
|
142
|
+
#seed = 0;
|
|
143
|
+
#clamped = 0;
|
|
144
|
+
#spawned = 0;
|
|
145
|
+
#recycledLive = 0;
|
|
146
|
+
#warned = false;
|
|
147
|
+
/** Recent requests: wall-clock second, first ring sequence number, count. Pruned past `lifetime`. */
|
|
148
|
+
#log = [];
|
|
149
|
+
#boundsOverride;
|
|
150
|
+
#measuredBounds;
|
|
151
|
+
#padding;
|
|
152
|
+
#onCull;
|
|
153
|
+
#graceSeconds = 1;
|
|
154
|
+
#boundsEveryFrames;
|
|
155
|
+
#partials;
|
|
156
|
+
#reduce;
|
|
157
|
+
#readback;
|
|
158
|
+
#frame = 0;
|
|
159
|
+
#lastMeasure = 0;
|
|
160
|
+
#lands = 0;
|
|
161
|
+
#dispatches = 0;
|
|
162
|
+
#outsideSince;
|
|
163
|
+
#authoredVisible = true;
|
|
164
|
+
#hiddenByCull = false;
|
|
165
|
+
#yielded = false;
|
|
166
|
+
#state = "running";
|
|
167
|
+
#reason;
|
|
168
|
+
#transitions = 0;
|
|
169
|
+
/** Cooperate with measured frame-budget shedding without changing particle storage. */
|
|
170
|
+
budget = {
|
|
171
|
+
significance: (camera) => {
|
|
172
|
+
if (this.#released || !this.emitting || !this.#authoredVisible || this.castShadow || this.#reason === "outside-view")
|
|
173
|
+
return void 0;
|
|
174
|
+
this.updateWorldMatrix(true, false);
|
|
175
|
+
if (camera.matrixWorldAutoUpdate) camera.updateMatrixWorld();
|
|
176
|
+
if (this.measuredBounds === void 0) {
|
|
177
|
+
sphere.center.setFromMatrixPosition(this.matrixWorld);
|
|
178
|
+
sphere.radius = 0;
|
|
179
|
+
} else {
|
|
180
|
+
this.measuredBounds.getBoundingSphere(sphere).applyMatrix4(this.matrixWorld);
|
|
181
|
+
}
|
|
182
|
+
cameraPosition.setFromMatrixPosition(camera.matrixWorld);
|
|
183
|
+
return (sphere.radius + this.#worldPadding) / Math.max(sphere.center.distanceTo(cameraPosition), 1e-3);
|
|
184
|
+
},
|
|
185
|
+
yield: (yielded) => {
|
|
186
|
+
this.#yielded = yielded;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
constructor(options) {
|
|
190
|
+
if (!Number.isInteger(options.amount) || options.amount <= 0)
|
|
191
|
+
throw new Error("GPUParticles3D.amount must be a positive integer.");
|
|
192
|
+
if (options.material === void 0) throw new Error("GPUParticles3D.material is required.");
|
|
193
|
+
if (options.spawn !== void 0 && typeof options.spawn !== "function")
|
|
194
|
+
throw new Error("GPUParticles3D.spawn must be a function.");
|
|
195
|
+
if (options.start !== void 0 || options.spawn === void 0) {
|
|
196
|
+
if (typeof options.start !== "function")
|
|
197
|
+
throw new Error("GPUParticles3D.start must be a function.");
|
|
198
|
+
}
|
|
199
|
+
if (typeof options.process !== "function")
|
|
200
|
+
throw new Error("GPUParticles3D.process must be a function.");
|
|
201
|
+
if (options.lifetime !== void 0 && !(options.lifetime > 0))
|
|
202
|
+
throw new Error("GPUParticles3D.lifetime must be a positive number.");
|
|
203
|
+
const maxPerFrame = options.maxSpawnPerFrame ?? options.amount;
|
|
204
|
+
if (!Number.isInteger(maxPerFrame) || maxPerFrame <= 0)
|
|
205
|
+
throw new Error("GPUParticles3D.maxSpawnPerFrame must be a positive integer.");
|
|
206
|
+
const boundsEveryFrames = options.boundsEveryFrames ?? 8;
|
|
207
|
+
if (!Number.isInteger(boundsEveryFrames) || boundsEveryFrames <= 0)
|
|
208
|
+
throw new Error("GPUParticles3D.boundsEveryFrames must be a positive integer.");
|
|
209
|
+
if (options.padding !== void 0 && (!Number.isFinite(options.padding) || options.padding < 0))
|
|
210
|
+
throw new Error("GPUParticles3D.padding must be finite and non-negative.");
|
|
211
|
+
if (options.onCull !== void 0 && options.onCull !== "pause" && options.onCull !== "clear")
|
|
212
|
+
throw new Error("GPUParticles3D.onCull must be pause or clear.");
|
|
213
|
+
if (options.bounds !== void 0 && (options.bounds.isBox3 !== true || options.bounds.isEmpty() || ![...options.bounds.min.toArray(), ...options.bounds.max.toArray()].every(Number.isFinite)))
|
|
214
|
+
throw new Error("GPUParticles3D.bounds must be a finite, non-empty Box3.");
|
|
215
|
+
super(options.material);
|
|
216
|
+
Object.defineProperty(this, "visible", {
|
|
217
|
+
configurable: true,
|
|
218
|
+
enumerable: true,
|
|
219
|
+
get: () => this.#authoredVisible && !this.#hiddenByCull,
|
|
220
|
+
set: (visible) => {
|
|
221
|
+
this.#authoredVisible = visible;
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
this.#boundsOverride = options.bounds;
|
|
225
|
+
this.#padding = options.padding;
|
|
226
|
+
this.#onCull = options.onCull ?? "pause";
|
|
227
|
+
this.graceSeconds = options.graceSeconds ?? 1;
|
|
228
|
+
this.#boundsEveryFrames = boundsEveryFrames;
|
|
229
|
+
this.amount = options.amount;
|
|
230
|
+
this.#lifetime = options.lifetime;
|
|
231
|
+
this.#maxPerFrame = maxPerFrame;
|
|
232
|
+
const positions = instancedArray(options.amount, "vec3");
|
|
233
|
+
const velocities = instancedArray(options.amount, "vec3");
|
|
234
|
+
const births = options.spawn === void 0 ? void 0 : instancedArray(options.amount, "float");
|
|
235
|
+
this.buffers = births === void 0 ? { positions, velocities } : { positions, velocities, births };
|
|
236
|
+
options.material.positionNode = this.buffers.positions.toAttribute();
|
|
237
|
+
this.count = options.amount;
|
|
238
|
+
this.frustumCulled = false;
|
|
239
|
+
this.localCoords = options.localCoords ?? options.spawn === void 0;
|
|
240
|
+
if (!this.localCoords) {
|
|
241
|
+
this.matrixAutoUpdate = false;
|
|
242
|
+
this.matrixWorldAutoUpdate = false;
|
|
243
|
+
this.matrixWorld.identity();
|
|
244
|
+
}
|
|
245
|
+
this.#start = options.start === void 0 ? void 0 : computeNode(
|
|
246
|
+
"start",
|
|
247
|
+
options.start(this.buffers)
|
|
248
|
+
);
|
|
249
|
+
this.#process = computeNode(
|
|
250
|
+
"process",
|
|
251
|
+
options.process(
|
|
252
|
+
this.buffers,
|
|
253
|
+
{ time, delta: deltaTime }
|
|
254
|
+
)
|
|
255
|
+
);
|
|
256
|
+
if (options.spawn !== void 0 && births !== void 0)
|
|
257
|
+
this.#spawn = this.#buildSpawn(options.spawn, births);
|
|
258
|
+
if (options.bounds === void 0) {
|
|
259
|
+
const partials = instancedArray(2 * LANES, "vec4");
|
|
260
|
+
const positions2 = this.buffers.positions;
|
|
261
|
+
const amount = this.amount;
|
|
262
|
+
this.#partials = partials;
|
|
263
|
+
this.#reduce = Fn(() => {
|
|
264
|
+
const lo = vec3(1e30).toVar();
|
|
265
|
+
const hi = vec3(-1e30).toVar();
|
|
266
|
+
Loop({ start: instanceIndex, end: amount, type: "uint", update: LANES }, ({ i }) => {
|
|
267
|
+
const position = positions2.element(i);
|
|
268
|
+
lo.assign(min(lo, position));
|
|
269
|
+
hi.assign(max(hi, position));
|
|
270
|
+
});
|
|
271
|
+
partials.element(instanceIndex).assign(vec4(lo, 0));
|
|
272
|
+
partials.element(instanceIndex.add(LANES)).assign(vec4(hi, 0));
|
|
273
|
+
})().compute(LANES);
|
|
274
|
+
this.#readback = new GPUReadback({ attribute: partials.value, everyFrames: 1 });
|
|
275
|
+
}
|
|
276
|
+
this.warmupNodes = [
|
|
277
|
+
...this.#start === void 0 ? [] : [this.#start],
|
|
278
|
+
...this.#spawn === void 0 ? [] : [this.#spawn.reset, this.#spawn.stamp, this.#spawn.spawn],
|
|
279
|
+
this.#process,
|
|
280
|
+
...this.#reduce === void 0 ? [] : [this.#reduce]
|
|
281
|
+
];
|
|
282
|
+
this.addEventListener("removed", this.#onRemoved);
|
|
283
|
+
}
|
|
284
|
+
#buildSpawn(spawn, births) {
|
|
285
|
+
const amount = this.amount;
|
|
286
|
+
const request = {
|
|
287
|
+
first: uniform(0, "uint"),
|
|
288
|
+
count: uniform(0, "uint"),
|
|
289
|
+
origin: uniform(new Vector3()),
|
|
290
|
+
direction: uniform(new Vector3()),
|
|
291
|
+
seed: uniform(0),
|
|
292
|
+
time
|
|
293
|
+
};
|
|
294
|
+
const stamp = Fn(() => {
|
|
295
|
+
If(instanceIndex.lessThan(request.count), () => {
|
|
296
|
+
births.element(request.first.add(instanceIndex).mod(uint(amount))).assign(time);
|
|
297
|
+
});
|
|
298
|
+
})().compute(amount);
|
|
299
|
+
const reset = Fn(() => {
|
|
300
|
+
births.element(instanceIndex).assign(DEAD_BIRTH);
|
|
301
|
+
})().compute(amount);
|
|
302
|
+
return {
|
|
303
|
+
births,
|
|
304
|
+
request,
|
|
305
|
+
reset,
|
|
306
|
+
spawn: computeNode("spawn", spawn({ ...this.buffers, births }, request)),
|
|
307
|
+
stamp
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
get released() {
|
|
311
|
+
return this.#released;
|
|
312
|
+
}
|
|
313
|
+
get stats() {
|
|
314
|
+
return {
|
|
315
|
+
clamped: this.#clamped,
|
|
316
|
+
recycledLive: this.#lifetime === void 0 ? void 0 : this.#recycledLive,
|
|
317
|
+
spawned: this.#spawned
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
/** Seconds outside view before culling; Infinity disables it at runtime. */
|
|
321
|
+
get graceSeconds() {
|
|
322
|
+
return this.#graceSeconds;
|
|
323
|
+
}
|
|
324
|
+
set graceSeconds(value) {
|
|
325
|
+
if (!Number.isFinite(value) && value !== Number.POSITIVE_INFINITY || value < 0)
|
|
326
|
+
throw new Error("GPUParticles3D.graceSeconds must be non-negative.");
|
|
327
|
+
this.#graceSeconds = value;
|
|
328
|
+
}
|
|
329
|
+
attachRenderer(renderer) {
|
|
330
|
+
if (this.#released) throw new Error("GPUParticles3D cannot be attached after release.");
|
|
331
|
+
if (this.#renderer === renderer) return;
|
|
332
|
+
this.#renderer = renderer;
|
|
333
|
+
this.#runStart(renderer);
|
|
334
|
+
}
|
|
335
|
+
/** The override or newest landed local bounds; absent until a valid sample lands. */
|
|
336
|
+
get measuredBounds() {
|
|
337
|
+
return this.#boundsOverride ?? this.#measuredBounds;
|
|
338
|
+
}
|
|
339
|
+
/** Simulation compute calls, excluding starts and bounds reduction. */
|
|
340
|
+
get dispatches() {
|
|
341
|
+
return this.#dispatches;
|
|
342
|
+
}
|
|
343
|
+
/** Current cull state and the number of state changes. */
|
|
344
|
+
get cull() {
|
|
345
|
+
return { state: this.#state, reason: this.#reason, transitions: this.#transitions };
|
|
346
|
+
}
|
|
347
|
+
get #worldPadding() {
|
|
348
|
+
return this.#padding ?? 0.5 * this.matrixWorld.getMaxScaleOnAxis();
|
|
349
|
+
}
|
|
350
|
+
process(renderer = this.#renderer, camera, _covered, _view, simulatedSeconds) {
|
|
351
|
+
if (this.#released || !this.emitting) return;
|
|
352
|
+
if (renderer === void 0) throw new Error("GPUParticles3D is not attached to a renderer.");
|
|
353
|
+
let readback = this.#readback;
|
|
354
|
+
if (readback !== void 0 && readback.stats.lands !== this.#lands) {
|
|
355
|
+
this.#lands = readback.stats.lands;
|
|
356
|
+
this.#measuredBounds = readback.data === void 0 ? void 0 : foldBounds(readback.data);
|
|
357
|
+
}
|
|
358
|
+
let reason;
|
|
359
|
+
if (camera !== void 0 && !this.castShadow && this.#authoredVisible) {
|
|
360
|
+
const bounds = this.measuredBounds;
|
|
361
|
+
if (bounds !== void 0 && this.#graceSeconds !== Number.POSITIVE_INFINITY) {
|
|
362
|
+
const engine = this.__boundsInView;
|
|
363
|
+
let inView;
|
|
364
|
+
if (typeof engine === "function")
|
|
365
|
+
inView = engine.call(this, camera, bounds, this.#padding ?? Number.NaN);
|
|
366
|
+
else {
|
|
367
|
+
if (camera.matrixWorldAutoUpdate) camera.updateMatrixWorld();
|
|
368
|
+
this.updateWorldMatrix(true, false);
|
|
369
|
+
viewProjection.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
|
|
370
|
+
frustum.setFromProjectionMatrix(
|
|
371
|
+
viewProjection,
|
|
372
|
+
camera.coordinateSystem,
|
|
373
|
+
camera.reversedDepth
|
|
374
|
+
);
|
|
375
|
+
worldBox.copy(bounds).applyMatrix4(this.matrixWorld).expandByScalar(this.#worldPadding);
|
|
376
|
+
inView = frustum.intersectsBox(worldBox);
|
|
377
|
+
}
|
|
378
|
+
if (inView) this.#outsideSince = void 0;
|
|
379
|
+
else {
|
|
380
|
+
const now = simulatedSeconds ?? performance.now() / 1e3;
|
|
381
|
+
this.#outsideSince ??= now;
|
|
382
|
+
if (now - this.#outsideSince >= this.#graceSeconds) reason = "outside-view";
|
|
383
|
+
}
|
|
384
|
+
} else this.#outsideSince = void 0;
|
|
385
|
+
reason ??= this.#yielded ? "over-budget" : void 0;
|
|
386
|
+
} else this.#outsideSince = void 0;
|
|
387
|
+
const state = reason === void 0 ? "running" : this.#onCull === "clear" ? "cleared" : "paused";
|
|
388
|
+
if (state !== this.#state) {
|
|
389
|
+
this.#transitions += 1;
|
|
390
|
+
if (state === "running" && this.#onCull === "clear") this.restart();
|
|
391
|
+
this.#state = state;
|
|
392
|
+
readback = this.#readback;
|
|
393
|
+
}
|
|
394
|
+
this.#reason = reason;
|
|
395
|
+
if (reason !== void 0) {
|
|
396
|
+
this.#hiddenByCull = true;
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
this.#hiddenByCull = false;
|
|
400
|
+
renderer.compute(this.#process);
|
|
401
|
+
this.#spawnedThisFrame = 0;
|
|
402
|
+
this.#dispatches += 1;
|
|
403
|
+
this.#frame += 1;
|
|
404
|
+
if (readback !== void 0 && this.#reduce !== void 0 && typeof renderer.readback === "function" && !readback.pending && !(readback.stats.failures > 0 && readback.stats.lands === 0) && this.#frame - this.#lastMeasure >= this.#boundsEveryFrames) {
|
|
405
|
+
this.#lastMeasure = this.#frame;
|
|
406
|
+
renderer.compute(this.#reduce);
|
|
407
|
+
readback.request(renderer);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Spawns `count` particles at `origin` (world space unless `localCoords`). Every call gets its own ring
|
|
412
|
+
* slots, so a second burst never touches the first one's live particles. No readback.
|
|
413
|
+
*/
|
|
414
|
+
emitParticle(request = {}) {
|
|
415
|
+
if (this.#released) throw new Error("GPUParticles3D cannot emit after release.");
|
|
416
|
+
if (this.#spawn === void 0)
|
|
417
|
+
throw new Error("GPUParticles3D.emitParticle requires a spawn kernel.");
|
|
418
|
+
if (this.#renderer === void 0)
|
|
419
|
+
throw new Error("GPUParticles3D is not attached to a renderer.");
|
|
420
|
+
const asked = request.count ?? 1;
|
|
421
|
+
if (!Number.isInteger(asked) || asked < 0)
|
|
422
|
+
throw new Error("GPUParticles3D.emitParticle count must be a non-negative integer.");
|
|
423
|
+
const { request: uniforms } = this.#spawn;
|
|
424
|
+
setVector(uniforms.origin, "origin", request.origin);
|
|
425
|
+
setVector(uniforms.direction, "direction", request.direction);
|
|
426
|
+
uniforms.seed.value = finite("seed", request.seed ?? this.#seed++);
|
|
427
|
+
const count = Math.min(asked, this.amount, this.#maxPerFrame - this.#spawnedThisFrame);
|
|
428
|
+
if (count < asked) {
|
|
429
|
+
this.#clamped += asked - count;
|
|
430
|
+
if (!this.#warned) {
|
|
431
|
+
this.#warned = true;
|
|
432
|
+
console.warn(
|
|
433
|
+
`GPUParticles3D: spawn request of ${asked} clamped to ${count} (amount ${this.amount}, maxSpawnPerFrame ${this.#maxPerFrame}).`
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (count === 0) return;
|
|
438
|
+
this.#countRecycled(count);
|
|
439
|
+
uniforms.first.value = this.#cursor % this.amount;
|
|
440
|
+
uniforms.count.value = count;
|
|
441
|
+
this.#cursor += count;
|
|
442
|
+
this.#spawnedThisFrame += count;
|
|
443
|
+
this.#spawned += count;
|
|
444
|
+
this.#renderer.compute(this.#spawn.stamp);
|
|
445
|
+
this.#renderer.compute(this.#spawn.spawn);
|
|
446
|
+
}
|
|
447
|
+
/** Reruns `start`. A spawn-driven emitter instead kills every particle and rewinds the ring. */
|
|
448
|
+
restart() {
|
|
449
|
+
if (this.#released) throw new Error("GPUParticles3D cannot restart after release.");
|
|
450
|
+
if (this.#renderer === void 0)
|
|
451
|
+
throw new Error("GPUParticles3D is not attached to a renderer.");
|
|
452
|
+
this.#runStart(this.#renderer);
|
|
453
|
+
this.#readback?.dispose();
|
|
454
|
+
this.#readback = this.#partials === void 0 ? void 0 : new GPUReadback({ attribute: this.#partials.value, everyFrames: 1 });
|
|
455
|
+
this.#measuredBounds = void 0;
|
|
456
|
+
this.#lands = 0;
|
|
457
|
+
this.#outsideSince = void 0;
|
|
458
|
+
this.#lastMeasure = this.#frame - this.#boundsEveryFrames;
|
|
459
|
+
}
|
|
460
|
+
detach() {
|
|
461
|
+
if (this.#released) return;
|
|
462
|
+
this.#renderer = void 0;
|
|
463
|
+
this.#start?.dispose();
|
|
464
|
+
this.#process.dispose();
|
|
465
|
+
if (this.#spawn !== void 0) {
|
|
466
|
+
this.#spawn.spawn.dispose();
|
|
467
|
+
this.#spawn.stamp.dispose();
|
|
468
|
+
this.#spawn.reset.dispose();
|
|
469
|
+
this.#spawn.births.value.dispose();
|
|
470
|
+
}
|
|
471
|
+
this.#reduce?.dispose();
|
|
472
|
+
this.#readback?.dispose();
|
|
473
|
+
this.#partials?.value.dispose();
|
|
474
|
+
this.buffers.positions.value.dispose();
|
|
475
|
+
this.buffers.velocities.value.dispose();
|
|
476
|
+
this.#released = true;
|
|
477
|
+
}
|
|
478
|
+
#runStart(renderer) {
|
|
479
|
+
if (this.#start !== void 0) renderer.compute(this.#start);
|
|
480
|
+
if (this.#spawn === void 0) return;
|
|
481
|
+
renderer.compute(this.#spawn.reset);
|
|
482
|
+
this.#cursor = 0;
|
|
483
|
+
this.#spawnedThisFrame = 0;
|
|
484
|
+
this.#log = [];
|
|
485
|
+
}
|
|
486
|
+
/** Counts the slots this request overwrites that a recent request filled less than `lifetime` ago. */
|
|
487
|
+
#countRecycled(count) {
|
|
488
|
+
const lifetime = this.#lifetime;
|
|
489
|
+
if (lifetime === void 0) return;
|
|
490
|
+
const now = performance.now() / 1e3;
|
|
491
|
+
const first = this.#cursor;
|
|
492
|
+
const overwritten = first - this.amount;
|
|
493
|
+
this.#log = this.#log.filter((entry) => now - entry.at < lifetime);
|
|
494
|
+
for (const entry of this.#log) {
|
|
495
|
+
const lo = Math.max(entry.first, overwritten);
|
|
496
|
+
const hi = Math.min(entry.first + entry.count, overwritten + count);
|
|
497
|
+
if (hi > lo) this.#recycledLive += hi - lo;
|
|
498
|
+
}
|
|
499
|
+
this.#log.push({ at: now, count, first });
|
|
500
|
+
}
|
|
501
|
+
#onRemoved = () => this.detach();
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
export { CameraShake, GPUParticles3D };
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// src/terrain-jobs.ts
|
|
2
|
+
function mergeBlockAttributes(job) {
|
|
3
|
+
let vertexCount = 0;
|
|
4
|
+
let indexCount = 0;
|
|
5
|
+
for (const part of job.parts) {
|
|
6
|
+
vertexCount += part.positions.length / 3;
|
|
7
|
+
indexCount += part.indices.length;
|
|
8
|
+
}
|
|
9
|
+
const positions = new Float32Array(vertexCount * 3);
|
|
10
|
+
const normals = new Float32Array(vertexCount * 3);
|
|
11
|
+
const indices = new Uint32Array(indexCount);
|
|
12
|
+
let vertexOffset = 0;
|
|
13
|
+
let indexOffset = 0;
|
|
14
|
+
for (const part of job.parts) {
|
|
15
|
+
const dx = part.origin.x - job.blockOrigin.x;
|
|
16
|
+
const dz = part.origin.z - job.blockOrigin.z;
|
|
17
|
+
const count = part.positions.length / 3;
|
|
18
|
+
for (let vertex = 0; vertex < count; vertex += 1) {
|
|
19
|
+
const at = (vertexOffset + vertex) * 3;
|
|
20
|
+
const from = vertex * 3;
|
|
21
|
+
positions[at] = part.positions[from] + dx;
|
|
22
|
+
positions[at + 1] = part.positions[from + 1];
|
|
23
|
+
positions[at + 2] = part.positions[from + 2] + dz;
|
|
24
|
+
normals[at] = part.normals[from];
|
|
25
|
+
normals[at + 1] = part.normals[from + 1];
|
|
26
|
+
normals[at + 2] = part.normals[from + 2];
|
|
27
|
+
}
|
|
28
|
+
for (let element = 0; element < part.indices.length; element += 1)
|
|
29
|
+
indices[indexOffset + element] = part.indices[element] + vertexOffset;
|
|
30
|
+
vertexOffset += count;
|
|
31
|
+
indexOffset += part.indices.length;
|
|
32
|
+
}
|
|
33
|
+
return { indices, kind: "merge", normals, positions };
|
|
34
|
+
}
|
|
35
|
+
function interpolatedEdgeSample(samples, normalized, name) {
|
|
36
|
+
const position = Math.max(0, Math.min(1, normalized)) * (samples.length - 1);
|
|
37
|
+
const lower = Math.floor(position);
|
|
38
|
+
const upper = Math.min(samples.length - 1, lower + 1);
|
|
39
|
+
const mix = position - lower;
|
|
40
|
+
const lowerValue = samples[lower];
|
|
41
|
+
const upperValue = samples[upper];
|
|
42
|
+
if (!Number.isFinite(lowerValue) || !Number.isFinite(upperValue))
|
|
43
|
+
throw new Error(`TerrainTiles retained ${name} edge sample must be finite.`);
|
|
44
|
+
return lowerValue * (1 - mix) + upperValue * mix;
|
|
45
|
+
}
|
|
46
|
+
function edgeWorldPoint(edge, normalized, height) {
|
|
47
|
+
const minimumX = edge.origin.x - edge.width / 2;
|
|
48
|
+
const minimumZ = edge.origin.z - edge.depth / 2;
|
|
49
|
+
const x = edge.side === "west" ? minimumX : edge.side === "east" ? minimumX + edge.width : minimumX + normalized * edge.width;
|
|
50
|
+
const z = edge.side === "north" ? minimumZ : edge.side === "south" ? minimumZ + edge.depth : minimumZ + normalized * edge.depth;
|
|
51
|
+
return [x, height, z];
|
|
52
|
+
}
|
|
53
|
+
function stitchBridgeAttributes(fine, coarse) {
|
|
54
|
+
const positions = new Float32Array(fine.resolution * 6);
|
|
55
|
+
const normals = new Float32Array(fine.resolution * 6);
|
|
56
|
+
const indices = new Uint32Array((fine.resolution - 1) * 12);
|
|
57
|
+
let coverageDepth = 0;
|
|
58
|
+
for (let index = 0; index < fine.resolution; index += 1) {
|
|
59
|
+
const normalized = index / (fine.resolution - 1);
|
|
60
|
+
const fineHeight = interpolatedEdgeSample(
|
|
61
|
+
fine.samples,
|
|
62
|
+
normalized,
|
|
63
|
+
`${fine.side} of finer LOD`
|
|
64
|
+
);
|
|
65
|
+
const coarseHeight = interpolatedEdgeSample(
|
|
66
|
+
coarse.samples,
|
|
67
|
+
normalized,
|
|
68
|
+
`${coarse.side} of coarser LOD`
|
|
69
|
+
);
|
|
70
|
+
coverageDepth = Math.max(coverageDepth, Math.abs(fineHeight - coarseHeight));
|
|
71
|
+
const at = index * 6;
|
|
72
|
+
const finePoint = edgeWorldPoint(fine, normalized, fineHeight);
|
|
73
|
+
const coarsePoint = edgeWorldPoint(coarse, normalized, coarseHeight);
|
|
74
|
+
positions[at] = finePoint[0];
|
|
75
|
+
positions[at + 1] = finePoint[1];
|
|
76
|
+
positions[at + 2] = finePoint[2];
|
|
77
|
+
positions[at + 3] = coarsePoint[0];
|
|
78
|
+
positions[at + 4] = coarsePoint[1];
|
|
79
|
+
positions[at + 5] = coarsePoint[2];
|
|
80
|
+
normals[at] = 0;
|
|
81
|
+
normals[at + 1] = 1;
|
|
82
|
+
normals[at + 2] = 0;
|
|
83
|
+
normals[at + 3] = 0;
|
|
84
|
+
normals[at + 4] = 1;
|
|
85
|
+
normals[at + 5] = 0;
|
|
86
|
+
}
|
|
87
|
+
for (let index = 0; index < fine.resolution - 1; index += 1) {
|
|
88
|
+
const fine2 = index * 2;
|
|
89
|
+
const coarse2 = fine2 + 1;
|
|
90
|
+
const nextFine = fine2 + 2;
|
|
91
|
+
const nextCoarse = coarse2 + 2;
|
|
92
|
+
const at = index * 12;
|
|
93
|
+
indices[at] = fine2;
|
|
94
|
+
indices[at + 1] = coarse2;
|
|
95
|
+
indices[at + 2] = nextFine;
|
|
96
|
+
indices[at + 3] = nextFine;
|
|
97
|
+
indices[at + 4] = coarse2;
|
|
98
|
+
indices[at + 5] = nextCoarse;
|
|
99
|
+
indices[at + 6] = nextFine;
|
|
100
|
+
indices[at + 7] = coarse2;
|
|
101
|
+
indices[at + 8] = fine2;
|
|
102
|
+
indices[at + 9] = nextCoarse;
|
|
103
|
+
indices[at + 10] = coarse2;
|
|
104
|
+
indices[at + 11] = nextFine;
|
|
105
|
+
}
|
|
106
|
+
return { coverageDepth, indices, normals, positions };
|
|
107
|
+
}
|
|
108
|
+
function runTerrainJob(job) {
|
|
109
|
+
if (job.kind === "merge") return mergeBlockAttributes(job);
|
|
110
|
+
return {
|
|
111
|
+
bridges: job.pairs.map(({ coarse, fine }) => stitchBridgeAttributes(fine, coarse)),
|
|
112
|
+
kind: "seam"
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function terrainJobTransfers(result) {
|
|
116
|
+
if (result.kind === "merge")
|
|
117
|
+
return [result.positions.buffer, result.normals.buffer, result.indices.buffer];
|
|
118
|
+
return result.bridges.flatMap((bridge) => [
|
|
119
|
+
bridge.positions.buffer,
|
|
120
|
+
bridge.normals.buffer,
|
|
121
|
+
bridge.indices.buffer
|
|
122
|
+
]);
|
|
123
|
+
}
|
|
124
|
+
function spawnTerrainWorker() {
|
|
125
|
+
if (typeof Worker === "undefined") return { reason: "no Worker on this host", worker: void 0 };
|
|
126
|
+
try {
|
|
127
|
+
const worker = new Worker(new URL("./terrain-jobs-worker.js", import.meta.url), {
|
|
128
|
+
type: "module"
|
|
129
|
+
});
|
|
130
|
+
return { reason: void 0, worker };
|
|
131
|
+
} catch (error) {
|
|
132
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
133
|
+
return { reason: `module worker refused: ${message}`, worker: void 0 };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function createTerrainJobRunner() {
|
|
137
|
+
const pending = /* @__PURE__ */ new Map();
|
|
138
|
+
let nextId = 0;
|
|
139
|
+
const spawned = spawnTerrainWorker();
|
|
140
|
+
let worker = spawned.worker;
|
|
141
|
+
const abandon = () => {
|
|
142
|
+
for (const [, job] of pending) job.resolve(job.compute());
|
|
143
|
+
pending.clear();
|
|
144
|
+
worker?.terminate();
|
|
145
|
+
worker = void 0;
|
|
146
|
+
};
|
|
147
|
+
if (worker !== void 0) {
|
|
148
|
+
worker.onmessage = (event) => {
|
|
149
|
+
const reply = event.data;
|
|
150
|
+
pending.get(reply.id)?.resolve(reply.result);
|
|
151
|
+
pending.delete(reply.id);
|
|
152
|
+
};
|
|
153
|
+
worker.onerror = abandon;
|
|
154
|
+
}
|
|
155
|
+
const dispatch = (job, compute) => {
|
|
156
|
+
const active = worker;
|
|
157
|
+
if (active === void 0) return compute();
|
|
158
|
+
const id = nextId;
|
|
159
|
+
nextId += 1;
|
|
160
|
+
const sent = new Promise((resolve) => {
|
|
161
|
+
pending.set(id, {
|
|
162
|
+
compute,
|
|
163
|
+
resolve
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
try {
|
|
167
|
+
active.postMessage({ id, job });
|
|
168
|
+
} catch {
|
|
169
|
+
pending.delete(id);
|
|
170
|
+
return compute();
|
|
171
|
+
}
|
|
172
|
+
return sent;
|
|
173
|
+
};
|
|
174
|
+
return {
|
|
175
|
+
dispose: () => {
|
|
176
|
+
pending.clear();
|
|
177
|
+
worker?.terminate();
|
|
178
|
+
worker = void 0;
|
|
179
|
+
},
|
|
180
|
+
inlineReason: spawned.reason,
|
|
181
|
+
merge: (job) => dispatch(job, () => runTerrainJob(job)),
|
|
182
|
+
offThread: worker !== void 0,
|
|
183
|
+
seam: (job) => dispatch(job, () => runTerrainJob(job))
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export { createTerrainJobRunner, edgeWorldPoint, runTerrainJob, terrainJobTransfers };
|