@margoengine/vfx 0.0.0-stage → 1.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.
Files changed (104) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +2240 -2
  4. package/dist/base-LGs65K7K.js +709 -0
  5. package/dist/base.d.ts +869 -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 +237 -0
  10. package/dist/clouds.js +583 -0
  11. package/dist/compat-BFjXAQuu.js +14 -0
  12. package/dist/compat-CVSEj1Bp.js +217 -0
  13. package/dist/compat.d.ts +59 -0
  14. package/dist/curves.d.ts +11 -0
  15. package/dist/curves.js +59 -0
  16. package/dist/debris.d.ts +154 -0
  17. package/dist/debris.js +290 -0
  18. package/dist/depth-collide.d.ts +7 -0
  19. package/dist/depth-collide.js +96 -0
  20. package/dist/dust-current.d.ts +66 -0
  21. package/dist/dust-current.js +92 -0
  22. package/dist/emitter.d.ts +10 -0
  23. package/dist/emitter.js +125 -0
  24. package/dist/events.d.ts +9 -0
  25. package/dist/events.js +131 -0
  26. package/dist/extra-CgGjbojB.js +4 -0
  27. package/dist/extra.d.ts +2 -0
  28. package/dist/fields.d.ts +11 -0
  29. package/dist/fields.js +95 -0
  30. package/dist/fire-front.d.ts +610 -0
  31. package/dist/fire-front.js +1080 -0
  32. package/dist/fire.d.ts +140 -0
  33. package/dist/fire.js +194 -0
  34. package/dist/flame-ramp-VoBKpYnM.js +75 -0
  35. package/dist/flame-ramp.d.ts +61 -0
  36. package/dist/flame.d.ts +428 -0
  37. package/dist/flame.js +817 -0
  38. package/dist/flipbook.d.ts +7 -0
  39. package/dist/flipbook.js +23 -0
  40. package/dist/flocking-shaders.d.ts +48 -0
  41. package/dist/flocking-sim.d.ts +623 -0
  42. package/dist/flocking.d.ts +145 -0
  43. package/dist/flocking.js +1846 -0
  44. package/dist/fog-B-kBv7g0.js +32 -0
  45. package/dist/frame-BuvMFBPp.js +4 -0
  46. package/dist/frame.d.ts +7 -0
  47. package/dist/gunfire.d.ts +742 -0
  48. package/dist/gunfire.js +1214 -0
  49. package/dist/hash-Btsp6fGK.js +7 -0
  50. package/dist/hash.d.ts +2 -0
  51. package/dist/index.d.ts +17 -0
  52. package/dist/index.js +60 -0
  53. package/dist/json.d.ts +53 -0
  54. package/dist/json.js +239 -0
  55. package/dist/lit-Dx2tNwP-.js +152 -0
  56. package/dist/lit.d.ts +18 -0
  57. package/dist/lit.js +1 -0
  58. package/dist/mesh.d.ts +16 -0
  59. package/dist/mesh.js +248 -0
  60. package/dist/node-system.d.ts +315 -0
  61. package/dist/node-system.js +384 -0
  62. package/dist/nodes.d.ts +14 -0
  63. package/dist/nodes.js +293 -0
  64. package/dist/obstacles.d.ts +3 -0
  65. package/dist/obstacles.js +25 -0
  66. package/dist/over-life.d.ts +14 -0
  67. package/dist/over-life.js +94 -0
  68. package/dist/reactive-mask-B8xbyzlk.js +69 -0
  69. package/dist/require-webgpu--tUXPsmA.js +7 -0
  70. package/dist/ribbons.d.ts +22 -0
  71. package/dist/ribbons.js +165 -0
  72. package/dist/schedule.d.ts +29 -0
  73. package/dist/schedule.js +47 -0
  74. package/dist/settings.d.ts +362 -0
  75. package/dist/settings.js +707 -0
  76. package/dist/shader.d.ts +49 -0
  77. package/dist/shadows.d.ts +26 -0
  78. package/dist/shadows.js +182 -0
  79. package/dist/shapes.d.ts +34 -0
  80. package/dist/shapes.js +81 -0
  81. package/dist/shared/fog.d.ts +33 -0
  82. package/dist/shared/pass.d.ts +53 -0
  83. package/dist/shared/settings.d.ts +89 -0
  84. package/dist/skinned.d.ts +11 -0
  85. package/dist/skinned.js +104 -0
  86. package/dist/smoke.d.ts +283 -0
  87. package/dist/smoke.js +504 -0
  88. package/dist/soft.d.ts +17 -0
  89. package/dist/soft.js +55 -0
  90. package/dist/solid.d.ts +168 -0
  91. package/dist/solid.js +403 -0
  92. package/dist/sort.d.ts +3 -0
  93. package/dist/sort.js +58 -0
  94. package/dist/space.d.ts +14 -0
  95. package/dist/space.js +88 -0
  96. package/dist/stretch.d.ts +7 -0
  97. package/dist/stretch.js +40 -0
  98. package/dist/texture.d.ts +8 -0
  99. package/dist/texture.js +45 -0
  100. package/dist/view-RKprOPAy.js +30 -0
  101. package/dist/view-rect-DkeiiUtw.js +13 -0
  102. package/dist/wrap.d.ts +3 -0
  103. package/dist/wrap.js +5 -0
  104. package/package.json +198 -4
