@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,384 @@
1
+ import { t as e } from "./hash-Btsp6fGK.js";
2
+ import { VFX as t } from "@margoengine/vfx";
3
+ //#region src/node-system.ts
4
+ var n = 1, r = 512, i = 32, a = "https://margoengine.com/errors#vfx-node-system", o = class extends Error {
5
+ constructor(e, t, n, r) {
6
+ super(`${t}; see ${a}`, r), this.name = "VFXNodeSystemError", this.code = e, this.block = n;
7
+ }
8
+ }, s = (e, t, n) => {
9
+ throw new o(e, t, n);
10
+ }, c = (e) => !!e && typeof e == "object" && !Array.isArray(e), l = /* @__PURE__ */ new Set([
11
+ "number",
12
+ "vector",
13
+ "color",
14
+ "random",
15
+ "math",
16
+ "gradient",
17
+ "curve"
18
+ ]), u = /* @__PURE__ */ new Set([
19
+ "create-capacity",
20
+ "create-rate",
21
+ "create-initial",
22
+ "create-lifetime",
23
+ "create-size",
24
+ "create-color",
25
+ "create-position",
26
+ "create-velocity",
27
+ "create-prewarm",
28
+ "create-burst"
29
+ ]), d = /* @__PURE__ */ new Set([
30
+ "update-size",
31
+ "update-alpha",
32
+ "update-color",
33
+ "update-spin",
34
+ "update-speed",
35
+ "update-velocity-limit",
36
+ "update-gravity",
37
+ "update-turbulence",
38
+ "update-ground",
39
+ "update-forces"
40
+ ]), f = /* @__PURE__ */ new Set([
41
+ ...l,
42
+ ...u,
43
+ ...d,
44
+ "shape",
45
+ "system",
46
+ "trigger"
47
+ ]), p = /* @__PURE__ */ new Set([
48
+ "birth",
49
+ "death",
50
+ "collision"
51
+ ]), m = {
52
+ number: ["number", "range"],
53
+ vector: ["vec3"],
54
+ color: ["color"],
55
+ random: [
56
+ "number",
57
+ "range",
58
+ "vec3"
59
+ ],
60
+ math: ["number", "range"],
61
+ gradient: ["gradient"],
62
+ curve: ["curve"]
63
+ };
64
+ function h(n, r = {}) {
65
+ (!c(n) || n.format !== "margo-vfx-node-system") && s("format", "Node system graph needs format \"margo-vfx-node-system\"; pass a saved or authored graph"), n.version !== 1 && s("version", `Node system version ${String(n.version)} is unsupported; migrate to version 1`), Array.isArray(n.blocks) || s("format", "Node system graph needs a blocks array; list every block"), n.blocks.length > 512 && s("limit", "Node system has more than 512 blocks; split it into several graphs");
66
+ let i = r.seed ?? 0;
67
+ if (!Number.isInteger(i) || i < 0 || i > 4294967295) throw RangeError("Node system seed must be an unsigned 32-bit integer; pass 0..4294967295");
68
+ let a = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map();
69
+ n.blocks.forEach((e, t) => {
70
+ (!c(e) || typeof e.id != "string" || e.id === "") && s("format", `Node system block ${t} needs a non-empty string id`), a.has(e.id) && s("duplicate-id", `Node system block id "${e.id}" is used twice`, e.id), (typeof e.type != "string" || !f.has(e.type)) && s("unknown-block", `Node system block "${e.id}" has unknown type ${JSON.stringify(e.type)}`, e.id), a.set(e.id, e), l.set(e.id, t);
71
+ });
72
+ let h = (e, t, n) => {
73
+ let r = typeof e == "string" ? a.get(e) : void 0;
74
+ return r || s("unknown-ref", `${t} names block ${JSON.stringify(e)}, which is not in the graph`, n), r;
75
+ }, g = /* @__PURE__ */ new Map(), _ = /* @__PURE__ */ new Set(), v = (t, n) => {
76
+ let r = typeof t.seed == "number" ? t.seed : l.get(t.id);
77
+ return e(e(i ^ r) + Math.imul(n + 1, 2654435769)) / 4294967296;
78
+ }, y = (e, t, n) => typeof e == "number" ? (Number.isFinite(e) || s("bad-param", `${t} must be a finite number`, n), e) : typeof e == "string" ? S(e, "number", t, n) : s("bad-param", `${t} needs a number or the id of a value block`, n), b = (e, t, n, r) => typeof e == "string" ? S(e, t === 2 ? "range" : t === 3 ? "vec3" : "color", n, r) : ((!Array.isArray(e) || e.length !== t) && s("bad-param", `${n} needs ${t} numbers or the id of a value block`, r), e.map((e, t) => y(e, `${n}[${t}]`, r))), x = (e, t, n, r) => typeof e == "string" ? S(e, t, n, r) : Array.isArray(e) ? t === "curve" ? e.map((e, t) => y(e, `${n}[${t}]`, r)) : e.map((e, t) => (c(e) || s("bad-param", `${n}[${t}] needs {at, color}`, r), {
79
+ at: y(e.at, `${n}[${t}].at`, r),
80
+ color: b(e.color, 3, `${n}[${t}].color`, r)
81
+ })) : s("bad-param", `${n} needs an array or the id of a ${t} block`, r), S = (e, t, n, r) => {
82
+ let i = h(e, n, r);
83
+ (m[i.type] ?? []).includes(t) || s("wrong-type", `${n} reads block "${e}" (${i.type}), which gives no ${t}`, r);
84
+ let a = `${e}:${t}`;
85
+ if (g.has(a)) return g.get(a);
86
+ _.has(e) && s("cycle", `Value block "${e}" feeds itself`, e), _.add(e);
87
+ let o, c = `${i.type} "${e}"`;
88
+ if (i.type === "number") o = t === "range" ? [y(i.value, c, e), y(i.value, c, e)] : y(i.value, c, e);
89
+ else if (i.type === "vector") o = b(i.value, 3, c, e);
90
+ else if (i.type === "color") o = b(i.value, 4, c, e);
91
+ else if (i.type === "random") {
92
+ let n = y(i.min, `${c} min`, e), r = y(i.max, `${c} max`, e);
93
+ n > r && s("bad-param", `${c} needs min <= max`, e), o = t === "range" ? [n, r] : t === "vec3" || i.vector ? [
94
+ 0,
95
+ 1,
96
+ 2
97
+ ].map((e) => n + (r - n) * v(i, e)) : n + (r - n) * v(i, 0);
98
+ } else if (i.type === "math") {
99
+ let n = y(i.a, `${c} a`, e), r = y(i.b, `${c} b`, e), a = i.op, l = i.t === void 0 ? .5 : y(i.t, `${c} t`, e), u = a === "add" ? n + r : a === "sub" ? n - r : a === "mul" ? n * r : a === "min" ? Math.min(n, r) : a === "max" ? Math.max(n, r) : a === "mix" ? n + (r - n) * l : s("bad-param", `${c} op must be add, sub, mul, min, max or mix`, e);
100
+ o = t === "range" ? [u, u] : u;
101
+ } else o = i.type === "gradient" ? x(i.stops, "gradient", `${c} stops`, e) : x(i.samples, "curve", `${c} samples`, e);
102
+ return _.delete(e), g.set(a, o), o;
103
+ }, C = (e, t, n) => b(e, 3, t, n), w = (e, t, n) => typeof e == "number" ? [e, e] : b(e, 2, t, n), T = (e, t) => {
104
+ let n = h(e, "system shape", t);
105
+ n.type !== "shape" && s("wrong-type", `System "${t}" shape must name a shape block, not ${n.type} "${e}"`, t);
106
+ let r = {};
107
+ for (let [e, t] of Object.entries(n)) e !== "id" && e !== "type" && (r[e] = e === "shell" ? t === !0 : e === "shape" ? t : e === "axis" || e === "spacing" ? C(t, `shape "${n.id}" ${e}`, n.id) : y(t, `shape "${n.id}" ${e}`, n.id));
108
+ return r;
109
+ }, E = (e, t) => {
110
+ let n = `${e.type} "${e.id}"`;
111
+ switch (e.type) {
112
+ case "create-capacity":
113
+ t.capacity = y(e.value, n, e.id);
114
+ break;
115
+ case "create-rate":
116
+ t.rate = y(e.value, n, e.id);
117
+ break;
118
+ case "create-initial":
119
+ t.initialCount = y(e.value, n, e.id);
120
+ break;
121
+ case "create-lifetime":
122
+ t.lifetime = w(e.value, n, e.id);
123
+ break;
124
+ case "create-size":
125
+ t.size = y(e.value, n, e.id);
126
+ break;
127
+ case "create-color":
128
+ t.color = b(e.value, 4, n, e.id);
129
+ break;
130
+ case "create-position":
131
+ t.position = C(e.value, n, e.id);
132
+ break;
133
+ case "create-velocity":
134
+ t.velocity = C(e.value, n, e.id), e.spread !== void 0 && (t.spread = y(e.spread, `${n} spread`, e.id));
135
+ break;
136
+ case "create-prewarm":
137
+ t.schedule = {
138
+ ...t.schedule,
139
+ prewarm: y(e.seconds, n, e.id)
140
+ }, e.seed !== void 0 && (t.schedule.seed = y(e.seed, `${n} seed`, e.id));
141
+ break;
142
+ case "create-burst": {
143
+ let r = {
144
+ time: y(e.time, `${n} time`, e.id),
145
+ count: y(e.count, `${n} count`, e.id)
146
+ };
147
+ for (let t of [
148
+ "cycles",
149
+ "interval",
150
+ "probability"
151
+ ]) e[t] !== void 0 && (r[t] = y(e[t], `${n} ${t}`, e.id));
152
+ t.schedule = {
153
+ ...t.schedule,
154
+ bursts: [...t.schedule?.bursts ?? [], r]
155
+ };
156
+ break;
157
+ }
158
+ case "update-size":
159
+ t.curves = {
160
+ ...t.curves,
161
+ size: x(e.curve, "curve", n, e.id)
162
+ };
163
+ break;
164
+ case "update-alpha":
165
+ t.curves = {
166
+ ...t.curves,
167
+ alpha: x(e.curve, "curve", n, e.id)
168
+ };
169
+ break;
170
+ case "update-color":
171
+ t.curves = {
172
+ ...t.curves,
173
+ color: x(e.gradient, "gradient", n, e.id)
174
+ };
175
+ break;
176
+ case "update-spin":
177
+ t.overLife = {
178
+ ...t.overLife,
179
+ spin: x(e.curve, "curve", n, e.id)
180
+ };
181
+ break;
182
+ case "update-speed":
183
+ t.overLife = {
184
+ ...t.overLife,
185
+ speed: x(e.curve, "curve", n, e.id)
186
+ };
187
+ break;
188
+ case "update-velocity-limit":
189
+ t.overLife = {
190
+ ...t.overLife,
191
+ velocityLimit: y(e.value, n, e.id)
192
+ };
193
+ break;
194
+ case "update-gravity":
195
+ t.gravity = C(e.value, n, e.id);
196
+ break;
197
+ case "update-turbulence":
198
+ t.turbulence = y(e.strength, `${n} strength`, e.id), e.frequency !== void 0 && (t.frequency = y(e.frequency, `${n} frequency`, e.id));
199
+ break;
200
+ case "update-ground":
201
+ t.ground = y(e.height, `${n} height`, e.id), e.bounce !== void 0 && (t.bounce = y(e.bounce, `${n} bounce`, e.id));
202
+ break;
203
+ case "update-forces": Array.isArray(e.graph) || s("bad-param", `${n} needs a graph array of update-node operators`, e.id), t.nodes = { graph: e.graph };
204
+ }
205
+ }, D = (e, t, n) => {
206
+ let r = e[t] ?? [];
207
+ return Array.isArray(r) || s("bad-param", `System "${e.id}" ${t} must be an array of block ids`, e.id), r.map((r) => {
208
+ let i = h(r, `System "${e.id}" ${t}`, e.id);
209
+ return n.has(i.type) || s("wrong-type", `System "${e.id}" ${t} lists "${r}" (${i.type}), which is not a ${t} block`, e.id), i;
210
+ });
211
+ }, O = [...a.values()].filter((e) => e.type === "system");
212
+ O.length === 0 && s("no-system", "Node system has no system block; add one with type \"system\""), O.length > 32 && s("limit", "Node system has more than 32 systems; split the graph");
213
+ let k = /* @__PURE__ */ new Map();
214
+ for (let e of O) {
215
+ let t = {};
216
+ e.shape !== void 0 && (t.emitter = T(e.shape, e.id));
217
+ for (let n of D(e, "create", u)) E(n, t);
218
+ for (let n of D(e, "update", d)) E(n, t);
219
+ k.set(e.id, t);
220
+ }
221
+ let A = new Map(O.map((e) => [e.id, []])), j = /* @__PURE__ */ new Set(), M = /* @__PURE__ */ new Set(), N = /* @__PURE__ */ new Set();
222
+ for (let e of [...a.values()].filter((e) => e.type === "trigger")) {
223
+ let t = `trigger "${e.id}"`, [n, r] = [h(e.source, `${t} source`, e.id), h(e.target, `${t} target`, e.id)];
224
+ (n.type !== "system" || r.type !== "system") && s("trigger", `${t} source and target must be system blocks`, e.id), n === r && s("trigger", `${t} cannot start its own source; name a second system`, e.id);
225
+ let [i, a] = [n.id, r.id];
226
+ if (e.on === "start" || e.on === "end") {
227
+ let n = e.delay === void 0 ? 0 : y(e.delay, `${t} delay`, e.id);
228
+ if (n < 0 || n > 1e6) throw RangeError(`Node system ${t} delay must be 0..1000000 seconds; pass a finite value in range`);
229
+ A.get(i).push({
230
+ on: e.on,
231
+ target: a,
232
+ delay: n
233
+ }), j.add(a);
234
+ } else if (typeof e.on == "string" && p.has(e.on)) {
235
+ N.has(i) && s("trigger", `System "${i}" already has an event trigger; a sub-emitter has one (on, count)`, e.id);
236
+ let n = k.get(a);
237
+ (n.emitter || Array.isArray(r.update) && r.update.length || A.get(a).length || j.has(a) || M.has(i)) && s("trigger", `${t} target "${a}" is a sub-emitter child: it takes its birth settings from create blocks only, with no shape, update blocks or triggers, and a child cannot itself raise events`, e.id);
238
+ let o = { on: e.on };
239
+ for (let n of [
240
+ "count",
241
+ "inherit",
242
+ "speed"
243
+ ]) e[n] !== void 0 && (o[n] = y(e[n], `${t} ${n}`, e.id));
244
+ n.lifetime && (o.lifetime = n.lifetime), n.size !== void 0 && (o.size = n.size), n.velocity && (o.velocity = n.velocity), n.spread !== void 0 && (o.spread = n.spread), n.color && (o.color = n.color), k.get(i).events = o, N.add(i), M.add(a);
245
+ } else s("trigger", `${t} on must be start, end, birth, death or collision`, e.id);
246
+ }
247
+ for (let e of M) (j.has(e) || N.has(e)) && s("trigger", `System "${e}" is both a sub-emitter child and a chained or event-raising system`, e);
248
+ let P = /* @__PURE__ */ new Map(), F = (e) => {
249
+ if (P.get(e) !== 2) {
250
+ P.get(e) === 1 && s("cycle", `Triggers chain system "${e}" back to itself; remove one trigger of the loop`, e), P.set(e, 1);
251
+ for (let t of A.get(e)) F(t.target);
252
+ P.set(e, 2);
253
+ }
254
+ };
255
+ O.forEach((e) => F(e.id));
256
+ let I = /* @__PURE__ */ new Map();
257
+ for (let e of O) {
258
+ if (M.has(e.id)) continue;
259
+ let n = e.duration === void 0 ? 0 : y(e.duration, `System "${e.id}" duration`, e.id);
260
+ if (n < 0 || n > 1e6) throw RangeError(`System "${e.id}" duration must be 0..1000000 seconds; pass a finite value in range`);
261
+ let r = k.get(e.id);
262
+ try {
263
+ new t(r).dispose();
264
+ } catch (t) {
265
+ throw new o("invalid-options", `System "${e.id}" options are refused: ${t.message}`, e.id, { cause: t });
266
+ }
267
+ I.set(e.id, {
268
+ id: e.id,
269
+ name: typeof e.name == "string" ? e.name : e.id,
270
+ duration: n,
271
+ options: r,
272
+ triggers: A.get(e.id)
273
+ });
274
+ }
275
+ let L = [...I.keys()].filter((e) => !j.has(e));
276
+ return L.length === 0 && s("no-system", "Node system has no system that starts by itself; every system is the target of a trigger"), {
277
+ systems: I,
278
+ roots: L
279
+ };
280
+ }
281
+ function g(e, t = {}) {
282
+ return h(e, t), JSON.stringify({
283
+ format: e.format,
284
+ version: e.version,
285
+ blocks: e.blocks
286
+ });
287
+ }
288
+ function _(e, t = {}) {
289
+ let n = t.maxChars ?? 16777216;
290
+ if (typeof e != "string" || e.length > n) throw RangeError("Node system JSON needs text within maxChars; pass a smaller string");
291
+ let r;
292
+ try {
293
+ r = JSON.parse(e, (e, t) => ((e === "__proto__" || e === "constructor" || e === "prototype") && s("format", `Node system JSON has reserved key ${e}; remove it`), t));
294
+ } catch (e) {
295
+ throw e instanceof o ? e : new o("format", "Node system JSON syntax is invalid; correct the text", void 0, { cause: e });
296
+ }
297
+ let i = r;
298
+ return (!c(r) || i.format !== "margo-vfx-node-system") && s("format", "Node system JSON must be an object with format \"margo-vfx-node-system\""), i.version !== 1 && s("version", `Node system JSON version ${String(i.version)} is unsupported; migrate to version 1`), Array.isArray(i.blocks) || s("format", "Node system JSON needs a blocks array"), r;
299
+ }
300
+ var v = class {
301
+ #e;
302
+ #t = 0;
303
+ #n = [];
304
+ #r = /* @__PURE__ */ new Set();
305
+ #i = /* @__PURE__ */ new Map();
306
+ #a = !1;
307
+ constructor(e, t) {
308
+ if (typeof t?.create != "function") throw TypeError("NodeParticleSystem needs a create(options, id) function; pass one");
309
+ this.plan = e, this.#e = t.create;
310
+ }
311
+ get elapsed() {
312
+ return this.#t;
313
+ }
314
+ get running() {
315
+ return [...this.#i.keys()];
316
+ }
317
+ get started() {
318
+ return [...this.#r];
319
+ }
320
+ start() {
321
+ return this.#o(), this.#s(), this.#t = 0, this.#n = this.plan.roots.map((e) => ({
322
+ time: 0,
323
+ kind: "start",
324
+ id: e
325
+ })), this.update(0), this;
326
+ }
327
+ update(e) {
328
+ if (this.#o(), typeof e != "number" || !Number.isFinite(e) || e < 0 || e > 3600) throw RangeError("NodeParticleSystem update needs dt of 0..3600 seconds; pass a finite step");
329
+ for (this.#t += e;;) {
330
+ let e = -1;
331
+ if (this.#n.forEach((t, n) => {
332
+ t.time <= this.#t && (e < 0 || t.time < this.#n[e].time) && (e = n);
333
+ }), e < 0) break;
334
+ let [t] = this.#n.splice(e, 1), n = this.plan.systems.get(t.id);
335
+ if (t.kind === "start") {
336
+ if (this.#r.has(t.id)) continue;
337
+ this.#r.add(t.id), this.#i.set(t.id, this.#e(n.options, t.id) ?? void 0), n.duration > 0 && this.#n.push({
338
+ time: t.time + n.duration,
339
+ kind: "end",
340
+ id: t.id
341
+ });
342
+ for (let e of n.triggers) e.on === "start" && this.#n.push({
343
+ time: t.time + e.delay,
344
+ kind: "start",
345
+ id: e.target
346
+ });
347
+ } else {
348
+ let e = this.#i.get(t.id);
349
+ this.#i.delete(t.id), e?.stop ? e.stop() : e?.dispose?.();
350
+ for (let e of n.triggers) e.on === "end" && this.#n.push({
351
+ time: t.time + e.delay,
352
+ kind: "start",
353
+ id: e.target
354
+ });
355
+ }
356
+ }
357
+ return this;
358
+ }
359
+ #o() {
360
+ if (this.#a) throw TypeError("NodeParticleSystem is disposed; create a new one");
361
+ }
362
+ #s() {
363
+ for (let e of this.#i.values()) e?.dispose?.();
364
+ this.#i.clear(), this.#r.clear(), this.#n = [];
365
+ }
366
+ dispose() {
367
+ this.#a ||= (this.#s(), !0);
368
+ }
369
+ [Symbol.dispose]() {
370
+ this.dispose();
371
+ }
372
+ };
373
+ function y(e, t) {
374
+ return new v(h(typeof e == "string" ? _(e) : e, t), t);
375
+ }
376
+ async function b(e, t) {
377
+ t.signal?.throwIfAborted();
378
+ let n = await fetch(e, { signal: t.signal });
379
+ if (!n.ok) throw new o("format", `Node system request returned ${n.status}; check the URL`);
380
+ let r = await n.text();
381
+ return t.signal?.throwIfAborted(), y(r, t);
382
+ }
383
+ //#endregion
384
+ export { i as MAX_NODE_SYSTEMS, r as MAX_NODE_SYSTEM_BLOCKS, n as NODE_SYSTEM_VERSION, v as NodeParticleSystem, o as VFXNodeSystemError, h as compileNodeSystem, y as createNodeSystem, b as loadNodeSystem, _ as parseNodeSystem, g as serializeNodeSystem };
@@ -0,0 +1,14 @@
1
+ import type { VFXExtension, VFXNode, VFXNodeSample, VFXNodes } from './base.js';
2
+ /** Most operators in one graph. */
3
+ export declare const MAX_NODES = 32;
4
+ /** Most numeric literals (const and scale), packed into the binding 24 uniform. */
5
+ export declare const MAX_NODE_CONSTS = 16;
6
+ /** Acceleration a graph adds, for the same sample the shader reads. `force` sums, `drag` subtracts coefficient times velocity. */
7
+ export declare const evaluateNodes: (graph: readonly VFXNode[], sample: VFXNodeSample) => [number, number, number];
8
+ /**
9
+ * Custom particle update: a graph of operators compiled into the simulation, after gravity, curl and fields.
10
+ * Built from Reeves 1983 (velocity, then position, from acceleration times the step) and a dataflow graph
11
+ * (Dennis 1974; Kahn 1962 for the order). Pass `{nodes}` from this entry. Const and scale numbers are read
12
+ * every frame from one uniform (group 0 binding 24); the operators and wires are fixed at attach.
13
+ */
14
+ export declare const nodes: VFXExtension<VFXNodes>;
package/dist/nodes.js ADDED
@@ -0,0 +1,293 @@
1
+ import { MargoError as e } from "@margoengine/math/errors";
2
+ //#region src/nodes.ts
3
+ var t = 32, n = 16, r = /* @__PURE__ */ new Set([
4
+ "const",
5
+ "attribute",
6
+ "add",
7
+ "mul",
8
+ "sub",
9
+ "mix",
10
+ "min",
11
+ "max",
12
+ "scale",
13
+ "normalize",
14
+ "length",
15
+ "force",
16
+ "drag"
17
+ ]), i = /* @__PURE__ */ new Set([
18
+ "position",
19
+ "velocity",
20
+ "size",
21
+ "color",
22
+ "life",
23
+ "age"
24
+ ]), a = /* @__PURE__ */ new Set([
25
+ "add",
26
+ "mul",
27
+ "sub",
28
+ "min",
29
+ "max"
30
+ ]), o = "f.gravityDelta.xyz+curlNoise(p.positionLife.xyz)+fieldForce(p.positionLife.xyz)", s = "f.gravityDelta.xyz+curlNoise(p.positionLife.xyz)", c = "var p=Particle(particles[at],particles[at+1],vec4f(0),vec4f(0));", l = /* @__PURE__ */ new WeakMap(), u = (e) => e === "position" || e === "velocity" || e === "color" ? 3 : 1, d = (e) => ({
31
+ position: "p.positionLife.xyz",
32
+ velocity: "p.velocitySize.xyz",
33
+ size: "p.velocitySize.w",
34
+ color: "p.color.rgb",
35
+ life: "p.positionLife.w",
36
+ age: "(p.rotation.z-p.positionLife.w)"
37
+ })[e];
38
+ function f(e) {
39
+ throw TypeError(e);
40
+ }
41
+ var p = (e, t) => {
42
+ if ((typeof e != "number" || !Number.isFinite(e)) && f(`VFX nodes ${t} needs a finite number`), !Number.isFinite(Math.fround(e))) throw RangeError(`VFX nodes ${t} is not a finite f32; see https://margoengine.com/errors#vfx-float`);
43
+ return e;
44
+ }, m = (e) => {
45
+ (!Array.isArray(e) || e.length > 32) && f("VFX nodes graph must be an array of at most 32 operators");
46
+ let t = e.length, n = e.map((e, n) => {
47
+ (!e || typeof e != "object" || !r.has(e.op)) && f(`VFX nodes graph[${n}] needs a known op`);
48
+ let i = e.inputs ?? [];
49
+ return (!Array.isArray(i) || i.some((e) => !Number.isInteger(e) || e < 0 || e >= t || e === n)) && f(`VFX nodes graph[${n}].inputs must be indices of other operators`), i;
50
+ }), o = (t, r) => {
51
+ n[t].length !== r && f(`VFX nodes graph[${t}] (${e[t].op}) needs ${r} inputs`);
52
+ };
53
+ e.forEach((e, t) => {
54
+ if (e.op === "const" || e.op === "attribute" ? o(t, 0) : a.has(e.op) ? o(t, 2) : e.op === "mix" ? o(t, 3) : o(t, 1), e.op === "attribute" && (typeof e.value != "string" || !i.has(e.value)) && f("VFX nodes attribute needs position, velocity, size, color, life or age"), e.op === "const") {
55
+ let t = e.value;
56
+ Array.isArray(t) ? (t.length !== 3 && f("VFX nodes const vector needs 3 finite numbers"), p(t[0], "const[0]"), p(t[1], "const[1]"), p(t[2], "const[2]")) : p(t, "const");
57
+ }
58
+ e.op === "scale" && p(e.value, "scale");
59
+ });
60
+ let s = Array.from({ length: t }, () => 0), c = Array.from({ length: t }, () => []);
61
+ n.forEach((e, t) => {
62
+ for (let n of e) s[t] += 1, c[n].push(t);
63
+ });
64
+ let l = s.flatMap((e, t) => e === 0 ? [t] : []), m = [];
65
+ for (let e = 0; e < l.length; e++) {
66
+ let t = l[e];
67
+ m.push(t);
68
+ for (let e of c[t]) --s[e], s[e] === 0 && l.push(e);
69
+ }
70
+ m.length !== t && f("VFX nodes graph has a cycle");
71
+ let h = Array.from({ length: t }, () => 0), g = (e, t) => {
72
+ let n = h[e];
73
+ return n === 0 && f(`VFX nodes graph[${e}] has no output (force and drag only add acceleration)`), n !== t && (n !== 1 || t !== 3) && f(`VFX nodes graph[${e}] is the wrong width for this input`), n;
74
+ };
75
+ for (let t of m) {
76
+ let r = e[t], i = n[t];
77
+ if (r.op === "const") h[t] = Array.isArray(r.value) ? 3 : 1;
78
+ else if (r.op === "attribute") h[t] = u(r.value);
79
+ else if (a.has(r.op)) {
80
+ let e = g(i[0], 3), n = g(i[1], 3);
81
+ h[t] = e === 3 || n === 3 ? 3 : 1, e === 1 && g(i[0], 1), n === 1 && g(i[1], 1);
82
+ } else if (r.op === "mix") {
83
+ let e = g(i[0], 3), n = g(i[1], 3);
84
+ g(i[2], 1), h[t] = e === 3 || n === 3 ? 3 : 1;
85
+ } else r.op === "scale" || r.op === "normalize" || r.op === "force" ? (g(i[0], 3), h[i[0]] !== 3 && f(`VFX nodes graph[${t}] (${r.op}) needs a vector`), h[t] = r.op === "force" ? 0 : 3) : r.op === "length" ? (g(i[0], 3), h[i[0]] !== 3 && f("VFX nodes length needs a vector"), h[t] = 1) : r.op === "drag" && (g(i[0], 1), h[i[0]] !== 1 && f("VFX nodes drag needs a number"));
86
+ }
87
+ let _ = /* @__PURE__ */ new Set(), v = (e) => {
88
+ if (!_.has(e)) {
89
+ _.add(e);
90
+ for (let t of n[e]) v(t);
91
+ }
92
+ };
93
+ e.forEach((e, t) => {
94
+ (e.op === "force" || e.op === "drag") && v(t);
95
+ });
96
+ let y = [], b = [], x = (e, t = 0, n = 0) => {
97
+ b.length / 4 >= 16 && f("VFX nodes supports at most 16 numeric literals");
98
+ let r = b.length / 4;
99
+ return b.push(e, t, n, 0), r;
100
+ };
101
+ for (let t of m) {
102
+ if (!_.has(t)) continue;
103
+ let n = e[t];
104
+ n.op === "const" && Array.isArray(n.value) ? y[t] = x(n.value[0], n.value[1], n.value[2]) : (n.op === "const" || n.op === "scale") && (y[t] = x(n.value));
105
+ }
106
+ let S = (e, t) => {
107
+ let n = `n${e}`;
108
+ return h[e] === t ? n : `vec3f(${n})`;
109
+ }, C = ["fn nodeAccel(p:Particle)->vec3f{", "var a=vec3f(0.0);"], w = !1, T = (e) => e === "add" ? "+" : e === "sub" ? "-" : e === "mul" ? "*" : e;
110
+ for (let t of m) {
111
+ if (!_.has(t)) continue;
112
+ let r = e[t], i = n[t];
113
+ if (r.op === "const") C.push(`let n${t}=nodeParams[${y[t]}].${h[t] === 3 ? "xyz" : "x"};`);
114
+ else if (r.op === "attribute") C.push(`let n${t}=${d(r.value)};`);
115
+ else if (a.has(r.op)) {
116
+ let e = h[t], n = T(r.op);
117
+ C.push(n === "+" || n === "-" || n === "*" ? `let n${t}=${S(i[0], e)}${n}${S(i[1], e)};` : `let n${t}=${n}(${S(i[0], e)},${S(i[1], e)});`);
118
+ } else if (r.op === "mix") {
119
+ let e = h[t];
120
+ C.push(`let n${t}=mix(${S(i[0], e)},${S(i[1], e)},${S(i[2], 1)});`);
121
+ } else r.op === "scale" ? C.push(`let n${t}=${S(i[0], 3)}*nodeParams[${y[t]}].x;`) : r.op === "normalize" ? (w = !0, C.push(`let n${t}=nodeNorm(n${i[0]});`)) : r.op === "length" ? C.push(`let n${t}=length(n${i[0]});`) : r.op === "force" ? C.push(`a+=${S(i[0], 3)};`) : C.push(`a-=${S(i[0], 1)}*p.velocitySize.xyz;`);
122
+ }
123
+ C.push("return a;}");
124
+ let E = C.join("\n"), D = w ? "fn nodeNorm(v:vec3f)->vec3f{let a=max(abs(v.x),max(abs(v.y),abs(v.z)));if(a==0.0){return vec3f(0.0);}let s=v/a;return s/length(s);}\n" : "", O = b.length === 0 ? "" : `@group(0) @binding(24) var<uniform> nodeParams:array<vec4f,${b.length / 4}>;\n`, k = m.some((t) => _.has(t) && e[t].op === "attribute" && (e[t].value === "color" || e[t].value === "age"));
125
+ return {
126
+ signature: e.map((e, t) => {
127
+ let r = e.op === "const" ? Array.isArray(e.value) ? "v" : "s" : e.op === "attribute" ? String(e.value) : e.op === "scale" ? "s" : "";
128
+ return `${e.op}:${n[t].join(".")}:${r}`;
129
+ }).join("|"),
130
+ wgsl: O + D + E + "\n",
131
+ constants: new Float32Array(b),
132
+ needsCold: k
133
+ };
134
+ }, h = (e, t) => {
135
+ m(e);
136
+ let n = e.length, r = e.map((e) => e.inputs ?? []), i = Array.from({ length: n }, () => 0), o = Array.from({ length: n }, () => []);
137
+ r.forEach((e, t) => {
138
+ for (let n of e) i[t] += 1, o[n].push(t);
139
+ });
140
+ let s = i.flatMap((e, t) => e === 0 ? [t] : []), c = [];
141
+ for (let e = 0; e < s.length; e++) {
142
+ let t = s[e];
143
+ c.push(t);
144
+ for (let e of o[t]) --i[e], i[e] === 0 && s.push(e);
145
+ }
146
+ let l = [], u = (e) => l[e], d = (e) => {
147
+ let t = u(e);
148
+ return t.kind === 3 ? t.v : t.kind === 1 ? [
149
+ t.n,
150
+ t.n,
151
+ t.n
152
+ ] : f(`VFX nodes graph[${e}] has no output`);
153
+ }, p = (e) => {
154
+ let t = u(e);
155
+ return t.kind === 1 ? t.n : f(`VFX nodes graph[${e}] needs to be a number`);
156
+ }, h = 0, g = 0, _ = 0, v = (e) => e === "position" ? {
157
+ kind: 3,
158
+ v: [...t.position]
159
+ } : e === "velocity" ? {
160
+ kind: 3,
161
+ v: [...t.velocity]
162
+ } : e === "color" ? {
163
+ kind: 3,
164
+ v: [...t.color]
165
+ } : e === "size" ? {
166
+ kind: 1,
167
+ n: t.size
168
+ } : e === "life" ? {
169
+ kind: 1,
170
+ n: t.life
171
+ } : {
172
+ kind: 1,
173
+ n: t.age
174
+ };
175
+ for (let n of c) {
176
+ let i = e[n], o = r[n];
177
+ if (i.op === "const") l[n] = Array.isArray(i.value) ? {
178
+ kind: 3,
179
+ v: [
180
+ i.value[0],
181
+ i.value[1],
182
+ i.value[2]
183
+ ]
184
+ } : {
185
+ kind: 1,
186
+ n: i.value
187
+ };
188
+ else if (i.op === "attribute") l[n] = v(i.value);
189
+ else if (a.has(i.op) || i.op === "mix") {
190
+ let e = u(o[0]).kind === 3 || u(o[1]).kind === 3, t = (t) => e ? d(t) : [
191
+ p(t),
192
+ 0,
193
+ 0
194
+ ], r = t(o[0]), a = t(o[1]), s = i.op === "mix" ? p(o[2]) : 0, c = (e) => [
195
+ e(r[0], a[0]),
196
+ e(r[1], a[1]),
197
+ e(r[2], a[2])
198
+ ], f = c((e, t) => e * (1 - s) + t * s), m = i.op === "add" ? c((e, t) => e + t) : i.op === "sub" ? c((e, t) => e - t) : i.op === "mul" ? c((e, t) => e * t) : i.op === "min" ? c((e, t) => Math.min(e, t)) : i.op === "max" ? c((e, t) => Math.max(e, t)) : f;
199
+ l[n] = e ? {
200
+ kind: 3,
201
+ v: m
202
+ } : {
203
+ kind: 1,
204
+ n: m[0]
205
+ };
206
+ } else if (i.op === "scale") {
207
+ let e = d(o[0]), t = i.value;
208
+ l[n] = {
209
+ kind: 3,
210
+ v: [
211
+ e[0] * t,
212
+ e[1] * t,
213
+ e[2] * t
214
+ ]
215
+ };
216
+ } else if (i.op === "normalize") {
217
+ let e = d(o[0]), t = Math.max(Math.abs(e[0]), Math.abs(e[1]), Math.abs(e[2]));
218
+ if (t === 0) l[n] = {
219
+ kind: 3,
220
+ v: [
221
+ 0,
222
+ 0,
223
+ 0
224
+ ]
225
+ };
226
+ else {
227
+ let r = e[0] / t, i = e[1] / t, a = e[2] / t, o = Math.hypot(r, i, a);
228
+ l[n] = {
229
+ kind: 3,
230
+ v: [
231
+ r / o,
232
+ i / o,
233
+ a / o
234
+ ]
235
+ };
236
+ }
237
+ } else if (i.op === "length") l[n] = {
238
+ kind: 1,
239
+ n: Math.hypot(...d(o[0]))
240
+ };
241
+ else if (i.op === "force") {
242
+ let e = d(o[0]);
243
+ h += e[0], g += e[1], _ += e[2], l[n] = { kind: 0 };
244
+ } else {
245
+ let e = p(o[0]);
246
+ h -= e * t.velocity[0], g -= e * t.velocity[1], _ -= e * t.velocity[2], l[n] = { kind: 0 };
247
+ }
248
+ }
249
+ return [
250
+ h,
251
+ g,
252
+ _
253
+ ];
254
+ }, g = (t, n) => {
255
+ let r = t.includes(o) ? o : t.includes(s) ? s : "";
256
+ if (!r || t.split(r).length !== 2) throw new e("unsupported", "VFX nodes need the simulation shader they were written for; update the edit");
257
+ let i = t.replace(r, `${r}+nodeAccel(p)`);
258
+ if (n.needsCold) {
259
+ if (!i.includes(c)) throw new e("unsupported", "VFX nodes need the simulation shader they were written for; update the edit");
260
+ i = i.replace(c, "var p=Particle(particles[at],particles[at+1],particles[cold],particles[cold+1]);");
261
+ }
262
+ return i.replace("fn update(", `${n.wgsl}fn update(`);
263
+ }, _ = {
264
+ validate(e) {
265
+ (!e || typeof e != "object") && f("VFX nodes must be an object; pass {graph}");
266
+ let t = m(e.graph), n = l.get(e);
267
+ n !== void 0 && n !== t.signature && f("VFX nodes are fixed at attach; change const and scale values in place");
268
+ },
269
+ simulation(e, t) {
270
+ let n = m(t.graph);
271
+ return l.set(t, n.signature), g(e, n);
272
+ },
273
+ create(e, t, n) {
274
+ let r = m(t.graph), i = r.constants.length === 0 ? void 0 : e.device.createBuffer({
275
+ size: r.constants.byteLength,
276
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
277
+ });
278
+ return i && n.push(i), {
279
+ compute: i ? [{
280
+ binding: 24,
281
+ resource: { buffer: i }
282
+ }] : [],
283
+ write(n) {
284
+ let a = e.options.nodes;
285
+ a !== t && f("VFX nodes are fixed at attach; change const and scale values in place");
286
+ let o = m(a.graph);
287
+ o.signature !== r.signature && f("VFX nodes are fixed at attach; change const and scale values in place"), i && n.device.queue.writeBuffer(i, 0, o.constants);
288
+ }
289
+ };
290
+ }
291
+ };
292
+ //#endregion
293
+ export { t as MAX_NODES, n as MAX_NODE_CONSTS, h as evaluateNodes, _ as nodes };