@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 ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
package/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ramesh Nair
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,85 @@
1
- # Temporary Holding Version
1
+ # Wind for null3D
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `@null3d/wind` makes the air in a null3D scene. One field moves every plant, flag and smoke card
4
+ that reads it, so a gust that crosses a field reaches all of them together. The air is solved: a
5
+ grid that follows the camera carries it from frame to frame. A gust therefore bends through the
6
+ scene, and a body leaves a wake that outlives it. A steady wind slows near the ground, and swirls
7
+ turn without piling up.
8
+
9
+ ## Use
10
+
11
+ ```ts
12
+ import { defineSketch } from '@null3d/engine';
13
+ import { createWind } from '@null3d/wind';
14
+
15
+ export default defineSketch((ctx) => {
16
+ const { scene } = ctx;
17
+ const camera = scene.createPerspectiveCamera({ position: [0, 6, 18], target: [0, 1, 0] });
18
+ scene.setActiveCamera(camera);
19
+
20
+ const wind = createWind(ctx, { camera, box: 128 });
21
+ wind.setBase(4, 0.4);
22
+
23
+ const leaf = ctx.materials.shader({
24
+ wgsl: leafShader,
25
+ textures: { wind: wind.field.texture },
26
+ uniforms: {
27
+ origin: wind.field.origin,
28
+ size: wind.field.size,
29
+ layer: wind.field.texture.layer,
30
+ base: wind.field.air.base,
31
+ gust: wind.field.air.gust,
32
+ swirl: wind.field.air.swirl,
33
+ },
34
+ });
35
+
36
+ let nextGust = 0;
37
+ return {
38
+ onUpdate() {
39
+ // A gust every four seconds, sent where the air enters the scene.
40
+ if (ctx.time.now > nextGust) {
41
+ wind.gust([-40, 6], 6);
42
+ nextGust = ctx.time.now + 4;
43
+ }
44
+ wind.update();
45
+ },
46
+ };
47
+ });
48
+ ```
49
+
50
+ Call `update` once in each frame. The add-on makes no object of its own; a sketch that pushes every frame keeps one two-number array for the place. Until the browser has optimized the add-on's functions, a fraction that crosses one of its calls becomes an object, as in the engine's own steps.
51
+
52
+ ## The field
53
+
54
+ The field is one `rgba16float` texture over a box of ground that follows the camera. One pass keeps
55
+ two states of it: the air of the frame before, and the air it steps toward. The preset sets the
56
+ time between steps, the pressure iterations of a cycle and the grid's size. The frame-budget
57
+ governor lightens them when frames take too long. A wake therefore lasts after the body that made
58
+ it has gone.
59
+
60
+ A reader samples it with one texture read at each vertex. The shader build resolves only the
61
+ engine's library, so a reader's WGSL holds copies of two blocks: the steady field and the sampling
62
+ function. They are in `shaders/field.wgsl` and on the `api/wind` docs page. A test keeps every copy
63
+ equal to the one in the package.
64
+
65
+ `read(points, out)` gives the air at many places, three numbers a place, worked out by the add-on's
66
+ WebAssembly in a job worker. Use it in a frame, which is what moves a sketch's own objects with the
67
+ air. The answer lands before the next frame's update. A call that arrives while another waits for
68
+ its answer replaces that ask: the newest ask wins, and the replaced one's array keeps its numbers.
69
+ `sample(x, y, z, out)` gives the analytic air
70
+ on the CPU, which suits a test and a read outside a frame. The solved air is on the GPU.
71
+
72
+ ## Costs and limits
73
+
74
+ - The pass fills one texture of `grid` by `grid` texels in each frame, and a reader spends one
75
+ texture slot on it. A page that gives `grid` keeps it; otherwise the preset's row sets it.
76
+ - The pass holds two states, so it costs twice the texture's memory, and one cycle takes the
77
+ preset's pressure steps and two more runs.
78
+ - A device without float render targets keeps no grid. There `field.size` is zero, so a reader
79
+ works the air out from the steady field and a plant still moves.
80
+ - A place outside the box reads the steady field at its own place, at the height that the texture
81
+ holds. So the air carries on past the box, instead of holding the box's edge. A page's pushes and
82
+ a gust that the page sends turn the air inside the box only.
83
+
84
+ `@null3d/wind` owns the wind. A scene makes its air with it, not with a hand-written field of its
85
+ own, so there is one wind for every reader.
package/dist/wind.wasm ADDED
Binary file
@@ -0,0 +1,130 @@
1
+ /**
2
+ * The first value of `null3d::noise`'s `pcg3d` hash, as a signed whole number. The library returns
3
+ * three, but value noise reads only the first, and this form makes no tuple and no closure.
4
+ *
5
+ * Each step keeps its number in the 32 bits that a whole number holds. The hash is the same 32 bits
6
+ * either way, and the top 24 come out the same. Read as unsigned, about half of them pass the
7
+ * largest number that the browser keeps in the value itself, and it makes an object of each such
8
+ * number it returns. So the steps wrap with `| 0` rather than `>>> 0`.
9
+ */
10
+ export declare function lattice(x: number, y: number, z: number): number;
11
+ /** `null3d::noise`'s value noise in 3D, from 0 to 1. */
12
+ export declare function value3(px: number, py: number, pz: number): number;
13
+ /**
14
+ * One slot of the ring: a push or a gust, each of which turns the air about a center. A push turns
15
+ * about the place it names and fades over its life. A gust turns as a wave about a center that
16
+ * travels along the heading from that place, for as long as the wave's reach holds it. The slot's
17
+ * place in the ring tells the two apart: the gust keeps the last slot, after the pushes.
18
+ */
19
+ export interface Push {
20
+ x: number;
21
+ z: number;
22
+ /** How hard the body shoved the air: a positive number turns it one way, a negative the other. */
23
+ strength: number;
24
+ /** The sketch time in seconds when the page sent the push or the gust. */
25
+ born: number;
26
+ }
27
+ /** The pushes that a page made, which the field reads in place. */
28
+ export interface PushRing {
29
+ /** The slots, which the field reads: the pushes first, then the newest gust. A slot that holds no
30
+ * strength turns nothing. */
31
+ readonly slots: readonly Push[];
32
+ /**
33
+ * Answers the slot that a push or a gust takes, without writing its numbers. The caller writes
34
+ * them in place, so a frame passes no freshly worked-out fraction into a call, where the browser
35
+ * would make a number object of it.
36
+ */
37
+ take(gust: boolean): number;
38
+ /**
39
+ * Writes a push or a gust sent at the time `born`, and answers the slot that it wrote. A push
40
+ * takes the oldest push slot, so the newest three pushes turn the air. A gust takes the gust's
41
+ * own slot, so a page's pushes never push its gust out. A call replaces the slot's numbers, so a
42
+ * page may call it in every frame and the call allocates nothing.
43
+ */
44
+ add(x: number, z: number, strength: number, born: number, gust: boolean): number;
45
+ }
46
+ /** Makes the empty ring that a page's pushes go into. The slots are made once, here. */
47
+ export declare function createPushRing(): PushRing;
48
+ /**
49
+ * The steady settings that a reader works out beyond the box, as the three vectors that its shader
50
+ * takes: the base wind, the gust and the swirl. A page gives them to a material once, and again
51
+ * when `setBase` or one of the air's settings changes; a frame changes nothing here.
52
+ *
53
+ * @category api/wind
54
+ */
55
+ export interface WindReading {
56
+ /** The speed, the heading in radians, how much slower the air is at the ground, and the seed. */
57
+ readonly base: [number, number, number, number];
58
+ /** The gust's strength, its radius in meters, its wave number and how fast it travels. */
59
+ readonly gust: [number, number, number, number];
60
+ /** The swirl's strength, its size, how fast its pattern drifts, and a number it does not use. */
61
+ readonly swirl: [number, number, number, number];
62
+ /** The gust a page sent: its ground place in x and z, its strength, and the time it was sent. */
63
+ readonly sent: [number, number, number, number];
64
+ }
65
+ /** Makes the three vectors of a reading from the field's settings. */
66
+ export declare function createReading(state: FieldState): WindReading;
67
+ /**
68
+ * The time that a reading holds when no page has sent a gust: a born time that no frame reaches, so
69
+ * the sent gust's life is over at every frame time.
70
+ */
71
+ export declare const SENT_NONE = -1000000000;
72
+ /** Writes the field's settings into a reading's vectors, in place, so a reader re-reads them. */
73
+ export declare function writeReading(state: FieldState, reading: WindReading): void;
74
+ /**
75
+ * The field's settings, which a page sets once or changes at any time.
76
+ *
77
+ * @category api/wind
78
+ */
79
+ export interface FieldState {
80
+ /** The wind's speed in meters a second, at the top of the profile. */
81
+ speed: number;
82
+ /** The heading that the wind blows toward, in radians: 0 blows toward +x, pi/2 toward +z. */
83
+ heading: number;
84
+ /** How much slower the air is at the ground, from 0 to 1, over `GROUND_HEIGHT` meters. */
85
+ ground: number;
86
+ /** The strength of the gust's turn, in meters a second. */
87
+ gustStrength: number;
88
+ /** How wide the gust is, in meters. */
89
+ gustRadius: number;
90
+ /** How far apart the gust's turns sit, so the gust arrives as a wave rather than one eddy. */
91
+ gustWave: number;
92
+ /** How fast the gust travels, in meters a second. */
93
+ gustSpeed: number;
94
+ /** How wide a push turns the air, in meters. */
95
+ pushRadius: number;
96
+ /** The strength of the noise-built swirl, in meters a second. */
97
+ swirl: number;
98
+ /** The swirl's size: larger makes wider, slower eddies. */
99
+ swirlScale: number;
100
+ /** How fast the swirl's pattern drifts, in meters a second. */
101
+ swirlDrift: number;
102
+ /** The seed of the swirl's pattern, so two pages may look different. */
103
+ seed: number;
104
+ }
105
+ /** The height over which the air slows to the ground, in meters. */
106
+ export declare const GROUND_HEIGHT = 2;
107
+ /** How long a push lasts before it has faded away, in seconds. */
108
+ export declare const PUSH_LIFE = 3;
109
+ /** The pushes that the field holds: the newest replaces the oldest. */
110
+ export declare const PUSH_SLOTS = 3;
111
+ /** The slot that holds the newest gust, after the pushes, so a page's pushes never push it out. */
112
+ export declare const GUST_SLOT = 3;
113
+ /** The slots of the ring: the pushes, then the gust. */
114
+ export declare const RING_SLOTS: number;
115
+ /** The distance over which the noise's curl is measured, in noise cells. */
116
+ export declare const CURL_STEP = 0.02;
117
+ /** The settings that a page gets when it names none. */
118
+ export declare function defaultFieldState(): FieldState;
119
+ /**
120
+ * The height's factor of the wind, from 1 at the top to `1 - ground` at the ground, so a plant's
121
+ * crown moves more than its base.
122
+ */
123
+ export declare function profile(state: FieldState, y: number): number;
124
+ /**
125
+ * The air's velocity at a world place and a sketch time, in meters a second, written into `out` as
126
+ * `[x, y, z]`. It allocates nothing and reads no state that a frame changes.
127
+ */
128
+ export declare function analyticVelocity(state: FieldState, pushes: readonly (Push | undefined)[], x: number, y: number, z: number, t: number, out: [number, number, number]): [number, number, number];
129
+ /** The divergence of the field at a place and a time, in a second, measured over a short step. */
130
+ export declare function divergence(state: FieldState, pushes: readonly (Push | undefined)[], x: number, y: number, z: number, t: number, step?: number): number;
@@ -0,0 +1,252 @@
1
+ // The wind's analytic field: the air's velocity at a place and a time, with no state between
2
+ // frames. The wind blows along a heading that is slower near the ground, a gust travels through it,
3
+ // and a noise-built swirl curls the air without making it pile up. The pushes that a page makes add
4
+ // short-lived vortices behind a moving body.
5
+ //
6
+ // The field is incompressible by construction: its swirl is the curl of a stream function, a push is
7
+ // a vortex, and a gust turns about its own center. Its divergence is therefore zero in the
8
+ // continuum, and the tests check that the discrete form keeps it near zero.
9
+ //
10
+ // The grid's compute shader and its WebGL2 form hold the same formula, and the noise functions
11
+ // mirror the engine's `null3d::noise`, so both forms give the same numbers. A test reads the two
12
+ // shader copies and fails when they drift apart or when a constant here changes without them.
13
+ //
14
+ // This form is the mirror of the add-on's WebAssembly: a test and a page's read outside a frame
15
+ // use it, and a frame's ask for many places runs the same formula in `crates/null3d-wind-wasm`, on
16
+ // the engine's job workers. A test compares the two.
17
+ /**
18
+ * The first value of `null3d::noise`'s `pcg3d` hash, as a signed whole number. The library returns
19
+ * three, but value noise reads only the first, and this form makes no tuple and no closure.
20
+ *
21
+ * Each step keeps its number in the 32 bits that a whole number holds. The hash is the same 32 bits
22
+ * either way, and the top 24 come out the same. Read as unsigned, about half of them pass the
23
+ * largest number that the browser keeps in the value itself, and it makes an object of each such
24
+ * number it returns. So the steps wrap with `| 0` rather than `>>> 0`.
25
+ */
26
+ export function lattice(x, y, z) {
27
+ let px = (Math.imul(x, 1664525) + 1013904223) | 0;
28
+ let py = (Math.imul(y, 1664525) + 1013904223) | 0;
29
+ let pz = (Math.imul(z, 1664525) + 1013904223) | 0;
30
+ px = (px + Math.imul(py, pz)) | 0;
31
+ py = (py + Math.imul(pz, px)) | 0;
32
+ pz = (pz + Math.imul(px, py)) | 0;
33
+ px = (px ^ (px >>> 16)) | 0;
34
+ py = (py ^ (py >>> 16)) | 0;
35
+ pz = (pz ^ (pz >>> 16)) | 0;
36
+ px = (px + Math.imul(py, pz)) | 0;
37
+ py = (py + Math.imul(pz, px)) | 0;
38
+ pz = (pz + Math.imul(px, py)) | 0;
39
+ return px;
40
+ }
41
+ /** A number from 0 up to 1 made from the top 24 bits of a hash. */
42
+ function toUnit(hash) {
43
+ return (hash >>> 8) / 16777216;
44
+ }
45
+ /** Perlin's fade curve, flat at both ends. */
46
+ function fade(t) {
47
+ return t * t * t * (t * (t * 6 - 15) + 10);
48
+ }
49
+ /** A blend of two numbers, written out so that a sample makes no closure. */
50
+ function blend(a, b, t) {
51
+ return a + (b - a) * t;
52
+ }
53
+ /** A value noise corner's number: the hash's first value, from 0 up to 1. */
54
+ function corner(x, y, z) {
55
+ return toUnit(lattice(x, y, z));
56
+ }
57
+ /** `null3d::noise`'s value noise in 3D, from 0 to 1. */
58
+ export function value3(px, py, pz) {
59
+ const fx = Math.floor(px);
60
+ const fy = Math.floor(py);
61
+ const fz = Math.floor(pz);
62
+ const u = fade(px - fx);
63
+ const v = fade(py - fy);
64
+ const w = fade(pz - fz);
65
+ const near = blend(blend(blend(corner(fx, fy, fz), corner(fx + 1, fy, fz), u), blend(corner(fx, fy + 1, fz), corner(fx + 1, fy + 1, fz), u), v), blend(blend(corner(fx, fy, fz + 1), corner(fx + 1, fy, fz + 1), u), blend(corner(fx, fy + 1, fz + 1), corner(fx + 1, fy + 1, fz + 1), u), v), w);
66
+ return near;
67
+ }
68
+ /** Makes the empty ring that a page's pushes go into. The slots are made once, here. */
69
+ export function createPushRing() {
70
+ const slots = Array.from({ length: RING_SLOTS }, () => ({
71
+ x: 0,
72
+ z: 0,
73
+ strength: 0,
74
+ born: 0,
75
+ }));
76
+ let next = 0;
77
+ const take = (gust) => {
78
+ const index = gust ? GUST_SLOT : next;
79
+ if (!gust)
80
+ next = (next + 1) % PUSH_SLOTS;
81
+ return index;
82
+ };
83
+ return {
84
+ slots,
85
+ take,
86
+ add(x, z, strength, born, gust) {
87
+ const index = take(gust);
88
+ const slot = slots[index];
89
+ slot.x = x;
90
+ slot.z = z;
91
+ slot.strength = strength;
92
+ slot.born = born;
93
+ return index;
94
+ },
95
+ };
96
+ }
97
+ /** Makes the three vectors of a reading from the field's settings. */
98
+ export function createReading(state) {
99
+ return {
100
+ base: [state.speed, state.heading, state.ground, state.seed],
101
+ gust: [state.gustStrength, state.gustRadius, state.gustWave, state.gustSpeed],
102
+ swirl: [state.swirl, state.swirlScale, state.swirlDrift, 0],
103
+ sent: [0, 0, 0, SENT_NONE],
104
+ };
105
+ }
106
+ /**
107
+ * The time that a reading holds when no page has sent a gust: a born time that no frame reaches, so
108
+ * the sent gust's life is over at every frame time.
109
+ */
110
+ export const SENT_NONE = -1e9;
111
+ /** Writes the field's settings into a reading's vectors, in place, so a reader re-reads them. */
112
+ export function writeReading(state, reading) {
113
+ reading.base[0] = state.speed;
114
+ reading.base[1] = state.heading;
115
+ reading.base[2] = state.ground;
116
+ reading.base[3] = state.seed;
117
+ reading.gust[0] = state.gustStrength;
118
+ reading.gust[1] = state.gustRadius;
119
+ reading.gust[2] = state.gustWave;
120
+ reading.gust[3] = state.gustSpeed;
121
+ reading.swirl[0] = state.swirl;
122
+ reading.swirl[1] = state.swirlScale;
123
+ reading.swirl[2] = state.swirlDrift;
124
+ }
125
+ /** The height over which the air slows to the ground, in meters. */
126
+ export const GROUND_HEIGHT = 2;
127
+ /** How long a push lasts before it has faded away, in seconds. */
128
+ export const PUSH_LIFE = 3;
129
+ /** The pushes that the field holds: the newest replaces the oldest. */
130
+ export const PUSH_SLOTS = 3;
131
+ /** The slot that holds the newest gust, after the pushes, so a page's pushes never push it out. */
132
+ export const GUST_SLOT = PUSH_SLOTS;
133
+ /** The slots of the ring: the pushes, then the gust. */
134
+ export const RING_SLOTS = PUSH_SLOTS + 1;
135
+ /** The distance over which the noise's curl is measured, in noise cells. */
136
+ export const CURL_STEP = 0.02;
137
+ /** The settings that a page gets when it names none. */
138
+ export function defaultFieldState() {
139
+ return {
140
+ speed: 4,
141
+ heading: 0,
142
+ ground: 0.65,
143
+ gustStrength: 3.5,
144
+ gustRadius: 14,
145
+ gustWave: 2,
146
+ gustSpeed: 6,
147
+ pushRadius: 6,
148
+ swirl: 1.6,
149
+ swirlScale: 0.06,
150
+ swirlDrift: 0.5,
151
+ seed: 0,
152
+ };
153
+ }
154
+ /**
155
+ * The height's factor of the wind, from 1 at the top to `1 - ground` at the ground, so a plant's
156
+ * crown moves more than its base.
157
+ */
158
+ export function profile(state, y) {
159
+ return 1 - state.ground * Math.exp(-Math.max(y, 0) / GROUND_HEIGHT);
160
+ }
161
+ /** The stream function of the swirl, in noise cells, at a place and a time. */
162
+ function stream(state, x, y, z, t) {
163
+ const drift = state.swirlDrift * t;
164
+ return value3(x * state.swirlScale + drift + state.seed, y * state.swirlScale, z * state.swirlScale + drift * 0.5);
165
+ }
166
+ /**
167
+ * The air's velocity at a world place and a sketch time, in meters a second, written into `out` as
168
+ * `[x, y, z]`. It allocates nothing and reads no state that a frame changes.
169
+ */
170
+ export function analyticVelocity(state, pushes, x, y, z, t, out) {
171
+ const height = profile(state, y);
172
+ const cos = Math.cos(state.heading);
173
+ const sin = Math.sin(state.heading);
174
+ let vx = cos * state.speed * height;
175
+ let vz = sin * state.speed * height;
176
+ // The gust: a wave of turns that travels along the heading, so the air curls as it passes. The
177
+ // turns are rings about the gust's center and depend only on the distance to it, so a ring of
178
+ // air turns without piling up.
179
+ const travel = state.gustSpeed * t;
180
+ const gx = x - cos * travel;
181
+ const gz = z - sin * travel;
182
+ const radius = state.gustRadius;
183
+ const distance = Math.sqrt(gx * gx + gz * gz) / radius;
184
+ const reach = distance * distance;
185
+ if (reach < 9) {
186
+ const envelope = Math.exp(-reach) * Math.cos(distance * state.gustWave) * state.gustStrength * height;
187
+ vx += (gz / radius) * envelope;
188
+ vz -= (gx / radius) * envelope;
189
+ }
190
+ // The swirl: the curl of one stream function in the ground plane, so the air turns without
191
+ // piling up. `psi` varies slowly, so a short step measures the slope.
192
+ const step = CURL_STEP / state.swirlScale;
193
+ const front = stream(state, x, y, z + step, t);
194
+ const back = stream(state, x, y, z - step, t);
195
+ const right = stream(state, x + step, y, z, t);
196
+ const left = stream(state, x - step, y, z, t);
197
+ vx += ((front - back) / (2 * step)) * state.swirl * height;
198
+ vz -= ((right - left) / (2 * step)) * state.swirl * height;
199
+ // The pushes and the gust: a vortex around each push, which fades as it ages, or a wave about a
200
+ // center that travels along the heading for the gust. The walk over the ring is the mirror of
201
+ // the loop that the field's WebAssembly runs, and a test holds the two equal.
202
+ for (let i = 0; i < pushes.length; i++) {
203
+ const slot = pushes[i];
204
+ if (!slot || slot.strength === 0)
205
+ continue;
206
+ let dx = x - slot.x;
207
+ let dz = z - slot.z;
208
+ let radius = state.pushRadius;
209
+ let turn = 0;
210
+ if (i === GUST_SLOT) {
211
+ // A gust: its wave's center has traveled along the heading since it was sent.
212
+ const age = t - slot.born;
213
+ const travel = state.gustSpeed * age;
214
+ dx -= cos * travel;
215
+ dz -= sin * travel;
216
+ radius = state.gustRadius;
217
+ const distance = Math.sqrt(dx * dx + dz * dz) / radius;
218
+ const reach = distance * distance;
219
+ if (reach < 9)
220
+ turn = Math.exp(-reach) * Math.cos(distance * state.gustWave);
221
+ }
222
+ else {
223
+ const age = t - slot.born;
224
+ if (age >= 0 && age <= PUSH_LIFE) {
225
+ const fadeOut = 1 - age / PUSH_LIFE;
226
+ const reach = radius * radius;
227
+ turn = Math.exp(-(dx * dx + dz * dz) / reach) * fadeOut;
228
+ }
229
+ }
230
+ if (turn === 0)
231
+ continue;
232
+ const strength = slot.strength * turn * height;
233
+ vx += (dz / radius) * strength;
234
+ vz -= (dx / radius) * strength;
235
+ }
236
+ out[0] = vx;
237
+ out[1] = 0;
238
+ out[2] = vz;
239
+ return out;
240
+ }
241
+ /** The divergence of the field at a place and a time, in a second, measured over a short step. */
242
+ export function divergence(state, pushes, x, y, z, t, step = 0.25) {
243
+ const a = [0, 0, 0];
244
+ const b = [0, 0, 0];
245
+ analyticVelocity(state, pushes, x + step, y, z, t, a);
246
+ analyticVelocity(state, pushes, x - step, y, z, t, b);
247
+ let sum = (a[0] - b[0]) / (2 * step);
248
+ analyticVelocity(state, pushes, x, y, z + step, t, a);
249
+ analyticVelocity(state, pushes, x, y, z - step, t, b);
250
+ sum += (a[2] - b[2]) / (2 * step);
251
+ return sum;
252
+ }