@null3d/wind 0.0.0-stage → 0.0.0
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/LICENSE-APACHE +176 -0
- package/LICENSE-MIT +21 -0
- package/README.md +84 -2
- package/dist/wind.wasm +0 -0
- package/lib/analytic.d.ts +130 -0
- package/lib/analytic.js +252 -0
- package/lib/field-layout.d.ts +34 -0
- package/lib/field-layout.js +47 -0
- package/lib/field-task.d.ts +14 -0
- package/lib/field-task.js +54 -0
- package/lib/field.d.ts +57 -0
- package/lib/field.js +140 -0
- package/lib/grid.fallback.wgsl-effect-glsl-0-c640d63bfe7fd852.js +24 -0
- package/lib/grid.fallback.wgsl-effect-wgsl-0-8a5918d6897a07b9.js +24 -0
- package/lib/grid.fallback.wgsl.d.ts +18 -0
- package/lib/grid.fallback.wgsl.js +32 -0
- package/lib/grid.wgsl.d.ts +6 -0
- package/lib/grid.wgsl.js +24 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +8 -0
- package/lib/namespace.d.ts +6 -0
- package/lib/namespace.js +13 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +3 -0
- package/lib/wind.d.ts +138 -0
- package/lib/wind.js +356 -0
- package/package.json +56 -5
- package/shaders/field.wgsl +190 -0
package/lib/wind.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { type Camera, type EngineCapabilities, type FeatureValue, type SketchContext, type Texture } from '@null3d/engine';
|
|
2
|
+
import { type FieldState, type WindReading } from './analytic.js';
|
|
3
|
+
/**
|
|
4
|
+
* What a page gives the wind, at the start or later.
|
|
5
|
+
*
|
|
6
|
+
* @category api/wind
|
|
7
|
+
*/
|
|
8
|
+
export interface WindOptions {
|
|
9
|
+
/** The camera whose ground place the box follows. The field is centered on it in every frame. */
|
|
10
|
+
camera: Camera;
|
|
11
|
+
/**
|
|
12
|
+
* The box's width across the ground, in meters. Zero keeps no grid, so every reader works the
|
|
13
|
+
* air out from the steady field.
|
|
14
|
+
*/
|
|
15
|
+
box?: number;
|
|
16
|
+
/**
|
|
17
|
+
* The grid's texels across each way. A page that gives it keeps it; otherwise the preset's
|
|
18
|
+
* `windGrid` row sets it, and 0 keeps no grid.
|
|
19
|
+
*/
|
|
20
|
+
grid?: number;
|
|
21
|
+
/** The air's settings: its speed, heading, gust, push and swirl. */
|
|
22
|
+
air?: Partial<FieldState>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The field that a reader samples: a texture of the air, and the box it covers.
|
|
26
|
+
*
|
|
27
|
+
* @category api/wind
|
|
28
|
+
*/
|
|
29
|
+
export interface WindField {
|
|
30
|
+
/**
|
|
31
|
+
* The texture. A reader declares it as `var wind: texture_2d<f32>;` and samples it through the
|
|
32
|
+
* sampling function, which gives the air back: the wind across the ground, the wind's rise,
|
|
33
|
+
* which is zero while the field is flat, and how fast the air moves. The grid keeps its state
|
|
34
|
+
* in the texture's own numbers, so a reader does not decode them itself. A preset change that
|
|
35
|
+
* changes the grid makes a new texture, so read this again after one.
|
|
36
|
+
*/
|
|
37
|
+
readonly texture: Texture;
|
|
38
|
+
/** The box's near corner across the ground, in meters: x and z. */
|
|
39
|
+
readonly origin: [number, number];
|
|
40
|
+
/**
|
|
41
|
+
* The box's width across the ground, in meters, or zero on a device that keeps no grid. A
|
|
42
|
+
* reader that is given zero works out the air from `air` everywhere. That is the steady field
|
|
43
|
+
* which the grid's edge and the far field hold where a device keeps one.
|
|
44
|
+
*/
|
|
45
|
+
readonly size: number;
|
|
46
|
+
/**
|
|
47
|
+
* The steady settings that a reader works out beyond the box. Give them to a material with the
|
|
48
|
+
* sampling function's three vectors, and give them again when `setBase` or the air changes.
|
|
49
|
+
*/
|
|
50
|
+
readonly air: WindReading;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The wind that a page reads and drives.
|
|
54
|
+
*
|
|
55
|
+
* @category api/wind
|
|
56
|
+
*/
|
|
57
|
+
export interface Wind {
|
|
58
|
+
/** The field that a plant, a flag or a smoke card samples. */
|
|
59
|
+
readonly field: WindField;
|
|
60
|
+
/** Sets the steady wind: its speed in meters a second and its heading in radians. */
|
|
61
|
+
setBase(speed: number, heading: number): void;
|
|
62
|
+
/**
|
|
63
|
+
* Sends a gust of `strength` from a ground place, whose wave then travels along the heading, so
|
|
64
|
+
* the air turns as it passes over the scene.
|
|
65
|
+
*/
|
|
66
|
+
gust(at: readonly [number, number], strength?: number): void;
|
|
67
|
+
/** Pushes the air about a ground place, as a body moving through it does. */
|
|
68
|
+
push(at: readonly [number, number], strength?: number): void;
|
|
69
|
+
/**
|
|
70
|
+
* The air at a world place, in meters a second, written into `out`, from the analytic field that
|
|
71
|
+
* the solver pulls the air toward and the pushes and the gust it injects. The solved air itself
|
|
72
|
+
* is on the GPU, so a page's own motion follows the forces rather than the wakes.
|
|
73
|
+
*
|
|
74
|
+
* This call works in TypeScript, which suits a test and a read outside a frame. A page that moves
|
|
75
|
+
* its own objects with the air in every frame asks `read` instead, whose work runs in a job
|
|
76
|
+
* worker.
|
|
77
|
+
*/
|
|
78
|
+
sample(x: number, y: number, z: number, out: [number, number, number]): [number, number, number];
|
|
79
|
+
/**
|
|
80
|
+
* The feature's settings, as the preset and the frame-budget governor resolve them now: the step
|
|
81
|
+
* interval, the pressure steps, the spring trees and the grid. The object stays the same and its
|
|
82
|
+
* values change in place, so read it in a frame without allocating.
|
|
83
|
+
*/
|
|
84
|
+
readonly settings: Readonly<Record<string, FeatureValue>>;
|
|
85
|
+
/**
|
|
86
|
+
* The field's budget, as the preset and the frame-budget governor leave it. The object stays the
|
|
87
|
+
* same, so read it in a frame without allocating.
|
|
88
|
+
*/
|
|
89
|
+
readonly budget: {
|
|
90
|
+
/** The time in ms that the field may take in a frame at this preset. */
|
|
91
|
+
readonly ms: number;
|
|
92
|
+
/** The share of full quality that the governor gives the field, from its floor to 1. */
|
|
93
|
+
readonly scale: number;
|
|
94
|
+
/** The time in ms that the field may take in a frame now. */
|
|
95
|
+
readonly allowedMs: number;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Asks a job worker for the air at each place in `points`, three numbers a place across the
|
|
99
|
+
* ground, and puts the velocities in `out`, three numbers a place. The answer lands before the
|
|
100
|
+
* next frame's update, so a page reads it in its next `onUpdate`. One call takes about five
|
|
101
|
+
* thousand places.
|
|
102
|
+
*/
|
|
103
|
+
read(points: Float32Array, out: Float32Array): void;
|
|
104
|
+
/**
|
|
105
|
+
* Places the box about the camera, runs the steps that the sketch time owes and sends the
|
|
106
|
+
* values to the GPU. Call once in each frame.
|
|
107
|
+
*/
|
|
108
|
+
update(): void;
|
|
109
|
+
/** Removes the pass and its texture. */
|
|
110
|
+
destroy(): void;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The divisor that keeps the ground's share below one while the pressure count fills the whole
|
|
114
|
+
* part of the same value. The grid multiplies the fraction by it to give the ground back, so the
|
|
115
|
+
* two must agree.
|
|
116
|
+
*/
|
|
117
|
+
export declare const GROUND_PACK = 100;
|
|
118
|
+
/** The Jacobi iterations that one cycle runs by default, before a preset applies. */
|
|
119
|
+
export declare const PRESSURE_STEPS = 20;
|
|
120
|
+
/** The runs that one cycle takes: the forces and the advection, the pressure, the projection. */
|
|
121
|
+
export declare const CYCLE_STEPS: number;
|
|
122
|
+
/** The fixed time that one cycle steps the air by, in seconds, before a preset applies. */
|
|
123
|
+
export declare const STEP_SECONDS: number;
|
|
124
|
+
/**
|
|
125
|
+
* True when the device draws into a 16-bit float texture, which the grid needs. Every WebGPU path
|
|
126
|
+
* does. A WebGL2 device keeps the grid only when the engine's own probe drew into `rgba16f` and read
|
|
127
|
+
* the pixels back, which `hdr` reports. A device that advertises the extensions but fails that probe
|
|
128
|
+
* keeps no grid, and its readers work the air out from the steady field instead, so a plant still
|
|
129
|
+
* moves there.
|
|
130
|
+
*/
|
|
131
|
+
export declare function holdsField(capabilities: Pick<EngineCapabilities, 'tier' | 'hdr'>): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Makes the air in a scene: the field, the pass that fills it, and the calls that a page uses to
|
|
134
|
+
* set the wind, send a gust and push the air.
|
|
135
|
+
*
|
|
136
|
+
* @category api/wind
|
|
137
|
+
*/
|
|
138
|
+
export declare function createWind(ctx: SketchContext, options: WindOptions): Wind;
|
package/lib/wind.js
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
// The wind add-on's field: one grid of air that follows the camera, filled by a compute pass on
|
|
2
|
+
// WebGPU and by its fragment form on WebGL2, and sampled by plants, flags and smoke with one
|
|
3
|
+
// texture read. A page makes the wind once, sets its base wind, sends gusts and pushes the air.
|
|
4
|
+
import { defineFeature, FeatureHost, } from '@null3d/engine';
|
|
5
|
+
import { analyticVelocity, createPushRing, createReading, defaultFieldState, RING_SLOTS, writeReading, } from './analytic.js';
|
|
6
|
+
import { WindProbes } from './field.js';
|
|
7
|
+
import { ASK_NUMBERS, ASK_PUSH_RADIUS, ASK_TIME, SLOT_FLOATS } from './field-layout.js';
|
|
8
|
+
import fallbackShader from './grid.fallback.wgsl.js';
|
|
9
|
+
import gridShader from './grid.wgsl.js';
|
|
10
|
+
import { NAMESPACE } from './namespace.js';
|
|
11
|
+
/** The presets in the order that a setting's row gives them. */
|
|
12
|
+
const PRESETS = ['low', 'medium', 'high', 'ultra'];
|
|
13
|
+
/** The time between field steps at each preset, in ms. Low keeps the analytic field. */
|
|
14
|
+
const STEP_MS = [0, 33, 33, 16];
|
|
15
|
+
/** The same on WebGL2, where a run costs far more. */
|
|
16
|
+
const STEP_MS_WEBGL2 = [0, 66, 66, 66];
|
|
17
|
+
/** The pressure iterations of one cycle at each preset. */
|
|
18
|
+
const PRESSURE_ROW = [0, 16, 24, 40];
|
|
19
|
+
/** The trees that keep branch springs at each preset. The vegetation add-on reads it. */
|
|
20
|
+
const SPRING_ROW = [0, 8, 32, 64];
|
|
21
|
+
/**
|
|
22
|
+
* The grid's texels across each way at each preset. Low keeps no grid and reads the steady field,
|
|
23
|
+
* so a phone pays nothing for the field. High and Ultra name the flat sizes that stand in for the
|
|
24
|
+
* layered and volume grids until the solver builds them.
|
|
25
|
+
*/
|
|
26
|
+
const GRID_ROW = [0, 64, 128, 256];
|
|
27
|
+
/** The field's own budget per frame at each preset, in ms, from M3-V14's figures. */
|
|
28
|
+
const BUDGET_MS = [0.5, 1.2, 4.9, 6.25];
|
|
29
|
+
/** The least share of full quality that the governor gives the field, so it always keeps moving. */
|
|
30
|
+
const BUDGET_MIN = 0.25;
|
|
31
|
+
/**
|
|
32
|
+
* The divisor that keeps the ground's share below one while the pressure count fills the whole
|
|
33
|
+
* part of the same value. The grid multiplies the fraction by it to give the ground back, so the
|
|
34
|
+
* two must agree.
|
|
35
|
+
*/
|
|
36
|
+
export const GROUND_PACK = 100;
|
|
37
|
+
/** The Jacobi iterations that one cycle runs by default, before a preset applies. */
|
|
38
|
+
export const PRESSURE_STEPS = 20;
|
|
39
|
+
/** The runs that one cycle takes: the forces and the advection, the pressure, the projection. */
|
|
40
|
+
export const CYCLE_STEPS = PRESSURE_STEPS + 2;
|
|
41
|
+
/** The fixed time that one cycle steps the air by, in seconds, before a preset applies. */
|
|
42
|
+
export const STEP_SECONDS = 1 / 60;
|
|
43
|
+
/**
|
|
44
|
+
* The most cycles that one frame runs, which is ten seconds of air. A longer stall in a page skips
|
|
45
|
+
* the air that it missed rather than spends one frame solving it.
|
|
46
|
+
*/
|
|
47
|
+
const MAX_CYCLES = 600;
|
|
48
|
+
/** How strong a gust is when the page names no strength. */
|
|
49
|
+
const GUST = 7;
|
|
50
|
+
/** How strong a push is when the page names no strength. */
|
|
51
|
+
const PUSH = 2.5;
|
|
52
|
+
/**
|
|
53
|
+
* True when the device draws into a 16-bit float texture, which the grid needs. Every WebGPU path
|
|
54
|
+
* does. A WebGL2 device keeps the grid only when the engine's own probe drew into `rgba16f` and read
|
|
55
|
+
* the pixels back, which `hdr` reports. A device that advertises the extensions but fails that probe
|
|
56
|
+
* keeps no grid, and its readers work the air out from the steady field instead, so a plant still
|
|
57
|
+
* moves there.
|
|
58
|
+
*/
|
|
59
|
+
export function holdsField(capabilities) {
|
|
60
|
+
return capabilities.tier !== 'webgl2' || capabilities.hdr;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Makes the air in a scene: the field, the pass that fills it, and the calls that a page uses to
|
|
64
|
+
* set the wind, send a gust and push the air.
|
|
65
|
+
*
|
|
66
|
+
* @category api/wind
|
|
67
|
+
*/
|
|
68
|
+
export function createWind(ctx, options) {
|
|
69
|
+
const { camera } = options;
|
|
70
|
+
const box = options.box ?? 128;
|
|
71
|
+
const air = { ...defaultFieldState(), ...options.air };
|
|
72
|
+
const pushes = createPushRing();
|
|
73
|
+
// The preset gives the field's step interval, its pressure count and its grid. A page's own
|
|
74
|
+
// grid wins over the preset's.
|
|
75
|
+
const at = PRESETS.indexOf(ctx.quality.preset);
|
|
76
|
+
const preset = at < 0 ? 1 : at;
|
|
77
|
+
let stepSeconds = ((ctx.engine.capabilities.tier === 'webgl2' ? STEP_MS_WEBGL2 : STEP_MS)[preset] ?? 33) / 1000;
|
|
78
|
+
let pressureSteps = PRESSURE_ROW[preset] ?? 16;
|
|
79
|
+
// A page that names a grid keeps it: it is the page's own, not the preset's.
|
|
80
|
+
const fixedGrid = options.grid !== undefined;
|
|
81
|
+
let grid = options.grid ?? GRID_ROW[preset] ?? 64;
|
|
82
|
+
let gridDirty = false;
|
|
83
|
+
// The solver runs one fixed step per cycle, and the pass takes eight value vectors: the step and
|
|
84
|
+
// the settings, the ring of pushes and gusts, and the box. A frame sets the runs that the sketch
|
|
85
|
+
// time owes, so the air keeps the same pace whatever the frame rate.
|
|
86
|
+
//
|
|
87
|
+
// The air starts when a page makes the wind. A held page's first drawn frame runs the air from
|
|
88
|
+
// then to the held time, so a held picture carries the air that the hold built. Each frame reads
|
|
89
|
+
// the sketch time from the engine's field, because a helper that answered it would box its
|
|
90
|
+
// fraction on a call that the browser does not inline.
|
|
91
|
+
let solved = ctx.time.now;
|
|
92
|
+
// The preset names no field at Low. A page that keeps its own grid there still gets a field, at
|
|
93
|
+
// the add-on's own step and pressure count, because air that never steps never moves.
|
|
94
|
+
const presetField = () => stepSeconds > 0;
|
|
95
|
+
const solverPressureSteps = () => (presetField() ? pressureSteps : PRESSURE_STEPS);
|
|
96
|
+
const cycleSteps = () => solverPressureSteps() + 2;
|
|
97
|
+
const owedSteps = () => {
|
|
98
|
+
// A held page steps its sketch many times and draws only the frames at the end, so the pass
|
|
99
|
+
// of a step before them never runs. The time stays owed until a frame that draws, which runs
|
|
100
|
+
// the air for the whole hold. A second drawn frame runs only the time since the first.
|
|
101
|
+
if (!ctx.time.drawn)
|
|
102
|
+
return 0;
|
|
103
|
+
const step = presetField() ? stepSeconds : STEP_SECONDS;
|
|
104
|
+
// The sketch time is a sum of frame times, so a frame that owes exactly one step can read a
|
|
105
|
+
// hair under it.
|
|
106
|
+
const owed = Math.floor((ctx.time.now - solved) / step + 1e-6);
|
|
107
|
+
if (owed <= 0)
|
|
108
|
+
return 0;
|
|
109
|
+
const cycles = Math.min(owed, MAX_CYCLES);
|
|
110
|
+
// A stall that owes more than one frame runs skips the air that it missed. Crediting only
|
|
111
|
+
// the cycles that ran would leave a debt, and every later frame would pay it.
|
|
112
|
+
solved = owed > MAX_CYCLES ? ctx.time.now : solved + cycles * step;
|
|
113
|
+
return cycles * cycleSteps();
|
|
114
|
+
};
|
|
115
|
+
const values = new Array(32).fill(0);
|
|
116
|
+
// The ring of pushes and the gust, in the order that the field's job worker reads it. A push
|
|
117
|
+
// writes its own slot where the page sends it, so a frame copies nothing across the ring.
|
|
118
|
+
const ringValues = new Float32Array(RING_SLOTS * SLOT_FLOATS);
|
|
119
|
+
// The push's width and the sketch time reach the ask in this array, whose numbers it reads for
|
|
120
|
+
// itself: a fraction passed as an argument becomes a number object where the browser does not
|
|
121
|
+
// inline the call.
|
|
122
|
+
const askNumbers = new Float64Array(ASK_NUMBERS);
|
|
123
|
+
const place = [0, 0, 0];
|
|
124
|
+
const write = () => {
|
|
125
|
+
// The first vector holds the solver's step, then the air's settings.
|
|
126
|
+
values[0] = presetField() ? stepSeconds : STEP_SECONDS;
|
|
127
|
+
// The ring's slots keep the numbers that the page's pushes gave them.
|
|
128
|
+
values[1] = air.speed;
|
|
129
|
+
values[2] = air.heading;
|
|
130
|
+
// A pass takes eight value vectors and every field of them already has a meaning, so the
|
|
131
|
+
// cycle's pressure count rides in the ground setting's whole part and the ground's share in
|
|
132
|
+
// its fraction. The grid splits the two before it uses them.
|
|
133
|
+
values[3] = solverPressureSteps() + air.ground / GROUND_PACK;
|
|
134
|
+
values[4] = air.gustStrength;
|
|
135
|
+
values[5] = air.gustRadius;
|
|
136
|
+
values[6] = air.gustWave;
|
|
137
|
+
values[7] = air.gustSpeed;
|
|
138
|
+
values[8] = air.swirl;
|
|
139
|
+
values[9] = air.swirlScale;
|
|
140
|
+
values[10] = air.swirlDrift;
|
|
141
|
+
values[11] = air.seed;
|
|
142
|
+
camera.getPosition(place);
|
|
143
|
+
values[28] = place[0] - box / 2;
|
|
144
|
+
values[29] = place[2] - box / 2;
|
|
145
|
+
values[30] = box;
|
|
146
|
+
values[31] = air.pushRadius;
|
|
147
|
+
};
|
|
148
|
+
// A device without float targets keeps no grid, so it costs no pass. A reader still samples a
|
|
149
|
+
// texture, which holds the field's neutral black, but its box has no width, so every sample is
|
|
150
|
+
// the steady field that `shaders/field.wgsl` works out. A box of no width asks for the same.
|
|
151
|
+
const holds = box > 0 && holdsField(ctx.engine.capabilities);
|
|
152
|
+
const neutral = ctx.textures.fromData({
|
|
153
|
+
width: 1,
|
|
154
|
+
height: 1,
|
|
155
|
+
depth: 1,
|
|
156
|
+
data: new Uint8Array(4),
|
|
157
|
+
});
|
|
158
|
+
/** The texture of the grid that the readers sample now. */
|
|
159
|
+
let gridTexture;
|
|
160
|
+
// Makes the grid's pass at the setting's size, or keeps no grid. The pass keeps two states, so a
|
|
161
|
+
// reader samples the one that the last run wrote.
|
|
162
|
+
const makePass = () => {
|
|
163
|
+
gridTexture = undefined;
|
|
164
|
+
if (!holds || grid <= 0)
|
|
165
|
+
return undefined;
|
|
166
|
+
const created = ctx.render.addPass({
|
|
167
|
+
kind: 'compute',
|
|
168
|
+
description: defineFeature({
|
|
169
|
+
name: 'wind',
|
|
170
|
+
frame: 'background',
|
|
171
|
+
kind: 'compute',
|
|
172
|
+
outputs: { wind: { default: 'black' } },
|
|
173
|
+
}),
|
|
174
|
+
wgsl: gridShader,
|
|
175
|
+
fallback: { wgsl: fallbackShader },
|
|
176
|
+
format: 'rgba16float',
|
|
177
|
+
size: [grid, grid],
|
|
178
|
+
history: true,
|
|
179
|
+
steps: cycleSteps(),
|
|
180
|
+
values,
|
|
181
|
+
name: 'wind-field',
|
|
182
|
+
});
|
|
183
|
+
gridTexture = ctx.textures.fromPass(created);
|
|
184
|
+
return created;
|
|
185
|
+
};
|
|
186
|
+
let pass = makePass();
|
|
187
|
+
const field = {
|
|
188
|
+
get texture() {
|
|
189
|
+
return gridTexture ?? neutral;
|
|
190
|
+
},
|
|
191
|
+
origin: [0, 0],
|
|
192
|
+
get size() {
|
|
193
|
+
return holds && grid > 0 ? box : 0;
|
|
194
|
+
},
|
|
195
|
+
air: createReading(air),
|
|
196
|
+
};
|
|
197
|
+
// The feature's settings reach the solver here. A change of grid needs a pass of another size,
|
|
198
|
+
// which the frame loop makes so a preview of the change never shows a half-made pass.
|
|
199
|
+
const applySettings = (own) => {
|
|
200
|
+
stepSeconds = own.windStepMs / 1000;
|
|
201
|
+
pressureSteps = own.windPressureSteps;
|
|
202
|
+
const next = fixedGrid ? grid : own.windGrid;
|
|
203
|
+
if (next !== grid) {
|
|
204
|
+
grid = next;
|
|
205
|
+
gridDirty = true;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
const describe = () => defineFeature({
|
|
209
|
+
name: 'wind',
|
|
210
|
+
gpu: pass ? { passes: [pass] } : undefined,
|
|
211
|
+
settings: {
|
|
212
|
+
windStepMs: {
|
|
213
|
+
presets: STEP_MS,
|
|
214
|
+
webgl2: STEP_MS_WEBGL2,
|
|
215
|
+
values: { min: 0, max: 133, whole: true },
|
|
216
|
+
governor: 'double',
|
|
217
|
+
},
|
|
218
|
+
windPressureSteps: {
|
|
219
|
+
presets: PRESSURE_ROW,
|
|
220
|
+
values: { min: 0, max: 80, whole: true },
|
|
221
|
+
governor: 'scale',
|
|
222
|
+
},
|
|
223
|
+
windSpringTrees: {
|
|
224
|
+
presets: SPRING_ROW,
|
|
225
|
+
values: { min: 0, max: 128, whole: true },
|
|
226
|
+
governor: 'scale',
|
|
227
|
+
},
|
|
228
|
+
windGrid: {
|
|
229
|
+
// A new size needs a new pass, so a preset change alone moves this row.
|
|
230
|
+
presets: GRID_ROW,
|
|
231
|
+
values: GRID_ROW,
|
|
232
|
+
governor: 'none',
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
budget: { presets: BUDGET_MS, min: BUDGET_MIN },
|
|
236
|
+
memory: () => (pass ? grid * grid * 2 * 8 : 0),
|
|
237
|
+
quality: (_preset, _settings, own) => applySettings(own.settings),
|
|
238
|
+
});
|
|
239
|
+
const host = new FeatureHost({
|
|
240
|
+
quality: ctx.quality,
|
|
241
|
+
tier: ctx.engine.capabilities.tier,
|
|
242
|
+
page: ctx.page,
|
|
243
|
+
namespace: NAMESPACE,
|
|
244
|
+
});
|
|
245
|
+
let feature = host.add(describe());
|
|
246
|
+
// A preset that changes the grid makes the pass again at the new size. The state starts empty,
|
|
247
|
+
// so the new grid fills from the analytic field over its first cycles.
|
|
248
|
+
const rebuild = () => {
|
|
249
|
+
gridDirty = false;
|
|
250
|
+
const old = pass;
|
|
251
|
+
if (old)
|
|
252
|
+
ctx.render.removePass(old);
|
|
253
|
+
pass = makePass();
|
|
254
|
+
solved = ctx.time.now;
|
|
255
|
+
host.remove('wind');
|
|
256
|
+
feature = host.add(describe());
|
|
257
|
+
feature.refresh();
|
|
258
|
+
};
|
|
259
|
+
// The box follows the camera and the pushes fade, so the values go up in every frame.
|
|
260
|
+
const apply = () => {
|
|
261
|
+
if (gridDirty)
|
|
262
|
+
rebuild();
|
|
263
|
+
write();
|
|
264
|
+
if (pass) {
|
|
265
|
+
ctx.render.setPassSteps(pass, owedSteps());
|
|
266
|
+
ctx.render.setPassUniforms(pass, values);
|
|
267
|
+
}
|
|
268
|
+
field.origin[0] = values[28];
|
|
269
|
+
field.origin[1] = values[29];
|
|
270
|
+
};
|
|
271
|
+
apply();
|
|
272
|
+
const sample = (x, y, z, out) => analyticVelocity(air, pushes.slots, x, y, z, ctx.time.now, out);
|
|
273
|
+
const probes = new WindProbes();
|
|
274
|
+
// A push takes the next of the three push slots, and a gust takes the slot after them, so a
|
|
275
|
+
// page's pushes never push its gust out. A gust travels from where it was sent, and a push
|
|
276
|
+
// stays there. The frame writes the slot's numbers here rather than passing them into a call,
|
|
277
|
+
// where the browser would box each fraction; they go into the pass's values and into the ring
|
|
278
|
+
// that a job worker reads, so a push copies no slot.
|
|
279
|
+
const turn = (at, strength, gust) => {
|
|
280
|
+
const born = ctx.time.now;
|
|
281
|
+
const index = pushes.take(gust);
|
|
282
|
+
const x = at[0];
|
|
283
|
+
const z = at[1];
|
|
284
|
+
const push = pushes.slots[index];
|
|
285
|
+
push.x = x;
|
|
286
|
+
push.z = z;
|
|
287
|
+
push.strength = strength;
|
|
288
|
+
push.born = born;
|
|
289
|
+
const slot = index * SLOT_FLOATS;
|
|
290
|
+
ringValues[slot] = x;
|
|
291
|
+
ringValues[slot + 1] = z;
|
|
292
|
+
ringValues[slot + 2] = strength;
|
|
293
|
+
ringValues[slot + 3] = born;
|
|
294
|
+
// The ring follows the pass's first twelve numbers, which are the step and the air's
|
|
295
|
+
// settings.
|
|
296
|
+
const into = 12 + slot;
|
|
297
|
+
values[into] = x;
|
|
298
|
+
values[into + 1] = z;
|
|
299
|
+
values[into + 2] = strength;
|
|
300
|
+
values[into + 3] = born;
|
|
301
|
+
// The steady field carries the sent gust too, so a reader that works the air out with no grid
|
|
302
|
+
// sees it travel from where it was sent.
|
|
303
|
+
if (gust) {
|
|
304
|
+
const sent = field.air.sent;
|
|
305
|
+
sent[0] = x;
|
|
306
|
+
sent[1] = z;
|
|
307
|
+
sent[2] = strength;
|
|
308
|
+
sent[3] = born;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
// The handle keeps its budget current, and its figures carry the full time that the preset
|
|
312
|
+
// gave, so the page reads both without asking the engine again.
|
|
313
|
+
const budget = {
|
|
314
|
+
get ms() {
|
|
315
|
+
return feature.stats.budgetMs ?? 0;
|
|
316
|
+
},
|
|
317
|
+
get scale() {
|
|
318
|
+
return feature.budgetScale;
|
|
319
|
+
},
|
|
320
|
+
get allowedMs() {
|
|
321
|
+
return feature.stats.allowedMs ?? 0;
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
return {
|
|
325
|
+
field,
|
|
326
|
+
get settings() {
|
|
327
|
+
return feature.settings;
|
|
328
|
+
},
|
|
329
|
+
budget,
|
|
330
|
+
setBase(speed, heading) {
|
|
331
|
+
air.speed = speed;
|
|
332
|
+
air.heading = heading;
|
|
333
|
+
writeReading(air, field.air);
|
|
334
|
+
},
|
|
335
|
+
gust(at, strength = GUST) {
|
|
336
|
+
turn(at, strength, true);
|
|
337
|
+
},
|
|
338
|
+
push(at, strength = PUSH) {
|
|
339
|
+
turn(at, strength, false);
|
|
340
|
+
},
|
|
341
|
+
sample,
|
|
342
|
+
read(points, out) {
|
|
343
|
+
askNumbers[ASK_PUSH_RADIUS] = air.pushRadius;
|
|
344
|
+
askNumbers[ASK_TIME] = ctx.time.now;
|
|
345
|
+
probes.ask(points, out, field.air, ringValues, askNumbers);
|
|
346
|
+
},
|
|
347
|
+
update: apply,
|
|
348
|
+
destroy() {
|
|
349
|
+
probes.stop();
|
|
350
|
+
host.destroy();
|
|
351
|
+
if (pass)
|
|
352
|
+
ctx.render.removePass(pass);
|
|
353
|
+
neutral.destroy();
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@null3d/wind",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Wind for null3D sketches: one field of air that plants, flags, smoke and water read, from an analytic wind of place and time with traveling gusts and pushes, solved on a grid that follows the camera, built on the engine's public API.",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"author": "Ramesh Nair (https://hiddentao.com)",
|
|
7
|
+
"homepage": "https://github.com/null3d-engine/null3d#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/null3d-engine/null3d.git",
|
|
11
|
+
"directory": "packages/wind"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/null3d-engine/null3d/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"null3d",
|
|
18
|
+
"3d",
|
|
19
|
+
"wind",
|
|
20
|
+
"gust",
|
|
21
|
+
"fluid",
|
|
22
|
+
"air",
|
|
23
|
+
"webgpu",
|
|
24
|
+
"webgl2"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"null3d-source": "./src/index.ts",
|
|
30
|
+
"types": "./lib/index.d.ts",
|
|
31
|
+
"default": "./lib/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"lib",
|
|
37
|
+
"dist",
|
|
38
|
+
"shaders",
|
|
39
|
+
"LICENSE-MIT",
|
|
40
|
+
"LICENSE-APACHE"
|
|
41
|
+
],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"provenance": true
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"prepack": "bun ../../tools/build-wind-wasm.ts && bun ../../tools/build-package.ts wind"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@null3d/engine": "0.0.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@null3d/engine": "0.0.0",
|
|
54
|
+
"@null3d/vite-plugin": "0.0.0",
|
|
55
|
+
"@webgpu/types": "0.1.74"
|
|
56
|
+
}
|
|
57
|
+
}
|