@threenative/core 0.3.10 → 0.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/capabilities.json +1096 -26
- package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
- package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-CWuFRT7G.d.ts} +1 -1
- package/dist/chunk-2Z44XSI2.js +35 -0
- package/dist/chunk-5O73CDMR.js +1849 -0
- package/dist/chunk-6KLNISRJ.js +153 -0
- package/dist/chunk-7UUT3TY5.js +133 -0
- package/dist/chunk-CFGTPFUH.js +231 -0
- package/dist/chunk-DBELTTTM.js +615 -0
- package/dist/chunk-GMHATRPW.js +7701 -0
- package/dist/chunk-GXY7B3JG.js +46 -0
- package/dist/chunk-KIRUKTBQ.js +187 -0
- package/dist/chunk-N5LRP4BR.js +220 -0
- package/dist/chunk-OKXUCASC.js +2238 -0
- package/dist/chunk-QWJEMUUT.js +94 -0
- package/dist/chunk-VAZM36TV.js +497 -0
- package/dist/{game-DzMcitDO.d.ts → game-D7pAzZUY.d.ts} +15 -3
- package/dist/hot.d.ts +3 -3
- package/dist/hot.js +2 -46
- package/dist/index.d.ts +329 -302
- package/dist/index.js +10565 -22533
- package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-B6BMqS1r.d.ts} +33 -3
- package/dist/playtest.d.ts +3 -3
- package/dist/playtest.js +24 -75
- package/dist/react-css.js +1 -215
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-rZm0hVqq.d.ts → renderer-DvFVoAgj.d.ts} +13 -4
- 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 +5 -384
- package/dist/worker.js +4 -41
- package/dist/world.d.ts +725 -259
- package/dist/world.js +16714 -18284
- package/mcp/animation-server.mjs +1 -1
- package/mcp/animation.mjs +2 -3
- package/mcp/engine-server.mjs +142 -47
- package/mcp/servers.mjs +9 -2
- package/mcp/terrain.mjs +8 -0
- package/package.json +14 -6
- package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
- package/patches/three@0.187.0.patch +2004 -407
- package/src/assets.ts +9 -2
- package/src/card-lod.ts +152 -0
- package/src/compressed-texture-preparation.ts +1008 -0
- package/src/frame-budget.ts +31 -20
- package/src/game-dev-tools.ts +93 -0
- package/src/game-frame-loop.ts +995 -0
- package/src/game-renderer-metrics.ts +77 -0
- package/src/game-startup.ts +1001 -0
- package/src/game-validate.ts +70 -0
- package/src/game.ts +253 -1935
- package/src/index.ts +27 -2
- package/src/launch-diagnostics.ts +22 -12
- package/src/look/ao-admission.ts +24 -0
- package/src/look/filmic-tone-mapping.ts +15 -13
- package/src/look/look-diagnostics.ts +3 -3
- package/src/look/quality.ts +10 -5
- package/src/look/world-environment.ts +76 -9
- package/src/loop.ts +29 -0
- package/src/model-lod.ts +42 -3
- package/src/particle-kinds.ts +515 -0
- package/src/picking.ts +17 -3
- package/src/playtest.ts +27 -1
- package/src/projection-apply.ts +4 -1
- package/src/projection-plan.ts +9 -6
- package/src/render/mesh-pool.ts +1 -1
- 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.ts +213 -992
- package/src/render/world-impostor-surface.ts +293 -34
- package/src/render-camera-cull.ts +32 -1
- package/src/render-pass-budget.ts +34 -7
- package/src/renderProjection.ts +22 -0
- package/src/renderer.ts +92 -37
- package/src/snow-field.ts +1 -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-readiness.ts +128 -34
- package/src/version.ts +1 -1
- package/src/warmup.ts +56 -8
- package/src/water-surface.ts +9 -3
- package/src/world-bundle-safety.ts +401 -0
- package/src/world-cell-batches.ts +1216 -0
- package/src/world-cell-chunks.ts +1132 -0
- package/src/world-cell-data.ts +995 -0
- package/src/world-cell-impostors.ts +1449 -0
- package/src/world-cell-lod-bias.ts +151 -0
- package/src/world-cell-models.ts +1074 -0
- package/src/world-cell-options.ts +189 -0
- package/src/world-cell-placements.ts +694 -0
- package/src/world-cell-prewarm.ts +557 -0
- package/src/world-cell-readiness.ts +357 -0
- package/src/world-cell-render-bundles.ts +459 -0
- package/src/world-cell-render-gpu-dress.ts +714 -0
- package/src/world-cell-render-main-cull.ts +484 -0
- package/src/world-cell-render-shadow-claims.ts +198 -0
- package/src/world-cell-render-util.ts +246 -0
- package/src/world-cell-render.ts +1493 -0
- package/src/world-cell-residency.ts +745 -0
- package/src/world-cells.ts +1318 -10418
- package/src/world-chunk-shadow.ts +308 -0
- package/src/world-collar.ts +315 -0
- package/src/world-gpu-flags.ts +163 -0
- package/src/world-gpu-readback.ts +881 -0
- package/src/world-gpu-reference.ts +510 -0
- package/src/world-gpu-scene.ts +214 -2264
- package/src/world-gpu-shadow.ts +190 -0
- package/src/world-gpu-types.ts +381 -0
- package/src/world-gpu-validation.ts +518 -0
- package/src/world-heightfield.ts +802 -0
- package/src/world-load-limiter.ts +106 -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-terrain-splat.ts +365 -149
- package/src/world-tile-budget.ts +256 -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 +170 -1745
- package/src/world-water-bodies.ts +359 -0
- package/src/world.ts +35 -741
|
@@ -0,0 +1,816 @@
|
|
|
1
|
+
import { mrt, texture, uniform } from "three/tsl";
|
|
2
|
+
// Ported from Ultra Elements src/vfx/FluidVolume.js @698b720 (MIT, Copyright (c) 2026 Humpalumps). JavaScript copied verbatim; GLSL transliterated to TSL.
|
|
3
|
+
//
|
|
4
|
+
// Engine (ThreeNative's C++ engine over WebGPU) differences, each forced:
|
|
5
|
+
// - the shader revisions upstream patches over FluidVolume.glsl.js at load (REACT's turbulence, soot
|
|
6
|
+
// cooling and erosion; RENDER_BODY) are written in place in FluidVolume.tsl.ts, marked "patch:".
|
|
7
|
+
// - no render-target viewport: every pass draws the whole atlas. Texels past the used tiles get a
|
|
8
|
+
// value no lookup reads (gridTexel and gridSample stay inside the used tiles), so the result is
|
|
9
|
+
// upstream's; the cost is the unused corner of the atlas.
|
|
10
|
+
// - upstream's rawPass/FullscreenQuad is a QuadMesh + NodeMaterial; renderer.compile is the warm step
|
|
11
|
+
// the constructor already runs (the engine builds each pipeline at its first draw).
|
|
12
|
+
// - the render material is a MeshBasicNodeMaterial; its uniforms live on the domain (`d.uniforms`).
|
|
13
|
+
import {
|
|
14
|
+
BackSide,
|
|
15
|
+
BoxGeometry,
|
|
16
|
+
type Camera,
|
|
17
|
+
Color,
|
|
18
|
+
type Group,
|
|
19
|
+
HalfFloatType,
|
|
20
|
+
LinearFilter,
|
|
21
|
+
Mesh,
|
|
22
|
+
MeshBasicNodeMaterial,
|
|
23
|
+
NoBlending,
|
|
24
|
+
NodeMaterial,
|
|
25
|
+
OrthographicCamera,
|
|
26
|
+
type PerspectiveCamera,
|
|
27
|
+
QuadMesh,
|
|
28
|
+
RGBAFormat,
|
|
29
|
+
RenderTarget,
|
|
30
|
+
Scene,
|
|
31
|
+
type Texture,
|
|
32
|
+
Vector2,
|
|
33
|
+
Vector3,
|
|
34
|
+
Vector4,
|
|
35
|
+
type WebGPURenderer,
|
|
36
|
+
} from "three/webgpu";
|
|
37
|
+
import type { Node } from "three/webgpu";
|
|
38
|
+
import { Q } from "../engine/quality.js";
|
|
39
|
+
import * as gl from "./FluidVolume.tsl.js";
|
|
40
|
+
import { FX_BLENDING, U } from "./common.js";
|
|
41
|
+
|
|
42
|
+
// GPU 3D combustion fluid (shared system; Fire is the first user).
|
|
43
|
+
//
|
|
44
|
+
// Each domain is an axis-aligned box of cubic voxels simulated on the GPU and raymarched in the
|
|
45
|
+
// half-res FX pass. 3D grids live in 2D atlases (slices tiled), so every sim pass is one
|
|
46
|
+
// fullscreen draw into a viewport covering the used tiles; trilinear lookups are two hardware
|
|
47
|
+
// bilinear taps in neighbouring slices. HalfFloat targets, ping-ponged.
|
|
48
|
+
//
|
|
49
|
+
// Fields: velocity (m/s), scalars (temperature th, fuel, soot, expansion), vorticity, pressure +
|
|
50
|
+
// divergence, and a half-res light volume. One step per frame (dt <= 1/30):
|
|
51
|
+
// 1 advect semi-Lagrangian, RK2 backtrace, compensated for the domain's own motion
|
|
52
|
+
// 2 correct MacCormack, clamped to the departure cell's extrema (no overshoot)
|
|
53
|
+
// 3 curl vorticity
|
|
54
|
+
// 4 react sources -> combustion (fuel + heat -> heat, soot, gas expansion) -> cooling ->
|
|
55
|
+
// buoyancy (heat up, soot down) -> vorticity confinement -> forceAccel() from
|
|
56
|
+
// the shared force field (wind, vortex, shock... spells push fire) -> damping
|
|
57
|
+
// 5 diverge b = h^2 (div u - expansion), warm-started pressure
|
|
58
|
+
// 6 jacobi Q-dependent iterations (12 / 14 / 16 / 20; Low profile 6), warm-started
|
|
59
|
+
// 7 project u -= grad p; the floor (world y < 0) is solid, the other faces are open air
|
|
60
|
+
// 8 light half-res: sun optical depth (march to the box exit), sky optical depth (march
|
|
61
|
+
// up), |vorticity|, occupancy, and the fire's own light (blurred blackbody
|
|
62
|
+
// emission, the "low mip" that lights the smoke orange from inside)
|
|
63
|
+
// Render: march the box from the camera to min(exit, scene depth), trapezoid slabs, skipping
|
|
64
|
+
// empty space through the light volume's occupancy. Emission is blackbody(T) x T^4 with an
|
|
65
|
+
// optical-thickness shoulder; soot scatters sun (HG + multiple-scattering octaves), sky and
|
|
66
|
+
// fire light. Sub-grid detail: the lookup is displaced by band-limited curl noise drifting with the
|
|
67
|
+
// gas, scaled by the resolved vorticity (wavelet-turbulence-like upres). The samples sit on a
|
|
68
|
+
// lattice from the ray's entry at one spacing per domain, refined in place at surfaces (see RENDER_BODY).
|
|
69
|
+
//
|
|
70
|
+
// API
|
|
71
|
+
// const fluid = FluidVolume.get(fx) create once (adds itself to fx.systems; meshes go in
|
|
72
|
+
// fx.scenes.fx so the warm-up compiles them)
|
|
73
|
+
// const d = fluid.acquire({ size: [x, y, z], res, position }) null when the pool is full
|
|
74
|
+
// size metres (rounded to whole voxels); res = voxels along the longest axis (default
|
|
75
|
+
// Q.fluidRes: 32 / 48 / 64 / 96 by tier; elongated boxes may ask for more if they
|
|
76
|
+
// fit the atlas). Fields start empty.
|
|
77
|
+
// d.position world centre (Vector3); move it to follow a source, advection compensates
|
|
78
|
+
// d.emit(src) this frame only (call every frame for a continuous source; max 8/frame):
|
|
79
|
+
// { pos, radius, fuel, temp, soot, vel, radial, noise, noiseScale, seed,
|
|
80
|
+
// coupling, burst }
|
|
81
|
+
// burst: values are 'at least' this (one-shot fill); else per second.
|
|
82
|
+
// temp is th (1 = the flame temperature). coupling: 1/s pull of the
|
|
83
|
+
// velocity towards vel + radial * outward (large = set).
|
|
84
|
+
// d.heat CPU estimate of the domain's heat (th m^3), for driving lights
|
|
85
|
+
// d.clear() empty the fields; d.release() back to the pool
|
|
86
|
+
// sim fields ignition, burnRate, heat release (heatPerFuel), sootYield, expansion,
|
|
87
|
+
// cooling, radiative, sootDecay, fuelDecay, buoyancy, sootWeight,
|
|
88
|
+
// vorticity, forceCoupling, damping; opt-in, 0 = off: turbulence (m/s2 on hot
|
|
89
|
+
// gas), turbulenceFreq (tiles/m), sootCool (soot radiates heat: dark lanes),
|
|
90
|
+
// sootErode (units/s, soot thins from its edges, keeping its billows)
|
|
91
|
+
// look fields emission, temperature (K at th = 1), tint (Color), sootColor (albedo),
|
|
92
|
+
// sootDensity, flameDensity, fuelDensity, absorb (0..1 black flame: hot gas
|
|
93
|
+
// absorbs instead of glowing), g (HG), sun, sky, fireLight, upres
|
|
94
|
+
// (voxels), upresScale (feature size, voxels), edgeFade (voxels), visible;
|
|
95
|
+
// opt-in cell detail: detailFreq (tiles/m, 0 = off; band-limited to the march
|
|
96
|
+
// step), detailErode (soot units), detailHeat (0..1), detailCenter (Vector3);
|
|
97
|
+
// nearFade (m: density ramps in over this distance from the camera);
|
|
98
|
+
// chroma (0 = grey; > 0 the gas's colour deepens with depth, thin gas paler)
|
|
99
|
+
// d.obstacle Vector4: a solid sphere in the sim (world centre xyz, radius m; w = 0 none):
|
|
100
|
+
// gas flows round it and none is left inside (a fireball on an Aegis dome)
|
|
101
|
+
// cost fields emptyBelow (extinction 1/m below which gas is skipped as empty),
|
|
102
|
+
// stepScale (march spacing x; wide thin layers can afford coarser)
|
|
103
|
+
// No allocation after construction; nothing recompiles (tier settings are uniforms or pass counts).
|
|
104
|
+
|
|
105
|
+
const TIER_RES: Record<string, number> = { low: 32, medium: 48, high: 64, ultra: 96 };
|
|
106
|
+
const TIER_SLOTS: Record<string, number> = { low: 1, medium: 2, high: 2, ultra: 2 };
|
|
107
|
+
const TIER_ITERS: Record<string, number> = { low: 12, medium: 14, high: 16, ultra: 20 };
|
|
108
|
+
const TIER_STEP: Record<string, number> = { low: 1.3, medium: 1.15, high: 1.0, ultra: 0.8 }; // render step, voxels
|
|
109
|
+
const TIER_MAXSTEPS: Record<string, number> = { low: 56, medium: 80, high: 112, ultra: 160 };
|
|
110
|
+
|
|
111
|
+
const DEFAULTS = {
|
|
112
|
+
ignition: 0.3,
|
|
113
|
+
burnRate: 3,
|
|
114
|
+
heatPerFuel: 0.8,
|
|
115
|
+
sootYield: 0.5,
|
|
116
|
+
expansion: 1,
|
|
117
|
+
cooling: 0.5,
|
|
118
|
+
radiative: 0.25,
|
|
119
|
+
sootDecay: 0.12,
|
|
120
|
+
fuelDecay: 0.3,
|
|
121
|
+
buoyancy: 5,
|
|
122
|
+
sootWeight: 0.4,
|
|
123
|
+
vorticity: 6,
|
|
124
|
+
forceCoupling: 1,
|
|
125
|
+
damping: 0.15,
|
|
126
|
+
emission: 4,
|
|
127
|
+
temperature: 1900,
|
|
128
|
+
sootDensity: 3,
|
|
129
|
+
flameDensity: 0.35,
|
|
130
|
+
fuelDensity: 0,
|
|
131
|
+
absorb: 0,
|
|
132
|
+
g: 0.45,
|
|
133
|
+
sun: 1,
|
|
134
|
+
sky: 1,
|
|
135
|
+
fireLight: 0.5,
|
|
136
|
+
upres: 1.4,
|
|
137
|
+
upresScale: 4,
|
|
138
|
+
edgeFade: 5,
|
|
139
|
+
visible: true,
|
|
140
|
+
turbulence: 0,
|
|
141
|
+
turbulenceFreq: 0.5,
|
|
142
|
+
sootCool: 0,
|
|
143
|
+
sootErode: 0,
|
|
144
|
+
detailFreq: 0,
|
|
145
|
+
detailErode: 0,
|
|
146
|
+
detailHeat: 0,
|
|
147
|
+
nearFade: 0.4,
|
|
148
|
+
emptyBelow: 0,
|
|
149
|
+
stepScale: 1,
|
|
150
|
+
chroma: 0,
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// the tiles a grid of n voxels takes in a W x H atlas, or null if it does not fit
|
|
154
|
+
function tileLayout(nx: number, ny: number, nz: number, W: number, H: number): number[] | null {
|
|
155
|
+
const tx = Math.floor(W / nx);
|
|
156
|
+
const ty = Math.floor(H / ny);
|
|
157
|
+
if (tx < 1 || ty < 1) return null;
|
|
158
|
+
const tilesX = Math.min(tx, nz);
|
|
159
|
+
const tilesY = Math.ceil(nz / tilesX);
|
|
160
|
+
return tilesY <= ty ? [tilesX, tilesY] : null;
|
|
161
|
+
}
|
|
162
|
+
const atlasSize = (N: number) => {
|
|
163
|
+
const t = Math.ceil(Math.sqrt(N));
|
|
164
|
+
return [t * N, Math.ceil(N / t) * N] as const;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
function target(w: number, h: number, count = 1): RenderTarget {
|
|
168
|
+
// ENGINE-GAP: a RenderTarget is RGBAFormat only (upstream: RGFormat pressure) and has no generateMipmaps
|
|
169
|
+
// option (a target has no mip chain)
|
|
170
|
+
const rt = new RenderTarget(w, h, {
|
|
171
|
+
count,
|
|
172
|
+
type: HalfFloatType,
|
|
173
|
+
format: RGBAFormat,
|
|
174
|
+
minFilter: LinearFilter,
|
|
175
|
+
magFilter: LinearFilter,
|
|
176
|
+
depthBuffer: false,
|
|
177
|
+
stencilBuffer: false,
|
|
178
|
+
});
|
|
179
|
+
// MRT: an mrtNode output writes the texture of its name (upstream: layout(location = 0/1) out oV, oS)
|
|
180
|
+
const [oV, oS] = rt.textures;
|
|
181
|
+
if (count === 2 && oV && oS) [oV.name, oS.name] = ["oV", "oS"];
|
|
182
|
+
return rt;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// one emitter record (fire.ts's SRC)
|
|
186
|
+
export interface IFluidSource {
|
|
187
|
+
pos: Vector3;
|
|
188
|
+
radius?: number;
|
|
189
|
+
fuel?: number;
|
|
190
|
+
temp?: number;
|
|
191
|
+
soot?: number;
|
|
192
|
+
vel?: Vector3;
|
|
193
|
+
radial?: number;
|
|
194
|
+
noise?: number;
|
|
195
|
+
noiseScale?: number;
|
|
196
|
+
seed?: number;
|
|
197
|
+
coupling?: number;
|
|
198
|
+
burst?: boolean;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
type U4 = Node<"vec4"> & { value: Vector4 };
|
|
202
|
+
type U3 = Node<"vec3"> & { value: Vector3 };
|
|
203
|
+
type U2 = Node<"vec2"> & { value: Vector2 };
|
|
204
|
+
type U1 = Node<"float"> & { value: number };
|
|
205
|
+
type Tex = gl.TexNode;
|
|
206
|
+
|
|
207
|
+
interface IRenderUniforms {
|
|
208
|
+
uS: Tex;
|
|
209
|
+
uL0: Tex;
|
|
210
|
+
uDim: U3;
|
|
211
|
+
uTiles: U2;
|
|
212
|
+
uInv: U2;
|
|
213
|
+
uCDim: U3;
|
|
214
|
+
uCTiles: U2;
|
|
215
|
+
uCInv: U2;
|
|
216
|
+
uOrigin: U3;
|
|
217
|
+
uH: U1;
|
|
218
|
+
uMarch: U4;
|
|
219
|
+
uMarch2: U4;
|
|
220
|
+
uLit: U4;
|
|
221
|
+
uSootColor: U3;
|
|
222
|
+
uDetail: U4;
|
|
223
|
+
uDetailC: U3;
|
|
224
|
+
uLook0: U4;
|
|
225
|
+
uLook1: U4;
|
|
226
|
+
uTint: U3;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
class FluidDomain {
|
|
230
|
+
// the fields fire.ts sets by name (Object.assign(f, {...}))
|
|
231
|
+
[field: string]: unknown;
|
|
232
|
+
sys: FluidVolume;
|
|
233
|
+
W: number;
|
|
234
|
+
H: number;
|
|
235
|
+
cW: number;
|
|
236
|
+
cH: number;
|
|
237
|
+
state: RenderTarget[];
|
|
238
|
+
tmp: RenderTarget;
|
|
239
|
+
curl: RenderTarget;
|
|
240
|
+
press: RenderTarget[];
|
|
241
|
+
light: RenderTarget;
|
|
242
|
+
all: RenderTarget[];
|
|
243
|
+
dim = new Vector3(8, 8, 8);
|
|
244
|
+
tiles = new Vector2(1, 1);
|
|
245
|
+
cdim = new Vector3(4, 4, 4);
|
|
246
|
+
ctiles = new Vector2(1, 1);
|
|
247
|
+
h = 0.1;
|
|
248
|
+
size = new Vector3();
|
|
249
|
+
position = new Vector3();
|
|
250
|
+
origin = new Vector3();
|
|
251
|
+
lastOrigin = new Vector3();
|
|
252
|
+
tint = new Color(1, 1, 1);
|
|
253
|
+
sootColor = new Color(0.13, 0.12, 0.11);
|
|
254
|
+
detailCenter = new Vector3();
|
|
255
|
+
obstacle = new Vector4();
|
|
256
|
+
srcP = Array.from({ length: gl.MAX_SRC }, () => new Vector4());
|
|
257
|
+
srcA = Array.from({ length: gl.MAX_SRC }, () => new Vector4());
|
|
258
|
+
srcV = Array.from({ length: gl.MAX_SRC }, () => new Vector4());
|
|
259
|
+
srcB = Array.from({ length: gl.MAX_SRC }, () => new Vector4());
|
|
260
|
+
nSrc = 0;
|
|
261
|
+
active = false;
|
|
262
|
+
cur = 0;
|
|
263
|
+
pc = 0;
|
|
264
|
+
fresh = true;
|
|
265
|
+
heat = 0;
|
|
266
|
+
fuelPool = 0;
|
|
267
|
+
mesh!: Mesh;
|
|
268
|
+
material!: MeshBasicNodeMaterial;
|
|
269
|
+
uniforms!: IRenderUniforms;
|
|
270
|
+
declare ignition: number;
|
|
271
|
+
declare burnRate: number;
|
|
272
|
+
declare heatPerFuel: number;
|
|
273
|
+
declare sootYield: number;
|
|
274
|
+
declare expansion: number;
|
|
275
|
+
declare cooling: number;
|
|
276
|
+
declare radiative: number;
|
|
277
|
+
declare sootDecay: number;
|
|
278
|
+
declare fuelDecay: number;
|
|
279
|
+
declare buoyancy: number;
|
|
280
|
+
declare sootWeight: number;
|
|
281
|
+
declare vorticity: number;
|
|
282
|
+
declare forceCoupling: number;
|
|
283
|
+
declare damping: number;
|
|
284
|
+
declare emission: number;
|
|
285
|
+
declare temperature: number;
|
|
286
|
+
declare sootDensity: number;
|
|
287
|
+
declare flameDensity: number;
|
|
288
|
+
declare fuelDensity: number;
|
|
289
|
+
declare absorb: number;
|
|
290
|
+
declare g: number;
|
|
291
|
+
declare sun: number;
|
|
292
|
+
declare sky: number;
|
|
293
|
+
declare fireLight: number;
|
|
294
|
+
declare upres: number;
|
|
295
|
+
declare upresScale: number;
|
|
296
|
+
declare edgeFade: number;
|
|
297
|
+
declare visible: boolean;
|
|
298
|
+
declare turbulence: number;
|
|
299
|
+
declare turbulenceFreq: number;
|
|
300
|
+
declare sootCool: number;
|
|
301
|
+
declare sootErode: number;
|
|
302
|
+
declare detailFreq: number;
|
|
303
|
+
declare detailErode: number;
|
|
304
|
+
declare detailHeat: number;
|
|
305
|
+
declare nearFade: number;
|
|
306
|
+
declare emptyBelow: number;
|
|
307
|
+
declare stepScale: number;
|
|
308
|
+
declare chroma: number;
|
|
309
|
+
|
|
310
|
+
constructor(sys: FluidVolume, W: number, H: number, cW: number, cH: number) {
|
|
311
|
+
this.sys = sys;
|
|
312
|
+
this.W = W;
|
|
313
|
+
this.H = H;
|
|
314
|
+
this.cW = cW;
|
|
315
|
+
this.cH = cH;
|
|
316
|
+
this.state = [target(W, H, 2), target(W, H, 2)]; // (velocity, scalars)
|
|
317
|
+
this.tmp = target(W, H, 2);
|
|
318
|
+
this.curl = target(W, H);
|
|
319
|
+
this.press = [target(W, H), target(W, H)];
|
|
320
|
+
this.light = target(cW, cH);
|
|
321
|
+
this.all = [...this.state, this.tmp, this.curl, ...this.press];
|
|
322
|
+
Object.assign(this, DEFAULTS);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
setGrid(
|
|
326
|
+
nx: number,
|
|
327
|
+
ny: number,
|
|
328
|
+
nz: number,
|
|
329
|
+
tiles: number[],
|
|
330
|
+
cn: number[],
|
|
331
|
+
ctiles: number[],
|
|
332
|
+
h: number,
|
|
333
|
+
): void {
|
|
334
|
+
this.dim.set(nx, ny, nz);
|
|
335
|
+
this.tiles.set(tiles[0] as number, tiles[1] as number);
|
|
336
|
+
this.cdim.set(cn[0] as number, cn[1] as number, cn[2] as number);
|
|
337
|
+
this.ctiles.set(ctiles[0] as number, ctiles[1] as number);
|
|
338
|
+
this.h = h;
|
|
339
|
+
this.size.set(nx * h, ny * h, nz * h);
|
|
340
|
+
// (engine: no render-target viewport, see the header; upstream set t.viewport to the used tiles)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: upstream Ultra Elements code, copied verbatim
|
|
344
|
+
emit(s: IFluidSource): void {
|
|
345
|
+
if (!this.active || this.nSrc >= gl.MAX_SRC) return;
|
|
346
|
+
const i = this.nSrc++;
|
|
347
|
+
const v = s.vel;
|
|
348
|
+
const burst = s.burst ? 1 : 0;
|
|
349
|
+
(this.srcP[i] as Vector4).set(s.pos.x, s.pos.y, s.pos.z, s.radius ?? 0.5);
|
|
350
|
+
(this.srcA[i] as Vector4).set(s.fuel ?? 0, s.temp ?? 0, s.soot ?? 0, s.noise ?? 0);
|
|
351
|
+
(this.srcV[i] as Vector4).set(v ? v.x : 0, v ? v.y : 0, v ? v.z : 0, s.radial ?? 0);
|
|
352
|
+
(this.srcB[i] as Vector4).set(burst, s.noiseScale ?? 1, s.seed ?? 0, s.coupling ?? 0);
|
|
353
|
+
// CPU mirror of the heat budget (drives lights): effective volume of the soft sphere
|
|
354
|
+
const r = s.radius ?? 0.5;
|
|
355
|
+
const vol = 1.6 * r * r * r * (1 - 0.5 * (s.noise ?? 0));
|
|
356
|
+
const k = burst ? 1 : this.sys.dt;
|
|
357
|
+
this.fuelPool += (s.fuel ?? 0) * vol * k;
|
|
358
|
+
this.heat += (s.temp ?? 0) * vol * k * 0.5;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
clear(): void {
|
|
362
|
+
this.fresh = true;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
release(): void {
|
|
366
|
+
this.active = false;
|
|
367
|
+
this.mesh.visible = false;
|
|
368
|
+
this.nSrc = 0;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const _c = new Color();
|
|
373
|
+
const quadCam = new OrthographicCamera();
|
|
374
|
+
const EMPTY = new Scene();
|
|
375
|
+
const _v = new Vector3();
|
|
376
|
+
|
|
377
|
+
// one fullscreen pass (upstream FullscreenQuad over rawPass). A two-output pass writes (oV, oS)
|
|
378
|
+
// through the material's mrtNode into a two-texture target.
|
|
379
|
+
class FullscreenPass<O extends object> {
|
|
380
|
+
readonly quad: QuadMesh;
|
|
381
|
+
constructor(
|
|
382
|
+
readonly uniforms: O,
|
|
383
|
+
outputs: Node<"vec4">[],
|
|
384
|
+
) {
|
|
385
|
+
const m = new NodeMaterial();
|
|
386
|
+
const [oV, oS] = outputs as [Node<"vec4">, Node<"vec4"> | undefined];
|
|
387
|
+
m.fragmentNode = oV;
|
|
388
|
+
if (oS) {
|
|
389
|
+
m.mrtNode = mrt({ oV, oS });
|
|
390
|
+
// ENGINE-GAP: the engine finds a material's textures from its node slots, not its mrtNode
|
|
391
|
+
// (MaterialNodes::pointers() in position_node.h), so a texture only oS reads has no binding
|
|
392
|
+
// ("TN_NATIVE_BINDING_UNSUPPORTED: t_nodeMapN has no texture this draw"). The fragmentNode names
|
|
393
|
+
// both outputs; each attachment still draws its own mrtNode output. Back to `oV` once fixed.
|
|
394
|
+
m.fragmentNode = oV.add(oS.mul(0.0));
|
|
395
|
+
}
|
|
396
|
+
m.depthTest = false;
|
|
397
|
+
m.blending = NoBlending; // every channel is data (alpha too)
|
|
398
|
+
m.depthWrite = false;
|
|
399
|
+
this.quad = new QuadMesh(m);
|
|
400
|
+
}
|
|
401
|
+
render(renderer: WebGPURenderer, out: RenderTarget): void {
|
|
402
|
+
renderer.setRenderTarget(out);
|
|
403
|
+
this.quad.render(renderer);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export interface IFluidFx {
|
|
408
|
+
renderer?: WebGPURenderer;
|
|
409
|
+
scenes: { fx: Group };
|
|
410
|
+
systems: { update(dt: number, camera: Camera): void }[];
|
|
411
|
+
fluidVolume?: FluidVolume;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export class FluidVolume {
|
|
415
|
+
// engine: fx.fluid is main.ts's service seam (a getter), so the instance lives in fx.fluidVolume
|
|
416
|
+
static get(fx: IFluidFx): FluidVolume {
|
|
417
|
+
if (!fx.fluidVolume) {
|
|
418
|
+
fx.fluidVolume = new FluidVolume(fx);
|
|
419
|
+
fx.systems.push(fx.fluidVolume);
|
|
420
|
+
}
|
|
421
|
+
return fx.fluidVolume;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
fx: IFluidFx;
|
|
425
|
+
renderer: WebGPURenderer;
|
|
426
|
+
res: number;
|
|
427
|
+
iterations: number;
|
|
428
|
+
stepVox: number;
|
|
429
|
+
maxSteps: number;
|
|
430
|
+
dt: number;
|
|
431
|
+
pending = 0;
|
|
432
|
+
lastFrame: number | undefined = -1;
|
|
433
|
+
su;
|
|
434
|
+
look;
|
|
435
|
+
passes;
|
|
436
|
+
domains: FluidDomain[];
|
|
437
|
+
|
|
438
|
+
constructor(fx: IFluidFx) {
|
|
439
|
+
this.fx = fx;
|
|
440
|
+
if (!fx.renderer)
|
|
441
|
+
throw new Error("FluidVolume: createFx needs the renderer (options.renderer)");
|
|
442
|
+
this.renderer = fx.renderer;
|
|
443
|
+
const tier = Q.tier;
|
|
444
|
+
const q = Q as typeof Q & {
|
|
445
|
+
fluidRes?: number;
|
|
446
|
+
fluidIterations?: number;
|
|
447
|
+
fluidDomains?: number;
|
|
448
|
+
};
|
|
449
|
+
this.res = q.fluidRes ?? TIER_RES[tier] ?? 64;
|
|
450
|
+
this.iterations = q.fluidIterations ?? TIER_ITERS[tier] ?? 20;
|
|
451
|
+
this.stepVox = TIER_STEP[tier] ?? 0.85;
|
|
452
|
+
this.maxSteps = TIER_MAXSTEPS[tier] ?? 112;
|
|
453
|
+
this.dt = 1 / 60;
|
|
454
|
+
const [W, H] = atlasSize(this.res);
|
|
455
|
+
const [cW, cH] = atlasSize(Math.ceil(this.res / 2));
|
|
456
|
+
|
|
457
|
+
// sim uniforms shared by reference across every pass; set per domain before its passes
|
|
458
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: upstream Ultra Elements code, copied verbatim
|
|
459
|
+
const su = (this.su = {
|
|
460
|
+
uDim: uniform(new Vector3()),
|
|
461
|
+
uTiles: uniform(new Vector2()),
|
|
462
|
+
uInv: uniform(new Vector2(1 / W, 1 / H)),
|
|
463
|
+
uOrigin: uniform(new Vector3()),
|
|
464
|
+
uH: uniform(0.1),
|
|
465
|
+
uSimDt: uniform(1 / 60),
|
|
466
|
+
uShift: uniform(new Vector3()),
|
|
467
|
+
uObst: uniform(new Vector4()),
|
|
468
|
+
});
|
|
469
|
+
// a texture uniform: a texture() node whose .value the passes swap (a placeholder of the same type)
|
|
470
|
+
const blank = target(1, 1).texture;
|
|
471
|
+
const tex = () => texture(blank);
|
|
472
|
+
const v4s = () => Array.from({ length: gl.MAX_SRC }, () => uniform(new Vector4()));
|
|
473
|
+
this.look = {
|
|
474
|
+
uLook0: uniform(new Vector4()),
|
|
475
|
+
uLook1: uniform(new Vector4()),
|
|
476
|
+
uTint: uniform(new Vector3(1, 1, 1)),
|
|
477
|
+
};
|
|
478
|
+
const sim = su as unknown as gl.ISim;
|
|
479
|
+
const own = <O extends object>(o: O, build: (u: O) => Node<"vec4">[]) =>
|
|
480
|
+
new FullscreenPass(o, build(o));
|
|
481
|
+
const both = (f: (out: "V" | "S") => Node<"vec4">) => [f("V"), f("S")];
|
|
482
|
+
const advect = { uV: tex(), uS: tex() };
|
|
483
|
+
const correct = { uV: tex(), uS: tex(), uVf: tex(), uSf: tex(), uMacCormack: uniform(1) };
|
|
484
|
+
const react = {
|
|
485
|
+
uV: tex(),
|
|
486
|
+
uS: tex(),
|
|
487
|
+
uC: tex(),
|
|
488
|
+
uSrcP: v4s(),
|
|
489
|
+
uSrcA: v4s(),
|
|
490
|
+
uSrcV: v4s(),
|
|
491
|
+
uSrcB: v4s(),
|
|
492
|
+
uSrcN: uniform(0, "int"),
|
|
493
|
+
uBurn: uniform(new Vector4()),
|
|
494
|
+
uDecay: uniform(new Vector4()),
|
|
495
|
+
uForce: uniform(new Vector4()),
|
|
496
|
+
uFlow: uniform(new Vector4()),
|
|
497
|
+
uTurb: uniform(new Vector4()),
|
|
498
|
+
};
|
|
499
|
+
const light = {
|
|
500
|
+
...this.look,
|
|
501
|
+
uS: tex(),
|
|
502
|
+
uC: tex(),
|
|
503
|
+
uCDim: uniform(new Vector3()),
|
|
504
|
+
uCTiles: uniform(new Vector2()),
|
|
505
|
+
uCInv: uniform(new Vector2(1 / cW, 1 / cH)),
|
|
506
|
+
uLightP: uniform(new Vector4(2.5, 3, 1, 0)),
|
|
507
|
+
};
|
|
508
|
+
this.passes = {
|
|
509
|
+
advect: own(advect, (u) => both((o) => gl.ADVECT(sim, u, o))),
|
|
510
|
+
correct: own(correct, (u) =>
|
|
511
|
+
both((o) => gl.CORRECT(sim, u as unknown as Parameters<typeof gl.CORRECT>[1], o)),
|
|
512
|
+
),
|
|
513
|
+
curl: own({ uV: tex() }, (u) => [gl.CURL(sim, u)]),
|
|
514
|
+
react: own(react, (u) => both((o) => gl.REACT(sim, u as unknown as gl.IReactOwn, o))),
|
|
515
|
+
diverge: own({ uV: tex(), uS: tex(), uP: tex(), uWarm: uniform(0.9) }, (u) => [
|
|
516
|
+
gl.DIVERGE(sim, u as unknown as Parameters<typeof gl.DIVERGE>[1]),
|
|
517
|
+
]),
|
|
518
|
+
jacobi: own({ uP: tex() }, (u) => [gl.JACOBI(sim, u)]),
|
|
519
|
+
project: own({ uV: tex(), uS: tex(), uP: tex() }, (u) => both((o) => gl.PROJECT(sim, u, o))),
|
|
520
|
+
light: own(light, (u) => [gl.LIGHT(sim, u as unknown as gl.ILightOwn)]),
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
// domains: each owns its targets and a box proxy in the fx scene
|
|
524
|
+
const box = new BoxGeometry(1, 1, 1);
|
|
525
|
+
const slots = q.fluidDomains ?? TIER_SLOTS[tier] ?? 2;
|
|
526
|
+
this.domains = [];
|
|
527
|
+
for (let i = 0; i < slots; i++) {
|
|
528
|
+
const d = new FluidDomain(this, W, H, cW, cH);
|
|
529
|
+
const m: IRenderUniforms = {
|
|
530
|
+
uS: texture((d.state[0] as RenderTarget).textures[1] as Texture),
|
|
531
|
+
uL0: texture(d.light.texture),
|
|
532
|
+
uDim: uniform(d.dim),
|
|
533
|
+
uTiles: uniform(d.tiles),
|
|
534
|
+
uInv: uniform(new Vector2(1 / W, 1 / H)),
|
|
535
|
+
uCDim: uniform(d.cdim),
|
|
536
|
+
uCTiles: uniform(d.ctiles),
|
|
537
|
+
uCInv: uniform(new Vector2(1 / cW, 1 / cH)),
|
|
538
|
+
uOrigin: uniform(d.origin),
|
|
539
|
+
uH: uniform(0.1),
|
|
540
|
+
uMarch: uniform(new Vector4()),
|
|
541
|
+
uMarch2: uniform(new Vector4()),
|
|
542
|
+
uLit: uniform(new Vector4()),
|
|
543
|
+
uSootColor: uniform(new Vector3()),
|
|
544
|
+
uDetail: uniform(new Vector4()),
|
|
545
|
+
uDetailC: uniform(d.detailCenter),
|
|
546
|
+
uLook0: uniform(new Vector4()),
|
|
547
|
+
uLook1: uniform(new Vector4()),
|
|
548
|
+
uTint: uniform(new Vector3(1, 1, 1)),
|
|
549
|
+
};
|
|
550
|
+
d.uniforms = m;
|
|
551
|
+
d.material = new MeshBasicNodeMaterial({ ...FX_BLENDING, depthTest: false, side: BackSide });
|
|
552
|
+
d.material.fog = false;
|
|
553
|
+
d.material.fragmentNode = gl.RENDER(m as unknown as gl.IRenderOwn);
|
|
554
|
+
d.mesh = new Mesh(box, d.material);
|
|
555
|
+
d.mesh.visible = false;
|
|
556
|
+
d.mesh.frustumCulled = false;
|
|
557
|
+
fx.scenes.fx.add(d.mesh);
|
|
558
|
+
this.domains.push(d);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// run every pass once now (ANGLE finishes some programs at first draw), then forget it
|
|
562
|
+
const d0 = this.domains[0] as FluidDomain;
|
|
563
|
+
this.layout(d0, [1, 1, 1], 8);
|
|
564
|
+
d0.active = true;
|
|
565
|
+
this.clearDomain(d0);
|
|
566
|
+
this.step(d0, 1 / 60);
|
|
567
|
+
d0.active = false;
|
|
568
|
+
d0.fresh = true;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// pick voxel counts for a box of `size` metres with `res` voxels on its longest axis
|
|
572
|
+
layout(d: FluidDomain, size: number[], res: number): boolean {
|
|
573
|
+
const [sx, sy, sz] = size as [number, number, number];
|
|
574
|
+
const big = Math.max(sx, sy, sz);
|
|
575
|
+
for (let r = Math.max(8, Math.round(res)); r >= 8; r -= 2) {
|
|
576
|
+
const h = big / r;
|
|
577
|
+
const nx = Math.max(4, Math.round(sx / h));
|
|
578
|
+
const ny = Math.max(4, Math.round(sy / h));
|
|
579
|
+
const nz = Math.max(4, Math.round(sz / h));
|
|
580
|
+
const cx = Math.ceil(nx / 2);
|
|
581
|
+
const cy = Math.ceil(ny / 2);
|
|
582
|
+
const cz = Math.ceil(nz / 2);
|
|
583
|
+
const t = tileLayout(nx, ny, nz, d.W, d.H);
|
|
584
|
+
const ct = tileLayout(cx, cy, cz, d.cW, d.cH);
|
|
585
|
+
if (!t || !ct) continue;
|
|
586
|
+
d.setGrid(nx, ny, nz, t, [cx, cy, cz], ct, h);
|
|
587
|
+
return true;
|
|
588
|
+
}
|
|
589
|
+
return false;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
acquire({
|
|
593
|
+
size = [4, 4, 4],
|
|
594
|
+
res = this.res,
|
|
595
|
+
position = null,
|
|
596
|
+
}: { size?: number[]; res?: number; position?: Vector3 | null } = {}): FluidDomain | null {
|
|
597
|
+
const d = this.domains.find((x) => !x.active);
|
|
598
|
+
if (!d || !this.layout(d, size, res)) return null;
|
|
599
|
+
Object.assign(d, DEFAULTS);
|
|
600
|
+
d.tint.setRGB(1, 1, 1);
|
|
601
|
+
d.sootColor.setRGB(0.13, 0.12, 0.11);
|
|
602
|
+
d.detailCenter.set(0, 0, 0);
|
|
603
|
+
d.obstacle.set(0, 0, 0, 0);
|
|
604
|
+
if (position) d.position.copy(position);
|
|
605
|
+
d.active = true;
|
|
606
|
+
d.fresh = true;
|
|
607
|
+
d.nSrc = 0;
|
|
608
|
+
d.heat = 0;
|
|
609
|
+
d.fuelPool = 0;
|
|
610
|
+
return d;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
get active(): boolean {
|
|
614
|
+
return this.domains.some((d) => d.active);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
clearDomain(d: FluidDomain): void {
|
|
618
|
+
const r = this.renderer;
|
|
619
|
+
const prev = r.getRenderTarget();
|
|
620
|
+
const alpha = r.getClearAlpha();
|
|
621
|
+
r.getClearColor(_c);
|
|
622
|
+
r.setClearColor(0x000000, 0);
|
|
623
|
+
// engine: no renderer.clear(); a target draw clears to the clear colour first, so an empty draw clears
|
|
624
|
+
for (const t of d.all) {
|
|
625
|
+
r.setRenderTarget(t);
|
|
626
|
+
r.render(EMPTY, quadCam);
|
|
627
|
+
}
|
|
628
|
+
r.setRenderTarget(d.light);
|
|
629
|
+
r.render(EMPTY, quadCam);
|
|
630
|
+
r.setClearColor(_c, alpha);
|
|
631
|
+
r.setRenderTarget(prev);
|
|
632
|
+
d.cur = 0;
|
|
633
|
+
d.pc = 0;
|
|
634
|
+
d.fresh = false;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
draw(name: keyof FluidVolume["passes"], out: RenderTarget) {
|
|
638
|
+
const p = this.passes[name];
|
|
639
|
+
p.render(this.renderer, out);
|
|
640
|
+
return p.uniforms;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// one sim step for domain d
|
|
644
|
+
step(d: FluidDomain, dt: number): void {
|
|
645
|
+
const su = this.su;
|
|
646
|
+
const P = this.passes;
|
|
647
|
+
d.origin.copy(d.position).addScaledVector(d.size, -0.5);
|
|
648
|
+
su.uDim.value.copy(d.dim);
|
|
649
|
+
su.uTiles.value.copy(d.tiles);
|
|
650
|
+
su.uOrigin.value.copy(d.origin);
|
|
651
|
+
su.uH.value = d.h;
|
|
652
|
+
su.uSimDt.value = dt;
|
|
653
|
+
su.uShift.value.subVectors(d.origin, d.lastOrigin).divideScalar(d.h);
|
|
654
|
+
d.lastOrigin.copy(d.origin);
|
|
655
|
+
const ob = d.obstacle;
|
|
656
|
+
su.uObst.value.set(
|
|
657
|
+
(ob.x - d.origin.x) / d.h - 0.5,
|
|
658
|
+
(ob.y - d.origin.y) / d.h - 0.5,
|
|
659
|
+
(ob.z - d.origin.z) / d.h - 0.5,
|
|
660
|
+
ob.w / d.h,
|
|
661
|
+
);
|
|
662
|
+
const A = d.state[d.cur] as RenderTarget;
|
|
663
|
+
const B = d.state[1 - d.cur] as RenderTarget;
|
|
664
|
+
const [Av, As] = A.textures as [Texture, Texture];
|
|
665
|
+
const [Bv, Bs] = B.textures as [Texture, Texture];
|
|
666
|
+
|
|
667
|
+
const ua = P.advect.uniforms;
|
|
668
|
+
ua.uV.value = Av;
|
|
669
|
+
ua.uS.value = As;
|
|
670
|
+
this.draw("advect", d.tmp);
|
|
671
|
+
const uc = P.correct.uniforms;
|
|
672
|
+
uc.uV.value = Av;
|
|
673
|
+
uc.uS.value = As;
|
|
674
|
+
uc.uVf.value = d.tmp.textures[0] as Texture;
|
|
675
|
+
uc.uSf.value = d.tmp.textures[1] as Texture;
|
|
676
|
+
this.draw("correct", B);
|
|
677
|
+
su.uShift.value.set(0, 0, 0);
|
|
678
|
+
P.curl.uniforms.uV.value = Bv;
|
|
679
|
+
this.draw("curl", d.curl);
|
|
680
|
+
|
|
681
|
+
const u = P.react.uniforms;
|
|
682
|
+
u.uV.value = Bv;
|
|
683
|
+
u.uS.value = Bs;
|
|
684
|
+
u.uC.value = d.curl.texture;
|
|
685
|
+
u.uSrcN.value = d.nSrc;
|
|
686
|
+
for (let i = 0; i < d.nSrc; i++) {
|
|
687
|
+
(u.uSrcP[i] as U4).value.copy(d.srcP[i] as Vector4);
|
|
688
|
+
(u.uSrcA[i] as U4).value.copy(d.srcA[i] as Vector4);
|
|
689
|
+
(u.uSrcV[i] as U4).value.copy(d.srcV[i] as Vector4);
|
|
690
|
+
(u.uSrcB[i] as U4).value.copy(d.srcB[i] as Vector4);
|
|
691
|
+
}
|
|
692
|
+
u.uBurn.value.set(d.ignition, d.burnRate, d.heatPerFuel, d.sootYield);
|
|
693
|
+
u.uDecay.value.set(d.cooling, d.radiative, d.sootDecay, d.fuelDecay);
|
|
694
|
+
u.uForce.value.set(d.buoyancy, d.sootWeight, d.vorticity, d.expansion);
|
|
695
|
+
u.uFlow.value.set(d.forceCoupling, d.damping, 0, 0);
|
|
696
|
+
u.uTurb.value.set(d.turbulence, d.turbulenceFreq, d.sootErode, d.sootCool);
|
|
697
|
+
this.draw("react", A);
|
|
698
|
+
|
|
699
|
+
const ud = P.diverge.uniforms;
|
|
700
|
+
ud.uV.value = Av;
|
|
701
|
+
ud.uS.value = As;
|
|
702
|
+
ud.uP.value = (d.press[d.pc] as RenderTarget).texture;
|
|
703
|
+
this.draw("diverge", d.press[1 - d.pc] as RenderTarget);
|
|
704
|
+
d.pc = 1 - d.pc;
|
|
705
|
+
const J = P.jacobi.uniforms;
|
|
706
|
+
for (let i = 0; i < this.iterations; i++) {
|
|
707
|
+
J.uP.value = (d.press[d.pc] as RenderTarget).texture;
|
|
708
|
+
this.draw("jacobi", d.press[1 - d.pc] as RenderTarget);
|
|
709
|
+
d.pc = 1 - d.pc;
|
|
710
|
+
}
|
|
711
|
+
const up = P.project.uniforms;
|
|
712
|
+
up.uV.value = Av;
|
|
713
|
+
up.uS.value = As;
|
|
714
|
+
up.uP.value = (d.press[d.pc] as RenderTarget).texture;
|
|
715
|
+
this.draw("project", B);
|
|
716
|
+
d.cur = 1 - d.cur;
|
|
717
|
+
|
|
718
|
+
// CPU heat mirror: pending fuel burns at the sim's rate, heat cools at its rate
|
|
719
|
+
const burned = d.fuelPool * (1 - Math.exp(-d.burnRate * dt));
|
|
720
|
+
d.fuelPool -= burned;
|
|
721
|
+
d.heat = (d.heat + burned * d.heatPerFuel) * Math.exp(-(d.cooling + 0.6 * d.radiative) * dt);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
setLook(d: FluidDomain, u: { uLook0: U4; uLook1: U4; uTint: U3 }): void {
|
|
725
|
+
u.uLook0.value.set(d.emission, d.sootDensity, d.flameDensity, d.absorb);
|
|
726
|
+
u.uLook1.value.set(d.temperature, 300, d.fuelDensity, d.chroma);
|
|
727
|
+
u.uTint.value.set(d.tint.r, d.tint.g, d.tint.b);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// main calls this every frame inside the timed 'sim' section
|
|
731
|
+
update(dt0: number, camera: Camera): void {
|
|
732
|
+
// engine: createFx's renderFrame runs per fixed update, which can be several per drawn frame; the
|
|
733
|
+
// fluid steps once per drawn frame (upstream: once per renderFrame) over the time since its last step
|
|
734
|
+
const frame = (this.renderer.info as { frame?: number }).frame;
|
|
735
|
+
this.pending += dt0;
|
|
736
|
+
if (frame !== undefined && frame === this.lastFrame) return;
|
|
737
|
+
this.lastFrame = frame;
|
|
738
|
+
const dt = this.pending;
|
|
739
|
+
this.pending = 0;
|
|
740
|
+
this.dt = Math.min(dt, 1 / 30);
|
|
741
|
+
const r = this.renderer;
|
|
742
|
+
const prev = r.getRenderTarget();
|
|
743
|
+
camera.getWorldDirection(gl.uCamFwd.value);
|
|
744
|
+
const fxH = U.uFxResolution.value.y;
|
|
745
|
+
// biome-ignore lint/style/noNonNullAssertion: upstream Ultra Elements code, copied verbatim
|
|
746
|
+
const mPerPx = 1 / (0.5 * fxH * (camera as PerspectiveCamera).projectionMatrix.elements[5]!);
|
|
747
|
+
for (const d of this.domains) {
|
|
748
|
+
d.mesh.visible = d.active && d.visible;
|
|
749
|
+
if (!d.active) continue;
|
|
750
|
+
if (d.fresh) {
|
|
751
|
+
this.clearDomain(d);
|
|
752
|
+
d.origin.copy(d.position).addScaledVector(d.size, -0.5);
|
|
753
|
+
d.lastOrigin.copy(d.origin);
|
|
754
|
+
}
|
|
755
|
+
if (dt > 0) this.step(d, this.dt);
|
|
756
|
+
d.nSrc = 0;
|
|
757
|
+
d.origin.copy(d.position).addScaledVector(d.size, -0.5);
|
|
758
|
+
// light volume (also while paused: the sun may move)
|
|
759
|
+
const su = this.su;
|
|
760
|
+
const L = this.passes.light.uniforms;
|
|
761
|
+
su.uDim.value.copy(d.dim);
|
|
762
|
+
su.uTiles.value.copy(d.tiles);
|
|
763
|
+
su.uOrigin.value.copy(d.origin);
|
|
764
|
+
su.uH.value = d.h;
|
|
765
|
+
L.uS.value = (d.state[d.cur] as RenderTarget).textures[1] as Texture;
|
|
766
|
+
L.uC.value = d.curl.texture;
|
|
767
|
+
L.uCDim.value.copy(d.cdim);
|
|
768
|
+
L.uCTiles.value.copy(d.ctiles);
|
|
769
|
+
L.uLightP.value.set(2.5, 2, d.fireLight, d.emptyBelow);
|
|
770
|
+
this.setLook(d, this.look);
|
|
771
|
+
this.draw("light", d.light);
|
|
772
|
+
// render uniforms
|
|
773
|
+
const m = d.uniforms;
|
|
774
|
+
m.uS.value = (d.state[d.cur] as RenderTarget).textures[1] as Texture;
|
|
775
|
+
m.uH.value = d.h;
|
|
776
|
+
this.setLook(d, m);
|
|
777
|
+
// sample spacing: a fraction of a voxel, never finer than the pixel footprint at the domain's
|
|
778
|
+
// distance, and coarse enough that the box's diagonal fits the step budget
|
|
779
|
+
const auto = Math.min(1, Q.auto?.steps ?? 1);
|
|
780
|
+
const steps = Math.max(8, Math.floor(this.maxSteps * auto));
|
|
781
|
+
const dist = _v.subVectors(camera.position, d.position).length();
|
|
782
|
+
const dtm = Math.max(
|
|
783
|
+
(this.stepVox * d.stepScale * d.h) / auto,
|
|
784
|
+
dist * mPerPx,
|
|
785
|
+
d.size.length() / steps,
|
|
786
|
+
);
|
|
787
|
+
// displacement: one curl octave, its finest band (1/16 tile) no finer than the march step
|
|
788
|
+
m.uMarch.value.set(
|
|
789
|
+
dtm,
|
|
790
|
+
steps,
|
|
791
|
+
d.upres,
|
|
792
|
+
Math.min(1 / (4 * d.upresScale * d.h), 1 / (16 * dtm)),
|
|
793
|
+
);
|
|
794
|
+
m.uMarch2.value.set(
|
|
795
|
+
d.edgeFade,
|
|
796
|
+
mPerPx,
|
|
797
|
+
d.origin.y > -0.5 * d.h ? 1 : 0,
|
|
798
|
+
1 / Math.max(d.nearFade, 0.01),
|
|
799
|
+
);
|
|
800
|
+
// the empty-space stride stays inside the occupancy's ~3 voxel dilation; the cell pattern's
|
|
801
|
+
// main octaves (a quarter and an eighth of a tile) stay above the sample spacing
|
|
802
|
+
m.uDetail.value.set(
|
|
803
|
+
d.detailErode,
|
|
804
|
+
Math.min(d.detailFreq, 1 / (12 * dtm)),
|
|
805
|
+
d.detailHeat,
|
|
806
|
+
Math.max(1, Math.floor((3 * d.h) / dtm)),
|
|
807
|
+
);
|
|
808
|
+
m.uLit.value.set(d.g, d.sun, d.sky, 1);
|
|
809
|
+
m.uSootColor.value.set(d.sootColor.r, d.sootColor.g, d.sootColor.b);
|
|
810
|
+
d.mesh.position.copy(d.position);
|
|
811
|
+
d.mesh.scale.copy(d.size);
|
|
812
|
+
d.mesh.updateMatrixWorld();
|
|
813
|
+
}
|
|
814
|
+
r.setRenderTarget(prev);
|
|
815
|
+
}
|
|
816
|
+
}
|