@margoengine/vfx 0.0.0-stage → 0.1.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.
Files changed (95) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1642 -3
  4. package/dist/base-C9upJytz.js +658 -0
  5. package/dist/base.d.ts +767 -0
  6. package/dist/base.js +1 -0
  7. package/dist/blown-sand.d.ts +218 -0
  8. package/dist/blown-sand.js +700 -0
  9. package/dist/clouds.d.ts +229 -0
  10. package/dist/clouds.js +566 -0
  11. package/dist/compat-CVSEj1Bp.js +217 -0
  12. package/dist/compat.d.ts +59 -0
  13. package/dist/curves.d.ts +11 -0
  14. package/dist/curves.js +59 -0
  15. package/dist/debris.d.ts +111 -0
  16. package/dist/debris.js +258 -0
  17. package/dist/depth-collide.d.ts +7 -0
  18. package/dist/depth-collide.js +96 -0
  19. package/dist/emitter.d.ts +10 -0
  20. package/dist/emitter.js +125 -0
  21. package/dist/events.d.ts +9 -0
  22. package/dist/events.js +131 -0
  23. package/dist/extra-CgGjbojB.js +4 -0
  24. package/dist/extra.d.ts +2 -0
  25. package/dist/fields.d.ts +11 -0
  26. package/dist/fields.js +95 -0
  27. package/dist/fire-front.d.ts +539 -0
  28. package/dist/fire-front.js +840 -0
  29. package/dist/fire.d.ts +119 -0
  30. package/dist/fire.js +173 -0
  31. package/dist/flame-ramp-VoBKpYnM.js +75 -0
  32. package/dist/flame-ramp.d.ts +61 -0
  33. package/dist/flame.d.ts +260 -0
  34. package/dist/flame.js +605 -0
  35. package/dist/flipbook.d.ts +7 -0
  36. package/dist/flipbook.js +23 -0
  37. package/dist/fog-B-kBv7g0.js +32 -0
  38. package/dist/frame-BuvMFBPp.js +4 -0
  39. package/dist/frame.d.ts +7 -0
  40. package/dist/gunfire.d.ts +696 -0
  41. package/dist/gunfire.js +1204 -0
  42. package/dist/hash-Btsp6fGK.js +7 -0
  43. package/dist/hash.d.ts +2 -0
  44. package/dist/index.d.ts +17 -0
  45. package/dist/index.js +60 -0
  46. package/dist/json.d.ts +53 -0
  47. package/dist/json.js +237 -0
  48. package/dist/lit.d.ts +11 -0
  49. package/dist/lit.js +105 -0
  50. package/dist/mesh.d.ts +14 -0
  51. package/dist/mesh.js +225 -0
  52. package/dist/node-system.d.ts +315 -0
  53. package/dist/node-system.js +384 -0
  54. package/dist/nodes.d.ts +14 -0
  55. package/dist/nodes.js +293 -0
  56. package/dist/obstacles.d.ts +3 -0
  57. package/dist/obstacles.js +25 -0
  58. package/dist/over-life.d.ts +14 -0
  59. package/dist/over-life.js +93 -0
  60. package/dist/require-webgpu--tUXPsmA.js +7 -0
  61. package/dist/ribbons.d.ts +20 -0
  62. package/dist/ribbons.js +151 -0
  63. package/dist/schedule.d.ts +29 -0
  64. package/dist/schedule.js +47 -0
  65. package/dist/settings.d.ts +362 -0
  66. package/dist/settings.js +707 -0
  67. package/dist/shader.d.ts +44 -0
  68. package/dist/shadows.d.ts +26 -0
  69. package/dist/shadows.js +182 -0
  70. package/dist/shapes.d.ts +34 -0
  71. package/dist/shapes.js +81 -0
  72. package/dist/shared/fog.d.ts +33 -0
  73. package/dist/shared/pass.d.ts +53 -0
  74. package/dist/shared/settings.d.ts +89 -0
  75. package/dist/skinned.d.ts +11 -0
  76. package/dist/skinned.js +104 -0
  77. package/dist/smoke.d.ts +215 -0
  78. package/dist/smoke.js +280 -0
  79. package/dist/soft.d.ts +17 -0
  80. package/dist/soft.js +55 -0
  81. package/dist/solid.d.ts +168 -0
  82. package/dist/solid.js +403 -0
  83. package/dist/sort.d.ts +3 -0
  84. package/dist/sort.js +58 -0
  85. package/dist/space.d.ts +14 -0
  86. package/dist/space.js +88 -0
  87. package/dist/stretch.d.ts +7 -0
  88. package/dist/stretch.js +40 -0
  89. package/dist/texture.d.ts +8 -0
  90. package/dist/texture.js +45 -0
  91. package/dist/view-RKprOPAy.js +30 -0
  92. package/dist/view-rect-DkeiiUtw.js +13 -0
  93. package/dist/wrap.d.ts +3 -0
  94. package/dist/wrap.js +5 -0
  95. package/package.json +192 -6
