@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,184 @@
|
|
|
1
|
+
// Ported from Ultra Elements src/engine/camera.js @698b720 (MIT, Copyright (c) 2026 Humpalumps). JavaScript copied verbatim; GLSL transliterated to TSL.
|
|
2
|
+
import { type Camera, Euler, type PerspectiveCamera, Quaternion, Vector3 } from "three";
|
|
3
|
+
|
|
4
|
+
// Camera game feel, all of it on the camera rather than in post:
|
|
5
|
+
// - trauma shake (Eiserloh, "Juicing Your Cameras With Math", GDC 2016): offsets = trauma^2 x Perlin
|
|
6
|
+
// noise, so small hits barely move it and big ones slam. Each source keeps its own frequency
|
|
7
|
+
// (earth rumbles at ~8 Hz, lightning cracks at ~30 Hz) and they mix weighted by trauma.
|
|
8
|
+
// - FOV punch and hitstop (Nijman, "The Art of Screenshake", 2013).
|
|
9
|
+
// apply() goes right after OrbitControls.update() and restore() right before the next one, so the
|
|
10
|
+
// controls never see the offsets and can't accumulate them. No allocations per frame.
|
|
11
|
+
|
|
12
|
+
const DEG = Math.PI / 180;
|
|
13
|
+
const YAW = 2 * DEG;
|
|
14
|
+
const PITCH = 2 * DEG;
|
|
15
|
+
const ROLL = 3 * DEG;
|
|
16
|
+
const MOVE = 0.06; // at trauma 1
|
|
17
|
+
const SOURCES = 4;
|
|
18
|
+
const QUEUE = 8;
|
|
19
|
+
|
|
20
|
+
// 1D gradient noise in [-1, 1] (hashed gradients on the integer lattice, quintic fade)
|
|
21
|
+
function grad(i0: number): number {
|
|
22
|
+
let i = i0;
|
|
23
|
+
i = Math.imul(i ^ (i >>> 16), 0x45d9f3b);
|
|
24
|
+
i = Math.imul(i ^ (i >>> 16), 0x45d9f3b);
|
|
25
|
+
return ((i ^ (i >>> 16)) >>> 0) / 2147483648 - 1;
|
|
26
|
+
}
|
|
27
|
+
function perlin(x: number, seed: number): number {
|
|
28
|
+
const i = Math.floor(x);
|
|
29
|
+
const f = x - i;
|
|
30
|
+
const s = seed * 1013;
|
|
31
|
+
const a = grad(i + s) * f;
|
|
32
|
+
const b = grad(i + 1 + s) * (f - 1);
|
|
33
|
+
return 2 * (a + (b - a) * f * f * f * (f * (f * 6 - 15) + 10));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const _q = new Quaternion();
|
|
37
|
+
const _e = new Euler();
|
|
38
|
+
|
|
39
|
+
type RigCamera = Camera & Partial<Pick<PerspectiveCamera, "fov" | "updateProjectionMatrix">>;
|
|
40
|
+
|
|
41
|
+
export class CameraRig {
|
|
42
|
+
reduced = false; // "Reduce flashes & shake": shake x0.25, no hitstop, no punch
|
|
43
|
+
amount = 1; // shake multiplier (GUI)
|
|
44
|
+
decay = 1.5; // trauma lost per second
|
|
45
|
+
|
|
46
|
+
private _src = Array.from({ length: SOURCES }, (_, i) => ({
|
|
47
|
+
trauma: 0,
|
|
48
|
+
freq: 15,
|
|
49
|
+
phase: 0,
|
|
50
|
+
seed: i * 6 + 1,
|
|
51
|
+
}));
|
|
52
|
+
private _queue = Array.from({ length: QUEUE }, () => ({ t: 0, trauma: 0, freq: 15 }));
|
|
53
|
+
private _stop = 0; // hitstop seconds left
|
|
54
|
+
private _punch = { deg: 0, t: 1, dur: 1 };
|
|
55
|
+
private _off = new Vector3(); // what apply() added, for restore()
|
|
56
|
+
private _rot = new Quaternion();
|
|
57
|
+
private _fov = 0;
|
|
58
|
+
private _applied = false;
|
|
59
|
+
|
|
60
|
+
// trauma 0..1 (it adds up and clamps); delay in seconds (thunder: distance / 343)
|
|
61
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: upstream Ultra Elements code, copied verbatim
|
|
62
|
+
shake(trauma: number, freqHz = 15, delay = 0): void {
|
|
63
|
+
if (!(trauma > 0)) return;
|
|
64
|
+
if (delay > 0) {
|
|
65
|
+
let slot = this._queue[0] as (typeof this._queue)[number];
|
|
66
|
+
for (const q of this._queue)
|
|
67
|
+
if (q.t <= 0) {
|
|
68
|
+
slot = q;
|
|
69
|
+
break;
|
|
70
|
+
// biome-ignore lint/style/noUselessElse: upstream Ultra Elements code, copied verbatim
|
|
71
|
+
} else if (q.t > slot.t) slot = q; // full: drop the latest
|
|
72
|
+
slot.t = delay;
|
|
73
|
+
slot.trauma = trauma;
|
|
74
|
+
slot.freq = freqHz;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
// same-ish frequency: same source, trauma adds; else a free slot, else the weakest one
|
|
78
|
+
let slot: (typeof this._src)[number] | null = null;
|
|
79
|
+
let weakest = this._src[0] as (typeof this._src)[number];
|
|
80
|
+
for (const s of this._src) {
|
|
81
|
+
if (s.trauma > 0 && Math.abs(s.freq - freqHz) < 0.2 * freqHz) {
|
|
82
|
+
slot = s;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
if (s.trauma < weakest.trauma) weakest = s;
|
|
86
|
+
}
|
|
87
|
+
if (!slot) {
|
|
88
|
+
slot = weakest;
|
|
89
|
+
slot.trauma = 0;
|
|
90
|
+
slot.freq = freqHz;
|
|
91
|
+
}
|
|
92
|
+
slot.trauma = Math.min(1, slot.trauma + trauma);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// FOV kick: +deg over a frame, then eases back over the rest of `duration`
|
|
96
|
+
punch(deg = 3, duration = 0.08): void {
|
|
97
|
+
if (this.reduced) return;
|
|
98
|
+
const p = this._punch;
|
|
99
|
+
if (p.t < p.dur && Math.abs(p.deg) * (1 - p.t / p.dur) > Math.abs(deg)) return; // a bigger one is still running
|
|
100
|
+
p.deg = deg;
|
|
101
|
+
p.t = 0;
|
|
102
|
+
p.dur = Math.max(duration, 1e-3);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
hitstop(seconds: number): void {
|
|
106
|
+
if (!this.reduced) this._stop = Math.max(this._stop, seconds);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// main multiplies the simulation dt by this
|
|
110
|
+
get timeScale(): number {
|
|
111
|
+
return this._stop > 0 && !this.reduced ? 0.05 : 1;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// dt: real (unscaled) seconds; the shake keeps going through hitstop
|
|
115
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: upstream Ultra Elements code, copied verbatim
|
|
116
|
+
apply(camera: RigCamera, dt0: number): void {
|
|
117
|
+
this.restore(camera); // in case restore() was skipped
|
|
118
|
+
const dt = dt0 > 0 ? Math.min(dt0, 0.1) : 0;
|
|
119
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: upstream Ultra Elements code, copied verbatim
|
|
120
|
+
for (const q of this._queue) if (q.t > 0 && (q.t -= dt) <= 0) this.shake(q.trauma, q.freq);
|
|
121
|
+
this._stop = Math.max(0, this._stop - dt);
|
|
122
|
+
|
|
123
|
+
let total = 0;
|
|
124
|
+
let yaw = 0;
|
|
125
|
+
let pitch = 0;
|
|
126
|
+
let roll = 0;
|
|
127
|
+
let x = 0;
|
|
128
|
+
let y = 0;
|
|
129
|
+
let z = 0;
|
|
130
|
+
for (const s of this._src) {
|
|
131
|
+
if (s.trauma <= 0) continue;
|
|
132
|
+
s.phase = (s.phase + s.freq * dt) % 65536;
|
|
133
|
+
const w = s.trauma;
|
|
134
|
+
const t = s.phase;
|
|
135
|
+
total += w;
|
|
136
|
+
yaw += w * perlin(t, s.seed);
|
|
137
|
+
pitch += w * perlin(t, s.seed + 1);
|
|
138
|
+
roll += w * perlin(t, s.seed + 2);
|
|
139
|
+
x += w * perlin(t, s.seed + 3);
|
|
140
|
+
y += w * perlin(t, s.seed + 4);
|
|
141
|
+
z += w * perlin(t, s.seed + 5);
|
|
142
|
+
s.trauma = Math.max(0, s.trauma - this.decay * dt);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
this._off.set(0, 0, 0);
|
|
146
|
+
this._rot.identity();
|
|
147
|
+
if (total > 0) {
|
|
148
|
+
const tr = Math.min(total, 1);
|
|
149
|
+
const k = (tr * tr * this.amount * (this.reduced ? 0.25 : 1)) / total;
|
|
150
|
+
this._rot.setFromEuler(_e.set(pitch * k * PITCH, yaw * k * YAW, roll * k * ROLL, "YXZ"));
|
|
151
|
+
camera.quaternion.multiply(this._rot);
|
|
152
|
+
this._off
|
|
153
|
+
.set(x, y, z)
|
|
154
|
+
.multiplyScalar(k * MOVE)
|
|
155
|
+
.applyQuaternion(camera.quaternion);
|
|
156
|
+
camera.position.add(this._off);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const p = this._punch;
|
|
160
|
+
this._fov = 0;
|
|
161
|
+
if (p.t < p.dur && !this.reduced) {
|
|
162
|
+
const u = p.t / p.dur;
|
|
163
|
+
this._fov = p.deg * (u < 0.2 ? u / 0.2 : ((1 - u) / 0.8) ** 2);
|
|
164
|
+
p.t += dt;
|
|
165
|
+
}
|
|
166
|
+
if (this._fov !== 0 && camera.fov !== undefined) {
|
|
167
|
+
camera.fov += this._fov;
|
|
168
|
+
camera.updateProjectionMatrix?.();
|
|
169
|
+
}
|
|
170
|
+
this._applied = true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// takes back exactly what apply() added (subtracting, so camera jumps made in between survive)
|
|
174
|
+
restore(camera: RigCamera): void {
|
|
175
|
+
if (!this._applied) return;
|
|
176
|
+
this._applied = false;
|
|
177
|
+
camera.position.sub(this._off);
|
|
178
|
+
camera.quaternion.multiply(_q.copy(this._rot).invert());
|
|
179
|
+
if (this._fov !== 0 && camera.fov !== undefined) {
|
|
180
|
+
camera.fov -= this._fov;
|
|
181
|
+
camera.updateProjectionMatrix?.();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Ported from Ultra Elements src/engine/quality.js @698b720 (MIT, Copyright (c) 2026 Humpalumps). JavaScript copied verbatim; GLSL transliterated to TSL.
|
|
2
|
+
// Engine: the tier comes from `?q=` when there is a location; there is no `matchMedia` probe on the
|
|
3
|
+
// native host, so `mobile` is `?mobile` only.
|
|
4
|
+
|
|
5
|
+
// Quality tiers. Spells author counts at High; emission is multiplied by Q.emission (tier x auto).
|
|
6
|
+
// Anything here that changes a shader define or a target size only changes on an explicit tier
|
|
7
|
+
// switch (main.js shows "Recompiling…"); the auto ladder only touches uniforms and sizes.
|
|
8
|
+
export const TIERS = {
|
|
9
|
+
low: {
|
|
10
|
+
resolution: 0.6,
|
|
11
|
+
msaa: 2,
|
|
12
|
+
fxScale: 0.5,
|
|
13
|
+
particles: [256, 128],
|
|
14
|
+
debris: 512,
|
|
15
|
+
volumeSteps: 12,
|
|
16
|
+
noiseSize: 64,
|
|
17
|
+
groundSize: 512,
|
|
18
|
+
reflections: false,
|
|
19
|
+
reflectionScale: 0.35,
|
|
20
|
+
reflectFx: false,
|
|
21
|
+
maxLights: 8,
|
|
22
|
+
shadowLights: 0,
|
|
23
|
+
bloomLevels: 3,
|
|
24
|
+
emission: 0.35,
|
|
25
|
+
fluidIterations: 6,
|
|
26
|
+
},
|
|
27
|
+
medium: {
|
|
28
|
+
resolution: 0.85,
|
|
29
|
+
msaa: 4,
|
|
30
|
+
fxScale: 0.5,
|
|
31
|
+
particles: [512, 256],
|
|
32
|
+
debris: 2048,
|
|
33
|
+
volumeSteps: 16,
|
|
34
|
+
noiseSize: 128,
|
|
35
|
+
groundSize: 1024,
|
|
36
|
+
reflections: false,
|
|
37
|
+
reflectionScale: 0.35,
|
|
38
|
+
reflectFx: false,
|
|
39
|
+
maxLights: 16,
|
|
40
|
+
shadowLights: 1,
|
|
41
|
+
bloomLevels: 6,
|
|
42
|
+
emission: 0.7,
|
|
43
|
+
},
|
|
44
|
+
high: {
|
|
45
|
+
resolution: 1,
|
|
46
|
+
msaa: 4,
|
|
47
|
+
fxScale: 0.5,
|
|
48
|
+
particles: [512, 512],
|
|
49
|
+
debris: 4096,
|
|
50
|
+
volumeSteps: 24,
|
|
51
|
+
noiseSize: 128,
|
|
52
|
+
groundSize: 1024,
|
|
53
|
+
reflections: false,
|
|
54
|
+
reflectionScale: 0.35,
|
|
55
|
+
reflectFx: true,
|
|
56
|
+
maxLights: 16,
|
|
57
|
+
shadowLights: 4,
|
|
58
|
+
bloomLevels: 6,
|
|
59
|
+
emission: 1,
|
|
60
|
+
},
|
|
61
|
+
ultra: {
|
|
62
|
+
resolution: 1,
|
|
63
|
+
msaa: 4,
|
|
64
|
+
fxScale: 1,
|
|
65
|
+
particles: [1024, 1024],
|
|
66
|
+
debris: 8192,
|
|
67
|
+
volumeSteps: 32,
|
|
68
|
+
noiseSize: 128,
|
|
69
|
+
groundSize: 2048,
|
|
70
|
+
reflections: false,
|
|
71
|
+
reflectionScale: 0.5,
|
|
72
|
+
reflectFx: true,
|
|
73
|
+
maxLights: 16,
|
|
74
|
+
shadowLights: 4,
|
|
75
|
+
bloomLevels: 6,
|
|
76
|
+
emission: 1.6,
|
|
77
|
+
fluid: true,
|
|
78
|
+
anamorphic: true,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
export type TierName = keyof typeof TIERS;
|
|
82
|
+
|
|
83
|
+
const args = new URLSearchParams(globalThis.location?.search ?? "");
|
|
84
|
+
export const mobile = args.has("mobile");
|
|
85
|
+
const asked = args.get("q");
|
|
86
|
+
export const tierName: TierName =
|
|
87
|
+
asked !== null && asked in TIERS ? (asked as TierName) : mobile ? "low" : "high";
|
|
88
|
+
|
|
89
|
+
// live values; auto scales multiply on top (auto.emission, auto.steps)
|
|
90
|
+
export const Q = {
|
|
91
|
+
tier: tierName,
|
|
92
|
+
mobile,
|
|
93
|
+
...TIERS[tierName],
|
|
94
|
+
autoResolution: mobile,
|
|
95
|
+
auto: { emission: 1, steps: 1 },
|
|
96
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Ultra Elements (MIT, Copyright (c) 2026 Humpalumps), ported @698b720: the shared foundation every
|
|
2
|
+
// ported spell module builds on, and the spells ported so far. Only what a game calls is public;
|
|
3
|
+
// ported spell modules import the TSL chunks, Volumes and FxTarget by relative path.
|
|
4
|
+
export { Q, TIERS } from "./engine/quality.js";
|
|
5
|
+
/**
|
|
6
|
+
* The shared Ultra Elements spell world: casts, volumes, spell lights and the per-frame CPU tick.
|
|
7
|
+
* Add `fx.scenes.fx` to the scene, `addSpell` each ported spell, call `renderFrame(dt)` each frame.
|
|
8
|
+
* @situation an Ultra Elements spell such as the raymarched teardrop Fireball cast at a target
|
|
9
|
+
* @situation a volumetric fire spell drawn through the Three.js TSL node API on WebGPU
|
|
10
|
+
* @constraint call bakeNoise first: the volume shaders sample its noise, curl and blackbody textures
|
|
11
|
+
* @example const ultra = createFx({ camera, size: () => ({ width: innerWidth, height: innerHeight }), seed: 777 }); const fireball = ultra.addSpell(fire); scene.add(ultra.fx.scenes.fx);
|
|
12
|
+
*/
|
|
13
|
+
export {
|
|
14
|
+
createFx,
|
|
15
|
+
type Fx,
|
|
16
|
+
type IFxOptions,
|
|
17
|
+
type ITrail,
|
|
18
|
+
type ITrails,
|
|
19
|
+
type IShocks,
|
|
20
|
+
type IGround,
|
|
21
|
+
type IFluid,
|
|
22
|
+
type IFluidDomain,
|
|
23
|
+
type IRig,
|
|
24
|
+
type IArena,
|
|
25
|
+
type IDummy,
|
|
26
|
+
} from "./main.js";
|
|
27
|
+
export type { ICast, ISpell, ISpellInstance, Phase } from "./spells/cast.js";
|
|
28
|
+
export { default as fire } from "./spells/fire.js";
|
|
29
|
+
/** The fixed arena layout the spells and the ground field read (floor, pool, dummies, caster focus). */
|
|
30
|
+
export { ARENA, U } from "./vfx/common.js";
|
|
31
|
+
/**
|
|
32
|
+
* The Ultra Elements arena floor's response to the ground field and spell impacts (scorch, char, wet,
|
|
33
|
+
* glowing hot ground) as nodes for a game's own floor material: `color`, `roughness`, `metalness`,
|
|
34
|
+
* `emissive` from the floor's own surface at a world position.
|
|
35
|
+
* @situation a floor that shows the Fireball's scorch and hot ground cooling over seconds
|
|
36
|
+
* @example const shade = groundShade(positionWorld, { albedo, rough: float(0.85), metal: float(0) }); floor.colorNode = vec4(shade.color, 1);
|
|
37
|
+
*/
|
|
38
|
+
export { groundShade, type IGroundSurface } from "./scene/arenaMaterial.js";
|
|
39
|
+
/**
|
|
40
|
+
* The Ultra Elements GPU particle system behind `fx.particles`: a spell's kinds, emitters and field emitters.
|
|
41
|
+
* @situation the flames, embers, sparks, smoke and debris an Ultra Elements spell emits as it charges, flies and hits
|
|
42
|
+
* @example const flames = ultra.fx.particles.emitter("fire.flame"); // ultra from createFx
|
|
43
|
+
*/
|
|
44
|
+
export { Particles } from "./vfx/Particles.js";
|
|
45
|
+
export type { IEmitter, KindDef } from "./vfx/Particles.js";
|
|
46
|
+
/**
|
|
47
|
+
* CPU bake of the Ultra Elements 3D noise, curl and blackbody textures that every spell volume samples.
|
|
48
|
+
* @situation the noise and blackbody textures an Ultra Elements spell needs before it first draws
|
|
49
|
+
* @constraint costs about 0.6 s at 64^3 and about 5 s at 128^3 on the main thread; bake once at load
|
|
50
|
+
* @example bakeNoise({ noiseSize: 64 }); // once at load, before createFx draws
|
|
51
|
+
*/
|
|
52
|
+
export { bakeNoise, type IBakedNoise } from "./vfx/noise.js";
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// Ported from Ultra Elements src/main.js @698b720 (MIT, Copyright (c) 2026 Humpalumps). JavaScript copied verbatim; GLSL transliterated to TSL.
|
|
2
|
+
//
|
|
3
|
+
// Only the part of main.js every spell depends on: the shared `fx` world (main.js ~line 94) and the
|
|
4
|
+
// CPU half of renderFrame (clock, casts, packWorld, systems). Upstream's renderer, arena, sky,
|
|
5
|
+
// PostFX, GUI and capture are not ported: the game's scene, its lights and WorldEnvironment replace
|
|
6
|
+
// them. The services later slices port (particles, trails, ground, shocks, fluid, rig, arena focus)
|
|
7
|
+
// are typed seams below; until a slice lands, each is an inert stand-in that keeps the spell code verbatim.
|
|
8
|
+
import {
|
|
9
|
+
type Camera,
|
|
10
|
+
type Color,
|
|
11
|
+
type Object3D,
|
|
12
|
+
PointLight,
|
|
13
|
+
type Scene,
|
|
14
|
+
type Vector3,
|
|
15
|
+
} from "three";
|
|
16
|
+
import { CameraRig } from "./engine/camera.js";
|
|
17
|
+
import { Q } from "./engine/quality.js";
|
|
18
|
+
import { Casts, type ICast, type ISpell } from "./spells/cast.js";
|
|
19
|
+
import { FluidVolume } from "./vfx/FluidVolume.js";
|
|
20
|
+
import { FxTarget } from "./vfx/FxTarget.js";
|
|
21
|
+
import { GroundField, type IFieldRenderer } from "./vfx/GroundField.js";
|
|
22
|
+
import { type IParticleGround, Particles } from "./vfx/Particles.js";
|
|
23
|
+
import { TrailRibbons } from "./vfx/Ribbons.js";
|
|
24
|
+
import { Shockwaves } from "./vfx/Shockwaves.js";
|
|
25
|
+
import { Volumes } from "./vfx/Volumes.js";
|
|
26
|
+
import {
|
|
27
|
+
ARENA,
|
|
28
|
+
U,
|
|
29
|
+
forces,
|
|
30
|
+
impacts,
|
|
31
|
+
lights,
|
|
32
|
+
mirrorLights,
|
|
33
|
+
occluders,
|
|
34
|
+
packWorld,
|
|
35
|
+
} from "./vfx/common.js";
|
|
36
|
+
|
|
37
|
+
// --- service seams (Ribbons.js, GroundField.js, Shockwaves.js, FluidVolume.js, camera.js, arena.js) ---
|
|
38
|
+
export interface ITrail {
|
|
39
|
+
width: number;
|
|
40
|
+
intensity: number;
|
|
41
|
+
glow: number;
|
|
42
|
+
glowIntensity: number;
|
|
43
|
+
fadeTime: number;
|
|
44
|
+
distortion: number;
|
|
45
|
+
color: Color;
|
|
46
|
+
push(p: Vector3): void;
|
|
47
|
+
release(): void;
|
|
48
|
+
}
|
|
49
|
+
export interface ITrails {
|
|
50
|
+
acquire(): ITrail | null;
|
|
51
|
+
}
|
|
52
|
+
export interface IShocks {
|
|
53
|
+
spawn(shock: object): void;
|
|
54
|
+
}
|
|
55
|
+
export interface IGround {
|
|
56
|
+
splat(splat: object): void;
|
|
57
|
+
}
|
|
58
|
+
export interface IFluidDomain {
|
|
59
|
+
position: Vector3;
|
|
60
|
+
size: Vector3;
|
|
61
|
+
h: number;
|
|
62
|
+
heat: number;
|
|
63
|
+
tint: Color;
|
|
64
|
+
sootColor: Color;
|
|
65
|
+
detailCenter: Vector3;
|
|
66
|
+
obstacle: { set(x: number, y: number, z: number, r: number): void };
|
|
67
|
+
emit(source: object): void;
|
|
68
|
+
release(): void;
|
|
69
|
+
[field: string]: unknown;
|
|
70
|
+
}
|
|
71
|
+
export interface IFluid {
|
|
72
|
+
res: number;
|
|
73
|
+
acquire(options: { size: number[]; res?: number }): IFluidDomain | null;
|
|
74
|
+
}
|
|
75
|
+
export interface IRig {
|
|
76
|
+
shake(amount: number, frequency: number): void;
|
|
77
|
+
punch(amount: number, seconds: number): void;
|
|
78
|
+
hitstop(seconds: number): void;
|
|
79
|
+
/** main multiplies the simulation dt by this (hitstop) */
|
|
80
|
+
readonly timeScale: number;
|
|
81
|
+
/** after the game's camera update: adds the shake and FOV punch; restore() takes them back */
|
|
82
|
+
apply(camera: Camera, dt: number): void;
|
|
83
|
+
restore(camera: Camera): void;
|
|
84
|
+
}
|
|
85
|
+
export interface IArena {
|
|
86
|
+
focus: { color: Color };
|
|
87
|
+
setFocus(color: Color, intensity: number): void;
|
|
88
|
+
}
|
|
89
|
+
export interface IDummy {
|
|
90
|
+
pos: Vector3;
|
|
91
|
+
status: { burn: number };
|
|
92
|
+
hit(dir: Vector3, amount: number): void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const NULL_GROUND: IGround = { splat() {} };
|
|
96
|
+
const NULL_FLUID: IFluid = { res: 0, acquire: () => null };
|
|
97
|
+
|
|
98
|
+
export interface IFxOptions {
|
|
99
|
+
camera: Camera;
|
|
100
|
+
/** compiles each spell's pipelines before its first cast (warmSpell); upstream: renderer.compileAsync */
|
|
101
|
+
renderer?: {
|
|
102
|
+
compileAsync(object: Object3D, camera: Camera, scene?: Scene): Promise<unknown>;
|
|
103
|
+
compute(node: unknown): unknown; // runs the particle sim kernel (Particles.ts)
|
|
104
|
+
};
|
|
105
|
+
/** drawing-buffer size in pixels (upstream: renderer.getDrawingBufferSize) */
|
|
106
|
+
size: () => { width: number; height: number };
|
|
107
|
+
seed?: number;
|
|
108
|
+
/** PointLights the spell lights drive (common.ts mirrorLights); parked at 0 when idle */
|
|
109
|
+
pointLights?: number;
|
|
110
|
+
trails?: ITrails;
|
|
111
|
+
ground?: IGround;
|
|
112
|
+
shocks?: IShocks;
|
|
113
|
+
fluid?: IFluid;
|
|
114
|
+
rig?: IRig;
|
|
115
|
+
arena?: IArena;
|
|
116
|
+
dummies?: IDummy[];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// the shared world every spell gets (PLAN 2.4). Spells only touch what's in here.
|
|
120
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: upstream main.js's world, one function
|
|
121
|
+
export function createFx(o: IFxOptions) {
|
|
122
|
+
let seed = o.seed ?? 1;
|
|
123
|
+
const fxTarget = new FxTarget();
|
|
124
|
+
const volumes = new Volumes();
|
|
125
|
+
const flashes: number[] = [];
|
|
126
|
+
// upstream main.js: GroundField, TrailRibbons, Shockwaves and CameraRig, shared by every spell.
|
|
127
|
+
// The field draws its sim passes with the raw renderer; it is built first, before any material
|
|
128
|
+
// that reads U.uGroundA/B (common.ts setGroundTexture). A game passes its own to override one.
|
|
129
|
+
const raw = (o.renderer as { raw?: unknown } | undefined)?.raw ?? o.renderer;
|
|
130
|
+
const groundField =
|
|
131
|
+
o.ground === undefined && raw !== undefined
|
|
132
|
+
? new GroundField(raw as IFieldRenderer, Q.groundSize)
|
|
133
|
+
: undefined;
|
|
134
|
+
const trailRibbons =
|
|
135
|
+
o.trails === undefined ? new TrailRibbons({ max: 64, points: 48 }) : undefined;
|
|
136
|
+
const shockwaves = o.shocks === undefined ? new Shockwaves() : undefined;
|
|
137
|
+
const fx = {
|
|
138
|
+
camera: o.camera,
|
|
139
|
+
scenes: { fx: fxTarget.scene },
|
|
140
|
+
U,
|
|
141
|
+
Q,
|
|
142
|
+
ARENA,
|
|
143
|
+
arena: o.arena ?? { focus: { color: { r: 1, g: 1, b: 1 } as Color }, setFocus() {} },
|
|
144
|
+
ground: o.ground ?? groundField ?? NULL_GROUND,
|
|
145
|
+
particles: null as unknown as Particles,
|
|
146
|
+
trails: (o.trails ?? trailRibbons) as ITrails,
|
|
147
|
+
volumes,
|
|
148
|
+
shocks: (o.shocks ?? shockwaves) as IShocks | undefined,
|
|
149
|
+
// the shared combustion fluid (upstream FluidVolume.get(fx)): built on first use, inside the spell's
|
|
150
|
+
// create(), so its box meshes count as that spell's objects and warmSpell compiles them
|
|
151
|
+
get fluid(): IFluid {
|
|
152
|
+
return o.fluid ?? (fx.renderer ? FluidVolume.get(fx) : NULL_FLUID);
|
|
153
|
+
},
|
|
154
|
+
fluidVolume: undefined as FluidVolume | undefined,
|
|
155
|
+
// the raw renderer the GPU systems draw their passes with (ctx.renderer.raw on ThreeNative)
|
|
156
|
+
renderer: ((o.renderer as { raw?: unknown } | undefined)?.raw ?? o.renderer) as
|
|
157
|
+
| ConstructorParameters<typeof FluidVolume>[0]["renderer"]
|
|
158
|
+
| undefined,
|
|
159
|
+
rig: o.rig ?? (new CameraRig() as IRig),
|
|
160
|
+
lights,
|
|
161
|
+
forces,
|
|
162
|
+
impacts,
|
|
163
|
+
occluders,
|
|
164
|
+
fxTarget,
|
|
165
|
+
dummies: o.dummies ?? [],
|
|
166
|
+
spells: {} as Record<string, ISpell>,
|
|
167
|
+
reduced: false,
|
|
168
|
+
systems: [] as { update(dt: number, camera: Camera): void }[], // extra GPU systems: { update(dt, camera) }, run in the timed sim section every frame
|
|
169
|
+
flashAmount: 0, // upstream: post.params.flash; the game's post reads it
|
|
170
|
+
rand() {
|
|
171
|
+
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
|
|
172
|
+
return seed / 4294967296;
|
|
173
|
+
},
|
|
174
|
+
// reduced-motion aware flash (WCAG 2.3.1: at most 3 flashes per second)
|
|
175
|
+
flash(amount: number) {
|
|
176
|
+
const now = globalThis.performance?.now() ?? Date.now();
|
|
177
|
+
while (flashes.length > 0 && now - (flashes[0] as number) >= 1000) flashes.shift();
|
|
178
|
+
// biome-ignore lint/style/noParameterAssign: upstream Ultra Elements code, copied verbatim
|
|
179
|
+
if (fx.reduced && (flashes.length >= 3 || amount > 0.3)) amount = Math.min(amount, 0.15);
|
|
180
|
+
flashes.push(now);
|
|
181
|
+
fx.flashAmount = Math.min(2, Math.max(fx.flashAmount, amount));
|
|
182
|
+
},
|
|
183
|
+
casts: null as unknown as Casts,
|
|
184
|
+
};
|
|
185
|
+
fxTarget.scene.add(volumes.group);
|
|
186
|
+
// shared meshes go in first, so what each spell's create() adds can be told apart
|
|
187
|
+
// (upstream: trails.lines in the lines scene, trails.fx and shocks.mesh in the fx scene)
|
|
188
|
+
if (trailRibbons) fxTarget.scene.add(trailRibbons.lines, trailRibbons.fx);
|
|
189
|
+
if (shockwaves) fxTarget.scene.add(shockwaves.mesh);
|
|
190
|
+
const points = Array.from({ length: o.pointLights ?? 4 }, (_, i) => {
|
|
191
|
+
const l = new PointLight(0xffffff, 0, 8, 2);
|
|
192
|
+
l.name = `ultra-spell-light-${i}`;
|
|
193
|
+
fxTarget.scene.add(l);
|
|
194
|
+
return l;
|
|
195
|
+
});
|
|
196
|
+
const casts = new Casts(fx);
|
|
197
|
+
fx.casts = casts;
|
|
198
|
+
// one GPU particle pool for every spell (upstream main.js: new Particles(renderer, { ground }))
|
|
199
|
+
const particles = new Particles(o.renderer ?? null, { ground: fx.ground as IParticleGround });
|
|
200
|
+
fx.particles = particles;
|
|
201
|
+
// upstream builds once every spell has registered: here, before the first warm-up or frame.
|
|
202
|
+
// Engine: one world scene, so glow (upstream: lines scene) and debris (opaque scene) join the fx group
|
|
203
|
+
function buildParticles(): void {
|
|
204
|
+
if (particles.built) return;
|
|
205
|
+
particles.build();
|
|
206
|
+
fxTarget.scene.add(particles.glow, particles.media, ...particles.debris);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let simTime = 0;
|
|
210
|
+
let w = 0;
|
|
211
|
+
let h = 0;
|
|
212
|
+
// the CPU half of upstream's renderFrame(dt). dt: real seconds; the sim runs at the rig's time
|
|
213
|
+
// scale (hitstop), upstream's sdt
|
|
214
|
+
function renderFrame(realDt: number): void {
|
|
215
|
+
const dt = realDt * fx.rig.timeScale;
|
|
216
|
+
const s = o.size();
|
|
217
|
+
if (s.width !== w || s.height !== h) {
|
|
218
|
+
w = s.width;
|
|
219
|
+
h = s.height;
|
|
220
|
+
fxTarget.setSize(w, h, Q.fxScale);
|
|
221
|
+
}
|
|
222
|
+
simTime += dt;
|
|
223
|
+
U.uTime.value = simTime % 3600;
|
|
224
|
+
U.uDt.value = dt;
|
|
225
|
+
U.uFrame.value++;
|
|
226
|
+
const cam = o.camera as Camera & { near?: number; far?: number };
|
|
227
|
+
if (cam.near !== undefined && cam.far !== undefined) U.uNearFar.value.set(cam.near, cam.far);
|
|
228
|
+
|
|
229
|
+
// CPU: casts emit, lights/forces/impacts pack into the world texture
|
|
230
|
+
casts.update(dt);
|
|
231
|
+
for (const sp of Object.values(fx.spells))
|
|
232
|
+
(sp.inst as { tick?(dt: number): void } | undefined)?.tick?.(dt);
|
|
233
|
+
packWorld(dt, { maxLights: Q.maxLights, maxShadowed: Q.shadowLights });
|
|
234
|
+
mirrorLights(points);
|
|
235
|
+
|
|
236
|
+
buildParticles();
|
|
237
|
+
particles.update(dt, simTime);
|
|
238
|
+
groundField?.update(dt);
|
|
239
|
+
trailRibbons?.update(dt);
|
|
240
|
+
volumes.update(o.camera);
|
|
241
|
+
shockwaves?.update(dt);
|
|
242
|
+
for (const sys of fx.systems) sys.update(dt, o.camera);
|
|
243
|
+
fxTarget.render();
|
|
244
|
+
fx.flashAmount = Math.max(0, fx.flashAmount - dt * 6);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// spell modules: create() once each (upstream: the SPELLS loop in main.js). Remember the scene
|
|
248
|
+
// objects it added: its programs are compiled and warmed on their own (warmSpell).
|
|
249
|
+
const sceneObjects = () => {
|
|
250
|
+
const set = new Set<Object3D>();
|
|
251
|
+
fxTarget.scene.traverse((o) => set.add(o));
|
|
252
|
+
return set;
|
|
253
|
+
};
|
|
254
|
+
const spellObjects = new Map<ISpell, Object3D[]>();
|
|
255
|
+
const warming = new Map<ISpell, Promise<void>>();
|
|
256
|
+
function addSpell<S extends ISpell & { create(fx: Fx): NonNullable<ISpell["inst"]> }>(s: S): S {
|
|
257
|
+
(s as { defaults?: object }).defaults = { ...s.params };
|
|
258
|
+
s.params = { ...s.params };
|
|
259
|
+
if (particles.built)
|
|
260
|
+
throw new Error(`addSpell(${s.id}): add every spell before the first frame`);
|
|
261
|
+
for (const [k, def] of Object.entries((s as { kinds?: object }).kinds ?? {}))
|
|
262
|
+
particles.addKind(`${s.id}.${k}`, def);
|
|
263
|
+
const before = sceneObjects();
|
|
264
|
+
s.inst = s.create(fx);
|
|
265
|
+
spellObjects.set(
|
|
266
|
+
s,
|
|
267
|
+
[...sceneObjects()].filter((o) => !before.has(o)),
|
|
268
|
+
);
|
|
269
|
+
fx.spells[s.id] = s;
|
|
270
|
+
return s;
|
|
271
|
+
}
|
|
272
|
+
// per-spell warm-up (upstream warmSpell): compile its pipelines with its objects forced on, so the
|
|
273
|
+
// first cast does not compile mid-frame. Engine: the compile reads the scene when it is called, so
|
|
274
|
+
// the objects are on for that call only and no presented frame draws them with unset uniforms.
|
|
275
|
+
// Upstream's second step, one draw into a 4x4 target, has no engine route: the Wasm renderer's
|
|
276
|
+
// setRenderTarget refuses any target (three-native browser-renderer.ts setRenderTarget).
|
|
277
|
+
function warmSpell(s: ISpell): Promise<void> {
|
|
278
|
+
let job = warming.get(s);
|
|
279
|
+
if (job === undefined) {
|
|
280
|
+
buildParticles();
|
|
281
|
+
particles.warm(true); // the particle draws compile with the first spell's warm-up
|
|
282
|
+
const objects = spellObjects.get(s) ?? [];
|
|
283
|
+
const saved = objects.map((obj) => obj.visible);
|
|
284
|
+
for (const obj of objects) obj.visible = true;
|
|
285
|
+
let root: Object3D = fxTarget.scene;
|
|
286
|
+
while (root.parent !== null) root = root.parent; // upstream sceneOf: the scene's lights pick the variant
|
|
287
|
+
try {
|
|
288
|
+
job = (
|
|
289
|
+
o.renderer?.compileAsync(fxTarget.scene, o.camera, root as Scene) ?? Promise.resolve()
|
|
290
|
+
).then(() => undefined);
|
|
291
|
+
} finally {
|
|
292
|
+
objects.forEach((obj, i) => {
|
|
293
|
+
obj.visible = saved[i] as boolean;
|
|
294
|
+
});
|
|
295
|
+
particles.warm(false);
|
|
296
|
+
}
|
|
297
|
+
warming.set(s, job);
|
|
298
|
+
}
|
|
299
|
+
return job;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return { fx, casts, renderFrame, addSpell, warmSpell, pointLights: points };
|
|
303
|
+
}
|
|
304
|
+
export type Fx = ReturnType<typeof createFx>["fx"];
|
|
305
|
+
export type { ICast };
|