@threenative/core 0.3.4 → 0.3.5
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 +1505 -79
- package/dist/{audio-7i3Xl0l3.d.ts → audio-DIJE5HTr.d.ts} +43 -3
- package/dist/{canvas-layer-DDmC_VVF.d.ts → canvas-layer-C1-5CviZ.d.ts} +5 -1
- package/dist/{game-CljaDv4D.d.ts → game-Bj8G1wmD.d.ts} +102 -11
- package/dist/hot.d.ts +4 -4
- package/dist/index.d.ts +712 -118
- package/dist/index.js +12423 -4955
- package/dist/{gpu-readback-CqJEfWNQ.d.ts → model-lod-BDAUYgsS.d.ts} +80 -5
- package/dist/playtest.d.ts +6 -10
- package/dist/playtest.js +47 -24
- package/dist/react-css.d.ts +62 -0
- package/dist/react-css.js +591 -0
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-CfsS2hxi.d.ts → renderer-9yEo9hPa.d.ts} +489 -22
- package/dist/terrain-jobs-worker.d.ts +2 -0
- package/dist/terrain-jobs-worker.js +130 -0
- package/dist/ui-layer.js +59 -8
- package/dist/world.d.ts +766 -73
- package/dist/world.js +9307 -3543
- package/gpl/recipes/export_world.py +18 -1
- package/mcp/blender-server.mjs +7 -4
- package/mcp/blender.mjs +3 -4
- package/mcp/engine-server.mjs +2 -2
- package/mcp/servers.mjs +4 -4
- package/package.json +15 -9
- package/patches/three@0.185.1-prd269-upgrade.patch +22418 -0
- package/patches/three@0.185.1.patch +6594 -700
- package/scripts/apply-three-patch.mjs +62 -4
- package/src/animation-blend.ts +264 -0
- package/src/animation-composition.ts +362 -0
- package/src/animation-layers.ts +260 -0
- package/src/animation-root-motion.ts +283 -0
- package/src/animation.ts +896 -0
- package/src/assets.ts +1303 -0
- package/src/atmosphere/index.ts +335 -0
- package/src/atmosphere/luts.ts +298 -0
- package/src/atmosphere/params.ts +323 -0
- package/src/audio.ts +912 -0
- package/src/billboard.ts +124 -0
- package/src/bone-lengths.ts +157 -0
- package/src/camera-shake.ts +116 -0
- package/src/canvas-layer.ts +47 -0
- package/src/clip-audit.ts +401 -0
- package/src/clustered-batch.ts +492 -0
- package/src/clustered-mesh.ts +638 -0
- package/src/compute-driven.ts +176 -0
- package/src/compute-timing.ts +318 -0
- package/src/config.ts +593 -0
- package/src/debug.ts +82 -0
- package/src/engine-render-hook.ts +25 -0
- package/src/entities.ts +79 -0
- package/src/entity-snapshot.ts +53 -0
- package/src/flight.ts +683 -0
- package/src/fluid-field.ts +748 -0
- package/src/fluid-particles.ts +1253 -0
- package/src/frame-budget.ts +1368 -0
- package/src/game.ts +2390 -0
- package/src/geometry-capture.ts +1098 -0
- package/src/gpu-class.ts +160 -0
- package/src/gpu-frame-observation.ts +424 -0
- package/src/gpu-readback.ts +143 -0
- package/src/gpu-scene-bvh.ts +464 -0
- package/src/grounding.ts +100 -0
- package/src/hot.ts +161 -0
- package/src/import-meta.d.ts +13 -0
- package/src/index.ts +1536 -0
- package/src/input.ts +690 -0
- package/src/instanced-batch-lod.ts +283 -0
- package/src/instanced-batch.ts +274 -0
- package/src/interpolation.ts +129 -0
- package/src/launch-diagnostics.ts +161 -0
- package/src/loop.ts +564 -0
- package/src/matrix-world.ts +245 -0
- package/src/merge-parts.ts +812 -0
- package/src/model-lod.ts +1315 -0
- package/src/net-protocol.ts +333 -0
- package/src/net-session.ts +477 -0
- package/src/net.ts +80 -0
- package/src/ocean/fft.ts +72 -0
- package/src/ocean/spectral.ts +728 -0
- package/src/particle-significance.ts +102 -0
- package/src/particles.ts +648 -0
- package/src/path-follow.ts +175 -0
- package/src/picking.ts +250 -0
- package/src/pipeline-census.ts +1566 -0
- package/src/platform.ts +210 -0
- package/src/playtest.ts +839 -0
- package/src/pointer-events.ts +411 -0
- package/src/pose-measure.ts +435 -0
- package/src/profiling/FrameCounters.ts +231 -0
- package/src/profiling/Spans.ts +548 -0
- package/src/profiling/opacity-convention.ts +21 -0
- package/src/profiling/render-list-validate.ts +175 -0
- package/src/profiling/render-trap-audit.ts +84 -0
- package/src/profiling/scene-warning.ts +190 -0
- package/src/profiling/span-probes.ts +191 -0
- package/src/profiling/unlit-foliage-warning.ts +149 -0
- package/src/projection-apply.ts +1529 -0
- package/src/projection-marker.ts +142 -0
- package/src/projection-plan.ts +1258 -0
- package/src/projection-skinned.ts +419 -0
- package/src/projection-stability.ts +409 -0
- package/src/projection-uniform.ts +328 -0
- package/src/random.ts +41 -0
- package/src/react-css.ts +604 -0
- package/src/react-glyphs.ts +114 -0
- package/src/react-host.ts +576 -0
- package/src/react-layout.ts +265 -0
- package/src/react.ts +60 -0
- package/src/render/alpha-antialiasing.ts +159 -0
- package/src/render/batched-velocity.ts +126 -0
- package/src/render/chain.ts +1021 -0
- package/src/render/daylight.ts +189 -0
- package/src/render/depth-pyramid.ts +596 -0
- package/src/render/foliage-alpha.ts +166 -0
- package/src/render/material-key.ts +60 -0
- package/src/render/mesh-pool.ts +144 -0
- package/src/render/probe-volume.ts +1668 -0
- package/src/render/resolution-subrect.ts +253 -0
- package/src/render/velocity.ts +442 -0
- package/src/render/virtual-shadow-pages.ts +827 -0
- package/src/render/virtual-shadow-partial.ts +174 -0
- package/src/render/virtual-shadow.ts +3330 -0
- package/src/render/world-impostor-surface.ts +722 -0
- package/src/render/world-impostor.ts +558 -0
- package/src/render-camera-cull.ts +441 -0
- package/src/render-pass-budget.ts +501 -0
- package/src/renderProjection.ts +758 -0
- package/src/renderer-config.ts +127 -0
- package/src/renderer.ts +1847 -0
- package/src/replay-protocol.ts +85 -0
- package/src/replay.ts +205 -0
- package/src/resolution-scaler.ts +819 -0
- package/src/rig-preparation.ts +271 -0
- package/src/ripple-field.ts +432 -0
- package/src/scale.ts +109 -0
- package/src/scene.ts +239 -0
- package/src/schedule.ts +155 -0
- package/src/skeletal-mesh.ts +61 -0
- package/src/skeleton.ts +100 -0
- package/src/snow-field.ts +744 -0
- package/src/softbody-topology.ts +269 -0
- package/src/softbody.ts +516 -0
- package/src/sprite-animator.ts +222 -0
- package/src/startup-readiness.ts +386 -0
- package/src/state.ts +66 -0
- package/src/static-transform.ts +274 -0
- package/src/storage-buffer.ts +253 -0
- package/src/streaming.ts +308 -0
- package/src/target-fps.ts +92 -0
- package/src/terrain-jobs-worker.ts +17 -0
- package/src/terrain-jobs.ts +353 -0
- package/src/textures.ts +39 -0
- package/src/tracers.ts +237 -0
- package/src/ui-bridge.ts +368 -0
- package/src/ui-hit-regions.ts +380 -0
- package/src/ui-layer.ts +83 -0
- package/src/ui-state.ts +220 -0
- package/src/version.ts +2 -0
- package/src/viewport.ts +253 -0
- package/src/warmup.ts +1357 -0
- package/src/water-surface.ts +384 -0
- package/src/wave-field.ts +524 -0
- package/src/world-capabilities.ts +131 -0
- package/src/world-cell-proxy.ts +392 -0
- package/src/world-cells.ts +10564 -0
- package/src/world-gpu-scene.ts +3354 -0
- package/src/world-heightmap.ts +82 -0
- package/src/world-package.ts +469 -0
- package/src/world-passes.ts +582 -0
- package/src/world-region.ts +17 -0
- package/src/world-terrain-splat.ts +564 -0
- package/src/world-tiles.ts +3826 -0
- package/src/world-topology.ts +370 -0
- package/src/world-validate.ts +48 -0
- package/src/world.ts +805 -0
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
import { Group, Vector4 } from "three";
|
|
2
|
+
import type { Node, UniformNode } from "three/src/nodes/Nodes.js";
|
|
3
|
+
import type UniformArrayNode from "three/src/nodes/accessors/UniformArrayNode.js";
|
|
4
|
+
import {
|
|
5
|
+
Fn,
|
|
6
|
+
If,
|
|
7
|
+
Loop,
|
|
8
|
+
abs,
|
|
9
|
+
clamp,
|
|
10
|
+
compute,
|
|
11
|
+
float,
|
|
12
|
+
instanceIndex,
|
|
13
|
+
length,
|
|
14
|
+
storageTexture,
|
|
15
|
+
textureStore,
|
|
16
|
+
uint,
|
|
17
|
+
uniform,
|
|
18
|
+
uniformArray,
|
|
19
|
+
uvec2,
|
|
20
|
+
vec2,
|
|
21
|
+
vec4,
|
|
22
|
+
} from "three/tsl";
|
|
23
|
+
import {
|
|
24
|
+
ClampToEdgeWrapping,
|
|
25
|
+
type ComputeNode,
|
|
26
|
+
FloatType,
|
|
27
|
+
RGBAFormat,
|
|
28
|
+
StorageTexture,
|
|
29
|
+
type StorageTextureNode,
|
|
30
|
+
} from "three/webgpu";
|
|
31
|
+
import type { IRendererLike } from "./renderer.js";
|
|
32
|
+
|
|
33
|
+
export interface IFluidFieldVector2 {
|
|
34
|
+
readonly x: number;
|
|
35
|
+
readonly y: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface IFluidFieldOptions {
|
|
39
|
+
readonly resolution: number;
|
|
40
|
+
readonly viscosity?: number;
|
|
41
|
+
readonly pressureIterations?: number;
|
|
42
|
+
readonly maxSplats?: number;
|
|
43
|
+
readonly timeStep?: number;
|
|
44
|
+
readonly vorticity?: number;
|
|
45
|
+
readonly splatRadius?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface IFluidFieldSampler {
|
|
49
|
+
sample(uv: Node<"vec2">): StorageTextureNode;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type TexturePair = readonly [StorageTexture, StorageTexture];
|
|
53
|
+
type ComputePair = readonly [ComputeNode, ComputeNode];
|
|
54
|
+
type ComputeMatrix = readonly [ComputePair, ComputePair];
|
|
55
|
+
|
|
56
|
+
const DEFAULT_MAX_SPLATS = 8;
|
|
57
|
+
const DEFAULT_TIME_STEP = 1 / 60;
|
|
58
|
+
const DEFAULT_VORTICITY = 0.2;
|
|
59
|
+
const DEFAULT_SPLAT_RADIUS = 0.08;
|
|
60
|
+
|
|
61
|
+
function positiveFinite(name: string, value: number): void {
|
|
62
|
+
if (!Number.isFinite(value) || value <= 0)
|
|
63
|
+
throw new Error(`FluidField2D.${name} must be positive.`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function nonNegativeFinite(name: string, value: number): void {
|
|
67
|
+
if (!Number.isFinite(value) || value < 0)
|
|
68
|
+
throw new Error(`FluidField2D.${name} must be non-negative.`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function storage(width: number, height: number): StorageTexture {
|
|
72
|
+
const texture = new StorageTexture(width, height);
|
|
73
|
+
texture.type = FloatType;
|
|
74
|
+
texture.format = RGBAFormat;
|
|
75
|
+
texture.wrapS = ClampToEdgeWrapping;
|
|
76
|
+
texture.wrapT = ClampToEdgeWrapping;
|
|
77
|
+
texture.generateMipmaps = false;
|
|
78
|
+
texture.needsUpdate = true;
|
|
79
|
+
return texture;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function computeKernel(name: string, resolution: number, callback: () => Node): ComputeNode {
|
|
83
|
+
const node = compute(Fn(callback)(), resolution * resolution);
|
|
84
|
+
node.setName(name);
|
|
85
|
+
return node;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @internal Keep the scalar regression model in lockstep with the TSL force mapping. */
|
|
89
|
+
export function mapVorticityForce<T>(
|
|
90
|
+
force: Readonly<{ x: T; y: T }>,
|
|
91
|
+
negate: (value: T) => T,
|
|
92
|
+
): readonly [T, T] {
|
|
93
|
+
return [force.x, negate(force.y)];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function pixelCoordinate(resolution: number) {
|
|
97
|
+
return uvec2(instanceIndex.mod(resolution), instanceIndex.div(resolution));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function pixelUv(resolution: number): Node<"vec2"> {
|
|
101
|
+
return vec2(
|
|
102
|
+
float(instanceIndex.mod(resolution)).add(0.5).div(resolution),
|
|
103
|
+
float(instanceIndex.div(resolution)).add(0.5).div(resolution),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function readFluidTexture(texture: StorageTexture): StorageTextureNode {
|
|
108
|
+
return storageTexture(texture).toReadOnly().setSampler(false);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function sampleAt(
|
|
112
|
+
texture: StorageTextureNode,
|
|
113
|
+
uv: Node<"vec2">,
|
|
114
|
+
resolution: number,
|
|
115
|
+
): StorageTextureNode {
|
|
116
|
+
const size = resolution;
|
|
117
|
+
return texture.sample(
|
|
118
|
+
uvec2(uint(clamp(uv.x.mul(size), 0, size - 1)), uint(clamp(uv.y.mul(size), 0, size - 1))),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
class FluidFieldSampler implements IFluidFieldSampler {
|
|
123
|
+
#texture: StorageTextureNode;
|
|
124
|
+
#resolution: number;
|
|
125
|
+
|
|
126
|
+
constructor(texture: StorageTexture, resolution: number) {
|
|
127
|
+
this.#texture = readFluidTexture(texture);
|
|
128
|
+
this.#resolution = resolution;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
sample(uv: Node<"vec2">): StorageTextureNode {
|
|
132
|
+
return sampleAt(this.#texture, uv, this.#resolution);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setTexture(texture: StorageTexture): void {
|
|
136
|
+
this.#texture.value = texture;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function write(texture: StorageTexture, resolution: number, value: Node): StorageTextureNode {
|
|
141
|
+
return textureStore(
|
|
142
|
+
storageTexture(texture).toWriteOnly(),
|
|
143
|
+
pixelCoordinate(resolution),
|
|
144
|
+
value,
|
|
145
|
+
).toWriteOnly();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function neighbour(
|
|
149
|
+
texture: StorageTextureNode,
|
|
150
|
+
uv: Node<"vec2">,
|
|
151
|
+
texel: Node<"vec2">,
|
|
152
|
+
resolution: number,
|
|
153
|
+
x: number,
|
|
154
|
+
y: number,
|
|
155
|
+
) {
|
|
156
|
+
return sampleAt(texture, clamp(uv.add(vec2(x, y).mul(texel)), 0, 1), resolution);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function pair<T>(first: T, second: T): readonly [T, T] {
|
|
160
|
+
return [first, second];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Run a deterministic GPU fluid field whose data stays available to the game's render graph.
|
|
165
|
+
*
|
|
166
|
+
* The class is deliberately a scene object with the compute-driven lifecycle contract: adding it
|
|
167
|
+
* to a game scene attaches the renderer, warm-up sees every pass, fixed steps dispatch the passes,
|
|
168
|
+
* and removing it releases every GPU allocation. `dye` and `velocity` are read-only numeric
|
|
169
|
+
* samplers; the game decides what those values become when drawn.
|
|
170
|
+
* @situation simulate smoke, fire, fog, wind, or fluid response on a grid
|
|
171
|
+
* @situation inject a touch, pointer, or gameplay impulse into a fluid field
|
|
172
|
+
* @situation sample fluid dye or velocity in a game-owned render node
|
|
173
|
+
* @constraint add the field through `ctx.add` so renderer attachment, fixed-step dispatch, and release are automatic
|
|
174
|
+
* @constraint `dye` and `velocity` are numeric samplers; appearance stays in the game's `src/render/` code
|
|
175
|
+
* @override pressureIterations, viscosity, vorticity, and splatRadius tune the solver without changing its pass order
|
|
176
|
+
* @example const field = new FluidField2D({ resolution: 256, viscosity: 0, pressureIterations: 20 });
|
|
177
|
+
* ctx.add(field);
|
|
178
|
+
* field.splat({ x: 0.5, y: 0.5 }, { x: 0.2, y: 0 }, 1);
|
|
179
|
+
*/
|
|
180
|
+
export class FluidField2D extends Group {
|
|
181
|
+
readonly resolution: number;
|
|
182
|
+
readonly viscosity: number;
|
|
183
|
+
readonly pressureIterations: number;
|
|
184
|
+
readonly maxSplats: number;
|
|
185
|
+
readonly timeStep: number;
|
|
186
|
+
readonly vorticity: number;
|
|
187
|
+
readonly splatRadius: number;
|
|
188
|
+
readonly processCadence = "fixed" as const;
|
|
189
|
+
readonly warmupNodes: readonly ComputeNode[];
|
|
190
|
+
readonly velocity: IFluidFieldSampler;
|
|
191
|
+
readonly dye: IFluidFieldSampler;
|
|
192
|
+
|
|
193
|
+
#velocityTextures: TexturePair;
|
|
194
|
+
#dyeTextures: TexturePair;
|
|
195
|
+
#pressureTextures: TexturePair;
|
|
196
|
+
#curlTexture: StorageTexture;
|
|
197
|
+
#divergenceTexture: StorageTexture;
|
|
198
|
+
#velocitySplat: ComputePair;
|
|
199
|
+
#dyeSplat: ComputePair;
|
|
200
|
+
#curl: ComputePair;
|
|
201
|
+
#vorticity: ComputePair;
|
|
202
|
+
#divergence: ComputePair;
|
|
203
|
+
#pressureReset: ComputeNode;
|
|
204
|
+
#pressureSolve: ComputePair;
|
|
205
|
+
#gradient: ComputeMatrix;
|
|
206
|
+
#advectVelocity: ComputePair;
|
|
207
|
+
#advectDye: ComputeMatrix;
|
|
208
|
+
#velocitySampler: FluidFieldSampler;
|
|
209
|
+
#dyeSampler: FluidFieldSampler;
|
|
210
|
+
#splatPositions: UniformArrayNode<"vec4">;
|
|
211
|
+
#splatAmounts: UniformArrayNode<"vec4">;
|
|
212
|
+
#splatCount: UniformNode<"uint", number>;
|
|
213
|
+
#renderer: IRendererLike | undefined;
|
|
214
|
+
#velocityIndex = 0;
|
|
215
|
+
#dyeIndex = 0;
|
|
216
|
+
#pressureIndex = 0;
|
|
217
|
+
#queuedSplats = 0;
|
|
218
|
+
#steps = 0;
|
|
219
|
+
#splatsApplied = 0;
|
|
220
|
+
#released = false;
|
|
221
|
+
|
|
222
|
+
constructor(options: IFluidFieldOptions) {
|
|
223
|
+
if (!Number.isInteger(options.resolution) || options.resolution < 2)
|
|
224
|
+
throw new Error("FluidField2D.resolution must be an integer of at least 2.");
|
|
225
|
+
const pressureIterations = options.pressureIterations ?? 20;
|
|
226
|
+
const maxSplats = options.maxSplats ?? DEFAULT_MAX_SPLATS;
|
|
227
|
+
const timeStep = options.timeStep ?? DEFAULT_TIME_STEP;
|
|
228
|
+
const viscosity = options.viscosity ?? 0;
|
|
229
|
+
const vorticity = options.vorticity ?? DEFAULT_VORTICITY;
|
|
230
|
+
const splatRadius = options.splatRadius ?? DEFAULT_SPLAT_RADIUS;
|
|
231
|
+
if (!Number.isInteger(pressureIterations) || pressureIterations < 0)
|
|
232
|
+
throw new Error("FluidField2D.pressureIterations must be a non-negative integer.");
|
|
233
|
+
if (!Number.isInteger(maxSplats) || maxSplats <= 0)
|
|
234
|
+
throw new Error("FluidField2D.maxSplats must be a positive integer.");
|
|
235
|
+
nonNegativeFinite("viscosity", viscosity);
|
|
236
|
+
positiveFinite("timeStep", timeStep);
|
|
237
|
+
nonNegativeFinite("vorticity", vorticity);
|
|
238
|
+
positiveFinite("splatRadius", splatRadius);
|
|
239
|
+
|
|
240
|
+
super();
|
|
241
|
+
this.resolution = options.resolution;
|
|
242
|
+
this.pressureIterations = pressureIterations;
|
|
243
|
+
this.maxSplats = maxSplats;
|
|
244
|
+
this.timeStep = timeStep;
|
|
245
|
+
this.viscosity = viscosity;
|
|
246
|
+
this.vorticity = vorticity;
|
|
247
|
+
this.splatRadius = splatRadius;
|
|
248
|
+
|
|
249
|
+
this.#velocityTextures = pair(
|
|
250
|
+
storage(this.resolution, this.resolution),
|
|
251
|
+
storage(this.resolution, this.resolution),
|
|
252
|
+
);
|
|
253
|
+
this.#dyeTextures = pair(
|
|
254
|
+
storage(this.resolution, this.resolution),
|
|
255
|
+
storage(this.resolution, this.resolution),
|
|
256
|
+
);
|
|
257
|
+
this.#pressureTextures = pair(
|
|
258
|
+
storage(this.resolution, this.resolution),
|
|
259
|
+
storage(this.resolution, this.resolution),
|
|
260
|
+
);
|
|
261
|
+
this.#curlTexture = storage(this.resolution, this.resolution);
|
|
262
|
+
this.#divergenceTexture = storage(this.resolution, this.resolution);
|
|
263
|
+
this.#velocitySampler = new FluidFieldSampler(this.#velocityTextures[0], this.resolution);
|
|
264
|
+
this.#dyeSampler = new FluidFieldSampler(this.#dyeTextures[0], this.resolution);
|
|
265
|
+
this.velocity = this.#velocitySampler;
|
|
266
|
+
this.dye = this.#dyeSampler;
|
|
267
|
+
this.#splatPositions = uniformArray(
|
|
268
|
+
Array.from({ length: this.maxSplats }, () => new Vector4()),
|
|
269
|
+
"vec4",
|
|
270
|
+
);
|
|
271
|
+
this.#splatAmounts = uniformArray(
|
|
272
|
+
Array.from({ length: this.maxSplats }, () => new Vector4()),
|
|
273
|
+
"vec4",
|
|
274
|
+
);
|
|
275
|
+
this.#splatCount = uniform(0, "uint");
|
|
276
|
+
|
|
277
|
+
const initial = this.#makeInitialPasses();
|
|
278
|
+
this.#velocitySplat = this.#makeVelocitySplatPair();
|
|
279
|
+
this.#dyeSplat = this.#makeDyeSplatPair();
|
|
280
|
+
this.#curl = this.#makeCurlPair();
|
|
281
|
+
this.#vorticity = this.#makeVorticityPair();
|
|
282
|
+
this.#divergence = this.#makeDivergence();
|
|
283
|
+
this.#pressureReset = this.#makePressureReset();
|
|
284
|
+
this.#pressureSolve = this.#makePressureSolvePair();
|
|
285
|
+
this.#gradient = this.#makeGradientMatrix();
|
|
286
|
+
this.#advectVelocity = this.#makeAdvectVelocityPair();
|
|
287
|
+
this.#advectDye = this.#makeAdvectDyeMatrix();
|
|
288
|
+
this.warmupNodes = [
|
|
289
|
+
...initial,
|
|
290
|
+
...this.#velocitySplat,
|
|
291
|
+
...this.#dyeSplat,
|
|
292
|
+
...this.#curl,
|
|
293
|
+
...this.#vorticity,
|
|
294
|
+
...this.#divergence,
|
|
295
|
+
this.#pressureReset,
|
|
296
|
+
...this.#pressureSolve,
|
|
297
|
+
...this.#gradient.flat(),
|
|
298
|
+
...this.#advectVelocity,
|
|
299
|
+
...this.#advectDye.flat(),
|
|
300
|
+
];
|
|
301
|
+
this.addEventListener("removed", this.#onRemoved);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
get released(): boolean {
|
|
305
|
+
return this.#released;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
get queuedSplats(): number {
|
|
309
|
+
return this.#queuedSplats;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
get steps(): number {
|
|
313
|
+
return this.#steps;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
get splatsApplied(): number {
|
|
317
|
+
return this.#splatsApplied;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
attachRenderer(renderer: IRendererLike): void {
|
|
321
|
+
if (this.#released) throw new Error("FluidField2D cannot be attached after release.");
|
|
322
|
+
if (this.#renderer === renderer) return;
|
|
323
|
+
if (this.#renderer !== undefined)
|
|
324
|
+
throw new Error("FluidField2D is already attached to a renderer.");
|
|
325
|
+
this.#renderer = renderer;
|
|
326
|
+
for (const node of this.warmupNodes.slice(0, 8)) renderer.compute(node);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
splat(uv: IFluidFieldVector2, velocity: IFluidFieldVector2, amount: number): void {
|
|
330
|
+
if (this.#released) throw new Error("FluidField2D cannot splat after release.");
|
|
331
|
+
if (![uv.x, uv.y, velocity.x, velocity.y, amount].every(Number.isFinite))
|
|
332
|
+
throw new Error("FluidField2D.splat arguments must be finite.");
|
|
333
|
+
if (amount < 0) throw new Error("FluidField2D.splat amount must be non-negative.");
|
|
334
|
+
if (amount === 0) return;
|
|
335
|
+
if (this.#queuedSplats >= this.maxSplats) return;
|
|
336
|
+
const offset = this.#queuedSplats * 4;
|
|
337
|
+
const position = this.#splatPositions.array[offset / 4] as Vector4;
|
|
338
|
+
const splatAmount = this.#splatAmounts.array[offset / 4] as Vector4;
|
|
339
|
+
position.set(clampNumber(uv.x, 0, 1), clampNumber(uv.y, 0, 1), velocity.x, velocity.y);
|
|
340
|
+
splatAmount.set(amount, 0, 0, 0);
|
|
341
|
+
this.#queuedSplats += 1;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
process(renderer = this.#renderer): void {
|
|
345
|
+
if (this.#released) return;
|
|
346
|
+
if (renderer === undefined) throw new Error("FluidField2D is not attached to a renderer.");
|
|
347
|
+
this.#splatCount.value = this.#queuedSplats;
|
|
348
|
+
this.#dispatch(
|
|
349
|
+
renderer,
|
|
350
|
+
tupleValue(this.#velocitySplat, this.#velocityIndex, "velocity splat"),
|
|
351
|
+
);
|
|
352
|
+
this.#velocityIndex = 1 - this.#velocityIndex;
|
|
353
|
+
this.#dispatch(renderer, tupleValue(this.#dyeSplat, this.#dyeIndex, "dye splat"));
|
|
354
|
+
this.#dyeIndex = 1 - this.#dyeIndex;
|
|
355
|
+
this.#queuedSplats = 0;
|
|
356
|
+
this.#dispatch(renderer, tupleValue(this.#curl, this.#velocityIndex, "curl"));
|
|
357
|
+
this.#dispatch(renderer, tupleValue(this.#vorticity, this.#velocityIndex, "vorticity"));
|
|
358
|
+
this.#velocityIndex = 1 - this.#velocityIndex;
|
|
359
|
+
this.#dispatch(renderer, tupleValue(this.#divergence, this.#velocityIndex, "divergence"));
|
|
360
|
+
this.#dispatch(renderer, this.#pressureReset);
|
|
361
|
+
this.#pressureIndex = 0;
|
|
362
|
+
for (let iteration = 0; iteration < this.pressureIterations; iteration += 1) {
|
|
363
|
+
this.#dispatch(
|
|
364
|
+
renderer,
|
|
365
|
+
tupleValue(this.#pressureSolve, this.#pressureIndex, "pressure solve"),
|
|
366
|
+
);
|
|
367
|
+
this.#pressureIndex = 1 - this.#pressureIndex;
|
|
368
|
+
}
|
|
369
|
+
this.#dispatch(
|
|
370
|
+
renderer,
|
|
371
|
+
matrixValue(this.#gradient, this.#velocityIndex, this.#pressureIndex, "gradient"),
|
|
372
|
+
);
|
|
373
|
+
this.#velocityIndex = 1 - this.#velocityIndex;
|
|
374
|
+
this.#dispatch(
|
|
375
|
+
renderer,
|
|
376
|
+
tupleValue(this.#advectVelocity, this.#velocityIndex, "velocity advection"),
|
|
377
|
+
);
|
|
378
|
+
this.#velocityIndex = 1 - this.#velocityIndex;
|
|
379
|
+
this.#dispatch(
|
|
380
|
+
renderer,
|
|
381
|
+
matrixValue(this.#advectDye, this.#dyeIndex, this.#velocityIndex, "dye advection"),
|
|
382
|
+
);
|
|
383
|
+
this.#dyeIndex = 1 - this.#dyeIndex;
|
|
384
|
+
this.#velocitySampler.setTexture(
|
|
385
|
+
tupleValue(this.#velocityTextures, this.#velocityIndex, "velocity texture"),
|
|
386
|
+
);
|
|
387
|
+
this.#dyeSampler.setTexture(tupleValue(this.#dyeTextures, this.#dyeIndex, "dye texture"));
|
|
388
|
+
this.#steps += 1;
|
|
389
|
+
this.#splatsApplied += this.#splatCount.value;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
detach(): void {
|
|
393
|
+
if (this.#released) return;
|
|
394
|
+
this.#renderer = undefined;
|
|
395
|
+
for (const node of this.warmupNodes) node.dispose();
|
|
396
|
+
for (const texture of [
|
|
397
|
+
...this.#velocityTextures,
|
|
398
|
+
...this.#dyeTextures,
|
|
399
|
+
...this.#pressureTextures,
|
|
400
|
+
this.#curlTexture,
|
|
401
|
+
this.#divergenceTexture,
|
|
402
|
+
])
|
|
403
|
+
texture.dispose();
|
|
404
|
+
this.#released = true;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
#dispatch(renderer: IRendererLike, node: ComputeNode): void {
|
|
408
|
+
renderer.compute(node);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
#makeInitialPasses(): ComputeNode[] {
|
|
412
|
+
const textures = [
|
|
413
|
+
...this.#velocityTextures,
|
|
414
|
+
...this.#dyeTextures,
|
|
415
|
+
...this.#pressureTextures,
|
|
416
|
+
this.#curlTexture,
|
|
417
|
+
this.#divergenceTexture,
|
|
418
|
+
];
|
|
419
|
+
return textures.map((texture, index) =>
|
|
420
|
+
computeKernel(`fluid.init.${index}`, this.resolution, () =>
|
|
421
|
+
write(texture, this.resolution, vec4(0)),
|
|
422
|
+
),
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
#makeVelocitySplatPair(): ComputePair {
|
|
427
|
+
return this.#makePair("fluid.splat.velocity", this.#velocityTextures, (source, target) => {
|
|
428
|
+
const sourceNode = readFluidTexture(source);
|
|
429
|
+
const uv = pixelUv(this.resolution);
|
|
430
|
+
const pixel = sampleAt(sourceNode, uv, this.resolution).toVar();
|
|
431
|
+
Loop({ type: "uint", start: 0, end: this.#splatCount }, ({ i }) => {
|
|
432
|
+
const position = this.#splatPositions.element(i);
|
|
433
|
+
const impulse = this.#splatAmounts.element(i).x;
|
|
434
|
+
const influence = clamp(
|
|
435
|
+
float(1).sub(length(uv.sub(position.xy)).div(this.splatRadius)),
|
|
436
|
+
0,
|
|
437
|
+
1,
|
|
438
|
+
);
|
|
439
|
+
If(influence.greaterThan(0), () =>
|
|
440
|
+
pixel.assign(vec4(pixel.xy.add(position.zw.mul(impulse).mul(influence)), pixel.zw)),
|
|
441
|
+
);
|
|
442
|
+
});
|
|
443
|
+
return write(target, this.resolution, pixel);
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
#makeDyeSplatPair(): ComputePair {
|
|
448
|
+
return this.#makePair("fluid.splat.dye", this.#dyeTextures, (source, target) => {
|
|
449
|
+
const sourceNode = readFluidTexture(source);
|
|
450
|
+
const uv = pixelUv(this.resolution);
|
|
451
|
+
const pixel = sampleAt(sourceNode, uv, this.resolution).toVar();
|
|
452
|
+
Loop({ type: "uint", start: 0, end: this.#splatCount }, ({ i }) => {
|
|
453
|
+
const position = this.#splatPositions.element(i);
|
|
454
|
+
const amount = this.#splatAmounts.element(i).x;
|
|
455
|
+
const influence = clamp(
|
|
456
|
+
float(1).sub(length(uv.sub(position.xy)).div(this.splatRadius)),
|
|
457
|
+
0,
|
|
458
|
+
1,
|
|
459
|
+
);
|
|
460
|
+
If(influence.greaterThan(0), () => pixel.x.addAssign(amount.mul(influence)));
|
|
461
|
+
});
|
|
462
|
+
return write(target, this.resolution, pixel);
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
#makeCurlPair(): ComputePair {
|
|
467
|
+
return pair(
|
|
468
|
+
this.#makeCurlKernel("fluid.curl.a", this.#velocityTextures[0]),
|
|
469
|
+
this.#makeCurlKernel("fluid.curl.b", this.#velocityTextures[1]),
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
#makeCurlKernel(name: string, sourceTexture: StorageTexture): ComputeNode {
|
|
474
|
+
const source = readFluidTexture(sourceTexture);
|
|
475
|
+
return computeKernel(name, this.resolution, () => {
|
|
476
|
+
const uv = pixelUv(this.resolution);
|
|
477
|
+
const texel = vec2(1 / this.resolution);
|
|
478
|
+
const center = sampleAt(source, uv, this.resolution);
|
|
479
|
+
const l = neighbour(source, uv, texel, this.resolution, -1, 0).y;
|
|
480
|
+
const r = neighbour(source, uv, texel, this.resolution, 1, 0).y;
|
|
481
|
+
const b = neighbour(source, uv, texel, this.resolution, 0, -1).x;
|
|
482
|
+
const t = neighbour(source, uv, texel, this.resolution, 0, 1).x;
|
|
483
|
+
return write(
|
|
484
|
+
this.#curlTexture,
|
|
485
|
+
this.resolution,
|
|
486
|
+
vec4(0, 0, r.sub(l).sub(t).add(b).mul(0.5), center.w),
|
|
487
|
+
);
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
#makeVorticityPair(): ComputePair {
|
|
492
|
+
return this.#makePair("fluid.vorticity", this.#velocityTextures, (source, target) => {
|
|
493
|
+
const velocity = readFluidTexture(source);
|
|
494
|
+
const curl = readFluidTexture(this.#curlTexture);
|
|
495
|
+
const uv = pixelUv(this.resolution);
|
|
496
|
+
const texel = vec2(1 / this.resolution);
|
|
497
|
+
const center = sampleAt(velocity, uv, this.resolution);
|
|
498
|
+
const curlCenter = sampleAt(curl, uv, this.resolution).z;
|
|
499
|
+
const gradient = vec2(
|
|
500
|
+
abs(neighbour(curl, uv, texel, this.resolution, 0, 1).z).sub(
|
|
501
|
+
abs(neighbour(curl, uv, texel, this.resolution, 0, -1).z),
|
|
502
|
+
),
|
|
503
|
+
abs(neighbour(curl, uv, texel, this.resolution, 1, 0).z).sub(
|
|
504
|
+
abs(neighbour(curl, uv, texel, this.resolution, -1, 0).z),
|
|
505
|
+
),
|
|
506
|
+
);
|
|
507
|
+
const force = gradient.div(length(gradient).add(0.0001));
|
|
508
|
+
const [forceX, forceY] = mapVorticityForce(force, (value) => value.negate());
|
|
509
|
+
const next = center.xy.add(
|
|
510
|
+
vec2(forceX, forceY)
|
|
511
|
+
.mul(curlCenter)
|
|
512
|
+
.mul(this.vorticity * this.timeStep),
|
|
513
|
+
);
|
|
514
|
+
return write(target, this.resolution, vec4(next, center.zw));
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
#makeDivergence(): ComputePair {
|
|
519
|
+
return pair(
|
|
520
|
+
this.#makeDivergenceKernel("fluid.divergence.a", this.#velocityTextures[0]),
|
|
521
|
+
this.#makeDivergenceKernel("fluid.divergence.b", this.#velocityTextures[1]),
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
#makeDivergenceKernel(name: string, sourceTexture: StorageTexture): ComputeNode {
|
|
526
|
+
const source = readFluidTexture(sourceTexture);
|
|
527
|
+
return computeKernel(name, this.resolution, () => {
|
|
528
|
+
const uv = pixelUv(this.resolution);
|
|
529
|
+
const texel = vec2(1 / this.resolution);
|
|
530
|
+
const left = neighbour(source, uv, texel, this.resolution, -1, 0).x;
|
|
531
|
+
const right = neighbour(source, uv, texel, this.resolution, 1, 0).x;
|
|
532
|
+
const bottom = neighbour(source, uv, texel, this.resolution, 0, -1).y;
|
|
533
|
+
const top = neighbour(source, uv, texel, this.resolution, 0, 1).y;
|
|
534
|
+
return write(
|
|
535
|
+
this.#divergenceTexture,
|
|
536
|
+
this.resolution,
|
|
537
|
+
vec4(0, 0, 0, right.sub(left).add(top).sub(bottom).mul(0.5)),
|
|
538
|
+
);
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
#makePressureReset(): ComputeNode {
|
|
543
|
+
return computeKernel("fluid.pressure.reset", this.resolution, () =>
|
|
544
|
+
write(this.#pressureTextures[0], this.resolution, vec4(0)),
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
#makePressureSolvePair(): ComputePair {
|
|
549
|
+
const divergence = readFluidTexture(this.#divergenceTexture);
|
|
550
|
+
return pair(
|
|
551
|
+
this.#makePressureKernel(
|
|
552
|
+
"fluid.pressure.solve.a",
|
|
553
|
+
this.#pressureTextures[0],
|
|
554
|
+
this.#pressureTextures[1],
|
|
555
|
+
divergence,
|
|
556
|
+
),
|
|
557
|
+
this.#makePressureKernel(
|
|
558
|
+
"fluid.pressure.solve.b",
|
|
559
|
+
this.#pressureTextures[1],
|
|
560
|
+
this.#pressureTextures[0],
|
|
561
|
+
divergence,
|
|
562
|
+
),
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
#makePressureKernel(
|
|
567
|
+
name: string,
|
|
568
|
+
sourceTexture: StorageTexture,
|
|
569
|
+
targetTexture: StorageTexture,
|
|
570
|
+
divergence: StorageTextureNode,
|
|
571
|
+
): ComputeNode {
|
|
572
|
+
return computeKernel(name, this.resolution, () => {
|
|
573
|
+
const pressure = readFluidTexture(sourceTexture);
|
|
574
|
+
const uv = pixelUv(this.resolution);
|
|
575
|
+
const texel = vec2(1 / this.resolution);
|
|
576
|
+
const left = neighbour(pressure, uv, texel, this.resolution, -1, 0).x;
|
|
577
|
+
const right = neighbour(pressure, uv, texel, this.resolution, 1, 0).x;
|
|
578
|
+
const bottom = neighbour(pressure, uv, texel, this.resolution, 0, -1).x;
|
|
579
|
+
const top = neighbour(pressure, uv, texel, this.resolution, 0, 1).x;
|
|
580
|
+
const value = left
|
|
581
|
+
.add(right)
|
|
582
|
+
.add(bottom)
|
|
583
|
+
.add(top)
|
|
584
|
+
.sub(sampleAt(divergence, uv, this.resolution).w)
|
|
585
|
+
.mul(0.25);
|
|
586
|
+
return write(targetTexture, this.resolution, vec4(value, 0, 0, 0));
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
#makeGradientMatrix(): ComputeMatrix {
|
|
591
|
+
return [
|
|
592
|
+
[
|
|
593
|
+
this.#makeGradientKernel(
|
|
594
|
+
"fluid.gradient.a.a",
|
|
595
|
+
this.#velocityTextures[0],
|
|
596
|
+
this.#velocityTextures[1],
|
|
597
|
+
this.#pressureTextures[0],
|
|
598
|
+
),
|
|
599
|
+
this.#makeGradientKernel(
|
|
600
|
+
"fluid.gradient.a.b",
|
|
601
|
+
this.#velocityTextures[0],
|
|
602
|
+
this.#velocityTextures[1],
|
|
603
|
+
this.#pressureTextures[1],
|
|
604
|
+
),
|
|
605
|
+
],
|
|
606
|
+
[
|
|
607
|
+
this.#makeGradientKernel(
|
|
608
|
+
"fluid.gradient.b.a",
|
|
609
|
+
this.#velocityTextures[1],
|
|
610
|
+
this.#velocityTextures[0],
|
|
611
|
+
this.#pressureTextures[0],
|
|
612
|
+
),
|
|
613
|
+
this.#makeGradientKernel(
|
|
614
|
+
"fluid.gradient.b.b",
|
|
615
|
+
this.#velocityTextures[1],
|
|
616
|
+
this.#velocityTextures[0],
|
|
617
|
+
this.#pressureTextures[1],
|
|
618
|
+
),
|
|
619
|
+
],
|
|
620
|
+
];
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
#makeGradientKernel(
|
|
624
|
+
name: string,
|
|
625
|
+
sourceTexture: StorageTexture,
|
|
626
|
+
targetTexture: StorageTexture,
|
|
627
|
+
pressureTexture: StorageTexture,
|
|
628
|
+
): ComputeNode {
|
|
629
|
+
return computeKernel(name, this.resolution, () => {
|
|
630
|
+
const velocity = readFluidTexture(sourceTexture);
|
|
631
|
+
const pressure = readFluidTexture(pressureTexture);
|
|
632
|
+
const uv = pixelUv(this.resolution);
|
|
633
|
+
const texel = vec2(1 / this.resolution);
|
|
634
|
+
const center = sampleAt(velocity, uv, this.resolution);
|
|
635
|
+
const gradient = vec2(
|
|
636
|
+
neighbour(pressure, uv, texel, this.resolution, 1, 0).x.sub(
|
|
637
|
+
neighbour(pressure, uv, texel, this.resolution, -1, 0).x,
|
|
638
|
+
),
|
|
639
|
+
neighbour(pressure, uv, texel, this.resolution, 0, 1).x.sub(
|
|
640
|
+
neighbour(pressure, uv, texel, this.resolution, 0, -1).x,
|
|
641
|
+
),
|
|
642
|
+
).mul(0.5);
|
|
643
|
+
return write(targetTexture, this.resolution, vec4(center.xy.sub(gradient), center.zw));
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
#makeAdvectVelocityPair(): ComputePair {
|
|
648
|
+
return this.#makePair("fluid.advect.velocity", this.#velocityTextures, (source, target) => {
|
|
649
|
+
const velocity = readFluidTexture(source);
|
|
650
|
+
const uv = pixelUv(this.resolution);
|
|
651
|
+
const texel = vec2(1 / this.resolution);
|
|
652
|
+
const center = sampleAt(velocity, uv, this.resolution);
|
|
653
|
+
const traced = clamp(uv.sub(center.xy.mul(this.timeStep)), 0, 1);
|
|
654
|
+
const advected = sampleAt(velocity, traced, this.resolution).xy;
|
|
655
|
+
const laplacian = neighbour(velocity, uv, texel, this.resolution, -1, 0)
|
|
656
|
+
.xy.add(neighbour(velocity, uv, texel, this.resolution, 1, 0).xy)
|
|
657
|
+
.add(neighbour(velocity, uv, texel, this.resolution, 0, -1).xy)
|
|
658
|
+
.add(neighbour(velocity, uv, texel, this.resolution, 0, 1).xy)
|
|
659
|
+
.sub(center.xy.mul(4));
|
|
660
|
+
const next = advected.add(laplacian.mul(this.viscosity * this.timeStep));
|
|
661
|
+
return write(target, this.resolution, vec4(next, center.zw));
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
#makeAdvectDyeMatrix(): ComputeMatrix {
|
|
666
|
+
return [
|
|
667
|
+
[
|
|
668
|
+
this.#makeAdvectDyeKernel(
|
|
669
|
+
"fluid.advect.dye.a.a",
|
|
670
|
+
this.#dyeTextures[0],
|
|
671
|
+
this.#dyeTextures[1],
|
|
672
|
+
this.#velocityTextures[0],
|
|
673
|
+
),
|
|
674
|
+
this.#makeAdvectDyeKernel(
|
|
675
|
+
"fluid.advect.dye.a.b",
|
|
676
|
+
this.#dyeTextures[0],
|
|
677
|
+
this.#dyeTextures[1],
|
|
678
|
+
this.#velocityTextures[1],
|
|
679
|
+
),
|
|
680
|
+
],
|
|
681
|
+
[
|
|
682
|
+
this.#makeAdvectDyeKernel(
|
|
683
|
+
"fluid.advect.dye.b.a",
|
|
684
|
+
this.#dyeTextures[1],
|
|
685
|
+
this.#dyeTextures[0],
|
|
686
|
+
this.#velocityTextures[0],
|
|
687
|
+
),
|
|
688
|
+
this.#makeAdvectDyeKernel(
|
|
689
|
+
"fluid.advect.dye.b.b",
|
|
690
|
+
this.#dyeTextures[1],
|
|
691
|
+
this.#dyeTextures[0],
|
|
692
|
+
this.#velocityTextures[1],
|
|
693
|
+
),
|
|
694
|
+
],
|
|
695
|
+
];
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
#makeAdvectDyeKernel(
|
|
699
|
+
name: string,
|
|
700
|
+
sourceTexture: StorageTexture,
|
|
701
|
+
targetTexture: StorageTexture,
|
|
702
|
+
velocityTexture: StorageTexture,
|
|
703
|
+
): ComputeNode {
|
|
704
|
+
return computeKernel(name, this.resolution, () => {
|
|
705
|
+
const dye = readFluidTexture(sourceTexture);
|
|
706
|
+
const velocity = readFluidTexture(velocityTexture);
|
|
707
|
+
const uv = pixelUv(this.resolution);
|
|
708
|
+
const traced = clamp(
|
|
709
|
+
uv.sub(sampleAt(velocity, uv, this.resolution).xy.mul(this.timeStep)),
|
|
710
|
+
0,
|
|
711
|
+
1,
|
|
712
|
+
);
|
|
713
|
+
return write(targetTexture, this.resolution, sampleAt(dye, traced, this.resolution));
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
#makePair(
|
|
718
|
+
name: string,
|
|
719
|
+
textures: TexturePair,
|
|
720
|
+
callback: (source: StorageTexture, target: StorageTexture) => Node,
|
|
721
|
+
): ComputePair {
|
|
722
|
+
return pair(
|
|
723
|
+
computeKernel(`${name}.a`, this.resolution, () => callback(textures[0], textures[1])),
|
|
724
|
+
computeKernel(`${name}.b`, this.resolution, () => callback(textures[1], textures[0])),
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
#onRemoved = (): void => this.detach();
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
function clampNumber(value: number, min: number, max: number): number {
|
|
732
|
+
return Math.min(max, Math.max(min, value));
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
function tupleValue<T>(values: readonly T[], index: number, name: string): T {
|
|
736
|
+
const value = values[index];
|
|
737
|
+
if (value === undefined) throw new Error(`FluidField2D.${name} pass is missing.`);
|
|
738
|
+
return value;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function matrixValue<T>(
|
|
742
|
+
values: readonly (readonly T[])[],
|
|
743
|
+
row: number,
|
|
744
|
+
column: number,
|
|
745
|
+
name: string,
|
|
746
|
+
): T {
|
|
747
|
+
return tupleValue(tupleValue(values, row, `${name} row`), column, `${name} column`);
|
|
748
|
+
}
|