package/dist/debris.js ADDED
@@ -0,0 +1,290 @@
1
+ import { t as e } from "./hash-Btsp6fGK.js";
2
+ //#region ../shared/film-time.js
3
+ function t(e, t, n) {
4
+ return (typeof e != "number" || !Number.isFinite(e) || e < 0) && t(`${n} must be 0 or more.`), e;
5
+ }
6
+ //#endregion
7
+ //#region src/debris.ts
8
+ var n = 1 / 60, r = 120, i = (e) => {
9
+ throw RangeError(`Debris option out of range; ${e}`);
10
+ }, a = (e) => typeof e == "number" && Number.isFinite(e), o = (t) => e(t) / 4294967296, s = {
11
+ radius: [.001, 1e5],
12
+ count: [1, 2e4],
13
+ start: [0, 1e9],
14
+ spread: [0, 30],
15
+ speed: [1, 1e3],
16
+ maxSpeed: [1, 2e3],
17
+ mu: [.2, .7],
18
+ angle: [10, 90],
19
+ angleSpread: [0, 40],
20
+ density: [100, 5e3],
21
+ drag: [0, 3],
22
+ airDensity: [0, 2],
23
+ tumble: [0, 100],
24
+ puffLife: [0, 60],
25
+ puffScale: [0, 1]
26
+ }, c = class {
27
+ constructor(e = {}) {
28
+ if (this.film = 0, this.puff = null, this.puffTarget = null, typeof e != "object" || !e) throw TypeError("Debris options must be an object; pass {} or leave it out.");
29
+ let t = e;
30
+ for (let [e, [n, r]] of Object.entries(s)) t[e] !== void 0 && (!a(t[e]) || t[e] < n || t[e] > r) && i(`${e} must be a number from ${n} to ${r}.`);
31
+ for (let e of ["color", "puffColor"]) if (t[e] !== void 0) {
32
+ let n = t[e];
33
+ (!Array.isArray(n) || n.length !== 3 || !n.every((e) => a(e) && e >= 0 && e <= 1)) && i(`${e} must be three linear RGB numbers from 0 to 1.`);
34
+ }
35
+ if (t.origin !== void 0 && (!Array.isArray(t.origin) || t.origin.length !== 3 || !t.origin.every(a))) throw TypeError("Debris origin must be three numbers; pass [x, y, z].");
36
+ if (t.size !== void 0 && (!Array.isArray(t.size) || t.size.length !== 2 || !t.size.every((e) => a(e) && e > 0) || t.size[0] > t.size[1]) && i("size must be [smallest, largest], each above 0."), t.count !== void 0 && !Number.isInteger(t.count) && i("count must be a whole number."), t.seed !== void 0 && !Number.isInteger(t.seed) && i("seed must be an integer."), t.ground !== void 0 && typeof t.ground != "function") throw TypeError("Debris ground must be a function; pass (x, z) => height.");
37
+ let c = e.speed ?? 40, l = e.maxSpeed ?? Math.min(4 * c, 2e3);
38
+ l < c && i("maxSpeed must be at least speed."), this.options = e;
39
+ let [u, d, f] = e.origin ?? [
40
+ 0,
41
+ 0,
42
+ 0
43
+ ], p = e.radius ?? 30, m = e.count ?? 300, h = e.seed ?? 1, g = e.mu ?? .41, _ = (e.angle ?? 50) * Math.PI / 180, v = (e.angleSpread ?? 12) * Math.PI / 180, [y, b] = e.size ?? [.2, 1.2], x = e.density ?? 1800, S = e.drag ?? 1, C = e.airDensity ?? 1.2, w = e.ground ?? (() => d), T = e.start ?? 0, E = e.spread ?? 1.5, D = e.tumble ?? 6, O = (e, t) => o((Math.imul(h, 2654435761) ^ Math.imul(e + 1, 2246822507) ^ Math.imul(t + 1, 3266489909)) >>> 0);
44
+ this.paths = [];
45
+ for (let e = 0; e < m; e++) {
46
+ let t = p * Math.sqrt(Math.max(O(e, 0), 1e-6)), i = O(e, 1) * 2 * Math.PI, a = Math.min(c * (t / p) ** (-1 / g), l), o = Math.min(Math.max(_ + v * (O(e, 2) + O(e, 3) - 1), .05), Math.PI / 2), s = y * (b / y) ** O(e, 4), d = x * s ** 3, m = .5 * C * S * s * s / d, h = u + t * Math.cos(i), k = f + t * Math.sin(i), A = T + E * (t / p) ** 2 * (.8 + .2 * O(e, 5)), j = [
47
+ h,
48
+ w(h, k),
49
+ k,
50
+ a * Math.cos(o) * Math.cos(i),
51
+ a * Math.sin(o),
52
+ a * Math.cos(o) * Math.sin(i)
53
+ ], M = [...j], N = (e) => {
54
+ let t = Math.hypot(e[3], e[4], e[5]);
55
+ return [
56
+ e[3],
57
+ e[4],
58
+ e[5],
59
+ -m * t * e[3],
60
+ -9.80665 - m * t * e[4],
61
+ -m * t * e[5]
62
+ ];
63
+ }, P = Infinity, F = 0;
64
+ for (let e = 1; e * n <= r; e++) {
65
+ let t = N(j), r = N(j.map((e, r) => e + .5 * n * t[r])), i = N(j.map((e, t) => e + .5 * n * r[t])), a = N(j.map((e, t) => e + n * i[t])), o = j.map((e, o) => e + n / 6 * (t[o] + 2 * r[o] + 2 * i[o] + a[o])), s = o[1] - w(o[0], o[2]);
66
+ if (o[4] < 0 && s <= 0) {
67
+ let t = j[1] - w(j[0], j[2]), r = t / Math.max(t - s, 1e-12), i = j.map((e, t) => e + (o[t] - e) * r);
68
+ i[1] = w(i[0], i[2]), M.push(...i), P = (e - 1 + r) * n, F = .5 * d * (i[3] ** 2 + i[4] ** 2 + i[5] ** 2);
69
+ break;
70
+ }
71
+ M.push(...o), j = o;
72
+ }
73
+ let I = O(e, 6) * 2 - 1, L = O(e, 7) * 2 - 1, R = O(e, 8) * 2 - 1, z = Math.hypot(I, L, R) || 1;
74
+ this.paths.push({
75
+ launch: A,
76
+ size: s,
77
+ mass: d,
78
+ shade: .7 + .6 * O(e, 9),
79
+ axis: [
80
+ I / z,
81
+ L / z,
82
+ R / z
83
+ ],
84
+ spin: D * (.3 + .7 * O(e, 10)),
85
+ rows: Float64Array.from(M),
86
+ flight: P,
87
+ impact: F
88
+ });
89
+ }
90
+ }
91
+ get time() {
92
+ return this.film;
93
+ }
94
+ seek(e) {
95
+ this.film = t(e, i, "seek time");
96
+ }
97
+ update(e) {
98
+ this.film += t(e, i, "update delta");
99
+ }
100
+ at(e, t) {
101
+ let r = t - e.launch, i = e.rows.length / 6 - 1;
102
+ if (r < 0) return {
103
+ pos: [
104
+ e.rows[0],
105
+ e.rows[1],
106
+ e.rows[2]
107
+ ],
108
+ vel: [
109
+ 0,
110
+ 0,
111
+ 0
112
+ ],
113
+ state: "waiting",
114
+ age: r
115
+ };
116
+ if (r >= e.flight) {
117
+ let t = i * 6;
118
+ return {
119
+ pos: [
120
+ e.rows[t],
121
+ e.rows[t + 1],
122
+ e.rows[t + 2]
123
+ ],
124
+ vel: [
125
+ 0,
126
+ 0,
127
+ 0
128
+ ],
129
+ state: "landed",
130
+ age: r
131
+ };
132
+ }
133
+ let a = r / n, o = Math.min(Math.floor(a), i - 1), s = Math.min((o + 1) * n, e.flight) - o * n, c = (r - o * n) / s, l = o * 6, u = (o + 1) * 6, d = 2 * c ** 3 - 3 * c ** 2 + 1, f = c ** 3 - 2 * c ** 2 + c, p = -2 * c ** 3 + 3 * c ** 2, m = c ** 3 - c ** 2;
134
+ return {
135
+ pos: [
136
+ 0,
137
+ 1,
138
+ 2
139
+ ].map((t) => d * e.rows[l + t] + f * s * e.rows[l + 3 + t] + p * e.rows[u + t] + m * s * e.rows[u + 3 + t]),
140
+ vel: [
141
+ 3,
142
+ 4,
143
+ 5
144
+ ].map((t) => e.rows[l + t] * (1 - c) + e.rows[u + t] * c),
145
+ state: "flying",
146
+ age: r
147
+ };
148
+ }
149
+ pieces(e = this.film) {
150
+ return this.paths.map((t) => {
151
+ let n = this.at(t, e);
152
+ return {
153
+ position: n.pos,
154
+ velocity: n.vel,
155
+ size: t.size,
156
+ state: n.state,
157
+ launch: t.launch,
158
+ landing: t.launch + t.flight
159
+ };
160
+ });
161
+ }
162
+ draw(e) {
163
+ let t = this.options.color ?? [
164
+ .22,
165
+ .16,
166
+ .1
167
+ ], n = /* @__PURE__ */ new Float32Array(16), r = /* @__PURE__ */ new Float32Array(3), i = 0;
168
+ for (let a of this.paths) {
169
+ if (i >= e.capacity) break;
170
+ let o = this.at(a, this.film);
171
+ if (o.state === "waiting") continue;
172
+ let s = a.spin * Math.min(Math.max(o.age, 0), a.flight), [c, l, u] = a.axis, d = Math.cos(s), f = Math.sin(s), p = 1 - d, m = a.size;
173
+ n.set([
174
+ (p * c * c + d) * m,
175
+ (p * c * l + f * u) * m,
176
+ (p * c * u - f * l) * m,
177
+ 0,
178
+ (p * c * l - f * u) * m,
179
+ (p * l * l + d) * m,
180
+ (p * l * u + f * c) * m,
181
+ 0,
182
+ (p * c * u + f * l) * m,
183
+ (p * l * u - f * c) * m,
184
+ (p * u * u + d) * m,
185
+ 0,
186
+ o.pos[0],
187
+ o.pos[1] + (o.state === "landed" ? .25 * m : 0),
188
+ o.pos[2],
189
+ 1
190
+ ]), e.setMatrixAt(i, n), e.setColorAt && (r[0] = Math.min(1, t[0] * a.shade), r[1] = Math.min(1, t[1] * a.shade), r[2] = Math.min(1, t[2] * a.shade), e.setColorAt(i, r)), i++;
191
+ }
192
+ return e.count = i, i;
193
+ }
194
+ puffs(e = this.film) {
195
+ let t = this.options.puffLife ?? 6, n = this.options.puffScale ?? .04, r = [];
196
+ if (t === 0 || n === 0) return r;
197
+ for (let i of this.paths) {
198
+ let a = e - i.launch - i.flight;
199
+ if (!(a >= 0 && a < t)) continue;
200
+ let o = n * Math.cbrt(i.impact) * Math.cbrt(Math.max(a / t, .001)) + i.size, s = (i.rows.length / 6 - 1) * 6;
201
+ r.push({
202
+ centre: [
203
+ i.rows[s],
204
+ i.rows[s + 1] + .3 * o,
205
+ i.rows[s + 2]
206
+ ],
207
+ size: o,
208
+ opacity: (1 - a / t) ** 2
209
+ });
210
+ }
211
+ return r;
212
+ }
213
+ drawPuffs(e) {
214
+ this.puffTarget && this.puffTarget !== e && this.clearPuffs(), this.puffTarget = e;
215
+ let t = this.puffs();
216
+ if (!t.length) {
217
+ this.puff &&= (e.remove(this.puff), null);
218
+ return;
219
+ }
220
+ let n = Infinity, r = Infinity, i = Infinity, a = -Infinity, o = -Infinity, s = -Infinity, c = 0, l = 0;
221
+ for (let e of t) n = Math.min(n, e.centre[0]), a = Math.max(a, e.centre[0]), r = Math.min(r, e.centre[1]), o = Math.max(o, e.centre[1]), i = Math.min(i, e.centre[2]), s = Math.max(s, e.centre[2]), c = Math.max(c, e.size), l += e.opacity;
222
+ let u = .5 * (n + a), d = .5 * (r + o), f = .5 * (i + s), p = [
223
+ Math.max(a - n, .001),
224
+ Math.max(o - r, .001),
225
+ Math.max(s - i, .001)
226
+ ], m = {
227
+ seed: this.options.seed ?? 1,
228
+ segments: t.length,
229
+ bounds: p,
230
+ volume: c,
231
+ color: [...this.options.puffColor ?? [
232
+ .36,
233
+ .3,
234
+ .23
235
+ ]],
236
+ opacity: Math.min(1, l / t.length),
237
+ distribute: (e) => {
238
+ let n = t[e];
239
+ return {
240
+ point: [
241
+ (n.centre[0] - u) * 2 / p[0],
242
+ (n.centre[1] - d) * 2 / p[1],
243
+ (n.centre[2] - f) * 2 / p[2]
244
+ ],
245
+ volume: n.size / c * (.4 + .6 * n.opacity)
246
+ };
247
+ }
248
+ };
249
+ this.puff ? Object.assign(this.puff.options, m) : this.puff = e.add(m);
250
+ let h = this.puff.matrix;
251
+ h.fill(0), h[0] = h[5] = h[10] = h[15] = 1, h[12] = u, h[13] = d, h[14] = f;
252
+ }
253
+ clearPuffs() {
254
+ this.puff && this.puffTarget?.remove(this.puff), this.puff = null;
255
+ }
256
+ };
257
+ function l(e, t, n = {}) {
258
+ let r = n.height ?? (() => 0), i = n.breakup ?? .5, a = n.bury ?? .35, s = n.meshSize ?? 1, c = n.colors ?? [[
259
+ .36,
260
+ .2,
261
+ .14,
262
+ 1
263
+ ]], l = n.seed ?? 1, u = t.sizes.length - 1, d = t.columns * t.rows;
264
+ if (!(i >= .1 && i <= 1) || !(a >= 0 && a <= 1) || !(s > 0) || !c.length) throw RangeError("drawSpoil: breakup 0.1 to 1, bury 0 to 1, meshSize above 0 and one colour or more; pass them so.");
265
+ if (!(u >= 1 && t.counts.length === d * u && t.cell > 0)) throw RangeError("drawSpoil: pass a spoil field of counts per cell and bin (ejectaSpoil of @margoengine/physics/crater).");
266
+ let f = 1 / (i * i * i), p = new Float64Array(u);
267
+ for (let e = 0; e < d; e++) for (let n = 0; n < u; n++) p[n] += t.counts[e * u + n] * f;
268
+ let m = new Float64Array(u), h = Math.floor(.97 * e.capacity), g = Infinity, _ = 0, v = 0;
269
+ for (let e = u - 1; e >= 0; e--) _ += p[e], m[e] = h <= 0 ? 0 : Math.min(1, h / Math.max(p[e], 1e-12)), h -= p[e] * m[e], v += p[e] * m[e], m[e] > 0 && (g = Math.min(g, i * t.sizes[e]));
270
+ let y = /* @__PURE__ */ new Float32Array(16), b = 0;
271
+ for (let n = 0; n < d && b < e.capacity; n++) {
272
+ let d = n % t.columns, p = (n - d) / t.columns;
273
+ for (let h = u - 1; h >= 0 && b < e.capacity; h--) {
274
+ let g = t.counts[n * u + h] * f * m[h];
275
+ if (!(g > 0)) continue;
276
+ let _ = Math.imul(n, 64) + h ^ Math.imul(l, 2654435769), v = Math.min(e.capacity - b, Math.floor(g + o(_)));
277
+ for (let n = 0; n < v; n++) {
278
+ let l = Math.imul(_, 2246822507) + n, u = t.origin[0] + (d + o(l * 5 + 1)) * t.cell, f = t.origin[1] + (p + o(l * 5 + 2)) * t.cell, m = t.sizes[h], g = i * m * (t.sizes[h + 1] / m) ** o(l * 5 + 3), v = 2 * Math.PI * o(l * 5 + 4), x = g / s, S = Math.cos(v), C = Math.sin(v);
279
+ y.fill(0), y[0] = x * S, y[2] = -x * C, y[5] = x, y[8] = x * C, y[10] = x * S, y[12] = u, y[13] = r(u, f) - a * g, y[14] = f, y[15] = 1, e.setMatrixAt(b, y), e.setColorAt?.(b, c[b % c.length]), b++;
280
+ }
281
+ }
282
+ }
283
+ return e.count = b, {
284
+ drawn: b,
285
+ smallest: Number.isFinite(g) ? g : 0,
286
+ share: _ > 0 ? v / _ : 1
287
+ };
288
+ }
289
+ //#endregion
290
+ export { c as Debris, l as drawSpoil };
@@ -0,0 +1,7 @@
1
+ import type { VFXDepthCollide, VFXExtension } from './base.js';
2
+ /**
3
+ * The `depthCollide` feature: particles bounce off whatever was drawn last frame, using the scene depth from the
4
+ * `depth` option (as `softness` does). Pass it as `{depthCollide}` to `VFX` from `@margoengine/vfx/base`; the root
5
+ * `VFX` has it. `thickness` is read every frame, `bounce` is the effect's own.
6
+ */
7
+ export declare const depthCollide: VFXExtension<VFXDepthCollide>;
@@ -0,0 +1,96 @@
1
+ import { n as e } from "./view-rect-DkeiiUtw.js";
2
+ import { invertMatrix as t } from "@margoengine/math/mat4";
3
+ //#region src/depth-collide.ts
4
+ var n = 176, r = (e) => `
5
+ struct Collide {last:mat4x4f,inverse:mat4x4f,eye:vec4f,screen:vec4f,shape:vec4f}
6
+ @group(0) @binding(0) var<uniform> collide:Collide;
7
+ @group(0) @binding(1) var<storage,read_write> particles:array<vec4f>;
8
+ @group(0) @binding(2) var<storage,read> alive:array<u32>;
9
+ @group(0) @binding(3) var<storage,read> visible:array<u32,5>;
10
+ @group(0) @binding(4) var sceneDepth:texture_depth${e > 1 ? "_multisampled" : ""}_2d;
11
+
12
+ fn surfaceAt(t:vec2i)->vec4f {
13
+ let at=clamp(t,vec2i(0),vec2i(textureDimensions(sceneDepth))-1);var d=textureLoad(sceneDepth,at,0);
14
+ ${e > 1 ? `for(var s=1;s<${e};s++){d=max(d,textureLoad(sceneDepth,at,s));}` : ""}
15
+ let pixel=vec2f(at)+0.5;let ndc=vec2f((pixel.x-collide.screen.z)/collide.screen.x*2-1,1-(pixel.y-collide.screen.w)/collide.screen.y*2);
16
+ let w=collide.inverse*vec4f(ndc,d,1);return vec4f(w.xyz/w.w,d);
17
+ }
18
+ @compute @workgroup_size(256) fn collideDepth(@builtin(global_invocation_id) id:vec3u) {
19
+ if(id.x>=visible[1]){return;}
20
+ let slot=alive[id.x];let a=particles[slot*2];let b=particles[slot*2+1];
21
+ if(a.w<=0.0){return;}
22
+ let clip=collide.last*vec4f(a.xyz,1);if(clip.w<=0.0){return;}
23
+ let ndc=clip.xy/clip.w;if(any(abs(ndc)>vec2f(1))){return;}
24
+ let t=vec2i(floor(vec2f(ndc.x*0.5+0.5,0.5-ndc.y*0.5)*collide.screen.xy+collide.screen.zw));
25
+ let s=surfaceAt(t);if(s.w<=0.0){return;}
26
+ let radius=b.w*0.5;let gap=dot(a.xyz-s.xyz,normalize(a.xyz-collide.eye.xyz));
27
+ if(gap<=-radius||gap>=collide.shape.x){return;}
28
+ let right=surfaceAt(t+vec2i(1,0));let left=surfaceAt(t-vec2i(1,0));let down=surfaceAt(t+vec2i(0,1));let up=surfaceAt(t-vec2i(0,1));
29
+ let dx=select(s.xyz-left.xyz,right.xyz-s.xyz,abs(right.w-s.w)<abs(left.w-s.w));
30
+ let dy=select(s.xyz-up.xyz,down.xyz-s.xyz,abs(down.w-s.w)<abs(up.w-s.w));
31
+ var n=cross(dx,dy);let size=length(n);if(size<=0.0){return;}n/=size;
32
+ if(dot(n,collide.eye.xyz-s.xyz)<0.0){n=-n;}
33
+ var v=b.xyz;let into=dot(v,n);if(into<0.0){v-=(1.0+collide.shape.y)*into*n;}
34
+ particles[slot*2]=vec4f(a.xyz+n*max(0.0,radius-dot(a.xyz-s.xyz,n)),a.w);particles[slot*2+1]=vec4f(v,b.w);
35
+ }
36
+ `, i = {
37
+ validate(e, t) {
38
+ if (!e || typeof e != "object") throw TypeError("VFX depthCollide must be an object; pass {} or {thickness: 0.5}");
39
+ let n = e.thickness ?? .5;
40
+ if (typeof n != "number" || !Number.isFinite(n) || n <= 0 || n > 1e6) throw RangeError("VFX depthCollide thickness must be a world distance above 0; pass e.g. 0.5");
41
+ if (typeof t.depth != "function") throw TypeError("VFX depthCollide needs scene depth: add DepthTexture from @margoengine/post/depth and pass depth: () => depth.texture");
42
+ },
43
+ create(i, a, o) {
44
+ let { device: s, options: c } = i, l = s.createBuffer({
45
+ size: n,
46
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
47
+ });
48
+ o.push(l);
49
+ let u = new Float32Array(n / 4), d = u.subarray(0, 16), f = u.subarray(16, 32), p = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new WeakMap(), h = Math.ceil(i.capacity / 256), g = !1;
50
+ return {
51
+ write(n) {
52
+ let o = c.depthCollide, _ = c.depth?.();
53
+ if (o && o !== a) throw TypeError("VFX depthCollide is fixed at attach; change its thickness in place or attach a new VFX");
54
+ if (o && g && _) {
55
+ let e = _.sampleCount;
56
+ if (!_.format.startsWith("depth") || _.dimension !== "2d" || !(_.usage & 4)) throw RangeError("VFX depthCollide needs a sampleable depth texture; pass the texture of DepthTexture from @margoengine/post/depth");
57
+ u[40] = o.thickness ?? .5, u[41] = c.bounce ?? .3, n.device.queue.writeBuffer(l, 0, u);
58
+ let t = p.get(e);
59
+ t || (t = s.createComputePipeline({
60
+ layout: "auto",
61
+ compute: {
62
+ module: s.createShaderModule({ code: r(e) }),
63
+ entryPoint: "collideDepth"
64
+ }
65
+ }), p.set(e, t));
66
+ let a = m.get(_);
67
+ a || m.set(_, a = s.createBindGroup({
68
+ layout: t.getBindGroupLayout(0),
69
+ entries: [...[
70
+ l,
71
+ i.particles,
72
+ i.alive,
73
+ i.visible
74
+ ].map((e, t) => ({
75
+ binding: t,
76
+ resource: { buffer: e }
77
+ })), {
78
+ binding: 4,
79
+ resource: _.createView({ aspect: "depth-only" })
80
+ }]
81
+ }));
82
+ let d = n.encoder.beginComputePass();
83
+ d.setPipeline(t), d.setBindGroup(0, a), d.dispatchWorkgroups(h), d.end();
84
+ }
85
+ g = t(n.camera.matrix, f) !== null, d.set(n.camera.matrix);
86
+ let v = n.camera.position, y = e(n);
87
+ u.set(v, 32), u[36] = y?.[2] ?? n.width, u[37] = y?.[3] ?? n.height, u[38] = y?.[0] ?? 0, u[39] = y?.[1] ?? 0;
88
+ },
89
+ reset() {
90
+ g = !1;
91
+ }
92
+ };
93
+ }
94
+ };
95
+ //#endregion
96
+ export { i as depthCollide };
@@ -0,0 +1,66 @@
1
+ /** The current to draw: its grid and, at a point, its depth (m) and its dust's optical depth; and the ground's height. */
2
+ export interface DustCurrentSource {
3
+ /** The grid: cells across X and Z, its size, m, and its centre [x, z]. */
4
+ readonly shape: {
5
+ readonly columns: number;
6
+ readonly rows: number;
7
+ readonly width: number;
8
+ readonly depth: number;
9
+ readonly centre: readonly [number, number];
10
+ };
11
+ /** The current at (x, z), m: its depth, m, and its dust's optical depth (1.5 a h / r for grains of radius r). */
12
+ sample(x: number, z: number, out: {
13
+ depth: number;
14
+ opacity: number;
15
+ }): {
16
+ depth: number;
17
+ opacity: number;
18
+ };
19
+ /** The ground's height at (x, z), m. */
20
+ ground(x: number, z: number): number;
21
+ }
22
+ /** What `DustCurrent.draw` draws into: a `Clouds` of `@margoengine/vfx/clouds` fits. */
23
+ export interface DustCurrentTarget {
24
+ add(options: object): {
25
+ readonly options: object;
26
+ readonly matrix: Float32Array;
27
+ };
28
+ remove(cloud: object): boolean;
29
+ }
30
+ /** Options of {@link DustCurrent}. Every field has a documented default; the constructor checks the ranges. */
31
+ export interface DustCurrentOptions {
32
+ /** Cells a puff covers along each axis (default 2): fewer puffs for a fine grid. A whole number from 1 to 64. */
33
+ readonly stride?: number;
34
+ /** The least optical depth a patch needs to be drawn, as the share of light it stops (default 0.1: about a tenth). From 0 to 10. */
35
+ readonly least?: number;
36
+ /** At most this many puffs (default 1024); a patch past it widens the stride. A whole number from 1 to 65,536. */
37
+ readonly most?: number;
38
+ /** The dust's colour, linear RGB (default [0.36, 0.3, 0.23], dry earth, as `Debris`'s dust). */
39
+ readonly color?: readonly [number, number, number];
40
+ /** The puffs' layout seed (default 1). */
41
+ readonly seed?: number;
42
+ }
43
+ /** One puff as `DustCurrent` lays it: its centre, m, its width, m, and the share of light its patch stops. */
44
+ export interface DustCurrentPuff {
45
+ readonly centre: readonly [number, number, number];
46
+ readonly size: number;
47
+ readonly visibility: number;
48
+ }
49
+ /** Draws a dusty current on the ground into a `Clouds` as puffs, one a visible patch, with its visibility as its size. */
50
+ export declare class DustCurrent {
51
+ readonly source: DustCurrentSource;
52
+ private readonly stride;
53
+ private readonly least;
54
+ private readonly most;
55
+ private readonly color;
56
+ private readonly seed;
57
+ private cloud;
58
+ private target;
59
+ constructor(source: DustCurrentSource, options?: DustCurrentOptions);
60
+ /** The puffs the current makes now: one for each visible patch of `stride` by `stride` cells, at most `most`. */
61
+ puffs(): DustCurrentPuff[];
62
+ /** Puts the current's puffs into `clouds` (one cloud, replaced each draw); none when nothing is visible. */
63
+ draw(clouds: DustCurrentTarget): void;
64
+ /** Takes the cloud {@link draw} added out of its `Clouds`. */
65
+ clear(): void;
66
+ }
@@ -0,0 +1,92 @@
1
+ //#region src/dust-current.ts
2
+ function e(e, t, n, r, i = !1) {
3
+ if (!(e >= n && e <= r) || i && !Number.isInteger(e)) throw RangeError(`DustCurrent ${t} is ${e}; pass ${i ? "a whole number" : "a number"} from ${n} to ${r}.`);
4
+ return e;
5
+ }
6
+ var t = class {
7
+ constructor(t, n = {}) {
8
+ if (this.source = t, this.cloud = null, this.target = null, !t?.shape || typeof t.sample != "function" || typeof t.ground != "function") throw TypeError("DustCurrent needs a source with its grid `shape`, `sample(x, z, out)` and `ground(x, z)`.");
9
+ this.stride = e(n.stride ?? 2, "stride", 1, 64, !0), this.least = e(n.least ?? .1, "least optical depth", 0, 10), this.most = e(n.most ?? 1024, "most puffs", 1, 65536, !0);
10
+ let r = n.color ?? [
11
+ .36,
12
+ .3,
13
+ .23
14
+ ];
15
+ if (!(r.length === 3 && r.every((e) => e >= 0 && Number.isFinite(e)))) throw RangeError("DustCurrent color is three linear values of 0 or more; pass [r, g, b].");
16
+ this.color = [
17
+ r[0],
18
+ r[1],
19
+ r[2]
20
+ ], this.seed = e(n.seed ?? 1, "seed", 0, 2 ** 31, !0);
21
+ }
22
+ puffs() {
23
+ let { columns: e, rows: t, width: n, depth: r, centre: i } = this.source.shape, a = n / e, o = r / t, s = {
24
+ depth: 0,
25
+ opacity: 0
26
+ };
27
+ for (let c = this.stride;; c *= 2) {
28
+ let l = [];
29
+ for (let u = 0; u < e; u += c) for (let d = 0; d < t; d += c) {
30
+ let f = 0, p = 0, m = 0;
31
+ for (let l = u; l < Math.min(e, u + c); l++) for (let e = d; e < Math.min(t, d + c); e++) {
32
+ let t = i[0] - n / 2 + (l + .5) * a, c = i[1] - r / 2 + (e + .5) * o;
33
+ this.source.sample(t, c, s), f = Math.max(f, s.depth), p += Math.exp(-Math.max(s.opacity, 0)), m++;
34
+ }
35
+ let h = 1 - p / m;
36
+ if (!(h >= 1 - Math.exp(-this.least)) || !(f > 0)) continue;
37
+ let g = Math.min(c, e - u) * a, _ = i[0] - n / 2 + (u + Math.min(c, e - u) / 2) * a, v = i[1] - r / 2 + (d + Math.min(c, t - d) / 2) * o;
38
+ l.push({
39
+ centre: [
40
+ _,
41
+ this.source.ground(_, v) + f / 2,
42
+ v
43
+ ],
44
+ size: Math.max(g, f),
45
+ visibility: h
46
+ });
47
+ }
48
+ if (l.length <= this.most || c >= Math.max(e, t)) return l.slice(0, this.most);
49
+ }
50
+ }
51
+ draw(e) {
52
+ this.target && this.target !== e && this.clear(), this.target = e;
53
+ let t = this.puffs();
54
+ if (!t.length) {
55
+ this.clear();
56
+ return;
57
+ }
58
+ let n = Infinity, r = Infinity, i = Infinity, a = -Infinity, o = -Infinity, s = -Infinity, c = 0, l = 0;
59
+ for (let e of t) n = Math.min(n, e.centre[0]), a = Math.max(a, e.centre[0]), r = Math.min(r, e.centre[1]), o = Math.max(o, e.centre[1]), i = Math.min(i, e.centre[2]), s = Math.max(s, e.centre[2]), c = Math.max(c, e.size), l += e.visibility;
60
+ let u = .5 * (n + a), d = .5 * (r + o), f = .5 * (i + s), p = [
61
+ Math.max(a - n, .001),
62
+ Math.max(o - r, .001),
63
+ Math.max(s - i, .001)
64
+ ], m = {
65
+ seed: this.seed,
66
+ segments: t.length,
67
+ bounds: p,
68
+ volume: c,
69
+ color: [...this.color],
70
+ opacity: Math.min(1, l / t.length),
71
+ distribute: (e) => {
72
+ let n = t[e];
73
+ return {
74
+ point: [
75
+ (n.centre[0] - u) * 2 / p[0],
76
+ (n.centre[1] - d) * 2 / p[1],
77
+ (n.centre[2] - f) * 2 / p[2]
78
+ ],
79
+ volume: n.size / c * (.4 + .6 * n.visibility)
80
+ };
81
+ }
82
+ };
83
+ this.cloud ? Object.assign(this.cloud.options, m) : this.cloud = e.add(m);
84
+ let h = this.cloud.matrix;
85
+ h.fill(0), h[0] = h[5] = h[10] = h[15] = 1, h[12] = u, h[13] = d, h[14] = f;
86
+ }
87
+ clear() {
88
+ this.cloud && this.target?.remove(this.cloud), this.cloud = null;
89
+ }
90
+ };
91
+ //#endregion
92
+ export { t as DustCurrent };
@@ -0,0 +1,10 @@
1
+ import type { VFXFeatures } from './base.js';
2
+ /** Most triangles of a mesh emitter (each takes 48 bytes on the GPU). */
3
+ export declare const MAX_EMITTER_TRIANGLES = 262144;
4
+ /**
5
+ * The `emitter` feature: where births start and which way `speed` pushes them. `sphere` (ball or shell), `cone` and
6
+ * `mesh` (the surface of a triangle mesh, area-weighted, pushed along each face's normal). Pass it as `{emitter}` to
7
+ * `VFX` from `@margoengine/vfx/base`; the root `VFX` has it. The shape and a mesh's triangles are read at attach; sizes,
8
+ * angle, axis and speed can change every frame.
9
+ */
10
+ export declare const emitter: NonNullable<VFXFeatures['emitter']>;