@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
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// The one sampling function for a reader of the wind field. A material that already has its own
|
|
2
|
+
// WGSL, such as a plant's leaf material, copies both blocks between their markers into its own
|
|
3
|
+
// file. The shader build resolves only the engine's library, so a shader cannot import another
|
|
4
|
+
// shader of the add-on; a test compares each copy with this one and fails when one is edited alone.
|
|
5
|
+
//
|
|
6
|
+
// A reader declares the field's texture, and the engine declares its sampler beside it. The pass
|
|
7
|
+
// keeps two states, so the texture is an array: `layer` names the first layer of the state that the
|
|
8
|
+
// last run wrote, which `field.texture.layer` gives each frame.
|
|
9
|
+
var wind: texture_2d_array<f32>;
|
|
10
|
+
|
|
11
|
+
// <wind-steady>
|
|
12
|
+
/// The height over which the air slows to the ground, in meters.
|
|
13
|
+
const WIND_GROUND_HEIGHT: f32 = 2.0;
|
|
14
|
+
/// The distance over which the noise's curl is measured, in noise cells.
|
|
15
|
+
const WIND_CURL_STEP: f32 = 0.02;
|
|
16
|
+
/// How long a gust that a page sends keeps its strength, in seconds, before the air is steady again.
|
|
17
|
+
const WIND_SENT_LIFE: f32 = 8.0;
|
|
18
|
+
/// The height that the grid samples at, where the air moves at its full speed. The grid stores the
|
|
19
|
+
/// air at this height, and a reader outside the grid's box works it out at the same height, so the
|
|
20
|
+
/// two meet at the box's edge.
|
|
21
|
+
const WIND_FULL_HEIGHT: f32 = 100.0;
|
|
22
|
+
|
|
23
|
+
/// The air's settings: its rule of place and time, the preset's gust and the swirl, and the gust
|
|
24
|
+
/// that a page sends. The grid's pass takes the first three vectors from its values and solves a
|
|
25
|
+
/// sent gust itself; a reader takes all four from its own settings.
|
|
26
|
+
struct WindAir {
|
|
27
|
+
/// The time in seconds, the wind's speed and its heading in radians, and how much slower the
|
|
28
|
+
/// air is at the ground.
|
|
29
|
+
time: f32,
|
|
30
|
+
speed: f32,
|
|
31
|
+
heading: f32,
|
|
32
|
+
ground: f32,
|
|
33
|
+
/// The gust's strength, its radius in meters, its wave number and how fast it travels.
|
|
34
|
+
gustStrength: f32,
|
|
35
|
+
gustRadius: f32,
|
|
36
|
+
gustWave: f32,
|
|
37
|
+
gustSpeed: f32,
|
|
38
|
+
/// The swirl's strength, its size, how fast its pattern drifts and its seed.
|
|
39
|
+
swirl: f32,
|
|
40
|
+
swirlScale: f32,
|
|
41
|
+
swirlDrift: f32,
|
|
42
|
+
seed: f32,
|
|
43
|
+
/// The gust a page sent: its ground place in x and z, its strength in meters a second, and the
|
|
44
|
+
/// time it was sent. Its strength is zero until a page sends one, and after it fades.
|
|
45
|
+
sentX: f32,
|
|
46
|
+
sentZ: f32,
|
|
47
|
+
sentStrength: f32,
|
|
48
|
+
sentBorn: f32,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// The field's settings read from three value vectors: the air, the gust and the swirl.
|
|
52
|
+
fn wind_air(v0: vec4f, v1: vec4f, v2: vec4f) -> WindAir {
|
|
53
|
+
var air: WindAir;
|
|
54
|
+
air.time = v0.x;
|
|
55
|
+
air.speed = v0.y;
|
|
56
|
+
air.heading = v0.z;
|
|
57
|
+
air.ground = v0.w;
|
|
58
|
+
air.gustStrength = v1.x;
|
|
59
|
+
air.gustRadius = v1.y;
|
|
60
|
+
air.gustWave = v1.z;
|
|
61
|
+
air.gustSpeed = v1.w;
|
|
62
|
+
air.swirl = v2.x;
|
|
63
|
+
air.swirlScale = v2.y;
|
|
64
|
+
air.swirlDrift = v2.z;
|
|
65
|
+
air.seed = v2.w;
|
|
66
|
+
// The grid solves a gust that a page sends itself, from the ring of pushes and gusts, so its
|
|
67
|
+
// readers hold no sent gust here.
|
|
68
|
+
air.sentX = 0.0;
|
|
69
|
+
air.sentZ = 0.0;
|
|
70
|
+
air.sentStrength = 0.0;
|
|
71
|
+
air.sentBorn = 0.0;
|
|
72
|
+
return air;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// The height's factor of the wind: one at the top, and one less the ground's share at the ground.
|
|
76
|
+
fn wind_profile(air: WindAir, y: f32) -> f32 {
|
|
77
|
+
return 1.0 - air.ground * exp(-max(y, 0.0) / WIND_GROUND_HEIGHT);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/// The stream function of the swirl, in noise cells, at a place and a time.
|
|
81
|
+
fn wind_stream(air: WindAir, p: vec3f) -> f32 {
|
|
82
|
+
let drift = air.swirlDrift * air.time;
|
|
83
|
+
let q = vec3f(
|
|
84
|
+
p.x * air.swirlScale + drift + air.seed,
|
|
85
|
+
p.y * air.swirlScale,
|
|
86
|
+
p.z * air.swirlScale + drift * 0.5,
|
|
87
|
+
);
|
|
88
|
+
return value3(q);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// The turn that one gust gives the air at a place, in meters a second: a wave of turns that travels
|
|
92
|
+
/// along the heading. The turns are rings about the gust's center and depend only on the distance to
|
|
93
|
+
/// it, so a ring of air turns without piling up.
|
|
94
|
+
fn wind_gust_wave(air: WindAir, center: vec2f, strength: f32, p: vec3f, height: f32) -> vec3f {
|
|
95
|
+
let g = vec2f(p.x - center.x, p.z - center.y);
|
|
96
|
+
let distance = length(g) / air.gustRadius;
|
|
97
|
+
let reach = distance * distance;
|
|
98
|
+
if (reach >= 9.0) {
|
|
99
|
+
return vec3f(0.0);
|
|
100
|
+
}
|
|
101
|
+
let envelope = exp(-reach) * cos(distance * air.gustWave) * strength * height;
|
|
102
|
+
return vec3f((g.y / air.gustRadius) * envelope, 0.0, -(g.x / air.gustRadius) * envelope);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// The wind's steady velocity at a world place, in meters a second: the base wind, the gust that
|
|
106
|
+
/// travels through it, the swirl, and the gust that a page sent, which travels from its own place.
|
|
107
|
+
/// A page's pushes reach readers only through the grid.
|
|
108
|
+
fn wind_steady(air: WindAir, p: vec3f) -> vec3f {
|
|
109
|
+
let height = wind_profile(air, p.y);
|
|
110
|
+
let cosHeading = cos(air.heading);
|
|
111
|
+
let sinHeading = sin(air.heading);
|
|
112
|
+
var v = vec3f(cosHeading * air.speed * height, 0.0, sinHeading * air.speed * height);
|
|
113
|
+
|
|
114
|
+
let travel = air.gustSpeed * air.time;
|
|
115
|
+
v += wind_gust_wave(air, vec2f(cosHeading * travel, sinHeading * travel), air.gustStrength, p, height);
|
|
116
|
+
|
|
117
|
+
// The gust that a page sent: the same wave, from the place it was sent, carried along the
|
|
118
|
+
// heading and fading over its life so the air comes back to its steady state.
|
|
119
|
+
if (air.sentStrength != 0.0) {
|
|
120
|
+
let age = air.time - air.sentBorn;
|
|
121
|
+
if (age >= 0.0 && age < WIND_SENT_LIFE) {
|
|
122
|
+
let sentTravel = air.gustSpeed * age;
|
|
123
|
+
let wane = 1.0 - age / WIND_SENT_LIFE;
|
|
124
|
+
v += wind_gust_wave(
|
|
125
|
+
air,
|
|
126
|
+
vec2f(air.sentX + cosHeading * sentTravel, air.sentZ + sinHeading * sentTravel),
|
|
127
|
+
air.sentStrength * wane,
|
|
128
|
+
p,
|
|
129
|
+
height,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// The swirl: the curl of one stream function in the ground plane. The function varies slowly,
|
|
135
|
+
// so a short step measures the slope.
|
|
136
|
+
let step = WIND_CURL_STEP / air.swirlScale;
|
|
137
|
+
let front = wind_stream(air, p + vec3f(0.0, 0.0, step));
|
|
138
|
+
let back = wind_stream(air, p - vec3f(0.0, 0.0, step));
|
|
139
|
+
let right = wind_stream(air, p + vec3f(step, 0.0, 0.0));
|
|
140
|
+
let left = wind_stream(air, p - vec3f(step, 0.0, 0.0));
|
|
141
|
+
let scale = air.swirl * height / (2.0 * step);
|
|
142
|
+
v.x += (front - back) * scale;
|
|
143
|
+
v.z -= (right - left) * scale;
|
|
144
|
+
return v;
|
|
145
|
+
}
|
|
146
|
+
// </wind-steady>
|
|
147
|
+
|
|
148
|
+
// <wind-sample>
|
|
149
|
+
/// The air's steady settings as a reader holds them: the base wind in the first vector, the gust in
|
|
150
|
+
/// the second, the swirl in the third and the gust that a page sent in the fourth, with the time
|
|
151
|
+
/// that the reader's own frame gives.
|
|
152
|
+
fn wind_reading(base: vec4f, gust: vec4f, swirl: vec4f, sent: vec4f, time: f32) -> WindAir {
|
|
153
|
+
var air: WindAir;
|
|
154
|
+
air.time = time;
|
|
155
|
+
air.speed = base.x;
|
|
156
|
+
air.heading = base.y;
|
|
157
|
+
air.ground = base.z;
|
|
158
|
+
air.seed = base.w;
|
|
159
|
+
air.gustStrength = gust.x;
|
|
160
|
+
air.gustRadius = gust.y;
|
|
161
|
+
air.gustWave = gust.z;
|
|
162
|
+
air.gustSpeed = gust.w;
|
|
163
|
+
air.swirl = swirl.x;
|
|
164
|
+
air.swirlScale = swirl.y;
|
|
165
|
+
air.swirlDrift = swirl.z;
|
|
166
|
+
air.sentX = sent.x;
|
|
167
|
+
air.sentZ = sent.y;
|
|
168
|
+
air.sentStrength = sent.z;
|
|
169
|
+
air.sentBorn = sent.w;
|
|
170
|
+
return air;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// The air at a world place: the wind across the ground in red and blue, the wind's rise in green,
|
|
174
|
+
/// and its speed in alpha, at the height the field's texture holds it. Inside the box the grid's
|
|
175
|
+
/// state gives it, with one read. Outside the box, and on a device that keeps no grid, the steady
|
|
176
|
+
/// field gives it, so the air carries on past the box instead of holding the box's edge.
|
|
177
|
+
fn wind_sample(world: vec3f, origin: vec2f, size: f32, layer: i32, air: WindAir) -> vec4f {
|
|
178
|
+
if (size > 0.0) {
|
|
179
|
+
let uv = (vec2f(world.x, world.z) - origin) / size;
|
|
180
|
+
if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0) {
|
|
181
|
+
// The grid keeps the air across the ground in red and green, the pressure in blue and
|
|
182
|
+
// the step of its cycle in alpha.
|
|
183
|
+
let state = textureSampleLevel(wind, windSampler, uv, layer, 0.0);
|
|
184
|
+
return vec4f(state.r, 0.0, state.g, length(state.rg));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
let v = wind_steady(air, vec3f(world.x, WIND_FULL_HEIGHT, world.z));
|
|
188
|
+
return vec4f(v, length(v));
|
|
189
|
+
}
|
|
190
|
+
// </wind-sample>
|