@@ -0,0 +1,104 @@
1
+ import { MargoError as e } from "@margoengine/math/errors";
2
+ //#region src/skinned.ts
3
+ var t = 262144, n = 20, r = "let origin=f.emitterTime.xyz+(vec3f(random(seed+5u),random(seed+6u),random(seed+7u))*2-1)*f.emissionExtent.xyz;", i = "f.velocitySpread.xyz+(vec3f(random(seed+1u),random(seed+2u),random(seed+3u))*2-1)*f.velocitySpread.w", a = `
4
+ struct Skinned {m:mat4x4f,a:vec4f,count:vec4u}
5
+ @group(0) @binding(22) var<uniform> sk:Skinned;
6
+ @group(0) @binding(23) var<storage,read> skin:array<u32>;
7
+ fn skinVertex(i:u32)->vec3f {let b=sk.count.y+i*${n}u;return (sk.m*vec4f(bitcast<f32>(skin[b]),bitcast<f32>(skin[b+1u]),bitcast<f32>(skin[b+2u]),1)).xyz;}
8
+ `, o = "\n let share=random(seed+8u);var lo=0u;var hi=sk.count.x-1u;\n loop{if(lo>=hi){break;}let mid=(lo+hi)/2u;if(bitcast<f32>(skin[mid*4u+3u])<share){lo=mid+1u;}else{hi=mid;}}\n let ta=skinVertex(skin[lo*4u]);let tb=skinVertex(skin[lo*4u+1u]);let tc=skinVertex(skin[lo*4u+2u]);\n let r1=sqrt(random(seed+9u));let r2=random(seed+10u);\n origin+=ta*(1.0-r1)+tb*(r1*(1.0-r2))+tc*(r1*r2);\n let face=cross(tb-ta,tc-ta);let shapeDir=select(vec3f(0),face*inverseSqrt(max(dot(face,face),1e-30)),dot(face,face)>1e-24);";
9
+ function s(t, n, r) {
10
+ if (t.split(n).length !== 2) throw new e("unsupported", "VFX skinned needs the simulation shader it was written for; update the edits");
11
+ return t.replace(n, r);
12
+ }
13
+ function c(e, t) {
14
+ let r = [], i = [], a = (t) => [
15
+ 0,
16
+ 1,
17
+ 2
18
+ ].map((r) => e[t * n + r]);
19
+ for (let e = 0; e + 2 < t.length; e += 3) {
20
+ let n = a(t[e]), o = a(t[e + 1]), s = a(t[e + 2]), c = [
21
+ o[0] - n[0],
22
+ o[1] - n[1],
23
+ o[2] - n[2]
24
+ ], l = [
25
+ s[0] - n[0],
26
+ s[1] - n[1],
27
+ s[2] - n[2]
28
+ ], u = .5 * Math.hypot(c[1] * l[2] - c[2] * l[1], c[2] * l[0] - c[0] * l[2], c[0] * l[1] - c[1] * l[0]);
29
+ u > 0 && (r.push(e), i.push(u));
30
+ }
31
+ if (!r.length) throw RangeError("VFX skinned primitive has no triangle with an area in its bind pose; pass a real surface");
32
+ let o = i.reduce((e, t) => e + t, 0), s = new Uint32Array(r.length * 4), c = new Float32Array(s.buffer), l = 0;
33
+ return r.forEach((e, n) => {
34
+ l += i[n], s.set([
35
+ t[e],
36
+ t[e + 1],
37
+ t[e + 2]
38
+ ], n * 4), c[n * 4 + 3] = n === r.length - 1 ? 1 : l / o;
39
+ }), s;
40
+ }
41
+ var l = [
42
+ 1,
43
+ 0,
44
+ 0,
45
+ 0,
46
+ 0,
47
+ 1,
48
+ 0,
49
+ 0,
50
+ 0,
51
+ 0,
52
+ 1,
53
+ 0,
54
+ 0,
55
+ 0,
56
+ 0,
57
+ 1
58
+ ], u = {
59
+ validate(e, r) {
60
+ if (!e || typeof e != "object" || !e.primitive || typeof e.primitive != "object") throw TypeError("VFX skinned needs {primitive}: an entry of a loaded model.primitives with a deformer attached");
61
+ if (r.emitter) throw TypeError("VFX skinned and emitter both place births; use one of them");
62
+ let { vertices: i, indices: a } = e.primitive;
63
+ if (!(i instanceof Float32Array) || i.length < 60 || i.length % n) throw TypeError("VFX skinned primitive.vertices must be a Float32Array of 20 floats a vertex (gltf/pbr layout)");
64
+ if (!a || typeof a.length != "number" || a.length < 3 || a.length % 3) throw TypeError("VFX skinned primitive.indices must be triangle triples");
65
+ if (a.length / 3 > 262144) throw RangeError(`VFX skinned primitive has more than ${t} triangles; pass a simpler mesh`);
66
+ let o = i.length / n;
67
+ for (let e = 0; e < a.length; e++) if (!(a[e] >= 0 && a[e] < o && Number.isInteger(a[e]))) throw RangeError("VFX skinned primitive index points past its vertices; check the index buffer");
68
+ let s = e.transform ?? l;
69
+ if (s.length !== 16 || !Array.from(s).every(Number.isFinite)) throw TypeError("VFX skinned transform must be 16 finite numbers, a column-major matrix");
70
+ let u = e.speed ?? 0;
71
+ if (typeof u != "number" || !Number.isFinite(u) || u < -1e3 || u > 1e3) throw RangeError("VFX skinned speed must be -1000 to 1000 world units a second; pass e.g. 1");
72
+ c(i, a);
73
+ },
74
+ simulation(e) {
75
+ return e = s(e, "fn update(", a + "fn update("), e = s(e, r, "var origin=" + r.slice(29) + o), s(e, i, "f.velocitySpread.xyz+shapeDir*sk.a.x+(" + i.slice(22));
76
+ },
77
+ create(e, t, n) {
78
+ let { device: r, options: i } = e, { primitive: a } = t, o = c(a.vertices, a.indices), s = o.byteLength, u = a.vertices.byteLength, d = r.createBuffer({
79
+ size: 96,
80
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
81
+ }), f = r.createBuffer({
82
+ size: s + u,
83
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
84
+ });
85
+ n.push(d, f), r.queue.writeBuffer(f, 0, o), r.queue.writeBuffer(f, s, a.vertices);
86
+ let p = /* @__PURE__ */ new Float32Array(24), m = new Uint32Array(p.buffer);
87
+ return {
88
+ compute: [{
89
+ binding: 22,
90
+ resource: { buffer: d }
91
+ }, {
92
+ binding: 23,
93
+ resource: { buffer: f }
94
+ }],
95
+ write(e) {
96
+ let t = i.skinned;
97
+ if (!t || t.primitive !== a) throw TypeError("VFX skinned primitive is fixed at attach; change transform and speed in place");
98
+ a.deformed && e.encoder.copyBufferToBuffer(a.deformed, a.deformedOffset ?? 0, f, s, u), p.set(t.transform ?? l, 0), p.fill(0, 16), p[16] = t.speed ?? 0, m[20] = o.length / 4, m[21] = s / 4, e.device.queue.writeBuffer(d, 0, p);
99
+ }
100
+ };
101
+ }
102
+ };
103
+ //#endregion
104
+ export { t as MAX_SKINNED_TRIANGLES, u as skinned, c as skinnedTriangles };
@@ -0,0 +1,215 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { EngineKernel } from '@margoengine/core';
3
+ import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
4
+ import { VFX, type FogLike, type VFXLightSource, type VFXTexture, type VFXWind } from '@margoengine/vfx/base';
5
+ type Vec3 = readonly [number, number, number];
6
+ /** MTT 1956's entrainment constant for a plume in calm air (Briggs 1969 eq. 4.22). */
7
+ export declare const PLUME_ENTRAINMENT = 0.093;
8
+ /** Briggs 1969's radius of a bent-over plume over its rise (eq. 4.23, about 0.5 from the TVA photographs). */
9
+ export declare const BENT_PLUME_RADIUS = 0.5;
10
+ /** Briggs 1969's "2/3 law" constant: rise 1.6 F^(1/3) U^(-1) x^(2/3) (eq. 4.31). */
11
+ export declare const TWO_THIRDS_LAW = 1.6;
12
+ /** Briggs 1969's final rise in stable calm air, 5.0 F^(1/4) s^(-3/8) (eq. 4.25). */
13
+ export declare const STABLE_CALM_RISE = 5;
14
+ /** Briggs 1969's final rise in stable air with a wind, 2.9 (F / (U s))^(1/3) (eq. 5.7). */
15
+ export declare const STABLE_WIND_RISE = 2.9;
16
+ /** Seconds a new puff takes to show fully: smoke leaves the flames already dense (KB-VFX-SMOKEROPE1: the old ramp was a fifth of the life, 12 s of a 60 s life). */
17
+ export declare const SMOKE_FADE_IN = 1;
18
+ /**
19
+ * The share of a fire's heat it radiates (Drysdale, An Introduction to Fire Dynamics, 3rd ed., Wiley 2011, ch. 2: 0.2
20
+ * to 0.4 for hydrocarbon and wood flames; McGrattan, Baum and Hamins, NISTIR 6546, 2000: about 0.3 falling for very
21
+ * large fires). The column's glow from below radiates this over the convective 0.7 of the release.
22
+ */
23
+ export declare const RADIANT_FRACTION = 0.3;
24
+ /**
25
+ * A puff's opacity over its normalized life: full after {@link SMOKE_FADE_IN} seconds whatever the lifetime (within the
26
+ * 32 samples' spacing: a long life starts part-opaque, with the fade's mean opacity over its first sample), then
27
+ * the column's old shape (full to 0.4 of the life, 0.85 at 0.6, 0.5 at 0.8, gone at the end), 32 samples.
28
+ */
29
+ export declare function smokeAlphaCurve(lifetime: number): number[];
30
+ /**
31
+ * Buoyancy flux F, m^4/s^3, of a heat emission: F = g Q / (pi c_p rho T) (Briggs 1969 eq. 4.20), with sea-level air
32
+ * (rho 1.225 kg/m^3, c_p 1,005 J/(kg K)). A 1 MW fire gives about 8.8.
33
+ * @param heat Convective heat release, watts, 0 or more.
34
+ * @param airTemperature Air temperature, kelvin. Default 288.15.
35
+ */
36
+ export declare function buoyancyFlux(heat: number, airTemperature?: number): number;
37
+ /**
38
+ * The stability s, 1/s^2, of air whose potential temperature rises `lapse` kelvin a metre: s = (g / T) dtheta/dz
39
+ * (Briggs 1969). Neutral air is 0; an isothermal layer (dtheta/dz about 0.0098 K/m) gives about 3.3e-4.
40
+ */
41
+ export declare function airStability(lapse: number, airTemperature?: number): number;
42
+ /** The column at one age, from {@link plumeRise}. */
43
+ export interface PlumePoint {
44
+ /** Height above the source, metres. */
45
+ rise: number;
46
+ /** Rise speed, m/s. */
47
+ speed: number;
48
+ /** Column radius, metres. */
49
+ radius: number;
50
+ /** True where the bent-over plume (the wind's) is the lower one. */
51
+ bent: boolean;
52
+ /** True where the column has reached its final rise in stable air. */
53
+ level: boolean;
54
+ }
55
+ /**
56
+ * The smoke column `seconds` after a puff left the source: the lower of the calm plume of MTT 1956 and the bent-over
57
+ * plume of Briggs 1969, capped at Briggs' final rise in stable air (see the module notes), with its radius.
58
+ * @param flux Buoyancy flux, m^4/s^3 ({@link buoyancyFlux}), 0 or more.
59
+ * @param wind Wind speed, m/s, 0 or more (0 is calm: no bending).
60
+ * @param radius Source radius, metres, above 0.
61
+ * @param stability Air stability s, 1/s^2, 0 or more ({@link airStability}); 0 is neutral (no ceiling). Default 0.
62
+ */
63
+ export declare function plumeRise(seconds: number, flux: number, wind: number, radius: number, stability?: number, out?: PlumePoint): PlumePoint;
64
+ /** Where the smoke comes from: a `Fire` of `@margoengine/vfx/fire` (its flames' position and wind). */
65
+ export interface SmokeSource {
66
+ /** Hearth flames. */
67
+ readonly particles: {
68
+ readonly flames?: VFX;
69
+ };
70
+ /** The flame volume of a large fire (`Fire` with a `flameLength`). */
71
+ readonly flame?: {
72
+ readonly options: {
73
+ readonly position: Vec3;
74
+ readonly wind?: VFXWind;
75
+ };
76
+ };
77
+ }
78
+ /**
79
+ * `Smoke.attach` settings. `position` or `fire` is required. Read every frame (change a field and the next frame shows
80
+ * it) except `lifetime`, `prewarm`, `texture`, `lights`, `shadows` and `depth`, which are read at attach.
81
+ */
82
+ export interface SmokeOptions {
83
+ /** The source, world units (the top of the fire). Default: the fire's flames' position. */
84
+ position?: Vec3;
85
+ /**
86
+ * A `Fire` to feed the column: the column stands on its flames and follows their wind (the fire's own, or the one a
87
+ * `WeatherDrive` writes) unless the smoke has a wind of its own. Default none.
88
+ */
89
+ fire?: SmokeSource;
90
+ /** Convective heat release, watts, 0 or more: it sets the rise. Default 5,000,000 (a house fire); 20,000 with `fire` (a wood hearth). */
91
+ heat?: number;
92
+ /** Source radius, metres, above 0. Default 2; 0.3 with `fire`. */
93
+ radius?: number;
94
+ /** Air temperature, kelvin, for the buoyancy flux. Default 288.15. */
95
+ airTemperature?: number;
96
+ /**
97
+ * Air stability s = (g / T) dtheta/dz, 1/s^2, 0 or more ({@link airStability}): above 0 the column levels off at
98
+ * Briggs' final rise and spreads there. Default 0 (neutral air, no ceiling).
99
+ */
100
+ stability?: number;
101
+ /**
102
+ * The wind, `[x, z]` m/s or `{sample(x, y, z, t)}` (a `WindField` fits): the column bends over by Briggs' law and
103
+ * leans atan(U / w). Default none, and then a `WeatherDrive` in the scene writes the weather's wind; a wind given
104
+ * here overrides the weather (the rule of `Fire`).
105
+ */
106
+ wind?: VFXWind;
107
+ /** Seconds a puff lives: the column's height is its rise at this age. Above 0, at most 3,600. Read at attach. Default 60. */
108
+ lifetime?: number;
109
+ /** Puffs born a second, above 0 (at most the capacity, `rate x lifetime`, that attach made). Default 20. */
110
+ rate?: number;
111
+ /** Smoke colour, linear RGB 0..1. Default [0.3, 0.29, 0.28] (wood smoke grey). */
112
+ color?: Vec3;
113
+ /** Opacity of a puff, 0..1. Default 0.5. */
114
+ opacity?: number;
115
+ /** Curl-noise churn, m/s^2, 0 or more. Default 0.4 (0 for a smooth column). */
116
+ turbulence?: number;
117
+ /** Fill the column before the first frame (simulated in steps of 0.1 s, or a 600th of a long life). Read at attach. Default true. */
118
+ prewarm?: boolean;
119
+ /**
120
+ * The fire's glow on the column from below: the puffs are lit by a point source at the source position radiating
121
+ * {@link RADIANT_FRACTION} of the fire's release (0.3 / 0.7 of `heat`), over the source radius (`VFXLit.glow`), so
122
+ * the column over a fire is lit orange from below, at night with no other light. A multiplier of that power, 0 (off)
123
+ * to 10. Read every frame. Default 1.
124
+ */
125
+ glow?: number;
126
+ /** A puff sprite (RGBA8 sRGB, alpha the coverage), multiplied by the colour. Read at attach. Default: a soft disc. */
127
+ texture?: VFXTexture;
128
+ /** Point and spot lights on the puffs (`Clustered` of `@margoengine/clustered`). Read at attach. Default: the sun and ambient only. */
129
+ lights?: VFXLightSource;
130
+ /** Sun shadows on the puffs (`Shadows` of `@margoengine/shadows`). Read at attach. Default none. */
131
+ shadows?: VFXLightSource;
132
+ /** The scene depth (`DepthTexture` of `@margoengine/post/depth`): puffs fade 1 m before surfaces. Read at attach. Default: hard-edged. */
133
+ depth?: () => GPUTexture | undefined;
134
+ }
135
+ /**
136
+ * A smoke column: lit soft puffs (`@margoengine/vfx` particles with `lit`, `overLife` and `curves`) that rise by the
137
+ * plume law of MTT 1956 and Briggs 1969 for the given heat, widen as they rise, lean with the wind and level off in
138
+ * stable air. The wind is read each frame (its own, the fire's, or the weather's through a `WeatherDrive`), and the
139
+ * column's speed and size curves are rewritten in place: no GPU object is made after attach. The same frames give the
140
+ * same pictures.
141
+ * @throws TypeError for a missing source or a position that is not three numbers; RangeError for an option out of range.
142
+ * A plugin of the scene it is attached to: disposing that scene disposes the smoke.
143
+ */
144
+ export declare class Smoke implements EnginePlugin {
145
+ readonly name: string;
146
+ /** The puffs (`smoke.effect.setFog(fog)` fogs them). */
147
+ readonly effect: VFX;
148
+ /** Read every frame; see {@link SmokeOptions}. */
149
+ readonly options: SmokeOptions;
150
+ /** Puff slots, `rate x lifetime` at attach. */
151
+ readonly capacity: number;
152
+ private engine?;
153
+ private disposed;
154
+ private clock;
155
+ private readonly lifetime;
156
+ private readonly speed;
157
+ private readonly size;
158
+ private readonly point;
159
+ private readonly key;
160
+ private readonly velocity;
161
+ private readonly extent;
162
+ private readonly rgba;
163
+ private readonly at;
164
+ private ownWind;
165
+ /** The still-air drag on the puffs' horizontal velocity, per second, as a node constant: 0 while a wind applies. */
166
+ private readonly still;
167
+ private readonly lit;
168
+ private readonly glowAt;
169
+ private constructor();
170
+ /** Turns the still-air drag on when no wind moves the puffs (none, [0, 0], or a field that is calm here now). */
171
+ private calm;
172
+ private heat;
173
+ private radius;
174
+ /** Checks every frame-read option and copies the source, colour and extent into place (no allocation). */
175
+ private check;
176
+ /** The wind the column follows this frame: its own, else what the fire or the weather wrote on the puffs. */
177
+ private wind;
178
+ /** Rewrites the speed and size curves in place for this flux, wind and air (no allocation). */
179
+ private write;
180
+ /** The column now: rise, speed and radius at a puff age of `seconds` for the wind it follows this frame. */
181
+ column(seconds: number): PlumePoint;
182
+ setup(engine: Engine): void;
183
+ /** Seconds on the column's clock: the sum of its frame deltas. */
184
+ get time(): number;
185
+ onPreUpdate(delta: number): void;
186
+ /**
187
+ * Moves the column's clock by `delta` seconds (0 or less moves nothing) and sets its puffs' wind, rate and curves for
188
+ * that time: the CPU part of its frame update, with no GPU call, which a seek from `@margoengine/cutscene/seek`
189
+ * replays. The puffs' own effect is a plugin attached before the column and keeps its own clock, so a seek moves both
190
+ * in the order a continuous run does (GAP-FILM-TIME).
191
+ */
192
+ advanceTime(delta: number): void;
193
+ /**
194
+ * Back to time 0, as a fresh column with the same options (GAP-FILM-RESET): its clock and its puffs' effect (particles,
195
+ * counters, step seeds and prewarm) start again, so the same frames after it draw what a fresh column draws.
196
+ */
197
+ resetTime(): void;
198
+ /**
199
+ * Empties the column and fills it again on the next frame for the options as they are then (the prewarm of attach,
200
+ * when `prewarm` is on): for a column moved to a new fire, so it does not grow up from the ground over its lifetime.
201
+ */
202
+ refill(): this;
203
+ /** Sets the heat release, watts (see {@link SmokeOptions.heat}); the column answers from the next frame. */
204
+ setHeat(heat: number): this;
205
+ /** Sets the column's own wind (see {@link SmokeOptions.wind}); undefined gives it back to the fire or the weather. */
206
+ setWind(wind: VFXWind | undefined): this;
207
+ /** Fogs the puffs (`Fog` of `@margoengine/environment`, read every frame); null for none. */
208
+ setFog(fog: FogLike | null): this;
209
+ /** A smoke column at `options.position` or over `options.fire`; see {@link SmokeOptions}. */
210
+ static attach(engine: EngineKernel, options: SmokeOptions): Smoke;
211
+ /** Removes the puffs. */
212
+ dispose(): void;
213
+ [Symbol.dispose](): void;
214
+ }
215
+ export {};
package/dist/smoke.js ADDED
@@ -0,0 +1,280 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const e=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
5
+ import { finiteVector as t } from "@margoengine/math/validate";
6
+ import { VFX as n } from "@margoengine/vfx/base";
7
+ import { curves as r } from "@margoengine/vfx/curves";
8
+ import { soft as i } from "@margoengine/vfx/soft";
9
+ import { overLife as a } from "@margoengine/vfx/over-life";
10
+ import { schedule as o } from "@margoengine/vfx/schedule";
11
+ import { lit as s } from "@margoengine/vfx/lit";
12
+ import { nodes as c } from "@margoengine/vfx/nodes";
13
+ import { textureFeature as l } from "@margoengine/vfx/texture";
14
+ //#region src/smoke.ts
15
+ var u = .093, d = .5, f = 1.6, p = 5, m = 2.9, h = 9.81, g = 1.225, _ = 1005, v = 32, y = 1, b = .3;
16
+ function x(e) {
17
+ let t = [], n = [
18
+ 1,
19
+ 1,
20
+ 1,
21
+ .85,
22
+ .5,
23
+ 0
24
+ ];
25
+ for (let r = 0; r < v; r++) {
26
+ let i = r / 31, a = i * 5, o = Math.min(4, Math.floor(a)), s = n[o] + (n[o + 1] - n[o]) * (a - o);
27
+ t.push(Math.max(0, Math.min(1, Math.min(1, i * e / 1) * s)));
28
+ }
29
+ return t[0] = Math.max(0, 1 - 1 / (e / 31)), t;
30
+ }
31
+ function S(e, t = 288.15) {
32
+ if (!(e >= 0 && Number.isFinite(e))) throw RangeError("Smoke heat must be a finite number of watts, 0 or more");
33
+ if (!(t > 0 && Number.isFinite(t))) throw RangeError("Smoke airTemperature must be a finite temperature above 0 K");
34
+ return h * e / (Math.PI * g * _ * t);
35
+ }
36
+ function C(e, t = 288.15) {
37
+ if (!Number.isFinite(e) || e < 0) throw RangeError("Smoke lapse must be a finite rise of 0 or more kelvin a metre");
38
+ return h / t * e;
39
+ }
40
+ function w(e, t, n, r, i = 0, a) {
41
+ let o = u, s = Math.cbrt(25 * t / (48 * o * o)), c = r / (1.2 * o), l = Math.max(0, e), p = l > 0 ? (4 / 3 * s * l + c ** (4 / 3)) ** .75 : c, h = p - c, g = s / Math.cbrt(p), _ = a ?? {
42
+ rise: 0,
43
+ speed: 0,
44
+ radius: 0,
45
+ bent: !1,
46
+ level: !1
47
+ };
48
+ if (_.rise = h, _.speed = g, _.radius = 1.2 * o * p, _.bent = !1, _.level = !1, n > 1e-6) {
49
+ let e = f * Math.cbrt(t / n), i = r / d, a = (i / e) ** 1.5, o = e * (l + a) ** (2 / 3) - i, s = 2 / 3 * e / Math.cbrt(l + a);
50
+ (l > 0 ? o < h : s < g) && (_.rise = o, _.speed = s, _.radius = d * (o + i), _.bent = !0);
51
+ }
52
+ if (i > 0) {
53
+ let e = n > 1e-6 ? Math.min(m * Math.cbrt(t / (n * i)), 5 * t ** .25 * i ** -.375) : 5 * t ** .25 * i ** -.375;
54
+ _.rise >= e && (_.rise = e, _.speed = 0, _.level = !0);
55
+ }
56
+ return _;
57
+ }
58
+ var T = (e) => e?.particles.flames?.options ?? e?.flame?.options, E = (e, t, n) => {
59
+ if (!e) return 0;
60
+ if (typeof e == "object" && "sample" in e) {
61
+ let r = e.sample(t[0], t[1], t[2], n);
62
+ return Math.hypot(r[0], r[2]);
63
+ }
64
+ let r = e;
65
+ return Math.hypot(r[0], r[1]);
66
+ }, D = (e, t, n, r, i = !1) => {
67
+ if (!(Number.isFinite(e) && (i ? e > n : e >= n) && e <= r)) throw RangeError(`Smoke ${t} is ${e}; pass a finite number ${i ? "above" : "from"} ${n} ${i ? "and at most" : "to"} ${r}`);
68
+ return e;
69
+ }, O = 0, k = class u {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
70
+ constructor(e, t) {
71
+ this.name = "vfx-smoke:" + O++, this.disposed = !1, this.clock = 0, this.speed = Array(v).fill(1), this.size = Array(v).fill(1), this.point = {
72
+ rise: 0,
73
+ speed: 0,
74
+ radius: 0,
75
+ bent: !1,
76
+ level: !1
77
+ }, this.key = [
78
+ NaN,
79
+ NaN,
80
+ NaN,
81
+ NaN,
82
+ NaN
83
+ ], this.velocity = [
84
+ 0,
85
+ 0,
86
+ 0
87
+ ], this.extent = [
88
+ 0,
89
+ 0,
90
+ 0
91
+ ], this.rgba = [
92
+ 0,
93
+ 0,
94
+ 0,
95
+ 0
96
+ ], this.at = [
97
+ 0,
98
+ 0,
99
+ 0
100
+ ], this.still = [
101
+ -4,
102
+ 0,
103
+ -4
104
+ ], this.glowAt = [
105
+ 0,
106
+ 0,
107
+ 0
108
+ ];
109
+ let u = t.fire;
110
+ if (u && !u.particles?.flames && !u.flame?.options) throw TypeError("Invalid Smoke fire; pass a Fire from @margoengine/vfx/fire");
111
+ if (this.options = t, this.lifetime = D(t.lifetime ?? 60, "lifetime", 0, 3600, !0), t.prewarm !== void 0 && typeof t.prewarm != "boolean") throw TypeError("Smoke prewarm must be true or false");
112
+ let d = D(t.rate ?? 20, "rate", 0, 1e5, !0);
113
+ this.capacity = Math.max(64, Math.ceil(d * this.lifetime * 1.05) + 16), this.lit = {
114
+ wrap: .6,
115
+ ...t.lights ? { lights: t.lights } : {},
116
+ ...t.shadows ? { shadows: t.shadows } : {}
117
+ }, this.check(), this.write();
118
+ let f = {
119
+ capacity: this.capacity,
120
+ rate: d,
121
+ position: [...this.at],
122
+ velocity: [...this.velocity],
123
+ spread: 0,
124
+ gravity: [
125
+ 0,
126
+ 0,
127
+ 0
128
+ ],
129
+ color: [...this.rgba],
130
+ size: 2 * this.radius(),
131
+ lifetime: [this.lifetime, this.lifetime],
132
+ ground: -1e9,
133
+ turbulence: this.options.turbulence ?? .4,
134
+ frequency: 1 / (8 * this.radius()),
135
+ emissionExtent: [...this.extent],
136
+ angularVelocity: .05,
137
+ ...t.wind ? { wind: t.wind } : {},
138
+ ...t.texture ? { texture: t.texture } : {},
139
+ curves: { alpha: x(this.lifetime) },
140
+ overLife: {
141
+ speed: this.speed,
142
+ size: this.size
143
+ },
144
+ lit: this.lit,
145
+ ...t.depth ? {
146
+ softness: 1,
147
+ depth: t.depth
148
+ } : {},
149
+ ...t.prewarm ?? !0 ? { schedule: {
150
+ prewarm: this.lifetime,
151
+ step: Math.max(.1, this.lifetime / 600)
152
+ } } : {},
153
+ nodes: { graph: [
154
+ {
155
+ op: "attribute",
156
+ value: "velocity"
157
+ },
158
+ {
159
+ op: "const",
160
+ value: this.still
161
+ },
162
+ {
163
+ op: "mul",
164
+ inputs: [0, 1]
165
+ },
166
+ {
167
+ op: "force",
168
+ inputs: [2]
169
+ }
170
+ ] }
171
+ };
172
+ this.ownWind = t.wind, this.calm(f.wind ?? T(u)?.wind), this.effect = n.attach(e, f, {
173
+ curves: r,
174
+ soft: i,
175
+ lit: s,
176
+ overLife: a,
177
+ schedule: o,
178
+ texture: l,
179
+ nodes: c
180
+ });
181
+ }
182
+ calm(e) {
183
+ let t = 0, n = 0;
184
+ if (e && "sample" in e) {
185
+ let r = e.sample(this.at[0], this.at[1], this.at[2], this.clock);
186
+ t = r[0], n = r[2];
187
+ } else e && (t = e[0], n = e[1]);
188
+ this.still[0] = this.still[2] = t === 0 && n === 0 ? -4 : 0;
189
+ }
190
+ heat() {
191
+ return this.options.heat ?? (this.options.fire ? 2e4 : 5e6);
192
+ }
193
+ radius() {
194
+ return this.options.radius ?? (this.options.fire ? .3 : 2);
195
+ }
196
+ check() {
197
+ let e = this.options, n = e.position ?? T(e.fire)?.position;
198
+ if (!n) throw TypeError("Smoke needs a position [x, y, z] or a fire");
199
+ t(n, 3, "Smoke position"), this.at[0] = n[0], this.at[1] = n[1], this.at[2] = n[2], S(this.heat(), e.airTemperature);
200
+ let r = D(this.radius(), "radius", 0, 1e4, !0);
201
+ D(e.stability ?? 0, "stability", 0, 1), D(e.rate ?? 20, "rate", 0, 1e5, !0), D(e.turbulence ?? .4, "turbulence", 0, 1e3);
202
+ let i = D(e.opacity ?? .5, "opacity", 0, 1), a = e.color ?? [
203
+ .3,
204
+ .29,
205
+ .28
206
+ ];
207
+ t(a, 3, "Smoke color");
208
+ for (let e = 0; e < 3; e++) this.rgba[e] = D(a[e], "color channel", 0, 1);
209
+ this.rgba[3] = i, this.extent[0] = this.extent[2] = r * .7;
210
+ let o = D(e.glow ?? 1, "glow", 0, 10);
211
+ if (o > 0) {
212
+ this.glowAt[0] = this.at[0], this.glowAt[1] = this.at[1], this.glowAt[2] = this.at[2];
213
+ let e = this.lit.glow ??= {
214
+ position: this.glowAt,
215
+ power: 0
216
+ };
217
+ e.position = this.glowAt, e.power = o * b / .7 * this.heat(), e.radius = Math.max(r, .01);
218
+ } else delete this.lit.glow;
219
+ }
220
+ wind() {
221
+ return E(this.options.wind ?? this.effect?.options.wind ?? T(this.options.fire)?.wind, this.at, this.clock);
222
+ }
223
+ write() {
224
+ let e = this.options, t = S(this.heat(), e.airTemperature), n = this.radius(), r = this.wind(), i = e.stability ?? 0, a = this.key;
225
+ if (a[0] === t && a[1] === r && a[2] === n && a[3] === i && a[4] === this.lifetime) return;
226
+ a[0] = t, a[1] = r, a[2] = n, a[3] = i, a[4] = this.lifetime;
227
+ let o = this.point, s = w(0, t, 0, n, 0, o).speed;
228
+ this.velocity[1] = s;
229
+ for (let e = 0; e < v; e++) w(e / 31 * this.lifetime, t, r, n, i, o), this.speed[e] = s > 0 ? Math.min(1e3, o.speed / s) : 0, this.size[e] = Math.min(1e3, o.radius / n);
230
+ }
231
+ column(e) {
232
+ let t = this.options;
233
+ return w(e, S(this.heat(), t.airTemperature), this.wind(), this.radius(), t.stability ?? 0);
234
+ }
235
+ setup(e) {
236
+ this.engine = e;
237
+ }
238
+ get time() {
239
+ return this.clock;
240
+ }
241
+ onPreUpdate(e) {
242
+ this.advanceTime(e);
243
+ }
244
+ advanceTime(e) {
245
+ if (this.disposed) return;
246
+ this.clock += e > 0 ? e : 0;
247
+ let t = this.options, n = this.effect, r = n.options, i = T(t.fire);
248
+ this.check(), t.wind !== this.ownWind && (n.setWind(t.wind), this.ownWind = t.wind), !t.wind && i && r.wind !== i.wind && n.setWind(i.wind), this.calm(r.wind), this.write();
249
+ let a = this.radius();
250
+ r.position = this.at, r.velocity = this.velocity, r.emissionExtent = this.extent, r.color = this.rgba, r.size = 2 * a, r.frequency = 1 / (8 * a), r.rate = t.rate ?? 20, r.turbulence = t.turbulence ?? .4;
251
+ }
252
+ resetTime() {
253
+ this.disposed || (this.clock = 0, this.check(), this.write(), this.calm(this.effect.options.wind), this.effect.resetTime());
254
+ }
255
+ refill() {
256
+ return this.effect.reset(), this;
257
+ }
258
+ setHeat(e) {
259
+ return S(e, this.options.airTemperature), this.options.heat = e, this;
260
+ }
261
+ setWind(e) {
262
+ return this.effect.setWind(e), this.options.wind = this.ownWind = e, this;
263
+ }
264
+ setFog(e) {
265
+ return this.effect.setFog(e), this;
266
+ }
267
+ static attach(t, n) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
268
+ if (!n || typeof n != "object") throw TypeError("Invalid Smoke options; pass {position: [x, y, z]} or {fire}");
269
+ let r = new u(t, n);
270
+ return e(t).use(r), r;
271
+ }
272
+ dispose() {
273
+ this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0, this.effect.dispose());
274
+ }
275
+ [Symbol.dispose]() {
276
+ this.dispose();
277
+ }
278
+ };
279
+ //#endregion
280
+ export { d as BENT_PLUME_RADIUS, u as PLUME_ENTRAINMENT, b as RADIANT_FRACTION, y as SMOKE_FADE_IN, p as STABLE_CALM_RISE, m as STABLE_WIND_RISE, k as Smoke, f as TWO_THIRDS_LAW, C as airStability, S as buoyancyFlux, w as plumeRise, x as smokeAlphaCurve };
package/dist/soft.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { VFXFeatures } from './base.js';
2
+ /**
3
+ * WGSL for depth-soft billboards, shared by the particle billboard and `@margoengine/vfx/clouds`: the uniform at
4
+ * `@group(0) @binding(11)` and the scene depth at binding 12 (multisampled when `samples` > 1), with
5
+ * `softGap(position, viewDepth)`, the gap from a fragment at `viewDepth` (its depth along the view axis,
6
+ * `dot(soft.plane.xyz, world) + soft.plane.w`) to the scene depth behind it, over the softness. Above 1 where the scene
7
+ * depth is empty. Clamp it to [0, 1] for a linear fade (Lorach 2007, "Soft Particles", NVIDIA).
8
+ */
9
+ export declare function softDepthWgsl(samples: number): string;
10
+ /**
11
+ * The `soft` feature: `softness` (world units) fades each particle out over that distance in front of whatever opaque
12
+ * surface is behind it, so a particle meeting the floor or a wall no longer shows a hard line. Scene depth comes from the
13
+ * `depth` option, a function returning a sampleable reversed-Z depth texture the size of the drawing buffer (for example
14
+ * `() => depthTexture.texture` with `DepthTexture` from `@margoengine/post/depth`). Pass it as `{soft}` to `VFX` from
15
+ * `@margoengine/vfx/base`; `VFX` from `@margoengine/vfx` already has it.
16
+ */
17
+ export declare const soft: NonNullable<VFXFeatures['soft']>;
package/dist/soft.js ADDED
@@ -0,0 +1,55 @@
1
+ import { n as e } from "./view-rect-DkeiiUtw.js";
2
+ import { MargoError as t } from "@margoengine/math/errors";
3
+ import { invertMatrix as n } from "@margoengine/math/mat4";
4
+ //#region src/soft.ts
5
+ var r = 112;
6
+ function i(e) {
7
+ let t = e > 1;
8
+ return `
9
+ struct Soft {inverse:mat4x4f,plane:vec4f,screen:vec4f,softness:vec4f}
10
+ @group(0) @binding(11) var<uniform> soft:Soft;
11
+ @group(0) @binding(12) var sceneDepth:texture_depth${t ? "_multisampled" : ""}_2d;
12
+
13
+ fn softGap(position:vec4f,viewDepth:f32)->f32 {
14
+ let at=vec2i(position.xy);var d=textureLoad(sceneDepth,at,0);
15
+ ${t ? `for(var s=1;s<${e};s++){d=max(d,textureLoad(sceneDepth,at,s));}` : ""}
16
+ if(d<=0.0){return 2.0;}
17
+ let ndc=vec2f((position.x-soft.screen.z)/soft.screen.x*2-1,1-(position.y-soft.screen.w)/soft.screen.y*2);
18
+ let w=soft.inverse*vec4f(ndc,d,1);
19
+ return (dot(soft.plane.xyz,w.xyz/w.w)+soft.plane.w-viewDepth)/soft.softness.x;
20
+ }
21
+ `;
22
+ }
23
+ var a = {
24
+ patch(e, t) {
25
+ return e.replace("@location(0) uv:vec2f,", "@location(0) uv:vec2f,@location(6) viewDepth:f32,").replace("o.clip=f.camera*vec4f(position,1);", "o.clip=f.camera*vec4f(position,1);o.viewDepth=dot(soft.plane.xyz,position)+soft.plane.w;").replace("@fragment fn fragment(i:Out)->@location(0) vec4f {", "@fragment fn fragment(i:Out)->@location(0) vec4f {return softened(i,unsoftened(i));}\nfn unsoftened(i:Out)->vec4f {") + i(t) + "\nfn softened(i:Out,c:vec4f)->vec4f {return vec4f(c.rgb,c.a*clamp(softGap(i.clip,i.viewDepth),0.0,1.0));}\n";
26
+ },
27
+ create(i, a) {
28
+ let o = i.createBuffer({
29
+ size: r,
30
+ usage: 72
31
+ });
32
+ a.push(o);
33
+ let s = new Float32Array(r / 4), c = s.subarray(0, 16), l = [{
34
+ binding: 11,
35
+ resource: { buffer: o }
36
+ }, {
37
+ binding: 12,
38
+ resource: void 0
39
+ }], u;
40
+ return {
41
+ entries: l,
42
+ changed: !1,
43
+ write(r, i, a, d) {
44
+ if (!i) throw new t("state", "VFX softness needs scene depth but depth() returned undefined; add DepthTexture from @margoengine/post/depth before the first frame, or set softness to 0");
45
+ let f = i.sampleCount;
46
+ if (!i.format.startsWith("depth") || i.dimension !== "2d" || i.width !== r.width || i.height !== r.height || !(i.usage & 4) || f !== 1 && f !== (r.descriptor.multisample?.count ?? 1)) throw RangeError("VFX depth must be a sampleable depth texture of the drawing size, with 1 sample or the scene sample count; pass the texture of DepthTexture from @margoengine/post/depth");
47
+ if (this.changed = i !== u, this.changed && (u = i, l[1].resource = i.createView({ aspect: "depth-only" })), !n(r.camera.matrix, c)) return f;
48
+ let p = -a[2], m = -a[6], h = -a[10], g = r.camera.position, _ = e(r);
49
+ return s[16] = p, s[17] = m, s[18] = h, s[19] = -(p * g[0] + m * g[1] + h * g[2]), s[20] = _?.[2] ?? r.width, s[21] = _?.[3] ?? r.height, s[22] = _?.[0] ?? 0, s[23] = _?.[1] ?? 0, s[24] = d, r.device.queue.writeBuffer(o, 0, s), f;
50
+ }
51
+ };
52
+ }
53
+ };
54
+ //#endregion
55
+ export { a as soft, i as softDepthWgsl };