@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
@@ -0,0 +1,217 @@
1
+ import { t as e } from "./frame-BuvMFBPp.js";
2
+ import { MargoError as t } from "@margoengine/math/errors";
3
+ //#region ../shared/prefix.js
4
+ var n = 128, r = "@group(0) @binding(0) var<uniform> n:vec4u;\n@group(0) @binding(1) var<storage,read> words:array<u32>;\n", i = r + "@group(0) @binding(2) var<storage,read_write> sums:array<u32>;\n@compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u){\n let first=id.x*16u;if(first>=n.x){return;}\n var sum=0u;for(var j=first;j<min(first+16u,n.x);j++){sum+=words[j];}\n sums[id.x]=sum;\n}", a = r + "@group(0) @binding(2) var<storage,read_write> counts:array<u32>;\n@compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u){\n if(id.x>n.x){return;}\n var count=0u;for(var j=0u;j<id.x;j++){count+=words[j];}\n counts[id.x]=count;\n}", o = r + "@group(0) @binding(2) var<storage,read_write> counts:array<u32>;\n@group(0) @binding(3) var<storage,read> parents:array<u32>;\n@compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u){\n if(id.x>n.x){return;}\n var count=parents[id.x>>4u];for(var j=id.x&~15u;j<id.x;j++){count+=words[j];}\n counts[id.x]=count;\n}";
5
+ function s(e) {
6
+ let t = [e];
7
+ for (; t.at(-1) > 16;) t.push(Math.ceil(t.at(-1) / 16));
8
+ return t;
9
+ }
10
+ var c = class {
11
+ constructor(e, t, r, { totalOnly: c = !1 } = {}) {
12
+ if (!Number.isInteger(t) || t < 1) throw RangeError("A prefix count needs at least one flag word; pass a positive whole number");
13
+ let l = (t, n) => {
14
+ let i = e.createBuffer({
15
+ size: t,
16
+ usage: n
17
+ });
18
+ return r.push(i), i;
19
+ }, u = s(t), d = (t) => e.createComputePipeline({
20
+ layout: "auto",
21
+ compute: {
22
+ module: e.createShaderModule({ code: t }),
23
+ entryPoint: "main"
24
+ }
25
+ }), f = d(i), p = d(a), m = c || u.length === 1 ? void 0 : d(o);
26
+ this.steps = [];
27
+ let h = (t, n, r, i) => {
28
+ let a = l(16, 72);
29
+ e.queue.writeBuffer(a, 0, new Uint32Array([
30
+ n,
31
+ 0,
32
+ 0,
33
+ 0
34
+ ])), this.steps.push([
35
+ t,
36
+ e.createBindGroup({
37
+ layout: t.getBindGroupLayout(0),
38
+ entries: [a, ...i].map((e, t) => ({
39
+ binding: t,
40
+ resource: { buffer: e }
41
+ }))
42
+ }),
43
+ Math.ceil(r / 256)
44
+ ]);
45
+ };
46
+ this.flags = l(Math.max(16, t * 4), n);
47
+ let g = [this.flags];
48
+ for (let e = 1; e < u.length; e++) {
49
+ let t = l(u[e] * 4, n);
50
+ h(f, u[e - 1], u[e], [g[e - 1], t]), g.push(t);
51
+ }
52
+ let _ = u.length - 1, v = l((u[_] + 1) * 4, n);
53
+ if (h(p, u[_], u[_] + 1, [g[_], v]), this.total = v, this.totalWord = u[_], m) for (let e = _ - 1; e >= 0; e--) {
54
+ let t = l((u[e] + 1) * 4, n);
55
+ h(m, u[e], u[e] + 1, [
56
+ g[e],
57
+ t,
58
+ v
59
+ ]), v = t;
60
+ }
61
+ this.prefix = v;
62
+ }
63
+ encode(e) {
64
+ for (let [t, n, r] of this.steps) e.setPipeline(t), e.setBindGroup(0, n), e.dispatchWorkgroups(r);
65
+ }
66
+ }, l = () => new t("unsupported", "VFX on WebGL2 needs the shader its compatibility route was written for; update the edits in vfx/src/compat.ts"), u = (e, t, n) => {
67
+ if (e.split(t).length !== 2) throw l();
68
+ return e.replace(t, n);
69
+ }, d = "@compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u", f = "atomicAdd(&indirect.instanceCount,1u);", p = " return 1u;\n}", m = "@group(0) @binding(1) var<storage,read> particles:array<vec4f>;\n", h = "var<private> eventHere:u32;", g = 28;
70
+ function _(t) {
71
+ let n = /fn dies\(i:u32\)->u32 \{[^\n]*\n/.exec(t)?.[0], r = /var<workgroup> scratch[^\n]*\nfn prefix[^\n]*\n/.exec(t)?.[0], i = t.indexOf(f), a = t.indexOf(p, i), o = t.indexOf("@compute @workgroup_size(256) fn main(");
72
+ if (!n || !r || i < 0 || a < 0 || o < a) throw l();
73
+ let s = t.slice(i + 38, a), c = t.slice(0, i) + t.slice(a, o);
74
+ for (let [e, t] of [
75
+ [n, ""],
76
+ [r, ""],
77
+ ["@group(0) @binding(2) var<storage,read_write> groups:array<u32>;\n", ""],
78
+ ["@group(0) @binding(3) var<storage,read_write> alive:array<u32>;\n", ""],
79
+ ["struct Indirect {vertexCount:u32,instanceCount:atomic<u32>,rest:array<u32,3>}\n", ""],
80
+ ["@group(0) @binding(4) var<storage,read_write> indirect:Indirect;\n", ""],
81
+ ["@group(0) @binding(5) var<storage,read> births:array<u32>;", "@group(0) @binding(5) var<storage,read> tickets:array<u32>;"],
82
+ ["@group(0) @binding(9) var<storage,read_write> listed:array<u32>;\n", ""],
83
+ ["@group(0) @binding(10) var<storage,read_write> total:array<u32,5>;\n", ""]
84
+ ]) c = u(c, e, t);
85
+ let g = c.includes(h);
86
+ return {
87
+ events: g,
88
+ flags: `${e}${m}@group(0) @binding(2) var<storage,read_write> flags:array<u32>;
89
+ ${n}${d}){if(id.x<f.control.x){flags[id.x]=dies(id.x);}}`,
90
+ update: g ? `${c}@group(0) @binding(28) var<storage,read_write> eventFlags:array<u32>;
91
+ ${d}){if(id.x<f.control.x){let drawn=update(id.x,tickets[id.x]);storeEvents(id.x);eventFlags[id.x]=eventHere;}}` : `${c}${d}){if(id.x<f.control.x){let drawn=update(id.x,tickets[id.x]);}}`,
92
+ marks: `${c}@group(0) @binding(2) var<storage,read_write> living:array<u32>;
93
+ @group(0) @binding(3) var<storage,read_write> shown:array<u32>;
94
+ fn onScreen(p:Particle)->u32 {${s} return 1u;
95
+ }
96
+ ${d}){
97
+ if(id.x>=f.control.x){return;}
98
+ let p=Particle(particles[id.x*2],particles[id.x*2+1],vec4f(0),vec4f(0));
99
+ var isLive=0u;var isShown=0u;if(p.positionLife.w>0){isLive=1u;isShown=onScreen(p);}
100
+ living[id.x]=isLive;shown[id.x]=isShown;
101
+ }`
102
+ };
103
+ }
104
+ var v = `@group(0) @binding(0) var<uniform> n:vec4u;
105
+ @group(0) @binding(1) var<storage,read> shown:array<u32>;
106
+ @group(0) @binding(2) var<storage,read> places:array<u32>;
107
+ @group(0) @binding(3) var<storage,read_write> alive:array<u32>;
108
+ ${d}){if(id.x<n.x&&shown[id.x]==1u){alive[places[id.x]]=id.x;}}`, y = `@group(0) @binding(0) var<uniform> n:vec4u;
109
+ @group(0) @binding(1) var<storage,read> raised:array<u32>;
110
+ @group(0) @binding(2) var<storage,read> places:array<u32>;
111
+ @group(0) @binding(3) var<storage,read_write> events:array<u32>;
112
+ ${d}){if(id.x<n.x&&raised[id.x]==1u){events[4u+n.y*257u+places[id.x]]=id.x;}}`, b = (e) => `@group(0) @binding(0) var<storage,read> total:array<u32>;
113
+ @group(0) @binding(1) var<storage,read_write> events:array<u32>;
114
+ @compute @workgroup_size(1) fn main(){events[1]=total[${e}];}`, x = (e, t) => `@group(0) @binding(0) var<storage,read> living:array<u32>;
115
+ @group(0) @binding(1) var<storage,read> shown:array<u32>;
116
+ @group(0) @binding(2) var<storage,read_write> indirect:array<u32,5>;
117
+ @group(0) @binding(3) var<storage,read_write> visible:array<u32,5>;
118
+ @compute @workgroup_size(1) fn main(){indirect[1]=living[${e}];visible[1]=shown[${t}];}`, S = "@vertex fn vertex(@builtin(vertex_index) vertex:u32,@builtin(instance_index) instance:u32)->Out {", C = 13, w = (e) => u(e, S, `@group(0) @binding(13) var<storage,read> drawn:array<u32,5>;
119
+ ${S}
120
+ if(instance>=drawn[1]){var none:Out;none.clip=vec4f(2,2,2,1);return none;}`);
121
+ function T(e) {
122
+ let t = e.replace(/\/\/[^\n]*/g, "").replace(/\/\*[\s\S]*?\*\//g, ""), n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
123
+ for (let e of t.matchAll(/@group\(0\)\s*@binding\((\d+)\)\s*var(?:<[^>]*>)?\s+(\w+)/g)) n.set(e[2], +e[1]);
124
+ for (let e of t.matchAll(/\bfn\s+(\w+)\s*\(/g)) {
125
+ let n = t.indexOf("{", e.index + e[0].length), i = 0, a = n;
126
+ for (; n < t.length; n++) if (t[n] === "{") i++;
127
+ else if (t[n] === "}" && --i === 0) break;
128
+ r.set(e[1], t.slice(a, n + 1));
129
+ }
130
+ let i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(["main"]), o = [r.get("main") ?? ""];
131
+ for (; o.length;) for (let [e] of o.pop().matchAll(/\b[A-Za-z_]\w*\b/g)) {
132
+ let t = n.get(e);
133
+ t !== void 0 && i.add(t), r.has(e) && !a.has(e) && (a.add(e), o.push(r.get(e)));
134
+ }
135
+ return i;
136
+ }
137
+ var E = class {
138
+ constructor(e, t, n, r, i, a) {
139
+ let o = _(t), s = Math.ceil(n / 256), u = (t, n, r) => {
140
+ let i = e.createComputePipeline({
141
+ layout: "auto",
142
+ compute: {
143
+ module: e.createShaderModule({ code: t }),
144
+ entryPoint: "main"
145
+ }
146
+ }), a = T(t);
147
+ return [
148
+ i,
149
+ e.createBindGroup({
150
+ layout: i.getBindGroupLayout(0),
151
+ entries: n.filter((e) => a.has(e.binding))
152
+ }),
153
+ r
154
+ ];
155
+ }, d = (e, t) => ({
156
+ binding: e,
157
+ resource: { buffer: t }
158
+ });
159
+ if (this.deaths = new c(e, n, a), this.living = new c(e, n, a, { totalOnly: !0 }), this.shown = new c(e, n, a), o.events !== (r.events !== void 0)) throw l();
160
+ let f = r.events ? new c(e, n, a) : void 0;
161
+ this.flagging = u(o.flags, [
162
+ d(0, r.uniform),
163
+ d(1, r.particles),
164
+ d(2, this.deaths.flags)
165
+ ], s), this.updating = u(o.update, [
166
+ d(0, r.uniform),
167
+ d(1, r.particles),
168
+ d(5, this.deaths.prefix),
169
+ ...f ? [d(28, f.flags)] : [],
170
+ ...i
171
+ ], s), this.marking = u(o.marks, [
172
+ d(0, r.uniform),
173
+ d(1, r.particles),
174
+ d(2, this.living.flags),
175
+ d(3, this.shown.flags),
176
+ d(5, this.deaths.prefix),
177
+ ...i
178
+ ], s);
179
+ let p = e.createBuffer({
180
+ size: 16,
181
+ usage: 72
182
+ });
183
+ a.push(p), e.queue.writeBuffer(p, 0, new Uint32Array([
184
+ n,
185
+ s,
186
+ 0,
187
+ 0
188
+ ])), this.compacting = u(v, [
189
+ d(0, p),
190
+ d(1, this.shown.flags),
191
+ d(2, this.shown.prefix),
192
+ d(3, r.alive)
193
+ ], s), this.totals = u(x(this.living.totalWord, this.shown.totalWord), [
194
+ d(0, this.living.total),
195
+ d(1, this.shown.total),
196
+ d(2, r.indirect),
197
+ d(3, r.visible)
198
+ ], 1), f && r.events && (this.events = {
199
+ count: f,
200
+ compact: u(y, [
201
+ d(0, p),
202
+ d(1, f.flags),
203
+ d(2, f.prefix),
204
+ d(3, r.events)
205
+ ], s),
206
+ total: u(b(f.totalWord), [d(0, f.total), d(1, r.events)], 1)
207
+ });
208
+ }
209
+ encode(e, t) {
210
+ let n = ([t, n, r]) => {
211
+ e.setPipeline(t), e.setBindGroup(0, n), e.dispatchWorkgroups(r);
212
+ };
213
+ t && (n(this.flagging), this.deaths.encode(e)), n(this.updating), this.events && (this.events.count.encode(e), n(this.events.compact), n(this.events.total)), n(this.marking), this.living.encode(e), this.shown.encode(e), n(this.compacting), n(this.totals);
214
+ }
215
+ };
216
+ //#endregion
217
+ export { C as COUNT_BINDING, g as EVENT_FLAGS_BINDING, E as SimulationRoute, v as compactShader, w as countedBillboard, y as eventCompactShader, b as eventTotalShader, _ as routeShaders, x as totalsShader, T as usedBindings };
@@ -0,0 +1,59 @@
1
+ /** Binding of the route's event flags in `update`; free in the simulation and in every feature's patch. */
2
+ export declare const EVENT_FLAGS_BINDING = 28;
3
+ /**
4
+ * The kernels of a simulation text (`simulationShader` with its features' patches): `flags` flags the dying slots,
5
+ * `update` runs each slot with its ticket, `marks` flags the live and the on-screen slots.
6
+ * @throws MargoError `'unsupported'` when the text is not the one these edits were written for.
7
+ */
8
+ export declare function routeShaders(simulation: string): {
9
+ flags: string;
10
+ update: string;
11
+ marks: string;
12
+ events: boolean;
13
+ };
14
+ /** Lists each on-screen slot at its place in slot order. */
15
+ export declare const compactShader = "@group(0) @binding(0) var<uniform> n:vec4u;\n@group(0) @binding(1) var<storage,read> shown:array<u32>;\n@group(0) @binding(2) var<storage,read> places:array<u32>;\n@group(0) @binding(3) var<storage,read_write> alive:array<u32>;\n@compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u){if(id.x<n.x&&shown[id.x]==1u){alive[places[id.x]]=id.x;}}";
16
+ /**
17
+ * Lists each slot that raised an event at its place in slot order, where events.ts reads the list:
18
+ * events[4 + groups x 257 + place]. `n` is (capacity, groups).
19
+ */
20
+ export declare const eventCompactShader = "@group(0) @binding(0) var<uniform> n:vec4u;\n@group(0) @binding(1) var<storage,read> raised:array<u32>;\n@group(0) @binding(2) var<storage,read> places:array<u32>;\n@group(0) @binding(3) var<storage,read_write> events:array<u32>;\n@compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u){if(id.x<n.x&&raised[id.x]==1u){events[4u+n.y*257u+places[id.x]]=id.x;}}";
21
+ /** One invocation: the frame's event count into events[1], as WebGPU's event scan leaves it. */
22
+ export declare const eventTotalShader: (word: number) => string;
23
+ /** One invocation: the frame's totals, as the WebGPU route leaves them in the two argument buffers. */
24
+ export declare const totalsShader: (living: number, shown: number) => string;
25
+ /** Binding of `visible` in the route's billboard; free in the billboard and in every feature's patch. */
26
+ export declare const COUNT_BINDING = 13;
27
+ /** A billboard that draws nothing for the slots at or past the frame's count, so a draw of every slot stands for it. */
28
+ export declare const countedBillboard: (billboard: string) => string;
29
+ /**
30
+ * The `@group(0)` bindings the entry point `main` of `code` statically uses (WGSL 7.7: the variables named in `main`
31
+ * and in every function it calls, transitively). An auto layout holds only those, and WebGPU refuses a bind group
32
+ * entry its layout does not have, so each kernel binds exactly these.
33
+ */
34
+ export declare function usedBindings(code: string): Set<number>;
35
+ /** The route's GPU state for one effect: its kernels and prefix counts. Every buffer it makes goes to `owned`. */
36
+ export declare class SimulationRoute {
37
+ private readonly deaths;
38
+ private readonly living;
39
+ private readonly shown;
40
+ private readonly flagging;
41
+ private readonly updating;
42
+ private readonly marking;
43
+ private readonly compacting;
44
+ private readonly totals;
45
+ private readonly events?;
46
+ constructor(device: GPUDevice, simulation: string, capacity: number, b: {
47
+ uniform: GPUBuffer;
48
+ particles: GPUBuffer;
49
+ alive: GPUBuffer;
50
+ indirect: GPUBuffer;
51
+ visible: GPUBuffer;
52
+ /** The `events` feature's list (its runtime's `eventList`), when the simulation raises events. */
53
+ events?: GPUBuffer;
54
+ }, more: GPUBindGroupEntry[], owned: {
55
+ destroy(): void;
56
+ }[]);
57
+ /** Records one frame of the simulation; a frame without births needs no tickets, as on WebGPU. */
58
+ encode(pass: GPUComputePassEncoder, births: boolean): void;
59
+ }
@@ -0,0 +1,11 @@
1
+ import type { VFXFeatures } from './base.js';
2
+ /** Most samples in a size or alpha curve (`curves.size`, `curves.alpha`). */
3
+ export declare const MAX_CURVE_SAMPLES = 32;
4
+ /** Most stops in a colour gradient (`curves.color`). */
5
+ export declare const MAX_GRADIENT_STOPS = 8;
6
+ /**
7
+ * The `curves` feature: size, opacity and colour over each particle's life. Pass it as `{curves}` to `VFX` from
8
+ * `@margoengine/vfx/base`; the root `VFX` has it. Which of the three curves are set is read at attach; their values
9
+ * can change every frame.
10
+ */
11
+ export declare const curves: NonNullable<VFXFeatures['curves']>;
package/dist/curves.js ADDED
@@ -0,0 +1,59 @@
1
+ import { MargoError as e } from "@margoengine/math/errors";
2
+ //#region src/curves.ts
3
+ var t = 32, n = 8, r = "\nstruct Curves {counts:vec4u,values:array<vec4f,24>}\n@group(0) @binding(7) var<uniform> curves:Curves;\nfn sampleCurve(base:u32,n:u32,t:f32)->f32 {\n if(n<2u){return 1.0;}\n let x=clamp(t,0.0,1.0)*f32(n-1u);let i=min(u32(x),n-2u);let j=i+1u;\n return mix(curves.values[base+i/4u][i%4u],curves.values[base+j/4u][j%4u],x-f32(i));\n}\nfn sampleGradient(n:u32,t:f32)->vec3f {\n if(n<2u){return vec3f(1);}\n var c=curves.values[16u].rgb;\n for(var k=1u;k<n;k++){let a=curves.values[15u+k];let b=curves.values[16u+k];if(t>=a.w){c=mix(a.rgb,b.rgb,clamp((t-a.w)/max(b.w-a.w,1e-6),0.0,1.0));}}\n return c;\n}\n", i = "let p=Particle(particles[at],particles[at+1],particles[cold],particles[cold+1]);", a = "o.color=vec4f(p.color.rgb,p.color.a*clamp(p.positionLife.w/p.rotation.z,0.0,1.0)/(grow*grow));";
4
+ function o(t, n, r) {
5
+ if (t.split(n).length !== 2) throw new e("unsupported", "VFX curves need the billboard shader they were written for; update the curve edits");
6
+ return t.replace(n, r);
7
+ }
8
+ var s = (e, t, n, r) => {
9
+ if (typeof e != "number" || !Number.isFinite(e) || e < n || e > r) throw RangeError(`VFX curves ${t} must be a number from ${n} to ${r}; pass a finite value in range`);
10
+ };
11
+ function c(e, t) {
12
+ if (!Array.isArray(e) || e.length < 2 || e.length > 32) throw TypeError(`VFX curves ${t} needs 2 to 32 samples; pass an array like [0, 1, 0.5]`);
13
+ e.forEach((e, n) => s(e, `${t}[${n}]`, 0, 1));
14
+ }
15
+ var l = {
16
+ validate(e) {
17
+ if (!e || typeof e != "object") throw TypeError("VFX curves must be an object with size, alpha or color; pass e.g. {size: [0, 1, 0]}");
18
+ if (e.size !== void 0 && c(e.size, "size"), e.alpha !== void 0 && c(e.alpha, "alpha"), e.color !== void 0) {
19
+ if (!Array.isArray(e.color) || e.color.length < 2 || e.color.length > 8) throw TypeError("VFX curves color needs 2 to 8 stops; pass [{at: 0, color: [1, 1, 1]}, {at: 1, color: [1, 0, 0]}]");
20
+ let t = -1;
21
+ e.color.forEach((e, n) => {
22
+ if (!e || !Array.isArray(e.color) || e.color.length !== 3) throw TypeError(`VFX curves color[${n}] needs {at, color: [r, g, b]}; pass three linear channels`);
23
+ if (s(e.at, `color[${n}].at`, 0, 1), e.color.forEach((e, t) => s(e, `color[${n}].color[${t}]`, 0, 16)), e.at < t) throw RangeError(`VFX curves color stops must ascend by "at"; move color[${n}] after color[${n - 1}]`);
24
+ t = e.at;
25
+ });
26
+ }
27
+ },
28
+ patch(e, t) {
29
+ let n = e.replace("struct Out", r + "struct Out");
30
+ return n = o(n, i, `let raw=Particle(particles[at],particles[at+1],particles[cold],particles[cold+1]);
31
+ let age=1.0-clamp(raw.positionLife.w/raw.rotation.z,0.0,1.0);var p=raw;${t.size ? "p.velocitySize.w=raw.velocitySize.w*sampleCurve(0u,curves.counts.x,age);" : ""}`), o(n, a, `o.color=vec4f(p.color.rgb${t.color ? "*sampleGradient(curves.counts.z,age)" : ""},p.color.a*${t.alpha ? "sampleCurve(8u,curves.counts.y,age)" : "clamp(p.positionLife.w/p.rotation.z,0.0,1.0)"}/(grow*grow));`);
32
+ },
33
+ create(e, t) {
34
+ let n = e.createBuffer({
35
+ size: 400,
36
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
37
+ });
38
+ t.push(n);
39
+ let r = /* @__PURE__ */ new Float32Array(100), i = new Uint32Array(r.buffer, 0, 4);
40
+ return {
41
+ entries: [{
42
+ binding: 7,
43
+ resource: { buffer: n }
44
+ }],
45
+ write(e, t) {
46
+ i.fill(0), r.fill(0, 4);
47
+ for (let [e, n] of [[0, t.size], [8, t.alpha]]) n && (i[e / 8] = n.length, r.set(n, 4 + e * 4));
48
+ t.color && (i[2] = t.color.length, t.color.forEach((e, t) => r.set([
49
+ e.color[0],
50
+ e.color[1],
51
+ e.color[2],
52
+ e.at
53
+ ], 4 + (16 + t) * 4))), e.writeBuffer(n, 0, r);
54
+ }
55
+ };
56
+ }
57
+ };
58
+ //#endregion
59
+ export { t as MAX_CURVE_SAMPLES, n as MAX_GRADIENT_STOPS, l as curves };
@@ -0,0 +1,154 @@
1
+ /** What {@link Debris.draw} writes into: a gltf `PBRInstances` (mesh.instances) fits. */
2
+ export interface DebrisInstanceTarget {
3
+ readonly capacity: number;
4
+ count: number;
5
+ setMatrixAt(i: number, m: ArrayLike<number>): unknown;
6
+ setColorAt?(i: number, color: ArrayLike<number>): unknown;
7
+ }
8
+ /** What {@link Debris.drawPuffs} draws into: a `Clouds` of `@margoengine/vfx/clouds` fits. */
9
+ export interface DebrisPuffTarget {
10
+ add(options: object): {
11
+ readonly options: object;
12
+ readonly matrix: Float32Array;
13
+ };
14
+ remove(cloud: object): boolean;
15
+ }
16
+ /** Options of {@link Debris}. Every field has a documented default; the constructor checks the ranges. */
17
+ export interface DebrisOptions {
18
+ /** World centre of the throw (the crater's centre on the ground), [x, y, z] metres. Default [0, 0, 0]. */
19
+ origin?: readonly [number, number, number];
20
+ /** Radius the pieces are launched from, metres above 0 (the crater's). Default 30. */
21
+ radius?: number;
22
+ /** Pieces, 1 to 20,000. Default 300. */
23
+ count?: number;
24
+ /** Film seconds the throw starts. Default 0. */
25
+ start?: number;
26
+ /** Seconds over which the pieces leave, inner first, 0 to 30. Default 1.5. */
27
+ spread?: number;
28
+ /** Launch speed at the rim, m/s, 1 to 1,000. Default 40. */
29
+ speed?: number;
30
+ /** Fastest launch speed (the inner pieces), m/s, at least `speed`, at most 2,000. Default 4 times `speed`. */
31
+ maxSpeed?: number;
32
+ /** Ejecta exponent mu of v proportional to (x / R)^(-1/mu) (Housen and Holsapple 2011), 0.2 to 0.7. Default 0.41. */
33
+ mu?: number;
34
+ /** Mean launch angle above the horizon, degrees, 10 to 90. Default 50. */
35
+ angle?: number;
36
+ /** Spread of the launch angle, degrees, 0 to 40. Default 12. */
37
+ angleSpread?: number;
38
+ /** Smallest and largest piece size (edge length), metres, each above 0. Default [0.2, 1.2]. */
39
+ size?: readonly [number, number];
40
+ /** Piece density, kg/m^3, 100 to 5,000. Default 1,800 (moist earth; 2,300 gypsum, 600 timber). */
41
+ density?: number;
42
+ /** Drag coefficient, 0 to 3 (Mastin 2001). Default 1. */
43
+ drag?: number;
44
+ /** Air density, kg/m^3, 0 to 2. Default 1.2. */
45
+ airDensity?: number;
46
+ /** Ground height under a world point; pieces land on it. Default flat at the origin's height. */
47
+ ground?: (x: number, z: number) => number;
48
+ /** Tumble rate, radians a second at most, 0 to 100. Default 6. */
49
+ tumble?: number;
50
+ /** Colour of the pieces, linear RGB 0 to 1, multiplied with each piece's shade. Default [0.22, 0.16, 0.1]. */
51
+ color?: readonly [number, number, number];
52
+ /** Seconds a landing puff lasts, 0 to 60 (0: no puffs). Default 6. */
53
+ puffLife?: number;
54
+ /** Colour of the landing dust, linear RGB 0 to 1. Default [0.36, 0.3, 0.23]. */
55
+ puffColor?: readonly [number, number, number];
56
+ /** Puff size per cube root of impact energy, metres per J^(1/3), 0 to 1. Default 0.04. */
57
+ puffScale?: number;
58
+ /** Seed of the pieces, an integer. Default 1. */
59
+ seed?: number;
60
+ }
61
+ /** One piece at a film time, from {@link Debris.pieces}. */
62
+ export interface DebrisPiece {
63
+ readonly position: readonly [number, number, number];
64
+ readonly velocity: readonly [number, number, number];
65
+ /** Edge length, metres. */
66
+ readonly size: number;
67
+ /** 'waiting' before it leaves, 'flying', or 'landed'. */
68
+ readonly state: 'waiting' | 'flying' | 'landed';
69
+ /** Film seconds it leaves the ground. */
70
+ readonly launch: number;
71
+ /** Film seconds it lands (Infinity if it never does within 120 s). */
72
+ readonly landing: number;
73
+ }
74
+ /**
75
+ * Ballistic debris of an explosion or impact over film time: `count` pieces launched from within `radius` of `origin`,
76
+ * the inner ones first and fastest (Housen and Holsapple 2011), flying through drag (Mastin 2001), tumbling, and landing
77
+ * on `ground` with a dust puff. `draw(instances)` writes the pieces into a gltf `PBRInstances` (any mesh: a clod, a
78
+ * block, a plank); `drawPuffs(clouds)` puts the landing dust into a `Clouds` of `@margoengine/vfx/clouds`.
79
+ */
80
+ export declare class Debris {
81
+ readonly options: Readonly<DebrisOptions>;
82
+ private readonly paths;
83
+ private film;
84
+ private puff;
85
+ private puffTarget;
86
+ constructor(options?: DebrisOptions);
87
+ /** Film time now, seconds. */
88
+ get time(): number;
89
+ /** Sets the film time; the debris is a function of it alone. */
90
+ seek(t: number): void;
91
+ /** Moves the film time on by `delta` seconds. */
92
+ update(delta: number): void;
93
+ private at;
94
+ /** Every piece at film time `t` (default now). */
95
+ pieces(t?: number): DebrisPiece[];
96
+ /**
97
+ * Writes the flying and landed pieces into `target` (a gltf `PBRInstances` of a unit-sized mesh: each instance is
98
+ * scaled to its piece's size, turned by its tumble and tinted by `color` times its shade). Returns the count written.
99
+ */
100
+ draw(target: DebrisInstanceTarget): number;
101
+ /** The landing puffs alive at film time `t` (default now): centre, size (metres across) and opacity 0 to 1. */
102
+ puffs(t?: number): {
103
+ centre: [number, number, number];
104
+ size: number;
105
+ opacity: number;
106
+ }[];
107
+ /** Puts the landing puffs into `clouds` (a `Clouds` of `@margoengine/vfx/clouds`) as one cloud. */
108
+ drawPuffs(clouds: DebrisPuffTarget): void;
109
+ /** Takes the puff cloud {@link drawPuffs} added out of its `Clouds`. */
110
+ clearPuffs(): void;
111
+ }
112
+ /** A crater's spoil as `ejectaSpoil` of @margoengine/physics/crater gives it: per cell, fragments per size bin. */
113
+ export interface SpoilScatterSource {
114
+ readonly columns: number;
115
+ readonly rows: number;
116
+ /** The cell's size, m, and the grid's minimum corner, [x, z] m. */
117
+ readonly cell: number;
118
+ readonly origin: readonly [number, number];
119
+ /** The size bins' edges, m (bins + 1). */
120
+ readonly sizes: readonly number[];
121
+ /** Expected fragments landing in each cell in each bin, at (c + r * columns) * bins + bin. */
122
+ readonly counts: ArrayLike<number>;
123
+ }
124
+ /** Options of {@link drawSpoil}. */
125
+ export interface SpoilScatterOptions {
126
+ /** The ground's height at (x, z), m. Default flat at 0. */
127
+ readonly height?: (x: number, z: number) => number;
128
+ /**
129
+ * The size of the clods a fragment breaks into on landing, over its own, 0.1 to 1 (default 0.5: eight clods of half
130
+ * its size, its volume kept). A look choice: no measurement of how ejecta clods break was found.
131
+ */
132
+ readonly breakup?: number;
133
+ /** How far each clod sits in the ground, as a share of its size, 0 to 1 (default 0.35, half buried as at Fauld). */
134
+ readonly bury?: number;
135
+ /** The drawn mesh's own size at a scale of 1, m (default 1). */
136
+ readonly meshSize?: number;
137
+ /** The clods' colours, linear RGBA, each clod one of them in turn (default the red-brown mudstone of Fauld's spoil). */
138
+ readonly colors?: readonly (readonly [number, number, number, number])[];
139
+ /** The layout seed, a whole number (default 1). */
140
+ readonly seed?: number;
141
+ }
142
+ /** What {@link drawSpoil} drew: clods, the smallest size it reached, m, and the share of the spoil's clods drawn. */
143
+ export interface SpoilScatterResult {
144
+ readonly drawn: number;
145
+ readonly smallest: number;
146
+ readonly share: number;
147
+ }
148
+ /**
149
+ * Draws a crater's spoil into `target` as clods: each fragment breaks on landing into 1 / breakup^3 clods of `breakup`
150
+ * its size, scattered over its cell, turned and half buried. The largest are drawn first; when the target cannot hold
151
+ * them all, the size class that fills it is thinned evenly and smaller ones are left out. Deterministic in its seed.
152
+ * @throws RangeError for an option out of range or a source of the wrong shape.
153
+ */
154
+ export declare function drawSpoil(target: DebrisInstanceTarget, source: SpoilScatterSource, options?: SpoilScatterOptions): SpoilScatterResult;