@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1642 -3
- package/dist/base-C9upJytz.js +658 -0
- package/dist/base.d.ts +767 -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 +229 -0
- package/dist/clouds.js +566 -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 +111 -0
- package/dist/debris.js +258 -0
- package/dist/depth-collide.d.ts +7 -0
- package/dist/depth-collide.js +96 -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 +539 -0
- package/dist/fire-front.js +840 -0
- package/dist/fire.d.ts +119 -0
- package/dist/fire.js +173 -0
- package/dist/flame-ramp-VoBKpYnM.js +75 -0
- package/dist/flame-ramp.d.ts +61 -0
- package/dist/flame.d.ts +260 -0
- package/dist/flame.js +605 -0
- package/dist/flipbook.d.ts +7 -0
- package/dist/flipbook.js +23 -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 +696 -0
- package/dist/gunfire.js +1204 -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 +237 -0
- package/dist/lit.d.ts +11 -0
- package/dist/lit.js +105 -0
- package/dist/mesh.d.ts +14 -0
- package/dist/mesh.js +225 -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 +93 -0
- package/dist/require-webgpu--tUXPsmA.js +7 -0
- package/dist/ribbons.d.ts +20 -0
- package/dist/ribbons.js +151 -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 +44 -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 +215 -0
- package/dist/smoke.js +280 -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 +192 -6
package/dist/debris.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { t as e } from "./hash-Btsp6fGK.js";
|
|
2
|
+
//#region ../shared/film-time.js
|
|
3
|
+
function t(e, t, n) {
|
|
4
|
+
return (typeof e != "number" || !Number.isFinite(e) || e < 0) && t(`${n} must be 0 or more.`), e;
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/debris.ts
|
|
8
|
+
var n = 1 / 60, r = 120, i = (e) => {
|
|
9
|
+
throw RangeError(`Debris option out of range; ${e}`);
|
|
10
|
+
}, a = (e) => typeof e == "number" && Number.isFinite(e), o = (t) => e(t) / 4294967296, s = {
|
|
11
|
+
radius: [.001, 1e5],
|
|
12
|
+
count: [1, 2e4],
|
|
13
|
+
start: [0, 1e9],
|
|
14
|
+
spread: [0, 30],
|
|
15
|
+
speed: [1, 1e3],
|
|
16
|
+
maxSpeed: [1, 2e3],
|
|
17
|
+
mu: [.2, .7],
|
|
18
|
+
angle: [10, 90],
|
|
19
|
+
angleSpread: [0, 40],
|
|
20
|
+
density: [100, 5e3],
|
|
21
|
+
drag: [0, 3],
|
|
22
|
+
airDensity: [0, 2],
|
|
23
|
+
tumble: [0, 100],
|
|
24
|
+
puffLife: [0, 60],
|
|
25
|
+
puffScale: [0, 1]
|
|
26
|
+
}, c = class {
|
|
27
|
+
constructor(e = {}) {
|
|
28
|
+
if (this.film = 0, this.puff = null, this.puffTarget = null, typeof e != "object" || !e) throw TypeError("Debris options must be an object; pass {} or leave it out.");
|
|
29
|
+
let t = e;
|
|
30
|
+
for (let [e, [n, r]] of Object.entries(s)) t[e] !== void 0 && (!a(t[e]) || t[e] < n || t[e] > r) && i(`${e} must be a number from ${n} to ${r}.`);
|
|
31
|
+
for (let e of ["color", "puffColor"]) if (t[e] !== void 0) {
|
|
32
|
+
let n = t[e];
|
|
33
|
+
(!Array.isArray(n) || n.length !== 3 || !n.every((e) => a(e) && e >= 0 && e <= 1)) && i(`${e} must be three linear RGB numbers from 0 to 1.`);
|
|
34
|
+
}
|
|
35
|
+
if (t.origin !== void 0 && (!Array.isArray(t.origin) || t.origin.length !== 3 || !t.origin.every(a))) throw TypeError("Debris origin must be three numbers; pass [x, y, z].");
|
|
36
|
+
if (t.size !== void 0 && (!Array.isArray(t.size) || t.size.length !== 2 || !t.size.every((e) => a(e) && e > 0) || t.size[0] > t.size[1]) && i("size must be [smallest, largest], each above 0."), t.count !== void 0 && !Number.isInteger(t.count) && i("count must be a whole number."), t.seed !== void 0 && !Number.isInteger(t.seed) && i("seed must be an integer."), t.ground !== void 0 && typeof t.ground != "function") throw TypeError("Debris ground must be a function; pass (x, z) => height.");
|
|
37
|
+
let c = e.speed ?? 40, l = e.maxSpeed ?? Math.min(4 * c, 2e3);
|
|
38
|
+
l < c && i("maxSpeed must be at least speed."), this.options = e;
|
|
39
|
+
let [u, d, f] = e.origin ?? [
|
|
40
|
+
0,
|
|
41
|
+
0,
|
|
42
|
+
0
|
|
43
|
+
], p = e.radius ?? 30, m = e.count ?? 300, h = e.seed ?? 1, g = e.mu ?? .41, _ = (e.angle ?? 50) * Math.PI / 180, v = (e.angleSpread ?? 12) * Math.PI / 180, [y, b] = e.size ?? [.2, 1.2], x = e.density ?? 1800, S = e.drag ?? 1, C = e.airDensity ?? 1.2, w = e.ground ?? (() => d), T = e.start ?? 0, E = e.spread ?? 1.5, D = e.tumble ?? 6, O = (e, t) => o((Math.imul(h, 2654435761) ^ Math.imul(e + 1, 2246822507) ^ Math.imul(t + 1, 3266489909)) >>> 0);
|
|
44
|
+
this.paths = [];
|
|
45
|
+
for (let e = 0; e < m; e++) {
|
|
46
|
+
let t = p * Math.sqrt(Math.max(O(e, 0), 1e-6)), i = O(e, 1) * 2 * Math.PI, a = Math.min(c * (t / p) ** (-1 / g), l), o = Math.min(Math.max(_ + v * (O(e, 2) + O(e, 3) - 1), .05), Math.PI / 2), s = y * (b / y) ** O(e, 4), d = x * s ** 3, m = .5 * C * S * s * s / d, h = u + t * Math.cos(i), k = f + t * Math.sin(i), A = T + E * (t / p) ** 2 * (.8 + .2 * O(e, 5)), j = [
|
|
47
|
+
h,
|
|
48
|
+
w(h, k),
|
|
49
|
+
k,
|
|
50
|
+
a * Math.cos(o) * Math.cos(i),
|
|
51
|
+
a * Math.sin(o),
|
|
52
|
+
a * Math.cos(o) * Math.sin(i)
|
|
53
|
+
], M = [...j], N = (e) => {
|
|
54
|
+
let t = Math.hypot(e[3], e[4], e[5]);
|
|
55
|
+
return [
|
|
56
|
+
e[3],
|
|
57
|
+
e[4],
|
|
58
|
+
e[5],
|
|
59
|
+
-m * t * e[3],
|
|
60
|
+
-9.80665 - m * t * e[4],
|
|
61
|
+
-m * t * e[5]
|
|
62
|
+
];
|
|
63
|
+
}, P = Infinity, F = 0;
|
|
64
|
+
for (let e = 1; e * n <= r; e++) {
|
|
65
|
+
let t = N(j), r = N(j.map((e, r) => e + .5 * n * t[r])), i = N(j.map((e, t) => e + .5 * n * r[t])), a = N(j.map((e, t) => e + n * i[t])), o = j.map((e, o) => e + n / 6 * (t[o] + 2 * r[o] + 2 * i[o] + a[o])), s = o[1] - w(o[0], o[2]);
|
|
66
|
+
if (o[4] < 0 && s <= 0) {
|
|
67
|
+
let t = j[1] - w(j[0], j[2]), r = t / Math.max(t - s, 1e-12), i = j.map((e, t) => e + (o[t] - e) * r);
|
|
68
|
+
i[1] = w(i[0], i[2]), M.push(...i), P = (e - 1 + r) * n, F = .5 * d * (i[3] ** 2 + i[4] ** 2 + i[5] ** 2);
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
M.push(...o), j = o;
|
|
72
|
+
}
|
|
73
|
+
let I = O(e, 6) * 2 - 1, L = O(e, 7) * 2 - 1, R = O(e, 8) * 2 - 1, z = Math.hypot(I, L, R) || 1;
|
|
74
|
+
this.paths.push({
|
|
75
|
+
launch: A,
|
|
76
|
+
size: s,
|
|
77
|
+
mass: d,
|
|
78
|
+
shade: .7 + .6 * O(e, 9),
|
|
79
|
+
axis: [
|
|
80
|
+
I / z,
|
|
81
|
+
L / z,
|
|
82
|
+
R / z
|
|
83
|
+
],
|
|
84
|
+
spin: D * (.3 + .7 * O(e, 10)),
|
|
85
|
+
rows: Float64Array.from(M),
|
|
86
|
+
flight: P,
|
|
87
|
+
impact: F
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
get time() {
|
|
92
|
+
return this.film;
|
|
93
|
+
}
|
|
94
|
+
seek(e) {
|
|
95
|
+
this.film = t(e, i, "seek time");
|
|
96
|
+
}
|
|
97
|
+
update(e) {
|
|
98
|
+
this.film += t(e, i, "update delta");
|
|
99
|
+
}
|
|
100
|
+
at(e, t) {
|
|
101
|
+
let r = t - e.launch, i = e.rows.length / 6 - 1;
|
|
102
|
+
if (r < 0) return {
|
|
103
|
+
pos: [
|
|
104
|
+
e.rows[0],
|
|
105
|
+
e.rows[1],
|
|
106
|
+
e.rows[2]
|
|
107
|
+
],
|
|
108
|
+
vel: [
|
|
109
|
+
0,
|
|
110
|
+
0,
|
|
111
|
+
0
|
|
112
|
+
],
|
|
113
|
+
state: "waiting",
|
|
114
|
+
age: r
|
|
115
|
+
};
|
|
116
|
+
if (r >= e.flight) {
|
|
117
|
+
let t = i * 6;
|
|
118
|
+
return {
|
|
119
|
+
pos: [
|
|
120
|
+
e.rows[t],
|
|
121
|
+
e.rows[t + 1],
|
|
122
|
+
e.rows[t + 2]
|
|
123
|
+
],
|
|
124
|
+
vel: [
|
|
125
|
+
0,
|
|
126
|
+
0,
|
|
127
|
+
0
|
|
128
|
+
],
|
|
129
|
+
state: "landed",
|
|
130
|
+
age: r
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
let a = r / n, o = Math.min(Math.floor(a), i - 1), s = Math.min((o + 1) * n, e.flight) - o * n, c = (r - o * n) / s, l = o * 6, u = (o + 1) * 6, d = 2 * c ** 3 - 3 * c ** 2 + 1, f = c ** 3 - 2 * c ** 2 + c, p = -2 * c ** 3 + 3 * c ** 2, m = c ** 3 - c ** 2;
|
|
134
|
+
return {
|
|
135
|
+
pos: [
|
|
136
|
+
0,
|
|
137
|
+
1,
|
|
138
|
+
2
|
|
139
|
+
].map((t) => d * e.rows[l + t] + f * s * e.rows[l + 3 + t] + p * e.rows[u + t] + m * s * e.rows[u + 3 + t]),
|
|
140
|
+
vel: [
|
|
141
|
+
3,
|
|
142
|
+
4,
|
|
143
|
+
5
|
|
144
|
+
].map((t) => e.rows[l + t] * (1 - c) + e.rows[u + t] * c),
|
|
145
|
+
state: "flying",
|
|
146
|
+
age: r
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
pieces(e = this.film) {
|
|
150
|
+
return this.paths.map((t) => {
|
|
151
|
+
let n = this.at(t, e);
|
|
152
|
+
return {
|
|
153
|
+
position: n.pos,
|
|
154
|
+
velocity: n.vel,
|
|
155
|
+
size: t.size,
|
|
156
|
+
state: n.state,
|
|
157
|
+
launch: t.launch,
|
|
158
|
+
landing: t.launch + t.flight
|
|
159
|
+
};
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
draw(e) {
|
|
163
|
+
let t = this.options.color ?? [
|
|
164
|
+
.22,
|
|
165
|
+
.16,
|
|
166
|
+
.1
|
|
167
|
+
], n = /* @__PURE__ */ new Float32Array(16), r = /* @__PURE__ */ new Float32Array(3), i = 0;
|
|
168
|
+
for (let a of this.paths) {
|
|
169
|
+
if (i >= e.capacity) break;
|
|
170
|
+
let o = this.at(a, this.film);
|
|
171
|
+
if (o.state === "waiting") continue;
|
|
172
|
+
let s = a.spin * Math.min(Math.max(o.age, 0), a.flight), [c, l, u] = a.axis, d = Math.cos(s), f = Math.sin(s), p = 1 - d, m = a.size;
|
|
173
|
+
n.set([
|
|
174
|
+
(p * c * c + d) * m,
|
|
175
|
+
(p * c * l + f * u) * m,
|
|
176
|
+
(p * c * u - f * l) * m,
|
|
177
|
+
0,
|
|
178
|
+
(p * c * l - f * u) * m,
|
|
179
|
+
(p * l * l + d) * m,
|
|
180
|
+
(p * l * u + f * c) * m,
|
|
181
|
+
0,
|
|
182
|
+
(p * c * u + f * l) * m,
|
|
183
|
+
(p * l * u - f * c) * m,
|
|
184
|
+
(p * u * u + d) * m,
|
|
185
|
+
0,
|
|
186
|
+
o.pos[0],
|
|
187
|
+
o.pos[1] + (o.state === "landed" ? .25 * m : 0),
|
|
188
|
+
o.pos[2],
|
|
189
|
+
1
|
|
190
|
+
]), e.setMatrixAt(i, n), e.setColorAt && (r[0] = Math.min(1, t[0] * a.shade), r[1] = Math.min(1, t[1] * a.shade), r[2] = Math.min(1, t[2] * a.shade), e.setColorAt(i, r)), i++;
|
|
191
|
+
}
|
|
192
|
+
return e.count = i, i;
|
|
193
|
+
}
|
|
194
|
+
puffs(e = this.film) {
|
|
195
|
+
let t = this.options.puffLife ?? 6, n = this.options.puffScale ?? .04, r = [];
|
|
196
|
+
if (t === 0 || n === 0) return r;
|
|
197
|
+
for (let i of this.paths) {
|
|
198
|
+
let a = e - i.launch - i.flight;
|
|
199
|
+
if (!(a >= 0 && a < t)) continue;
|
|
200
|
+
let o = n * Math.cbrt(i.impact) * Math.cbrt(Math.max(a / t, .001)) + i.size, s = (i.rows.length / 6 - 1) * 6;
|
|
201
|
+
r.push({
|
|
202
|
+
centre: [
|
|
203
|
+
i.rows[s],
|
|
204
|
+
i.rows[s + 1] + .3 * o,
|
|
205
|
+
i.rows[s + 2]
|
|
206
|
+
],
|
|
207
|
+
size: o,
|
|
208
|
+
opacity: (1 - a / t) ** 2
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return r;
|
|
212
|
+
}
|
|
213
|
+
drawPuffs(e) {
|
|
214
|
+
this.puffTarget && this.puffTarget !== e && this.clearPuffs(), this.puffTarget = e;
|
|
215
|
+
let t = this.puffs();
|
|
216
|
+
if (!t.length) {
|
|
217
|
+
this.puff &&= (e.remove(this.puff), null);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
let n = Infinity, r = Infinity, i = Infinity, a = -Infinity, o = -Infinity, s = -Infinity, c = 0, l = 0;
|
|
221
|
+
for (let e of t) n = Math.min(n, e.centre[0]), a = Math.max(a, e.centre[0]), r = Math.min(r, e.centre[1]), o = Math.max(o, e.centre[1]), i = Math.min(i, e.centre[2]), s = Math.max(s, e.centre[2]), c = Math.max(c, e.size), l += e.opacity;
|
|
222
|
+
let u = .5 * (n + a), d = .5 * (r + o), f = .5 * (i + s), p = [
|
|
223
|
+
Math.max(a - n, .001),
|
|
224
|
+
Math.max(o - r, .001),
|
|
225
|
+
Math.max(s - i, .001)
|
|
226
|
+
], m = {
|
|
227
|
+
seed: this.options.seed ?? 1,
|
|
228
|
+
segments: t.length,
|
|
229
|
+
bounds: p,
|
|
230
|
+
volume: c,
|
|
231
|
+
color: [...this.options.puffColor ?? [
|
|
232
|
+
.36,
|
|
233
|
+
.3,
|
|
234
|
+
.23
|
|
235
|
+
]],
|
|
236
|
+
opacity: Math.min(1, l / t.length),
|
|
237
|
+
distribute: (e) => {
|
|
238
|
+
let n = t[e];
|
|
239
|
+
return {
|
|
240
|
+
point: [
|
|
241
|
+
(n.centre[0] - u) * 2 / p[0],
|
|
242
|
+
(n.centre[1] - d) * 2 / p[1],
|
|
243
|
+
(n.centre[2] - f) * 2 / p[2]
|
|
244
|
+
],
|
|
245
|
+
volume: n.size / c * (.4 + .6 * n.opacity)
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
this.puff ? Object.assign(this.puff.options, m) : this.puff = e.add(m);
|
|
250
|
+
let h = this.puff.matrix;
|
|
251
|
+
h.fill(0), h[0] = h[5] = h[10] = h[15] = 1, h[12] = u, h[13] = d, h[14] = f;
|
|
252
|
+
}
|
|
253
|
+
clearPuffs() {
|
|
254
|
+
this.puff && this.puffTarget?.remove(this.puff), this.puff = null;
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
//#endregion
|
|
258
|
+
export { c as Debris };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { VFXDepthCollide, VFXExtension } from './base.js';
|
|
2
|
+
/**
|
|
3
|
+
* The `depthCollide` feature: particles bounce off whatever was drawn last frame, using the scene depth from the
|
|
4
|
+
* `depth` option (as `softness` does). Pass it as `{depthCollide}` to `VFX` from `@margoengine/vfx/base`; the root
|
|
5
|
+
* `VFX` has it. `thickness` is read every frame, `bounce` is the effect's own.
|
|
6
|
+
*/
|
|
7
|
+
export declare const depthCollide: VFXExtension<VFXDepthCollide>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { n as e } from "./view-rect-DkeiiUtw.js";
|
|
2
|
+
import { invertMatrix as t } from "@margoengine/math/mat4";
|
|
3
|
+
//#region src/depth-collide.ts
|
|
4
|
+
var n = 176, r = (e) => `
|
|
5
|
+
struct Collide {last:mat4x4f,inverse:mat4x4f,eye:vec4f,screen:vec4f,shape:vec4f}
|
|
6
|
+
@group(0) @binding(0) var<uniform> collide:Collide;
|
|
7
|
+
@group(0) @binding(1) var<storage,read_write> particles:array<vec4f>;
|
|
8
|
+
@group(0) @binding(2) var<storage,read> alive:array<u32>;
|
|
9
|
+
@group(0) @binding(3) var<storage,read> visible:array<u32,5>;
|
|
10
|
+
@group(0) @binding(4) var sceneDepth:texture_depth${e > 1 ? "_multisampled" : ""}_2d;
|
|
11
|
+
|
|
12
|
+
fn surfaceAt(t:vec2i)->vec4f {
|
|
13
|
+
let at=clamp(t,vec2i(0),vec2i(textureDimensions(sceneDepth))-1);var d=textureLoad(sceneDepth,at,0);
|
|
14
|
+
${e > 1 ? `for(var s=1;s<${e};s++){d=max(d,textureLoad(sceneDepth,at,s));}` : ""}
|
|
15
|
+
let pixel=vec2f(at)+0.5;let ndc=vec2f((pixel.x-collide.screen.z)/collide.screen.x*2-1,1-(pixel.y-collide.screen.w)/collide.screen.y*2);
|
|
16
|
+
let w=collide.inverse*vec4f(ndc,d,1);return vec4f(w.xyz/w.w,d);
|
|
17
|
+
}
|
|
18
|
+
@compute @workgroup_size(256) fn collideDepth(@builtin(global_invocation_id) id:vec3u) {
|
|
19
|
+
if(id.x>=visible[1]){return;}
|
|
20
|
+
let slot=alive[id.x];let a=particles[slot*2];let b=particles[slot*2+1];
|
|
21
|
+
if(a.w<=0.0){return;}
|
|
22
|
+
let clip=collide.last*vec4f(a.xyz,1);if(clip.w<=0.0){return;}
|
|
23
|
+
let ndc=clip.xy/clip.w;if(any(abs(ndc)>vec2f(1))){return;}
|
|
24
|
+
let t=vec2i(floor(vec2f(ndc.x*0.5+0.5,0.5-ndc.y*0.5)*collide.screen.xy+collide.screen.zw));
|
|
25
|
+
let s=surfaceAt(t);if(s.w<=0.0){return;}
|
|
26
|
+
let radius=b.w*0.5;let gap=dot(a.xyz-s.xyz,normalize(a.xyz-collide.eye.xyz));
|
|
27
|
+
if(gap<=-radius||gap>=collide.shape.x){return;}
|
|
28
|
+
let right=surfaceAt(t+vec2i(1,0));let left=surfaceAt(t-vec2i(1,0));let down=surfaceAt(t+vec2i(0,1));let up=surfaceAt(t-vec2i(0,1));
|
|
29
|
+
let dx=select(s.xyz-left.xyz,right.xyz-s.xyz,abs(right.w-s.w)<abs(left.w-s.w));
|
|
30
|
+
let dy=select(s.xyz-up.xyz,down.xyz-s.xyz,abs(down.w-s.w)<abs(up.w-s.w));
|
|
31
|
+
var n=cross(dx,dy);let size=length(n);if(size<=0.0){return;}n/=size;
|
|
32
|
+
if(dot(n,collide.eye.xyz-s.xyz)<0.0){n=-n;}
|
|
33
|
+
var v=b.xyz;let into=dot(v,n);if(into<0.0){v-=(1.0+collide.shape.y)*into*n;}
|
|
34
|
+
particles[slot*2]=vec4f(a.xyz+n*max(0.0,radius-dot(a.xyz-s.xyz,n)),a.w);particles[slot*2+1]=vec4f(v,b.w);
|
|
35
|
+
}
|
|
36
|
+
`, i = {
|
|
37
|
+
validate(e, t) {
|
|
38
|
+
if (!e || typeof e != "object") throw TypeError("VFX depthCollide must be an object; pass {} or {thickness: 0.5}");
|
|
39
|
+
let n = e.thickness ?? .5;
|
|
40
|
+
if (typeof n != "number" || !Number.isFinite(n) || n <= 0 || n > 1e6) throw RangeError("VFX depthCollide thickness must be a world distance above 0; pass e.g. 0.5");
|
|
41
|
+
if (typeof t.depth != "function") throw TypeError("VFX depthCollide needs scene depth: add DepthTexture from @margoengine/post/depth and pass depth: () => depth.texture");
|
|
42
|
+
},
|
|
43
|
+
create(i, a, o) {
|
|
44
|
+
let { device: s, options: c } = i, l = s.createBuffer({
|
|
45
|
+
size: n,
|
|
46
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
47
|
+
});
|
|
48
|
+
o.push(l);
|
|
49
|
+
let u = new Float32Array(n / 4), d = u.subarray(0, 16), f = u.subarray(16, 32), p = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new WeakMap(), h = Math.ceil(i.capacity / 256), g = !1;
|
|
50
|
+
return {
|
|
51
|
+
write(n) {
|
|
52
|
+
let o = c.depthCollide, _ = c.depth?.();
|
|
53
|
+
if (o && o !== a) throw TypeError("VFX depthCollide is fixed at attach; change its thickness in place or attach a new VFX");
|
|
54
|
+
if (o && g && _) {
|
|
55
|
+
let e = _.sampleCount;
|
|
56
|
+
if (!_.format.startsWith("depth") || _.dimension !== "2d" || !(_.usage & 4)) throw RangeError("VFX depthCollide needs a sampleable depth texture; pass the texture of DepthTexture from @margoengine/post/depth");
|
|
57
|
+
u[40] = o.thickness ?? .5, u[41] = c.bounce ?? .3, n.device.queue.writeBuffer(l, 0, u);
|
|
58
|
+
let t = p.get(e);
|
|
59
|
+
t || (t = s.createComputePipeline({
|
|
60
|
+
layout: "auto",
|
|
61
|
+
compute: {
|
|
62
|
+
module: s.createShaderModule({ code: r(e) }),
|
|
63
|
+
entryPoint: "collideDepth"
|
|
64
|
+
}
|
|
65
|
+
}), p.set(e, t));
|
|
66
|
+
let a = m.get(_);
|
|
67
|
+
a || m.set(_, a = s.createBindGroup({
|
|
68
|
+
layout: t.getBindGroupLayout(0),
|
|
69
|
+
entries: [...[
|
|
70
|
+
l,
|
|
71
|
+
i.particles,
|
|
72
|
+
i.alive,
|
|
73
|
+
i.visible
|
|
74
|
+
].map((e, t) => ({
|
|
75
|
+
binding: t,
|
|
76
|
+
resource: { buffer: e }
|
|
77
|
+
})), {
|
|
78
|
+
binding: 4,
|
|
79
|
+
resource: _.createView({ aspect: "depth-only" })
|
|
80
|
+
}]
|
|
81
|
+
}));
|
|
82
|
+
let d = n.encoder.beginComputePass();
|
|
83
|
+
d.setPipeline(t), d.setBindGroup(0, a), d.dispatchWorkgroups(h), d.end();
|
|
84
|
+
}
|
|
85
|
+
g = t(n.camera.matrix, f) !== null, d.set(n.camera.matrix);
|
|
86
|
+
let v = n.camera.position, y = e(n);
|
|
87
|
+
u.set(v, 32), u[36] = y?.[2] ?? n.width, u[37] = y?.[3] ?? n.height, u[38] = y?.[0] ?? 0, u[39] = y?.[1] ?? 0;
|
|
88
|
+
},
|
|
89
|
+
reset() {
|
|
90
|
+
g = !1;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
//#endregion
|
|
96
|
+
export { i as depthCollide };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { VFXFeatures } from './base.js';
|
|
2
|
+
/** Most triangles of a mesh emitter (each takes 48 bytes on the GPU). */
|
|
3
|
+
export declare const MAX_EMITTER_TRIANGLES = 262144;
|
|
4
|
+
/**
|
|
5
|
+
* The `emitter` feature: where births start and which way `speed` pushes them. `sphere` (ball or shell), `cone` and
|
|
6
|
+
* `mesh` (the surface of a triangle mesh, area-weighted, pushed along each face's normal). Pass it as `{emitter}` to
|
|
7
|
+
* `VFX` from `@margoengine/vfx/base`; the root `VFX` has it. The shape and a mesh's triangles are read at attach; sizes,
|
|
8
|
+
* angle, axis and speed can change every frame.
|
|
9
|
+
*/
|
|
10
|
+
export declare const emitter: NonNullable<VFXFeatures['emitter']>;
|
package/dist/emitter.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/emitter.ts
|
|
3
|
+
var t = 262144, n = "\nstruct Emitter {a:vec4f,b:vec4f,count:vec4u}\n@group(0) @binding(7) var<uniform> em:Emitter;\n", r = "@group(0) @binding(8) var<storage,read> tris:array<vec4f>;\n", i = "let origin=f.emitterTime.xyz+(vec3f(random(seed+5u),random(seed+6u),random(seed+7u))*2-1)*f.emissionExtent.xyz;", a = "f.velocitySpread.xyz+(vec3f(random(seed+1u),random(seed+2u),random(seed+3u))*2-1)*f.velocitySpread.w", o = {
|
|
4
|
+
sphere: "\n let su=random(seed+8u);let sv=random(seed+9u);let sw=random(seed+10u);\n let sz=1.0-2.0*sv;let sr=sqrt(max(0.0,1.0-sz*sz));let sa=6.2831853*su;\n shapeDir=vec3f(sr*cos(sa),sz,sr*sin(sa));\n origin+=shapeDir*em.a.x*select(pow(sw,0.3333333),1.0,em.a.y>0.5);",
|
|
5
|
+
cone: "\n let axis=normalize(em.b.xyz);let helper=select(vec3f(0,1,0),vec3f(1,0,0),abs(axis.y)>0.99);\n let tx=normalize(cross(helper,axis));let ty=cross(axis,tx);\n let ca=random(seed+8u)*6.2831853;let cv=random(seed+9u);let cd=random(seed+10u)*6.2831853;let cw=random(seed+11u);\n let cosT=mix(1.0,em.a.w,cv);let sinT=sqrt(max(0.0,1.0-cosT*cosT));\n shapeDir=axis*cosT+(tx*cos(ca)+ty*sin(ca))*sinT;\n origin+=(tx*cos(cd)+ty*sin(cd))*sqrt(cw)*em.a.x;",
|
|
6
|
+
mesh: "\n let share=random(seed+8u);var lo=0u;var hi=em.count.x-1u;\n loop{if(lo>=hi){break;}let mid=(lo+hi)/2u;if(tris[mid*3u].w<share){lo=mid+1u;}else{hi=mid;}}\n let ta=tris[lo*3u].xyz;let tb=tris[lo*3u+1u].xyz;let tc=tris[lo*3u+2u].xyz;\n let r1=sqrt(random(seed+9u));let r2=random(seed+10u);\n origin+=ta*(1.0-r1)+tb*(r1*(1.0-r2))+tc*(r1*r2);\n shapeDir=normalize(cross(tb-ta,tc-ta));"
|
|
7
|
+
};
|
|
8
|
+
function s(t, n, r) {
|
|
9
|
+
if (t.split(n).length !== 2) throw new e("unsupported", "VFX emitter needs the simulation shader it was written for; update the emitter edits");
|
|
10
|
+
return t.replace(n, r);
|
|
11
|
+
}
|
|
12
|
+
var c = (e, t, n, r) => {
|
|
13
|
+
if (typeof e != "number" || !Number.isFinite(e) || e < n || e > r) throw RangeError(`VFX emitter ${t} must be a number from ${n} to ${r}; pass a finite value in range`);
|
|
14
|
+
};
|
|
15
|
+
function l(e) {
|
|
16
|
+
let t = e.scale ?? 1, n = e.positions, r = e.indices ?? Array.from({ length: n.length / 3 }, (e, t) => t), i = [], a = [], o = (e) => [
|
|
17
|
+
n[e * 3] * t,
|
|
18
|
+
n[e * 3 + 1] * t,
|
|
19
|
+
n[e * 3 + 2] * t
|
|
20
|
+
];
|
|
21
|
+
for (let e = 0; e < r.length; e += 3) {
|
|
22
|
+
let t = o(r[e]), n = o(r[e + 1]), s = o(r[e + 2]), c = [
|
|
23
|
+
n[0] - t[0],
|
|
24
|
+
n[1] - t[1],
|
|
25
|
+
n[2] - t[2]
|
|
26
|
+
], l = [
|
|
27
|
+
s[0] - t[0],
|
|
28
|
+
s[1] - t[1],
|
|
29
|
+
s[2] - t[2]
|
|
30
|
+
], u = .5 * Math.hypot(c[1] * l[2] - c[2] * l[1], c[2] * l[0] - c[0] * l[2], c[0] * l[1] - c[1] * l[0]);
|
|
31
|
+
u > 0 && (i.push([
|
|
32
|
+
...t,
|
|
33
|
+
...n,
|
|
34
|
+
...s
|
|
35
|
+
]), a.push(u));
|
|
36
|
+
}
|
|
37
|
+
if (!i.length) throw RangeError("VFX mesh emitter has no triangle with an area; pass positions and indices of a real surface");
|
|
38
|
+
let s = a.reduce((e, t) => e + t, 0), c = new Float32Array(i.length * 12), l = 0;
|
|
39
|
+
return i.forEach((e, t) => {
|
|
40
|
+
l += a[t], c.set([
|
|
41
|
+
e[0],
|
|
42
|
+
e[1],
|
|
43
|
+
e[2],
|
|
44
|
+
t === i.length - 1 ? 1 : l / s
|
|
45
|
+
], t * 12), c.set([
|
|
46
|
+
e[3],
|
|
47
|
+
e[4],
|
|
48
|
+
e[5],
|
|
49
|
+
0
|
|
50
|
+
], t * 12 + 4), c.set([
|
|
51
|
+
e[6],
|
|
52
|
+
e[7],
|
|
53
|
+
e[8],
|
|
54
|
+
0
|
|
55
|
+
], t * 12 + 8);
|
|
56
|
+
}), c;
|
|
57
|
+
}
|
|
58
|
+
var u = {
|
|
59
|
+
validate(e, n = !0) {
|
|
60
|
+
if (!e || typeof e != "object" || e.shape !== "sphere" && e.shape !== "cone" && e.shape !== "mesh") throw TypeError("VFX emitter needs a shape of \"sphere\", \"cone\" or \"mesh\"; pass e.g. {shape: \"sphere\", radius: 1}");
|
|
61
|
+
if (e.speed !== void 0 && c(e.speed, "speed", -1e3, 1e3), e.shape === "sphere") {
|
|
62
|
+
if (e.radius !== void 0 && c(e.radius, "radius", 0, 1e6), e.shell !== void 0 && typeof e.shell != "boolean") throw TypeError("VFX emitter shell must be true or false; pass a boolean");
|
|
63
|
+
} else if (e.shape === "cone") {
|
|
64
|
+
if (e.radius !== void 0 && c(e.radius, "radius", 0, 1e6), e.angle !== void 0 && c(e.angle, "angle", 0, Math.PI / 2), e.axis !== void 0) {
|
|
65
|
+
if (!Array.isArray(e.axis) || e.axis.length !== 3) throw TypeError("VFX emitter axis needs [x, y, z]; pass three numbers");
|
|
66
|
+
if (e.axis.forEach((e, t) => c(e, `axis[${t}]`, -1e6, 1e6)), !Math.hypot(...e.axis)) throw RangeError("VFX emitter axis must not be zero; pass a direction");
|
|
67
|
+
}
|
|
68
|
+
} else if (n) {
|
|
69
|
+
let { positions: n, indices: r } = e;
|
|
70
|
+
if (!n || typeof n.length != "number" || n.length < 9 || n.length % 3) throw TypeError("VFX mesh emitter needs positions as xyz triples, at least one triangle; pass a Float32Array");
|
|
71
|
+
for (let e = 0; e < n.length; e++) if (!Number.isFinite(n[e])) throw RangeError("VFX mesh emitter positions must be finite; remove NaN and Infinity");
|
|
72
|
+
let i = r ? r.length : n.length / 3;
|
|
73
|
+
if (i % 3 || i < 3) throw TypeError("VFX mesh emitter indices must be triangle triples; pass a multiple of 3 indices");
|
|
74
|
+
if (i / 3 > 262144) throw RangeError(`VFX mesh emitter has more than ${t} triangles; pass a simpler mesh`);
|
|
75
|
+
if (r) {
|
|
76
|
+
for (let e = 0; e < r.length; e++) if (!Number.isInteger(r[e]) || r[e] < 0 || r[e] >= n.length / 3) throw RangeError("VFX mesh emitter index points past the positions; check the index buffer");
|
|
77
|
+
}
|
|
78
|
+
e.scale !== void 0 && c(e.scale, "scale", 1e-6, 1e6), l(e);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
patch(e, t) {
|
|
82
|
+
if (t.shape !== "sphere" && t.shape !== "cone" && t.shape !== "mesh") throw TypeError("VFX emitter needs the shapes entry for this shape; pass {emitter: shapes}");
|
|
83
|
+
let c = s(e, "fn hash(v:u32)", n + (t.shape === "mesh" ? r : "") + "fn hash(v:u32)");
|
|
84
|
+
return c = s(c, i, "var origin=" + i.slice(11) + "\n var shapeDir=vec3f(0);" + o[t.shape]), s(c, a, "f.velocitySpread.xyz+shapeDir*em.a.z+(" + a.slice(22));
|
|
85
|
+
},
|
|
86
|
+
create(e, t, n) {
|
|
87
|
+
let r = e.createBuffer({
|
|
88
|
+
size: 48,
|
|
89
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
90
|
+
}), i = /* @__PURE__ */ new Float32Array(12), a = new Uint32Array(i.buffer);
|
|
91
|
+
n.push(r);
|
|
92
|
+
let o = [{
|
|
93
|
+
binding: 7,
|
|
94
|
+
resource: { buffer: r }
|
|
95
|
+
}];
|
|
96
|
+
if (t.shape === "mesh") {
|
|
97
|
+
let r = l(t), i = e.createBuffer({
|
|
98
|
+
size: r.byteLength,
|
|
99
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
100
|
+
});
|
|
101
|
+
n.push(i), e.queue.writeBuffer(i, 0, r), a[8] = r.length / 12, o.push({
|
|
102
|
+
binding: 8,
|
|
103
|
+
resource: { buffer: i }
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
entries: o,
|
|
108
|
+
write(e, t) {
|
|
109
|
+
if (i[2] = t.speed ?? 0, t.shape === "sphere") i[0] = t.radius ?? 1, i[1] = +!!t.shell;
|
|
110
|
+
else if (t.shape === "cone") {
|
|
111
|
+
let e = t.axis ?? [
|
|
112
|
+
0,
|
|
113
|
+
1,
|
|
114
|
+
0
|
|
115
|
+
], n = Math.hypot(...e);
|
|
116
|
+
i[0] = t.radius ?? 0, i[3] = Math.cos(t.angle ?? Math.PI / 6);
|
|
117
|
+
for (let t = 0; t < 3; t++) i[4 + t] = e[t] / n;
|
|
118
|
+
}
|
|
119
|
+
e.writeBuffer(r, 0, i);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
//#endregion
|
|
125
|
+
export { t as MAX_EMITTER_TRIANGLES, u as emitter };
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { VFXEvents, VFXExtension } from './base.js';
|
|
2
|
+
/**
|
|
3
|
+
* The `events` feature (sub-emitters): a particle that is born, dies, or hits the ground faster than `speed`, raises an event on
|
|
4
|
+
* the GPU, and the next frame `count` new particles are born at it into the free slots of the same effect, with their
|
|
5
|
+
* own velocity, life, size and colour. Events and their births are listed in slot order, so the same frames give the
|
|
6
|
+
* same particles. Born particles never raise events. Pass it as `{events}` to `VFX` from `@margoengine/vfx/base`; the
|
|
7
|
+
* root `VFX` has it. `on` is read at attach; the rest every frame.
|
|
8
|
+
*/
|
|
9
|
+
export declare const events: VFXExtension<VFXEvents>;
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/events.ts
|
|
3
|
+
var t = "\n@group(0) @binding(15) var<storage,read_write> events:array<u32>;\n@group(0) @binding(16) var<storage,read_write> eventData:array<vec4f>;\nstruct Events {control:vec4u,shape:vec4f,velocity:vec4f,color:vec4f}\n@group(0) @binding(17) var<uniform> ev:Events;\nvar<private> eventHere:u32;\nvar<private> eventPosition:vec3f;\nvar<private> eventVelocity:vec3f;\nfn eventGroups()->u32 {return (f.control.x+255u)/256u;}\nfn raiseEvent(p:Particle){eventPosition=p.positionLife.xyz;eventVelocity=p.velocitySize.xyz;eventHere=1u;}\nfn storeEvents(i:u32){if(eventHere==1u){let at=(f.control.z%2u)*f.control.x*2u+i*2u;eventData[at]=vec4f(eventPosition,0);eventData[at+1u]=vec4f(eventVelocity,0);}}\n", n = "\n@compute @workgroup_size(256) fn eventScan(@builtin(local_invocation_index) l:u32){\n let n=eventGroups();let per=(n+255u)/256u;let a=min(l*per,n);let b=min(a+per,n);\n var sum=0u;for(var k=a;k<b;k++){sum+=events[4u+k];}\n var run=prefix(l,sum)-sum;for(var k=a;k<b;k++){let v=events[4u+k];events[4u+k]=run;run+=v;}\n if(l==255u){events[1]=run;}\n}\n@compute @workgroup_size(256) fn eventCompact(@builtin(workgroup_id) w:vec3u,@builtin(local_invocation_index) l:u32){\n let n=eventGroups();let start=events[4u+w.x];var end=events[1];if(w.x+1u<n){end=events[4u+w.x+1u];}\n if(l<end-start){events[4u+n*257u+start+l]=events[4u+n+w.x*256u+l];}\n}", r = {
|
|
4
|
+
generation: ["let at=i*2;let cold=f.control.x*2+at;", "let at=i*2;let cold=f.control.x*2+at;var generation=particles[cold+1].w;"],
|
|
5
|
+
ticket: ["if(ticket>=f.control.y){particles[at].w=0;return 0u;}", "var child=0u;if(ticket>=f.control.y){let e=(ticket-f.control.y)/ev.control.x;if(e>=events[1]){particles[at].w=0;return 0u;}child=events[4u+eventGroups()*257u+e]+1u;}"],
|
|
6
|
+
birth: ["particles[cold]=p.color;particles[cold+1]=p.rotation;", "if(child>0u){let source=((f.control.z+1u)%2u)*f.control.x*2u+(child-1u)*2u;let childLife=mix(ev.shape.x,ev.shape.y,random(seed));\n p.positionLife=vec4f(eventData[source].xyz,max(0.0,childLife-dt));\n p.velocitySize=vec4f(eventData[source+1u].xyz*ev.shape.w+ev.velocity.xyz+(vec3f(random(seed+1u),random(seed+2u),random(seed+3u))*2-1)*ev.velocity.w,ev.shape.z);\n p.color=vec4f(ev.color.rgb*f.emissionExtent.w,ev.color.a);p.rotation=vec4f(p.rotation.x,p.rotation.y,childLife,1);}\n particles[cold]=p.color;particles[cold+1]=p.rotation;generation=p.rotation.w;"],
|
|
7
|
+
death: [" p.positionLife.w-=dt;", " p.positionLife.w-=dt;if(p.positionLife.w<=0&&particles[at].w>0&&generation==0){raiseEvent(p);}"],
|
|
8
|
+
collision: ["if(p.velocitySize.y<0){p.velocitySize.y=-p.velocitySize.y*f.physics.z;}", "if(p.velocitySize.y<0){if(-p.velocitySize.y>bitcast<f32>(ev.control.z)&&generation==0){raiseEvent(p);}p.velocitySize.y=-p.velocitySize.y*f.physics.z;}"],
|
|
9
|
+
list: ["if(l==255u){groups[w.x]=rank;}", "if(l==255u){groups[w.x]=rank;}\n storeEvents(id.x);let er=prefix(l,eventHere);if(eventHere==1u){events[4u+eventGroups()+w.x*256u+er-1u]=id.x;}if(l==255u){events[4u+w.x]=er;}"]
|
|
10
|
+
};
|
|
11
|
+
function i(t, [n, r]) {
|
|
12
|
+
if (t.split(n).length !== 2) throw new e("unsupported", "VFX events need the simulation shader they were written for; update the event edits");
|
|
13
|
+
return t.replace(n, r);
|
|
14
|
+
}
|
|
15
|
+
var a = (e, t, n, r) => {
|
|
16
|
+
if (typeof e != "number" || !Number.isFinite(e) || e < n || e > r) throw RangeError(`VFX events ${t} must be a number from ${n} to ${r}; pass a finite value in range`);
|
|
17
|
+
}, o = {
|
|
18
|
+
validate(e) {
|
|
19
|
+
if (!e || typeof e != "object" || e.on !== "death" && e.on !== "collision" && e.on !== "birth") throw TypeError("VFX events needs on: 'birth', 'death' or 'collision'; pass e.g. {on: 'death', count: 8}");
|
|
20
|
+
let t = e.count ?? 8;
|
|
21
|
+
if (!Number.isInteger(t) || t < 1 || t > 64) throw RangeError(`VFX events count is ${t}; pass a whole number of births per event from 1 to 64`);
|
|
22
|
+
let n = e.lifetime ?? [.5, 1];
|
|
23
|
+
if (!Array.isArray(n) || n.length !== 2) throw TypeError("VFX events lifetime needs [min, max] seconds");
|
|
24
|
+
a(n[0], "lifetime[0]", 1e-6, 1e6), a(n[1], "lifetime[1]", n[0], 1e6), a(e.size ?? .05, "size", 1e-6, 1e6), a(e.spread ?? 1, "spread", 0, 1e4), a(e.inherit ?? 0, "inherit", -10, 10), a(e.speed ?? .5, "speed", 0, 1e4);
|
|
25
|
+
let r = e.velocity ?? [
|
|
26
|
+
0,
|
|
27
|
+
0,
|
|
28
|
+
0
|
|
29
|
+
];
|
|
30
|
+
if (!Array.isArray(r) || r.length !== 3) throw TypeError("VFX events velocity needs [x, y, z]");
|
|
31
|
+
r.forEach((e, t) => a(e, `velocity[${t}]`, -1e4, 1e4));
|
|
32
|
+
let i = e.color ?? [
|
|
33
|
+
1,
|
|
34
|
+
1,
|
|
35
|
+
1,
|
|
36
|
+
1
|
|
37
|
+
];
|
|
38
|
+
if (!Array.isArray(i) || i.length !== 4) throw TypeError("VFX events color needs linear [r, g, b, a]");
|
|
39
|
+
i.forEach((e, t) => a(e, `color[${t}]`, 0, 1));
|
|
40
|
+
},
|
|
41
|
+
simulation(e, a) {
|
|
42
|
+
let o = e.replace("fn update(", t + "fn update(");
|
|
43
|
+
for (let e of [
|
|
44
|
+
"generation",
|
|
45
|
+
"ticket",
|
|
46
|
+
"birth",
|
|
47
|
+
"list"
|
|
48
|
+
]) o = i(o, r[e]);
|
|
49
|
+
return o = a.on === "birth" ? i(o, ["generation=p.rotation.w;", "generation=p.rotation.w;if(child==0u){raiseEvent(p);}"]) : i(o, r[a.on]), o + n;
|
|
50
|
+
},
|
|
51
|
+
create(e, t, n) {
|
|
52
|
+
let { device: r, capacity: i, options: a } = e, o = Math.ceil(i / 256), s = r.createBuffer({
|
|
53
|
+
size: (4 + o * 257 + i) * 4,
|
|
54
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
55
|
+
});
|
|
56
|
+
n.push(s);
|
|
57
|
+
let c = r.createBuffer({
|
|
58
|
+
size: i * 64,
|
|
59
|
+
usage: GPUBufferUsage.STORAGE
|
|
60
|
+
});
|
|
61
|
+
n.push(c);
|
|
62
|
+
let l = r.createBuffer({
|
|
63
|
+
size: 64,
|
|
64
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
65
|
+
});
|
|
66
|
+
n.push(l);
|
|
67
|
+
let u = t.on, d = (t) => {
|
|
68
|
+
let n = r.createComputePipeline({
|
|
69
|
+
layout: "auto",
|
|
70
|
+
compute: {
|
|
71
|
+
module: e.module,
|
|
72
|
+
entryPoint: t
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return [n, r.createBindGroup({
|
|
76
|
+
layout: n.getBindGroupLayout(0),
|
|
77
|
+
entries: [{
|
|
78
|
+
binding: 0,
|
|
79
|
+
resource: { buffer: e.uniform }
|
|
80
|
+
}, {
|
|
81
|
+
binding: 15,
|
|
82
|
+
resource: { buffer: s }
|
|
83
|
+
}]
|
|
84
|
+
})];
|
|
85
|
+
}, f = d("eventScan"), p = d("eventCompact"), m = /* @__PURE__ */ new Float32Array(16), h = new Uint32Array(m.buffer);
|
|
86
|
+
return {
|
|
87
|
+
births: !0,
|
|
88
|
+
eventList: s,
|
|
89
|
+
compute: [
|
|
90
|
+
{
|
|
91
|
+
binding: 15,
|
|
92
|
+
resource: { buffer: s }
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
binding: 16,
|
|
96
|
+
resource: { buffer: c }
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
binding: 17,
|
|
100
|
+
resource: { buffer: l }
|
|
101
|
+
}
|
|
102
|
+
],
|
|
103
|
+
reset: () => r.queue.writeBuffer(s, 0, /* @__PURE__ */ new Uint32Array(4)),
|
|
104
|
+
write(e) {
|
|
105
|
+
let t = a.events;
|
|
106
|
+
if (!t || t.on !== u) throw TypeError("VFX events and their on are fixed at attach; attach a new VFX to change them");
|
|
107
|
+
let n = t.lifetime ?? [.5, 1];
|
|
108
|
+
h[0] = t.count ?? 8, m[2] = t.speed ?? .5, m.set([
|
|
109
|
+
n[0],
|
|
110
|
+
n[1],
|
|
111
|
+
t.size ?? .05,
|
|
112
|
+
t.inherit ?? 0
|
|
113
|
+
], 4), m.set(t.velocity ?? [
|
|
114
|
+
0,
|
|
115
|
+
0,
|
|
116
|
+
0
|
|
117
|
+
], 8), m[11] = t.spread ?? 1, m.set(t.color ?? [
|
|
118
|
+
1,
|
|
119
|
+
1,
|
|
120
|
+
1,
|
|
121
|
+
1
|
|
122
|
+
], 12), e.device.queue.writeBuffer(l, 0, m);
|
|
123
|
+
},
|
|
124
|
+
run(e) {
|
|
125
|
+
for (let [[t, n], r] of [[f, 1], [p, o]]) e.setPipeline(t), e.setBindGroup(0, n), e.dispatchWorkgroups(r);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
131
|
+
export { o as events };
|
package/dist/extra.d.ts
ADDED
package/dist/fields.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { VFXExtension, VFXFields } from './base.js';
|
|
2
|
+
/** Most attractors of an effect. */
|
|
3
|
+
export declare const MAX_ATTRACTORS = 16;
|
|
4
|
+
/** Most cells of a vector field (128^3), 16 bytes each on the GPU. */
|
|
5
|
+
export declare const MAX_FIELD_CELLS = 2097152;
|
|
6
|
+
/**
|
|
7
|
+
* The `fields` feature: up to 16 point attractors (or repellers) and one 3D vector field over a box, added to each
|
|
8
|
+
* particle's acceleration. Pass it as `{fields}` to `VFX` from `@margoengine/vfx/base`; the root `VFX` has it. The
|
|
9
|
+
* field's cells are read at attach (and its presence is fixed); attractors, the box and the strengths every frame.
|
|
10
|
+
*/
|
|
11
|
+
export declare const fields: VFXExtension<VFXFields>;
|