@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +2240 -2
- package/dist/base-LGs65K7K.js +709 -0
- package/dist/base.d.ts +869 -0
- package/dist/base.js +1 -0
- package/dist/blown-sand.d.ts +218 -0
- package/dist/blown-sand.js +700 -0
- package/dist/clouds.d.ts +237 -0
- package/dist/clouds.js +583 -0
- package/dist/compat-BFjXAQuu.js +14 -0
- package/dist/compat-CVSEj1Bp.js +217 -0
- package/dist/compat.d.ts +59 -0
- package/dist/curves.d.ts +11 -0
- package/dist/curves.js +59 -0
- package/dist/debris.d.ts +154 -0
- package/dist/debris.js +290 -0
- package/dist/depth-collide.d.ts +7 -0
- package/dist/depth-collide.js +96 -0
- package/dist/dust-current.d.ts +66 -0
- package/dist/dust-current.js +92 -0
- package/dist/emitter.d.ts +10 -0
- package/dist/emitter.js +125 -0
- package/dist/events.d.ts +9 -0
- package/dist/events.js +131 -0
- package/dist/extra-CgGjbojB.js +4 -0
- package/dist/extra.d.ts +2 -0
- package/dist/fields.d.ts +11 -0
- package/dist/fields.js +95 -0
- package/dist/fire-front.d.ts +610 -0
- package/dist/fire-front.js +1080 -0
- package/dist/fire.d.ts +140 -0
- package/dist/fire.js +194 -0
- package/dist/flame-ramp-VoBKpYnM.js +75 -0
- package/dist/flame-ramp.d.ts +61 -0
- package/dist/flame.d.ts +428 -0
- package/dist/flame.js +817 -0
- package/dist/flipbook.d.ts +7 -0
- package/dist/flipbook.js +23 -0
- package/dist/flocking-shaders.d.ts +48 -0
- package/dist/flocking-sim.d.ts +623 -0
- package/dist/flocking.d.ts +145 -0
- package/dist/flocking.js +1846 -0
- package/dist/fog-B-kBv7g0.js +32 -0
- package/dist/frame-BuvMFBPp.js +4 -0
- package/dist/frame.d.ts +7 -0
- package/dist/gunfire.d.ts +742 -0
- package/dist/gunfire.js +1214 -0
- package/dist/hash-Btsp6fGK.js +7 -0
- package/dist/hash.d.ts +2 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +60 -0
- package/dist/json.d.ts +53 -0
- package/dist/json.js +239 -0
- package/dist/lit-Dx2tNwP-.js +152 -0
- package/dist/lit.d.ts +18 -0
- package/dist/lit.js +1 -0
- package/dist/mesh.d.ts +16 -0
- package/dist/mesh.js +248 -0
- package/dist/node-system.d.ts +315 -0
- package/dist/node-system.js +384 -0
- package/dist/nodes.d.ts +14 -0
- package/dist/nodes.js +293 -0
- package/dist/obstacles.d.ts +3 -0
- package/dist/obstacles.js +25 -0
- package/dist/over-life.d.ts +14 -0
- package/dist/over-life.js +94 -0
- package/dist/reactive-mask-B8xbyzlk.js +69 -0
- package/dist/require-webgpu--tUXPsmA.js +7 -0
- package/dist/ribbons.d.ts +22 -0
- package/dist/ribbons.js +165 -0
- package/dist/schedule.d.ts +29 -0
- package/dist/schedule.js +47 -0
- package/dist/settings.d.ts +362 -0
- package/dist/settings.js +707 -0
- package/dist/shader.d.ts +49 -0
- package/dist/shadows.d.ts +26 -0
- package/dist/shadows.js +182 -0
- package/dist/shapes.d.ts +34 -0
- package/dist/shapes.js +81 -0
- package/dist/shared/fog.d.ts +33 -0
- package/dist/shared/pass.d.ts +53 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/skinned.d.ts +11 -0
- package/dist/skinned.js +104 -0
- package/dist/smoke.d.ts +283 -0
- package/dist/smoke.js +504 -0
- package/dist/soft.d.ts +17 -0
- package/dist/soft.js +55 -0
- package/dist/solid.d.ts +168 -0
- package/dist/solid.js +403 -0
- package/dist/sort.d.ts +3 -0
- package/dist/sort.js +58 -0
- package/dist/space.d.ts +14 -0
- package/dist/space.js +88 -0
- package/dist/stretch.d.ts +7 -0
- package/dist/stretch.js +40 -0
- package/dist/texture.d.ts +8 -0
- package/dist/texture.js +45 -0
- package/dist/view-RKprOPAy.js +30 -0
- package/dist/view-rect-DkeiiUtw.js +13 -0
- package/dist/wrap.d.ts +3 -0
- package/dist/wrap.js +5 -0
- package/package.json +198 -4
package/dist/mesh.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { t as e } from "./frame-BuvMFBPp.js";
|
|
2
|
+
import { t } from "./view-rect-DkeiiUtw.js";
|
|
3
|
+
import { n, r, t as i } from "./reactive-mask-B8xbyzlk.js";
|
|
4
|
+
//#region src/mesh.ts
|
|
5
|
+
var a = 65536, o = [
|
|
6
|
+
.4,
|
|
7
|
+
1,
|
|
8
|
+
.3
|
|
9
|
+
];
|
|
10
|
+
function s(e) {
|
|
11
|
+
let t = e.positions, n = t.length / 3, r = e.indices ?? Array.from({ length: n }, (e, t) => t), i = 0;
|
|
12
|
+
for (let e = 0; e < n; e++) i = Math.max(i, Math.hypot(t[e * 3], t[e * 3 + 1], t[e * 3 + 2]));
|
|
13
|
+
let a = new Float32Array(r.length * 8);
|
|
14
|
+
for (let n = 0; n < r.length; n += 3) {
|
|
15
|
+
let o = (e) => [
|
|
16
|
+
0,
|
|
17
|
+
1,
|
|
18
|
+
2
|
|
19
|
+
].map((i) => t[r[n + e] * 3 + i]), s = o(0), c = o(1), l = o(2), u = [
|
|
20
|
+
c[0] - s[0],
|
|
21
|
+
c[1] - s[1],
|
|
22
|
+
c[2] - s[2]
|
|
23
|
+
], d = [
|
|
24
|
+
l[0] - s[0],
|
|
25
|
+
l[1] - s[1],
|
|
26
|
+
l[2] - s[2]
|
|
27
|
+
], f = [
|
|
28
|
+
u[1] * d[2] - u[2] * d[1],
|
|
29
|
+
u[2] * d[0] - u[0] * d[2],
|
|
30
|
+
u[0] * d[1] - u[1] * d[0]
|
|
31
|
+
], p = Math.hypot(...f) || 1;
|
|
32
|
+
[
|
|
33
|
+
s,
|
|
34
|
+
c,
|
|
35
|
+
l
|
|
36
|
+
].forEach((t, o) => {
|
|
37
|
+
let s = e.normals ? [
|
|
38
|
+
0,
|
|
39
|
+
1,
|
|
40
|
+
2
|
|
41
|
+
].map((t) => e.normals[r[n + o] * 3 + t]) : f.map((e) => e / p), c = Math.hypot(...s) || 1;
|
|
42
|
+
a.set([
|
|
43
|
+
t[0] / i,
|
|
44
|
+
t[1] / i,
|
|
45
|
+
t[2] / i,
|
|
46
|
+
0,
|
|
47
|
+
s[0] / c,
|
|
48
|
+
s[1] / c,
|
|
49
|
+
s[2] / c,
|
|
50
|
+
0
|
|
51
|
+
], (n + o) * 8);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return a;
|
|
55
|
+
}
|
|
56
|
+
var c = (t, n = "") => e + n + `
|
|
57
|
+
@group(0) @binding(1) var<storage,read> particles:array<vec4f>;
|
|
58
|
+
@group(0) @binding(2) var<storage,read> alive:array<u32>;
|
|
59
|
+
@group(0) @binding(15) var<storage,read> meshCorners:array<vec4f>;
|
|
60
|
+
struct MeshLight {direction:vec4f,color:vec4f}
|
|
61
|
+
@group(0) @binding(16) var<uniform> meshLight:MeshLight;
|
|
62
|
+
struct Out {@builtin(position) clip:vec4f,@location(0) color:vec3f,@location(1) normal:vec3f}
|
|
63
|
+
fn turn(v:vec3f,axis:vec3f,angle:f32)->vec3f {let c=cos(angle);let s=sin(angle);return v*c+cross(axis,v)*s+axis*dot(axis,v)*(1-c);}
|
|
64
|
+
fn meshHash(v:u32)->u32 {var x=v;x=(x^(x>>16u))*0x7feb352du;x=(x^(x>>15u))*0x846ca68bu;return x^(x>>16u);}
|
|
65
|
+
@vertex fn vertex(@builtin(vertex_index) corner:u32,@builtin(instance_index) instance:u32)->Out {
|
|
66
|
+
let at=alive[instance]*2;let cold=f.control.x*2+at;let p=particles[at];let vs=particles[at+1];let color=particles[cold];let r=particles[cold+1];
|
|
67
|
+
let angle=r.x+r.y*(r.z-p.w)${n ? "+drawnSpin(r.z,p.w)" : ""};var q=meshCorners[corner*2].xyz;var n=meshCorners[corner*2+1].xyz;
|
|
68
|
+
${t === "random" ? "let h=meshHash(bitcast<u32>(r.x));let z=f32(h&0xffffu)/32767.5-1;let lon=f32(h>>16u)/65536*6.2831853;let s=sqrt(max(0.0,1-z*z));\n let axis=vec3f(s*cos(lon),z,s*sin(lon));q=turn(q,axis,angle);n=turn(n,axis,angle);" : "let speed=length(vs.xyz);let y=select(vec3f(0,1,0),vs.xyz/speed,speed>1e-6);\n let x=normalize(cross(y,select(vec3f(0,0,1),vec3f(1,0,0),abs(y.z)>0.9)));let z=cross(x,y);\n q=turn(q,vec3f(0,1,0),angle);n=turn(n,vec3f(0,1,0),angle);q=x*q.x+y*q.y+z*q.z;n=x*n.x+y*n.y+z*n.z;"}
|
|
69
|
+
var o:Out;o.clip=f.camera*vec4f(p.xyz+q*vs.w*0.5,1);o.normal=n;o.color=color.rgb;return o;
|
|
70
|
+
}
|
|
71
|
+
@fragment fn fragment(i:Out)->@location(0) vec4f {
|
|
72
|
+
let lit=meshLight.direction.w+(1-meshLight.direction.w)*max(dot(normalize(i.normal),meshLight.direction.xyz),0.0);
|
|
73
|
+
return vec4f(i.color*meshLight.color.rgb*lit,1);
|
|
74
|
+
}
|
|
75
|
+
@fragment fn reactive(i:Out)->@location(0) vec4f {return vec4f(1,0,0,1);}`, l = (e, t) => {
|
|
76
|
+
let n = e;
|
|
77
|
+
if (!n || typeof n != "object" || typeof n.length != "number") throw TypeError(`VFX mesh ${t} must be an array of numbers; pass a Float32Array or number[]`);
|
|
78
|
+
for (let e = 0; e < n.length; e++) if (typeof n[e] != "number" || !Number.isFinite(n[e])) throw RangeError(`VFX mesh ${t}[${e}] is not a finite number; pass finite values`);
|
|
79
|
+
return n;
|
|
80
|
+
}, u = /* @__PURE__ */ new WeakSet();
|
|
81
|
+
function d(e) {
|
|
82
|
+
let t = l(e.positions, "positions");
|
|
83
|
+
if (t.length % 3 || t.length < 9) throw RangeError("VFX mesh positions must be xyz triples, at least one triangle; pass 3 numbers a vertex");
|
|
84
|
+
let n = t.length / 3, r = e.indices === void 0 ? void 0 : l(e.indices, "indices"), i = r ? r.length : n;
|
|
85
|
+
if (i % 3 || !i || i / 3 > 65536) throw RangeError(`VFX mesh needs 1 to ${a} whole triangles; pass indices (or positions) a multiple of 3`);
|
|
86
|
+
if (r) {
|
|
87
|
+
for (let e = 0; e < r.length; e++) if (!Number.isInteger(r[e]) || r[e] < 0 || r[e] >= n) throw RangeError(`VFX mesh indices[${e}] is ${r[e]}; pass whole numbers below ${n}`);
|
|
88
|
+
}
|
|
89
|
+
if (e.normals !== void 0 && l(e.normals, "normals").length !== t.length) throw RangeError("VFX mesh normals must have one xyz normal per vertex, as many numbers as positions");
|
|
90
|
+
let o = 0;
|
|
91
|
+
for (let e = 0; e < t.length; e++) o = Math.max(o, Math.abs(t[e]));
|
|
92
|
+
if (!o) throw RangeError("VFX mesh positions are all at the origin; pass a mesh with some size");
|
|
93
|
+
u.add(e);
|
|
94
|
+
}
|
|
95
|
+
var f = {
|
|
96
|
+
validate(e) {
|
|
97
|
+
if (!e || typeof e != "object") throw TypeError("VFX mesh must be an object with positions; pass e.g. {positions, indices}");
|
|
98
|
+
if (u.has(e) || d(e), e.align !== void 0 && e.align !== "random" && e.align !== "velocity") throw TypeError(`VFX mesh align is ${String(e.align)}; pass 'random' or 'velocity'`);
|
|
99
|
+
let t = e.light ?? o;
|
|
100
|
+
if (!Array.isArray(t) || t.length !== 3 || !t.every(Number.isFinite) || !Math.hypot(...t)) throw RangeError("VFX mesh light must be a nonzero direction [x, y, z] towards the light");
|
|
101
|
+
let n = e.lightColor ?? [
|
|
102
|
+
1,
|
|
103
|
+
1,
|
|
104
|
+
1
|
|
105
|
+
];
|
|
106
|
+
if (!Array.isArray(n) || n.length !== 3 || !n.every((e) => Number.isFinite(e) && e >= 0 && e <= 16)) throw RangeError("VFX mesh lightColor must be linear [r, g, b], each 0 to 16");
|
|
107
|
+
if (e.ambient !== void 0 && !(typeof e.ambient == "number" && e.ambient >= 0 && e.ambient <= 1)) throw RangeError("VFX mesh ambient must be 0 to 1; pass e.g. 0.35");
|
|
108
|
+
},
|
|
109
|
+
create(e, a, l) {
|
|
110
|
+
d(a);
|
|
111
|
+
let { device: u, options: f } = e, p = s(a), m = p.length / 8, h = a.align ?? "random", g = u.createBuffer({
|
|
112
|
+
size: p.byteLength,
|
|
113
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
114
|
+
});
|
|
115
|
+
l.push(g), u.queue.writeBuffer(g, 0, p);
|
|
116
|
+
let _ = u.createBuffer({
|
|
117
|
+
size: 32,
|
|
118
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
119
|
+
});
|
|
120
|
+
l.push(_);
|
|
121
|
+
let v = u.createBuffer({
|
|
122
|
+
size: 16,
|
|
123
|
+
usage: GPUBufferUsage.INDIRECT | GPUBufferUsage.COPY_DST
|
|
124
|
+
});
|
|
125
|
+
l.push(v), u.queue.writeBuffer(v, 0, new Uint32Array([
|
|
126
|
+
m,
|
|
127
|
+
0,
|
|
128
|
+
0,
|
|
129
|
+
0
|
|
130
|
+
]));
|
|
131
|
+
let y = /* @__PURE__ */ new Float32Array(8), b = /* @__PURE__ */ new Map(), x = {
|
|
132
|
+
view: void 0,
|
|
133
|
+
loadOp: "load",
|
|
134
|
+
storeOp: "store"
|
|
135
|
+
}, S = {
|
|
136
|
+
view: void 0,
|
|
137
|
+
depthLoadOp: "load",
|
|
138
|
+
depthStoreOp: "store"
|
|
139
|
+
}, C = {
|
|
140
|
+
colorAttachments: [x],
|
|
141
|
+
depthStencilAttachment: S
|
|
142
|
+
}, w = {
|
|
143
|
+
view: void 0,
|
|
144
|
+
depthReadOnly: !0
|
|
145
|
+
}, T = [], E = {
|
|
146
|
+
colorAttachments: T,
|
|
147
|
+
depthStencilAttachment: w
|
|
148
|
+
}, D = {}, O, k = () => {
|
|
149
|
+
if (f.mesh !== a) throw TypeError("VFX mesh is fixed at attach; change its light in place or attach a new VFX");
|
|
150
|
+
return a;
|
|
151
|
+
};
|
|
152
|
+
return {
|
|
153
|
+
hides: () => !0,
|
|
154
|
+
write(e) {
|
|
155
|
+
let t = k(), n = t.light ?? o, r = Math.hypot(n[0], n[1], n[2]);
|
|
156
|
+
y.set([
|
|
157
|
+
n[0] / r,
|
|
158
|
+
n[1] / r,
|
|
159
|
+
n[2] / r,
|
|
160
|
+
t.ambient ?? .35,
|
|
161
|
+
...t.lightColor ?? [
|
|
162
|
+
1,
|
|
163
|
+
1,
|
|
164
|
+
1
|
|
165
|
+
],
|
|
166
|
+
0
|
|
167
|
+
]), e.device.queue.writeBuffer(_, 0, y);
|
|
168
|
+
},
|
|
169
|
+
draw(a) {
|
|
170
|
+
let o = a.descriptor.depthStencil.format, s = a.descriptor.multisample?.count ?? 1, l = a.format + ":" + o + ":" + s, d = (t) => {
|
|
171
|
+
let n = b.get(l + (t ? ":reactive" : ""));
|
|
172
|
+
if (n) return n;
|
|
173
|
+
O ??= u.createShaderModule({ code: c(h, e.spin?.code) });
|
|
174
|
+
let r = u.createRenderPipeline({
|
|
175
|
+
layout: "auto",
|
|
176
|
+
vertex: {
|
|
177
|
+
module: O,
|
|
178
|
+
entryPoint: "vertex"
|
|
179
|
+
},
|
|
180
|
+
fragment: t ? {
|
|
181
|
+
module: O,
|
|
182
|
+
entryPoint: "reactive",
|
|
183
|
+
targets: [{
|
|
184
|
+
format: "r8unorm",
|
|
185
|
+
blend: i
|
|
186
|
+
}]
|
|
187
|
+
} : {
|
|
188
|
+
module: O,
|
|
189
|
+
entryPoint: "fragment",
|
|
190
|
+
targets: [{ format: a.format }]
|
|
191
|
+
},
|
|
192
|
+
primitive: {
|
|
193
|
+
topology: "triangle-list",
|
|
194
|
+
cullMode: "back",
|
|
195
|
+
frontFace: "ccw"
|
|
196
|
+
},
|
|
197
|
+
multisample: a.descriptor.multisample,
|
|
198
|
+
depthStencil: t ? {
|
|
199
|
+
format: o,
|
|
200
|
+
depthWriteEnabled: !1,
|
|
201
|
+
depthCompare: "greater-equal"
|
|
202
|
+
} : {
|
|
203
|
+
format: o,
|
|
204
|
+
depthWriteEnabled: !0,
|
|
205
|
+
depthCompare: "greater"
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
return n = [r, u.createBindGroup({
|
|
209
|
+
layout: r.getBindGroupLayout(0),
|
|
210
|
+
entries: [
|
|
211
|
+
{
|
|
212
|
+
binding: 0,
|
|
213
|
+
resource: { buffer: e.uniform }
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
binding: 1,
|
|
217
|
+
resource: { buffer: e.particles }
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
binding: 2,
|
|
221
|
+
resource: { buffer: e.alive }
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
binding: 15,
|
|
225
|
+
resource: { buffer: g }
|
|
226
|
+
},
|
|
227
|
+
...t ? [] : [{
|
|
228
|
+
binding: 16,
|
|
229
|
+
resource: { buffer: _ }
|
|
230
|
+
}],
|
|
231
|
+
...e.spin ? [e.spin.entry] : []
|
|
232
|
+
]
|
|
233
|
+
})], b.set(l + (t ? ":reactive" : ""), n), n;
|
|
234
|
+
};
|
|
235
|
+
a.encoder.copyBufferToBuffer(e.visible, 4, v, 4, 4), x.view = a.color, S.view = w.view = a.depth;
|
|
236
|
+
let p = o.includes("stencil");
|
|
237
|
+
S.stencilLoadOp = p ? "load" : void 0, S.stencilStoreOp = p ? "store" : void 0, w.stencilReadOnly = p ? !0 : void 0;
|
|
238
|
+
let m = (n, [r, i]) => {
|
|
239
|
+
let o = a.encoder.beginRenderPass(n);
|
|
240
|
+
o.setPipeline(r), o.setBindGroup(0, i), t(o, a), o.drawIndirect(v, 0), o.end(), e.stats.drawCalls++;
|
|
241
|
+
};
|
|
242
|
+
m(C, d(!1)), (f.reactive ?? n(a, D)) && (T[0] = r(a), m(E, d(!0)));
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
//#endregion
|
|
248
|
+
export { a as MAX_PARTICLE_MESH_TRIANGLES, f as mesh, s as meshCorners };
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import type { VFXNode, VFXOptions } from './base.js';
|
|
2
|
+
/** Current node-system document version. Unknown versions reject instead of loading half a graph. */
|
|
3
|
+
export declare const NODE_SYSTEM_VERSION = 1;
|
|
4
|
+
/** Most blocks in one graph. */
|
|
5
|
+
export declare const MAX_NODE_SYSTEM_BLOCKS = 512;
|
|
6
|
+
/** Most particle systems in one graph. */
|
|
7
|
+
export declare const MAX_NODE_SYSTEMS = 32;
|
|
8
|
+
/**
|
|
9
|
+
* Why a node-system graph was refused. Stable across releases: branch on `code`, not on message text.
|
|
10
|
+
* - `format`, `version`: the JSON is not a node-system document, or its version is not 1.
|
|
11
|
+
* - `limit`: more blocks or systems than the limits allow.
|
|
12
|
+
* - `duplicate-id`, `unknown-block`, `unknown-ref`: block ids are not unique, a block has an unknown `type`, or a wire names a missing block.
|
|
13
|
+
* - `wrong-type`: a wire feeds a block whose output does not fit the input (a color into a lifetime).
|
|
14
|
+
* - `bad-param`: a literal is missing, not finite or out of range.
|
|
15
|
+
* - `cycle`: value wires or start/end triggers loop back on themselves.
|
|
16
|
+
* - `no-system`: the graph has no system block, or no system that starts by itself.
|
|
17
|
+
* - `trigger`: a trigger names a block that is not a system, or an event trigger cannot be built (see its message).
|
|
18
|
+
* - `invalid-options`: the compiled options were refused by `VFX` (a size or rate out of range).
|
|
19
|
+
*/
|
|
20
|
+
export type VFXNodeSystemCode = 'format' | 'version' | 'limit' | 'duplicate-id' | 'unknown-block' | 'unknown-ref' | 'wrong-type' | 'bad-param' | 'cycle' | 'no-system' | 'trigger' | 'invalid-options';
|
|
21
|
+
/** A refused node-system graph. `block` is the id of the offending block when there is one. */
|
|
22
|
+
export declare class VFXNodeSystemError extends Error {
|
|
23
|
+
readonly code: VFXNodeSystemCode;
|
|
24
|
+
readonly block: string | undefined;
|
|
25
|
+
constructor(code: VFXNodeSystemCode, message: string, block?: string, options?: {
|
|
26
|
+
cause?: unknown;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/** A literal or the id of a value block that produces it. */
|
|
30
|
+
export type NodeRef<T> = T | string;
|
|
31
|
+
type Num = NodeRef<number>;
|
|
32
|
+
type Vec3 = NodeRef<readonly [Num, Num, Num]>;
|
|
33
|
+
/** `number`: a number. As a lifetime it is the range [v, v]. */
|
|
34
|
+
export interface NumberBlock {
|
|
35
|
+
id: string;
|
|
36
|
+
type: 'number';
|
|
37
|
+
value: Num;
|
|
38
|
+
}
|
|
39
|
+
/** `vector`: three numbers, for velocity, position, gravity, shape axes. */
|
|
40
|
+
export interface VectorBlock {
|
|
41
|
+
id: string;
|
|
42
|
+
type: 'vector';
|
|
43
|
+
value: readonly [Num, Num, Num];
|
|
44
|
+
}
|
|
45
|
+
/** `color`: linear RGBA, each 0..1 (the stock range of the options it feeds). */
|
|
46
|
+
export interface ColorBlock {
|
|
47
|
+
id: string;
|
|
48
|
+
type: 'color';
|
|
49
|
+
value: readonly [Num, Num, Num, Num];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* `random`: a number drawn between `min` and `max` once per compile, deterministically from `seed` and the graph's
|
|
53
|
+
* seed (default seed: the block's position). Fed to a range input (a lifetime) it stays the range [min, max], so
|
|
54
|
+
* the GPU draws a fresh value per particle. With `vector: true` it is three independent draws.
|
|
55
|
+
*/
|
|
56
|
+
export interface RandomBlock {
|
|
57
|
+
id: string;
|
|
58
|
+
type: 'random';
|
|
59
|
+
min: Num;
|
|
60
|
+
max: Num;
|
|
61
|
+
seed?: number;
|
|
62
|
+
vector?: boolean;
|
|
63
|
+
}
|
|
64
|
+
/** `math`: `add`, `sub`, `mul`, `min`, `max` of `a` and `b`, or `mix` of `a` to `b` by `t` (default 0.5). */
|
|
65
|
+
export interface MathBlock {
|
|
66
|
+
id: string;
|
|
67
|
+
type: 'math';
|
|
68
|
+
op: 'add' | 'sub' | 'mul' | 'min' | 'max' | 'mix';
|
|
69
|
+
a: Num;
|
|
70
|
+
b: Num;
|
|
71
|
+
t?: Num;
|
|
72
|
+
}
|
|
73
|
+
/** `gradient`: 2..8 stops ascending by `at` (0..1) with a linear RGB `color` (each 0..16); feeds `update-color`. */
|
|
74
|
+
export interface GradientBlock {
|
|
75
|
+
id: string;
|
|
76
|
+
type: 'gradient';
|
|
77
|
+
stops: readonly {
|
|
78
|
+
at: Num;
|
|
79
|
+
color: readonly [Num, Num, Num];
|
|
80
|
+
}[];
|
|
81
|
+
}
|
|
82
|
+
/** `curve`: 2..32 evenly spaced samples over normalized life; feeds the `update-*` curve blocks. */
|
|
83
|
+
export interface CurveBlock {
|
|
84
|
+
id: string;
|
|
85
|
+
type: 'curve';
|
|
86
|
+
samples: readonly Num[];
|
|
87
|
+
}
|
|
88
|
+
/** The value blocks, which only feed other blocks. */
|
|
89
|
+
export type NodeSystemValueBlock = NumberBlock | VectorBlock | ColorBlock | RandomBlock | MathBlock | GradientBlock | CurveBlock;
|
|
90
|
+
/** `shape`: where births start (the `emitter` option). `shape` is one of `sphere`, `cone`, `hemisphere`, `cylinder`, `torus`, `rectangle-edge`, `grid`; the other fields are that shape's own, each a literal or a wire. */
|
|
91
|
+
export interface ShapeBlock {
|
|
92
|
+
id: string;
|
|
93
|
+
type: 'shape';
|
|
94
|
+
shape: 'sphere' | 'cone' | 'hemisphere' | 'cylinder' | 'torus' | 'rectangle-edge' | 'grid';
|
|
95
|
+
radius?: Num;
|
|
96
|
+
shell?: boolean;
|
|
97
|
+
speed?: Num;
|
|
98
|
+
angle?: Num;
|
|
99
|
+
axis?: Vec3;
|
|
100
|
+
height?: Num;
|
|
101
|
+
tube?: Num;
|
|
102
|
+
width?: Num;
|
|
103
|
+
columns?: Num;
|
|
104
|
+
rows?: Num;
|
|
105
|
+
layers?: Num;
|
|
106
|
+
spacing?: Vec3;
|
|
107
|
+
}
|
|
108
|
+
/** Creation blocks: the settings a particle is born with. */
|
|
109
|
+
export type NodeSystemCreateBlock = {
|
|
110
|
+
id: string;
|
|
111
|
+
type: 'create-capacity';
|
|
112
|
+
value: Num;
|
|
113
|
+
} | {
|
|
114
|
+
id: string;
|
|
115
|
+
type: 'create-rate';
|
|
116
|
+
value: Num;
|
|
117
|
+
} | {
|
|
118
|
+
id: string;
|
|
119
|
+
type: 'create-initial';
|
|
120
|
+
value: Num;
|
|
121
|
+
} | {
|
|
122
|
+
id: string;
|
|
123
|
+
type: 'create-lifetime';
|
|
124
|
+
value: NodeRef<readonly [Num, Num] | Num>;
|
|
125
|
+
} | {
|
|
126
|
+
id: string;
|
|
127
|
+
type: 'create-size';
|
|
128
|
+
value: Num;
|
|
129
|
+
} | {
|
|
130
|
+
id: string;
|
|
131
|
+
type: 'create-color';
|
|
132
|
+
value: NodeRef<readonly [Num, Num, Num, Num]>;
|
|
133
|
+
} | {
|
|
134
|
+
id: string;
|
|
135
|
+
type: 'create-position';
|
|
136
|
+
value: Vec3;
|
|
137
|
+
} | {
|
|
138
|
+
id: string;
|
|
139
|
+
type: 'create-velocity';
|
|
140
|
+
value: Vec3;
|
|
141
|
+
spread?: Num;
|
|
142
|
+
} | {
|
|
143
|
+
id: string;
|
|
144
|
+
type: 'create-prewarm';
|
|
145
|
+
seconds: Num;
|
|
146
|
+
seed?: number;
|
|
147
|
+
} | {
|
|
148
|
+
id: string;
|
|
149
|
+
type: 'create-burst';
|
|
150
|
+
time: Num;
|
|
151
|
+
count: Num;
|
|
152
|
+
cycles?: Num;
|
|
153
|
+
interval?: Num;
|
|
154
|
+
probability?: Num;
|
|
155
|
+
};
|
|
156
|
+
/** Update blocks: what happens to a particle while it lives. */
|
|
157
|
+
export type NodeSystemUpdateBlock = {
|
|
158
|
+
id: string;
|
|
159
|
+
type: 'update-size';
|
|
160
|
+
curve: NodeRef<readonly Num[]>;
|
|
161
|
+
} | {
|
|
162
|
+
id: string;
|
|
163
|
+
type: 'update-alpha';
|
|
164
|
+
curve: NodeRef<readonly Num[]>;
|
|
165
|
+
} | {
|
|
166
|
+
id: string;
|
|
167
|
+
type: 'update-color';
|
|
168
|
+
gradient: NodeRef<GradientBlock['stops']>;
|
|
169
|
+
} | {
|
|
170
|
+
id: string;
|
|
171
|
+
type: 'update-spin';
|
|
172
|
+
curve: NodeRef<readonly Num[]>;
|
|
173
|
+
} | {
|
|
174
|
+
id: string;
|
|
175
|
+
type: 'update-speed';
|
|
176
|
+
curve: NodeRef<readonly Num[]>;
|
|
177
|
+
} | {
|
|
178
|
+
id: string;
|
|
179
|
+
type: 'update-velocity-limit';
|
|
180
|
+
value: Num;
|
|
181
|
+
} | {
|
|
182
|
+
id: string;
|
|
183
|
+
type: 'update-gravity';
|
|
184
|
+
value: Vec3;
|
|
185
|
+
} | {
|
|
186
|
+
id: string;
|
|
187
|
+
type: 'update-turbulence';
|
|
188
|
+
strength: Num;
|
|
189
|
+
frequency?: Num;
|
|
190
|
+
} | {
|
|
191
|
+
id: string;
|
|
192
|
+
type: 'update-ground';
|
|
193
|
+
height: Num;
|
|
194
|
+
bounce?: Num;
|
|
195
|
+
} | {
|
|
196
|
+
id: string;
|
|
197
|
+
type: 'update-forces';
|
|
198
|
+
graph: readonly VFXNode[];
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* `system`: one particle system. `shape` names a shape block, `create` and `update` list creation and update blocks.
|
|
202
|
+
* `duration` is the seconds it runs after it starts (0..1,000,000, default 0: until the set is stopped); the `end`
|
|
203
|
+
* triggers fire when it passes.
|
|
204
|
+
*/
|
|
205
|
+
export interface SystemBlock {
|
|
206
|
+
id: string;
|
|
207
|
+
type: 'system';
|
|
208
|
+
name?: string;
|
|
209
|
+
duration?: number;
|
|
210
|
+
shape?: string;
|
|
211
|
+
create?: readonly string[];
|
|
212
|
+
update?: readonly string[];
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* `trigger`: starts or chains another system.
|
|
216
|
+
* - `start`: `target` starts `delay` seconds (0..1,000,000, default 0) after `source` starts.
|
|
217
|
+
* - `end`: `target` starts `delay` seconds after `source`'s duration ends.
|
|
218
|
+
* - `birth`, `death`, `collision`: `target`'s creation blocks become `source`'s sub-emitter (the `events` option):
|
|
219
|
+
* `count` children (1..64, default 8) are born on the GPU where a particle is born, dies or hits the ground faster
|
|
220
|
+
* than `speed` (default 0.5), with `inherit` of its velocity (-10..10, default 0). A source has at most one.
|
|
221
|
+
* The target is then a child, not a system of its own: it takes its lifetime, size, velocity, spread and color
|
|
222
|
+
* from its creation blocks, and may have no shape, update blocks or triggers.
|
|
223
|
+
*/
|
|
224
|
+
export interface TriggerBlock {
|
|
225
|
+
id: string;
|
|
226
|
+
type: 'trigger';
|
|
227
|
+
on: 'start' | 'end' | 'birth' | 'death' | 'collision';
|
|
228
|
+
source: string;
|
|
229
|
+
target: string;
|
|
230
|
+
delay?: number;
|
|
231
|
+
count?: number;
|
|
232
|
+
inherit?: number;
|
|
233
|
+
speed?: number;
|
|
234
|
+
}
|
|
235
|
+
/** Every block a graph holds. */
|
|
236
|
+
export type NodeSystemBlock = NodeSystemValueBlock | ShapeBlock | NodeSystemCreateBlock | NodeSystemUpdateBlock | SystemBlock | TriggerBlock;
|
|
237
|
+
/** A whole particle system as data. Wires are block ids. */
|
|
238
|
+
export interface NodeSystemGraph {
|
|
239
|
+
format: 'margo-vfx-node-system';
|
|
240
|
+
version: typeof NODE_SYSTEM_VERSION;
|
|
241
|
+
blocks: readonly NodeSystemBlock[];
|
|
242
|
+
}
|
|
243
|
+
/** A system after compilation: ready to hand to `new VFX(options)` when it starts. */
|
|
244
|
+
export interface NodeSystemEntry {
|
|
245
|
+
id: string;
|
|
246
|
+
name: string;
|
|
247
|
+
/** Seconds it runs; 0 runs until stopped. */
|
|
248
|
+
duration: number;
|
|
249
|
+
/** The effect options, validated by `VFX`. */
|
|
250
|
+
options: VFXOptions;
|
|
251
|
+
/** Systems that start when this one starts or ends. */
|
|
252
|
+
triggers: readonly {
|
|
253
|
+
on: 'start' | 'end';
|
|
254
|
+
target: string;
|
|
255
|
+
delay: number;
|
|
256
|
+
}[];
|
|
257
|
+
}
|
|
258
|
+
/** A compiled graph. Systems that no trigger starts are `roots`; sub-emitter children are folded into their parent's options. */
|
|
259
|
+
export interface NodeSystemPlan {
|
|
260
|
+
systems: ReadonlyMap<string, NodeSystemEntry>;
|
|
261
|
+
roots: readonly string[];
|
|
262
|
+
}
|
|
263
|
+
/** Settings for compiling a graph. */
|
|
264
|
+
export interface NodeSystemCompileOptions {
|
|
265
|
+
/** Unsigned 32-bit seed mixed into every `random` block. Default 0. */
|
|
266
|
+
seed?: number;
|
|
267
|
+
}
|
|
268
|
+
/** Compile a graph into one validated option set per system. Throws {@link VFXNodeSystemError}. */
|
|
269
|
+
export declare function compileNodeSystem(graph: NodeSystemGraph, options?: NodeSystemCompileOptions): NodeSystemPlan;
|
|
270
|
+
/** Save a graph as stable JSON. The graph is compiled first, so a document that saves also loads. */
|
|
271
|
+
export declare function serializeNodeSystem(graph: NodeSystemGraph, options?: NodeSystemCompileOptions): string;
|
|
272
|
+
/** Read a saved graph. It does not compile: pass it to {@link compileNodeSystem} or {@link createNodeSystem}. */
|
|
273
|
+
export declare function parseNodeSystem(text: string, limits?: {
|
|
274
|
+
maxChars?: number;
|
|
275
|
+
}): NodeSystemGraph;
|
|
276
|
+
/** What `create` returns for a started system: any object; its `stop` or `dispose` is called when the system ends or the set is disposed. */
|
|
277
|
+
export interface NodeSystemHandle {
|
|
278
|
+
/** Called at the end of a system's `duration`; default: `dispose`. */
|
|
279
|
+
stop?(): void;
|
|
280
|
+
dispose?(): void;
|
|
281
|
+
}
|
|
282
|
+
/** Settings for a running set. */
|
|
283
|
+
export interface NodeSystemOptions extends NodeSystemCompileOptions {
|
|
284
|
+
/** Makes the effect for a system when it starts, for example `(options) => engine.use(new VFX(options))`. */
|
|
285
|
+
create(options: VFXOptions, id: string): NodeSystemHandle | void;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Runs a compiled graph on a clock: roots start at `start()`, and `start`/`end` triggers start their targets after
|
|
289
|
+
* their delays as `update(dt)` advances time. Nothing runs by itself; the caller owns the clock and the handles.
|
|
290
|
+
*/
|
|
291
|
+
export declare class NodeParticleSystem implements Disposable {
|
|
292
|
+
#private;
|
|
293
|
+
readonly plan: NodeSystemPlan;
|
|
294
|
+
constructor(plan: NodeSystemPlan, options: Pick<NodeSystemOptions, 'create'>);
|
|
295
|
+
/** Seconds since `start()`. */
|
|
296
|
+
get elapsed(): number;
|
|
297
|
+
/** Ids of the systems started and not yet ended. */
|
|
298
|
+
get running(): readonly string[];
|
|
299
|
+
/** Ids of every system that has started since `start()`, in start order. */
|
|
300
|
+
get started(): readonly string[];
|
|
301
|
+
/** Restart the clock at 0 and start the roots; ends whatever was running. */
|
|
302
|
+
start(): this;
|
|
303
|
+
/** Advance by `dt` seconds (finite, 0..3600), starting and ending systems whose time has come, in time order. */
|
|
304
|
+
update(dt: number): this;
|
|
305
|
+
/** Dispose every effect still running. Safe to call twice. */
|
|
306
|
+
dispose(): void;
|
|
307
|
+
[Symbol.dispose](): void;
|
|
308
|
+
}
|
|
309
|
+
/** Compile (or parse, then compile) a graph into a set that is ready to `start()`. The caller disposes it. */
|
|
310
|
+
export declare function createNodeSystem(source: string | NodeSystemGraph, options: NodeSystemOptions): NodeParticleSystem;
|
|
311
|
+
/** Fetch a saved graph and make a set from it. AbortSignal cancels the request and the build. */
|
|
312
|
+
export declare function loadNodeSystem(url: string | URL, options: NodeSystemOptions & {
|
|
313
|
+
signal?: AbortSignal;
|
|
314
|
+
}): Promise<NodeParticleSystem>;
|
|
315
|
+
export {};
|