@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,1214 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const t=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { r as e } from "./flame-ramp-VoBKpYnM.js";
5
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
6
+ import { finiteVector as n } from "@margoengine/math/validate";
7
+ import { VFX as r } from "@margoengine/vfx/base";
8
+ import { curves as i } from "@margoengine/vfx/curves";
9
+ import { stretch as a } from "@margoengine/vfx/stretch";
10
+ import { overLife as o } from "@margoengine/vfx/over-life";
11
+ import { Flame as s } from "@margoengine/vfx/flame";
12
+ import { Smoke as c } from "@margoengine/vfx/smoke";
13
+ import { Clouds as l } from "@margoengine/vfx/clouds";
14
+ //#region src/gunfire.ts
15
+ var u = 9.81, d = 1.225, f = 1005, p = 288.15, m = 340.3, h = Object.freeze({
16
+ saltpetre: .75,
17
+ charcoal: .15,
18
+ sulphur: .1,
19
+ gasFraction: .4298,
20
+ solidFraction: .5591,
21
+ waterFraction: .0111,
22
+ gasVolume: .2713,
23
+ heat: 3004530.4,
24
+ flameTemperature: 2473
25
+ }), g = (e, t, n, r, i = !1) => {
26
+ if (!(typeof e == "number" && Number.isFinite(e) && (i ? e > n : e >= n) && e <= r)) throw RangeError(`Gunfire ${t} is ${e}; pass a finite number ${i ? "above" : "from"} ${n} ${i ? "and at most" : "to"} ${r}`);
27
+ return e;
28
+ };
29
+ function _(e) {
30
+ g(e, "charge", 0, 1e4);
31
+ let t = h;
32
+ return {
33
+ gas: e * t.gasFraction,
34
+ smoke: e * t.solidFraction,
35
+ water: e * t.waterFraction,
36
+ gasVolume: e * t.gasVolume,
37
+ heat: e * t.heat
38
+ };
39
+ }
40
+ var v = .04, y = .05;
41
+ function b(t, n = v) {
42
+ let r = _(t), i = (r.gasVolume + r.water * 22.414 / 18.015) * h.flameTemperature / 273.15;
43
+ return g(n, "flash duration", 0, 10, !0), {
44
+ radius: Math.cbrt(3 * i / (4 * Math.PI)),
45
+ duration: n,
46
+ color: e(h.flameTemperature)
47
+ };
48
+ }
49
+ function x(e, t, n = v) {
50
+ if (!(Number.isFinite(e) && Number.isFinite(t) && t >= 0 && n > 0)) throw RangeError("Gunfire flashExposure needs finite numbers, shutter 0 or more, duration above 0");
51
+ if (e < 0) return 0;
52
+ if (t < 1e-9) return Math.exp(-e / n);
53
+ let r = Math.max(0, e - t);
54
+ return n / t * (Math.exp(-r / n) - Math.exp(-e / n));
55
+ }
56
+ var S = 160, C = class {
57
+ constructor(e) {
58
+ if (this.times = new Float64Array(S), this.table = /* @__PURE__ */ new Float64Array(640), !e || typeof e != "object") throw TypeError("PowderPuff needs {charge, muzzleVelocity}");
59
+ let t = g(e.charge, "charge", 0, 1e4, !0), n = g(e.muzzleVelocity, "muzzleVelocity", 0, 3e3, !0), r = g(e.elevation ?? 0, "elevation", -Math.PI / 2, Math.PI / 2), i = g(e.momentum ?? 1.5, "momentum", 0, 10), a = g(e.heatFraction ?? .4, "heatFraction", 0, 1), o = g(e.entrainment ?? .25, "entrainment", .05, 1), s = g(e.stability ?? 0, "stability", 0, 1), c = g(e.airTemperature ?? p, "airTemperature", 150, 350), l = g(e.duration ?? 600, "duration", 1, 7200);
60
+ this.duration = l;
61
+ let m = b(t).radius, h = _(t);
62
+ this.startRadius = m, this.momentum = i * t * n / d, this.buoyancy = u * a * h.heat / (f * d * c);
63
+ let v = 4 / 3 * Math.PI * o ** 3, y = (e, t) => {
64
+ let n = v * e[0] ** 3, r = e[1] / n, i = e[2] / n;
65
+ t[0] = Math.hypot(r, i), t[1] = 0, t[2] = e[3], t[3] = -s * n * i, t[4] = r, t[5] = i;
66
+ }, x = new Float64Array([
67
+ m / o,
68
+ this.momentum * Math.cos(r),
69
+ this.momentum * Math.sin(r),
70
+ this.buoyancy,
71
+ 0,
72
+ 0
73
+ ]), C = /* @__PURE__ */ new Float64Array(6), w = /* @__PURE__ */ new Float64Array(6), T = /* @__PURE__ */ new Float64Array(6), E = /* @__PURE__ */ new Float64Array(6), D = /* @__PURE__ */ new Float64Array(6), O = (e) => {
74
+ y(x, C);
75
+ for (let t = 0; t < 6; t++) D[t] = x[t] + .5 * e * C[t];
76
+ y(D, w);
77
+ for (let t = 0; t < 6; t++) D[t] = x[t] + .5 * e * w[t];
78
+ y(D, T);
79
+ for (let t = 0; t < 6; t++) D[t] = x[t] + e * T[t];
80
+ y(D, E);
81
+ for (let t = 0; t < 6; t++) x[t] = x[t] + e / 6 * (C[t] + 2 * w[t] + 2 * T[t] + E[t]);
82
+ }, k = .001, A = (l / k) ** (1 / 158), j = 0, M = (e) => {
83
+ let t = v * x[0] ** 3;
84
+ this.times[e] = j, this.table[e * 4] = x[4], this.table[e * 4 + 1] = x[5], this.table[e * 4 + 2] = o * x[0], this.table[e * 4 + 3] = Math.hypot(x[1], x[2]) / t;
85
+ };
86
+ M(0);
87
+ for (let e = 1; e < S; e++) {
88
+ let t = k * A ** (e - 1), n = Math.max(1, Math.ceil(50 * Math.min(1, (t - j) / Math.max(t, 1e-9)) * 4)), r = (t - j) / n;
89
+ for (let e = 0; e < n; e++) O(r);
90
+ j = t, M(e);
91
+ }
92
+ }
93
+ at(e, t) {
94
+ let n = t ?? {
95
+ along: 0,
96
+ rise: 0,
97
+ radius: 0,
98
+ speed: 0
99
+ }, r = this.times, i = Math.max(0, Math.min(this.duration, e)), a = 0, o = 159;
100
+ for (; o - a > 1;) {
101
+ let e = a + o >> 1;
102
+ r[e] <= i ? a = e : o = e;
103
+ }
104
+ let s = r[o] > r[a] ? (i - r[a]) / (r[o] - r[a]) : 0, c = a * 4, l = o * 4, u = this.table;
105
+ return n.along = u[c] + (u[l] - u[c]) * s, n.rise = u[c + 1] + (u[l + 1] - u[c + 1]) * s, n.radius = u[c + 2] + (u[l + 2] - u[c + 2]) * s, n.speed = u[c + 3] + (u[l + 3] - u[c + 3]) * s, n;
106
+ }
107
+ };
108
+ function w(e, t, n = 10, r = .5) {
109
+ if (!(e >= 0 && t >= 0 && n > 0 && r >= 0)) throw RangeError("Gunfire smokeDispersion needs seconds, wind and constant 0 or more and a height above 0");
110
+ let i = (Math.sqrt(.0012) * t) ** 3 / (.4 * n);
111
+ return Math.sqrt(r * i * e ** 3);
112
+ }
113
+ function T(e) {
114
+ let t = Math.max(0, e), n = [
115
+ [0, .47],
116
+ [.6, .5],
117
+ [.8, .6],
118
+ [1, .82],
119
+ [1.2, .95],
120
+ [2, .92],
121
+ [5, .92]
122
+ ];
123
+ for (let e = 1; e < n.length; e++) {
124
+ let [r, i] = n[e];
125
+ if (t <= r) {
126
+ let [a, o] = n[e - 1];
127
+ return o + (i - o) * (t - a) / (r - a);
128
+ }
129
+ }
130
+ return .92;
131
+ }
132
+ var E = (e, t) => Math.cbrt(3 * e / (4 * Math.PI * t)), D = Object.freeze({
133
+ density: 680,
134
+ strength: 20451765,
135
+ inertia: 409.0353
136
+ }), O = (e = {}) => ({
137
+ name: "timber",
138
+ strength: g(e.strength ?? D.strength, "wood strength", 1e5, 1e9),
139
+ inertia: g(e.inertia ?? (e.density === void 0 ? D.inertia : .5 * g(e.density, "wood density", 100, 2e3)), "wood inertia", 50, 1e3)
140
+ });
141
+ function k(e, t, n, r) {
142
+ let i = O(r), a = g(e, "strike speed", 0, 3e3), o = Math.PI * g(t, "shot radius", 1e-4, 10) ** 2, s = g(n, "shot mass", 0, 2e3, !0), c = i.inertia;
143
+ return s / (2 * c * o) * Math.log(1 + c * a * a / i.strength);
144
+ }
145
+ function A(e, t, n, r, i) {
146
+ let a = O(i), o = g(e, "strike speed", 0, 3e3), s = Math.PI * g(t, "shot radius", 1e-4, 10) ** 2, c = g(n, "shot mass", 0, 2e3, !0), l = g(r, "thickness", 0, 100), u = a.strength / a.inertia, d = (u + o * o) * Math.exp(-2 * a.inertia * s * l / c) - u;
147
+ return d > 0 ? Math.sqrt(d) : 0;
148
+ }
149
+ var j = .8, M = .3, N = 6e-5, P = (e) => typeof e.matrix == "function" ? e.matrix() : e, F = (e, t) => {
150
+ let n = [
151
+ t[0] - e[12],
152
+ t[1] - e[13],
153
+ t[2] - e[14]
154
+ ], r = [
155
+ 0,
156
+ 0,
157
+ 0
158
+ ];
159
+ for (let t = 0; t < 3; t++) {
160
+ let i = e[t * 4], a = e[t * 4 + 1], o = e[t * 4 + 2];
161
+ r[t] = (n[0] * i + n[1] * a + n[2] * o) / (i * i + a * a + o * o || 1);
162
+ }
163
+ return r;
164
+ }, I = (e) => {
165
+ if (!e || typeof e != "object" || !e.ship) throw TypeError("Gunfire target needs {ship}: a Floater or a world matrix");
166
+ let t = P(e.ship);
167
+ if (!t || t.length < 16) throw TypeError("Gunfire target ship must have matrix() or be 16 numbers");
168
+ return {
169
+ m: t,
170
+ a: g(e.beam ?? 13, "target beam", .5, 100) / 2,
171
+ b: g(e.length ?? 50, "target length", 1, 500) / 2,
172
+ top: g(e.height ?? 6, "target height", 0, 50),
173
+ keel: -g(e.draft ?? 5, "target draft", 0, 50),
174
+ thickness: g(e.thickness ?? .5, "target thickness", .01, 10),
175
+ wood: O(e.wood)
176
+ };
177
+ }, L = (e, t) => t[1] >= e.keel && t[1] <= e.top && (t[0] / e.a) ** 2 + (t[2] / e.b) ** 2 <= 1, ee = (e, t) => {
178
+ let n = (t[0] / e.a) ** 2 + (t[2] / e.b) ** 2 < .98 && t[1] > e.top - .05 ? [
179
+ 0,
180
+ 1,
181
+ 0
182
+ ] : [
183
+ t[0] / (e.a * e.a),
184
+ 0,
185
+ t[2] / (e.b * e.b)
186
+ ], r = q(e.m, n), i = Math.hypot(r[0], r[1], r[2]) || 1;
187
+ return [
188
+ r[0] / i,
189
+ r[1] / i,
190
+ r[2] / i
191
+ ];
192
+ };
193
+ function R(e, t, r, i = {}, a = {}) {
194
+ n(e, 3, "Gunfire shot position"), n(t, 3, "Gunfire shot direction"), g(r, "shot speed", 0, 3e3);
195
+ let o = g(i.mass ?? 14.5, "shot mass", 0, 2e3, !0), s = g(i.density ?? 7200, "shot density", 500, 25e3), c = E(o, s), l = Math.PI * c * c, u = Math.hypot(t[0], t[1], t[2]);
196
+ if (!(u > 0)) throw RangeError("Gunfire shot direction must not be zero");
197
+ let f = a.seaLevel ?? 0, p = g(a.maxTime ?? 30, "maxTime", 0, 600), h = a.drag ?? !0, _ = a.fireTime ?? 0, v = a.wind, y = (a.targets ?? []).map(I), b = y.map((t) => L(t, F(t.m, e))), x = [], S = a.ballistics, C = {
198
+ mass: o,
199
+ diameter: 2 * c
200
+ }, w = (e, t) => a.water?.sample(e, t)?.height ?? f, D = [
201
+ e[0],
202
+ e[1],
203
+ e[2],
204
+ t[0] / u * r,
205
+ t[1] / u * r,
206
+ t[2] / u * r
207
+ ], O = (e, t) => {
208
+ let [n, r] = J(v, [
209
+ e[0],
210
+ e[1],
211
+ e[2]
212
+ ], _ + t), i = e[3] - n, a = e[4], s = e[5] - r, c = Math.hypot(i, a, s), u = h && c > 0 ? .5 * d * T(c / m) * l * c / o : 0;
213
+ return [
214
+ e[3],
215
+ e[4],
216
+ e[5],
217
+ -u * i,
218
+ -9.81 - u * a,
219
+ -u * s
220
+ ];
221
+ }, P = .002, R = 0, z = (e, t, n, r) => {
222
+ let i = y[e], a = 0, s = 1;
223
+ for (let e = 0; e < 30; e++) {
224
+ let e = (a + s) / 2, o = [
225
+ t[0] + (n[0] - t[0]) * e,
226
+ t[1] + (n[1] - t[1]) * e,
227
+ t[2] + (n[2] - t[2]) * e
228
+ ];
229
+ L(i, F(i.m, o)) === r ? a = e : s = e;
230
+ }
231
+ let u = t.map((e, t) => e + (n[t] - e) * s), d = [
232
+ u[0],
233
+ u[1],
234
+ u[2]
235
+ ], f = [
236
+ u[3],
237
+ u[4],
238
+ u[5]
239
+ ], p = F(i.m, d), m = ee(i, p), h = Math.abs(f[0] * m[0] + f[1] * m[1] + f[2] * m[2]), g = Math.min(S ? 1e3 : 3e3, h), v = S ? S.penetration(i.wood, C, g).depth : k(g, c, o, i.wood), w = {
240
+ target: e,
241
+ kind: "lodged",
242
+ time: _ + R + s * P,
243
+ position: d,
244
+ local: p,
245
+ normal: m,
246
+ velocity: f,
247
+ after: [
248
+ 0,
249
+ 0,
250
+ 0
251
+ ],
252
+ depth: v,
253
+ hole: c * 1.1,
254
+ splinters: 0
255
+ };
256
+ x.push(w);
257
+ let T = [
258
+ f[0] + h * m[0],
259
+ f[1] + h * m[1],
260
+ f[2] + h * m[2]
261
+ ], E = Math.hypot(T[0], T[1], T[2]), O = Math.hypot(E, h), I = !r && v < c, z = j, B = M * h;
262
+ if (S && !r && h > 0) {
263
+ let e = S.ricochet(i.wood, C, Math.min(1e3, O), Math.asin(Math.min(1, h / O)), 0);
264
+ I = e.kind === "ricochet" && e.depth < i.thickness, I && (z = E > 0 ? e.speed * Math.cos(e.angle) / E : 0, B = e.speed * Math.sin(e.angle), w.depth = e.depth);
265
+ }
266
+ if (I) {
267
+ w.kind = "ricochet", w.hole = 0, w.after = [
268
+ z * T[0] + B * m[0],
269
+ z * T[1] + B * m[1],
270
+ z * T[2] + B * m[2]
271
+ ], w.splinters = Math.round(l * w.depth / N);
272
+ for (let e = 0; e < 3; e++) D[e] = d[e] + m[e] * c * .01, D[e + 3] = w.after[e];
273
+ return "on";
274
+ }
275
+ let V = S ? S.residualSpeed(i.wood, C, g, i.thickness) : A(g, c, o, i.thickness, i.wood);
276
+ if (w.splinters = Math.round(l * Math.min(v, i.thickness) / N), V <= 0) return "lodged";
277
+ w.kind = r ? "exit" : "through";
278
+ let H = V / h;
279
+ w.after = [
280
+ f[0] * H,
281
+ f[1] * H,
282
+ f[2] * H
283
+ ];
284
+ for (let e = 0; e < 3; e++) D[e] = d[e] + f[e] / (O || 1) * (r ? c * .01 : i.thickness + c * .01), D[e + 3] = w.after[e];
285
+ return b[e] = !r, "on";
286
+ };
287
+ for (; R < p;) {
288
+ let e = O(D, R), t = O(D.map((t, n) => t + .5 * P * e[n]), R + P / 2), n = O(D.map((e, n) => e + .5 * P * t[n]), R + P / 2), r = O(D.map((e, t) => e + P * n[t]), R + P), i = D.map((i, a) => i + P / 6 * (e[a] + 2 * t[a] + 2 * n[a] + r[a])), a = !1;
289
+ for (let e = 0; e < y.length && !a; e++) if (L(y[e], F(y[e].m, [
290
+ i[0],
291
+ i[1],
292
+ i[2]
293
+ ])) !== b[e]) {
294
+ if (z(e, D, i, b[e]) === "lodged") return { hits: x };
295
+ a = !0;
296
+ }
297
+ if (a) {
298
+ R += P;
299
+ continue;
300
+ }
301
+ let o = b.some(Boolean), l = D[1] - w(D[0], D[2]), u = i[1] - w(i[0], i[2]);
302
+ if (!o && u <= 0 && l > 0) {
303
+ let e = l / (l - u), t = D.map((t, n) => t + (i[n] - t) * e);
304
+ return {
305
+ hits: x,
306
+ impact: {
307
+ time: _ + R + e * P,
308
+ position: [
309
+ t[0],
310
+ w(t[0], t[2]),
311
+ t[2]
312
+ ],
313
+ velocity: [
314
+ t[3],
315
+ t[4],
316
+ t[5]
317
+ ],
318
+ radius: c,
319
+ density: s
320
+ }
321
+ };
322
+ }
323
+ for (let e = 0; e < 6; e++) D[e] = i[e];
324
+ R += P;
325
+ }
326
+ return { hits: x };
327
+ }
328
+ function z(e, t, n, r = {}, i = {}) {
329
+ return R(e, t, n, r, i).impact;
330
+ }
331
+ var B = .01, V = 1750, H = .02;
332
+ function U(e, t = .003, n = 490) {
333
+ g(e, "charge", 0, 1e4);
334
+ let r = g(t, "grain", 5e-4, .02), i = g(n, "grain speed", 0, 3e3), a = Math.PI / 6 * r ** 3 * V, o = r / (2 * B), s = .5 * d * Math.PI * (r / 2) ** 2 / a, c = [1], l = i, u = 0, f = 0, p = (e) => -s * T(e / m) * e * e;
335
+ for (let e = 1; e < 32; e++) {
336
+ let t = o * e / 31;
337
+ for (; f < t - 1e-12;) {
338
+ let e = Math.min(.001, t - f), n = p(l), r = p(l + .5 * e * n), i = p(l + .5 * e * r), a = p(l + e * i);
339
+ u += e / 6 * (l + 2 * (l + .5 * e * n) + 2 * (l + .5 * e * r) + (l + e * i)), l += e / 6 * (n + 2 * r + 2 * i + a), f += e;
340
+ }
341
+ c.push(i > 0 ? Math.max(0, l / i) : 0);
342
+ }
343
+ return {
344
+ count: e * H / a,
345
+ burnTime: o,
346
+ stopping: 4 / 3 * r * V / (d * .47),
347
+ reach: u,
348
+ speeds: c
349
+ };
350
+ }
351
+ function W(e) {
352
+ g(e, "soot", 0, 1);
353
+ let t = 8.7 * e, n = 3 * (1 - e), r = (.25 * t + .97 * n) / Math.max(1e-9, t + n);
354
+ return [
355
+ r,
356
+ r * .985,
357
+ r * .96
358
+ ];
359
+ }
360
+ var G = 0, K = (e, t) => [
361
+ e[0] * t[0] + e[4] * t[1] + e[8] * t[2] + e[12],
362
+ e[1] * t[0] + e[5] * t[1] + e[9] * t[2] + e[13],
363
+ e[2] * t[0] + e[6] * t[1] + e[10] * t[2] + e[14]
364
+ ], q = (e, t) => [
365
+ e[0] * t[0] + e[4] * t[1] + e[8] * t[2],
366
+ e[1] * t[0] + e[5] * t[1] + e[9] * t[2],
367
+ e[2] * t[0] + e[6] * t[1] + e[10] * t[2]
368
+ ], J = (e, t, n) => {
369
+ if (!e) return [0, 0];
370
+ if ("sample" in e) {
371
+ let r = e.sample(t[0], t[1], t[2], n);
372
+ return [r[0], r[2]];
373
+ }
374
+ return [e[0], e[1]];
375
+ }, te = [
376
+ 1,
377
+ 0,
378
+ 0,
379
+ 0,
380
+ 0,
381
+ 1,
382
+ 0,
383
+ 0,
384
+ 0,
385
+ 0,
386
+ 1,
387
+ 0,
388
+ 0,
389
+ 0,
390
+ 0,
391
+ 1
392
+ ], ne = class e {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
393
+ constructor(e, n) {
394
+ if (this.name = "vfx-gunfire:" + G++, this.flashes = [], this.lights = [], this.fired = [], this.sparks = [], this.pendingHits = [], this.hitCursor = 0, this.disposed = !1, this.clock = 0, this.cursor = 0, this.banks = [], this.drift = [0, 0], this.puffs = /* @__PURE__ */ new Map(), this.point = {
395
+ along: 0,
396
+ rise: 0,
397
+ radius: 0,
398
+ speed: 0
399
+ }, this.lastDelta = 1 / 30, this.options = n, !Array.isArray(n.guns) || n.guns.length < 1 || n.guns.length > 128) throw TypeError("Battery needs 1 to 128 guns; pass {guns: [{position, direction}]}");
400
+ for (let e of n.guns) this.checkGun(e);
401
+ this.queue = [...n.shots ?? []];
402
+ for (let e of this.queue) this.checkShot(e);
403
+ if (this.queue.sort((e, t) => e.time - t.time || e.gun - t.gun), n.ship && typeof n.ship.matrix != "function") throw TypeError("Battery ship needs matrix(), such as a Floater");
404
+ this.checkLive();
405
+ let s = g(n.banks ?? 48, "banks", 1, 512), c = g(n.puffs ?? 24, "puffs", 4, 128);
406
+ if (!Number.isInteger(s) || !Number.isInteger(c)) throw RangeError("Battery banks and puffs must be whole numbers");
407
+ let u = n.lights, d = u && "names" in u && "clustered" in u ? u : void 0, f = d ? d.clustered : u && "maxLights" in u ? u : void 0;
408
+ if (u && !f) throw TypeError("Invalid Battery lights; pass a Clustered from @margoengine/clustered or its LightGroups");
409
+ if (d) {
410
+ let e = "gunfire";
411
+ for (let t = 2; d.names.includes(e); t++) e = `gunfire ${t}`;
412
+ this.target = this.group = d.group(e);
413
+ } else f && (this.target = f);
414
+ t(e).use(this), this.clouds = l.attach(e, {
415
+ ...n.clouds ?? {},
416
+ limit: s * c
417
+ });
418
+ let p = (e, t) => {
419
+ let n = t(), r = t(), i = Math.cbrt(t()), a = 2 * Math.PI * n, o = Math.acos(2 * r - 1);
420
+ return {
421
+ point: [
422
+ i * Math.sin(o) * Math.cos(a),
423
+ i * Math.sin(o) * Math.sin(a) * .8,
424
+ i * Math.cos(o)
425
+ ],
426
+ volume: .75 + .25 * (1 - i) + 0 * e
427
+ };
428
+ };
429
+ for (let e = 0; e < s; e++) this.banks.push({
430
+ cloud: this.clouds.add({
431
+ seed: 1 + e * 7,
432
+ segments: c,
433
+ bounds: [
434
+ 1.6,
435
+ 1.6,
436
+ 1.6
437
+ ],
438
+ volume: 1.2,
439
+ distribute: p,
440
+ opacity: 0,
441
+ speed: .15
442
+ }),
443
+ shot: null,
444
+ puff: null
445
+ });
446
+ for (let [t, s] of n.guns.entries()) {
447
+ let t = b(s.charge ?? 4.5), c = n.flashDuration ?? .04, l = {
448
+ capacity: 64,
449
+ rate: 0,
450
+ position: [
451
+ 0,
452
+ -1e6,
453
+ 0
454
+ ],
455
+ velocity: [
456
+ 0,
457
+ 0,
458
+ 0
459
+ ],
460
+ spread: 0,
461
+ gravity: [
462
+ 0,
463
+ 0,
464
+ 0
465
+ ],
466
+ size: t.radius,
467
+ lifetime: [Math.max(.05, 3 * c), Math.max(.08, 4 * c)],
468
+ ground: -1e9,
469
+ turbulence: 0,
470
+ intensity: 0,
471
+ color: [
472
+ t.color[0],
473
+ t.color[1],
474
+ t.color[2],
475
+ y
476
+ ],
477
+ curves: {
478
+ size: [
479
+ .5,
480
+ 1,
481
+ 1,
482
+ .8
483
+ ],
484
+ alpha: [
485
+ 1,
486
+ 1,
487
+ .6,
488
+ 0
489
+ ]
490
+ }
491
+ };
492
+ this.flashes.push(r.attach(e, l, { curves: i })), this.sparks.push(r.attach(e, {
493
+ capacity: 2048,
494
+ rate: 0,
495
+ position: [
496
+ 0,
497
+ -1e6,
498
+ 0
499
+ ],
500
+ velocity: [
501
+ 0,
502
+ 0,
503
+ 0
504
+ ],
505
+ spread: 0,
506
+ gravity: [
507
+ 0,
508
+ -9.81,
509
+ 0
510
+ ],
511
+ size: n.sparkSize ?? .3,
512
+ lifetime: [.1, .2],
513
+ ground: -1e9,
514
+ turbulence: 0,
515
+ intensity: 0,
516
+ color: [
517
+ t.color[0],
518
+ t.color[1],
519
+ t.color[2],
520
+ 1
521
+ ],
522
+ curves: { alpha: [
523
+ 1,
524
+ 1,
525
+ .8,
526
+ 0
527
+ ] },
528
+ overLife: { speed: [1, 1] },
529
+ stretch: { factor: 1 / 60 }
530
+ }, {
531
+ curves: i,
532
+ overLife: o,
533
+ stretch: a
534
+ })), this.target && this.lights.push({
535
+ position: [
536
+ 0,
537
+ -1e6,
538
+ 0
539
+ ],
540
+ range: 1,
541
+ intensity: 0,
542
+ color: t.color,
543
+ radius: t.radius
544
+ });
545
+ }
546
+ this.splinters = r.attach(e, {
547
+ capacity: 2048,
548
+ rate: 0,
549
+ position: [
550
+ 0,
551
+ -1e6,
552
+ 0
553
+ ],
554
+ velocity: [
555
+ 0,
556
+ 0,
557
+ 0
558
+ ],
559
+ spread: 0,
560
+ gravity: [
561
+ 0,
562
+ -9.81,
563
+ 0
564
+ ],
565
+ size: .12,
566
+ lifetime: [1.5, 3],
567
+ ground: n.seaLevel ?? 0,
568
+ turbulence: .2,
569
+ angularVelocity: 8,
570
+ intensity: 1,
571
+ color: [
572
+ .42,
573
+ .3,
574
+ .18,
575
+ 1
576
+ ]
577
+ }), this.target?.setLights(this.lights);
578
+ }
579
+ checkGun(e) {
580
+ if (!e || typeof e != "object") throw TypeError("Battery gun needs {position, direction}");
581
+ if (n(e.position, 3, "Battery gun position"), n(e.direction, 3, "Battery gun direction"), !(Math.hypot(e.direction[0], e.direction[1], e.direction[2]) > 0)) throw RangeError("Battery gun direction must not be zero");
582
+ g(e.charge ?? 4.5, "charge", 0, 1e4, !0), g(e.muzzleVelocity ?? 490, "muzzleVelocity", 0, 3e3, !0);
583
+ }
584
+ checkShot(e) {
585
+ if (!e || !Number.isInteger(e.gun) || e.gun < 0 || e.gun >= this.options.guns.length) throw RangeError("Battery shot gun must be a gun index");
586
+ g(e.time, "shot time", 0, 1e7);
587
+ }
588
+ checkLive() {
589
+ let e = this.options;
590
+ if (g(e.flashDuration ?? .04, "flashDuration", .001, 1), g(e.flashSize ?? 1, "flashSize", 0, 10), g(e.flashRadiance ?? 40, "flashRadiance", 0, 3e3), g(e.flashLight ?? 1, "flashLight", 0, 100), g(e.shutter ?? .5, "shutter", 0, 1), g(e.extinction ?? 3, "extinction", 0, 100), g(e.dispersion ?? .5, "dispersion", 0, 5), g(e.smokeLife ?? 600, "smokeLife", 1, 7200), g(e.stability ?? 1e-4, "stability", 0, 1), g(e.heatFraction ?? .4, "heatFraction", 0, 1), g(e.entrainment ?? .25, "entrainment", .05, 1), g(e.sparks ?? 1, "sparks", 0, 10), g(e.sparkRadiance ?? 30, "sparkRadiance", 0, 3e3), g(e.sparkSize ?? .3, "sparkSize", .01, 2), g(e.grain ?? .003, "grain", 5e-4, .02), g(e.splinters ?? 1, "splinters", 0, 10), e.targets !== void 0 && !Array.isArray(e.targets)) throw TypeError("Battery targets must be a list of {ship, length, beam}");
591
+ if (e.smokeColor) {
592
+ n(e.smokeColor, 3, "Battery smokeColor");
593
+ for (let t of e.smokeColor) g(t, "smokeColor channel", 0, 1);
594
+ }
595
+ }
596
+ resetTime() {
597
+ if (this.disposed) return;
598
+ this.clock = this.cursor = this.hitCursor = 0, this.lastDelta = 1 / 30, this.drift[0] = this.drift[1] = 0, this.queue.length = 0, this.queue.push(...this.options.shots ?? []), this.queue.sort((e, t) => e.time - t.time || e.gun - t.gun), this.fired.length = 0, this.pendingHits.length = 0, this.puffs.clear(), this.point.along = this.point.rise = this.point.radius = this.point.speed = 0;
599
+ for (let e of this.banks) e.shot = e.puff = null, e.cloud.options.opacity = 0, e.cloud.matrix.set(te);
600
+ this.clouds.resetTime();
601
+ for (let e of [
602
+ ...this.flashes,
603
+ ...this.sparks,
604
+ this.splinters
605
+ ]) e.resetTime();
606
+ for (let e of this.lights) e.position.splice(0, 3, 0, -1e6, 0), e.intensity = 0;
607
+ this.target?.setLights(this.lights);
608
+ let e = /* @__PURE__ */ new Set();
609
+ for (let t of this.options.targets ?? []) {
610
+ if (!t.mask || e.has(t.mask)) continue;
611
+ e.add(t.mask);
612
+ let n = $(t.mask).data;
613
+ for (let e = 2; e < n.length; e += 4) n[e] = 0;
614
+ t.mask.update();
615
+ }
616
+ }
617
+ fire(e, t = this.clock) {
618
+ let n = {
619
+ gun: e,
620
+ time: t
621
+ };
622
+ this.checkShot(n);
623
+ let r = this.queue.length;
624
+ for (; r > this.cursor && this.queue[r - 1].time > t;) r--;
625
+ return this.queue.splice(Math.max(r, this.cursor), 0, n), this;
626
+ }
627
+ broadside(e, t = .25, n) {
628
+ return g(t, "broadside interval", 0, 60), (n ?? this.options.guns.map((e, t) => t)).forEach((n, r) => this.fire(n, e + r * t)), this;
629
+ }
630
+ puffOf(e) {
631
+ let t = this.options, n = t.guns[e], r = this.worldDirection(n), i = Math.asin(Math.max(-1, Math.min(1, r[1]))), a = [
632
+ n.charge ?? 4.5,
633
+ n.muzzleVelocity ?? 490,
634
+ i.toFixed(4),
635
+ t.stability ?? 1e-4,
636
+ t.heatFraction ?? .4,
637
+ t.entrainment ?? .25,
638
+ t.smokeLife ?? 600
639
+ ].join("|"), o = this.puffs.get(a);
640
+ return o || (o = new C({
641
+ charge: n.charge ?? 4.5,
642
+ muzzleVelocity: n.muzzleVelocity ?? 490,
643
+ elevation: i,
644
+ stability: t.stability ?? 1e-4,
645
+ heatFraction: t.heatFraction ?? .4,
646
+ entrainment: t.entrainment ?? .25,
647
+ duration: t.smokeLife ?? 600
648
+ }), this.puffs.size > 64 && this.puffs.clear(), this.puffs.set(a, o)), o;
649
+ }
650
+ worldDirection(e) {
651
+ let t = this.options.ship ? q(this.options.ship.matrix(), e.direction) : [
652
+ e.direction[0],
653
+ e.direction[1],
654
+ e.direction[2]
655
+ ], n = Math.hypot(t[0], t[1], t[2]);
656
+ return [
657
+ t[0] / n,
658
+ t[1] / n,
659
+ t[2] / n
660
+ ];
661
+ }
662
+ wind() {
663
+ let e = this.options.wind ?? this.flashes[0]?.options.wind, t = this.options.guns[0].position;
664
+ return J(e, this.options.ship ? K(this.options.ship.matrix(), t) : t, this.clock);
665
+ }
666
+ impacts() {
667
+ return this.fired.flatMap((e) => e.impact ? [e.impact] : []);
668
+ }
669
+ get time() {
670
+ return this.clock;
671
+ }
672
+ setup(e) {
673
+ this.engine = e;
674
+ }
675
+ onPreUpdate(e) {
676
+ this.advanceTime(e);
677
+ }
678
+ advanceTime(e) {
679
+ if (this.disposed) return;
680
+ let t = e > 0 ? e : 0;
681
+ t > 0 && (this.lastDelta = t);
682
+ let n = this.wind();
683
+ this.drift[0] += n[0] * t, this.drift[1] += n[1] * t, this.clock += t, this.checkLive();
684
+ let r = this.options;
685
+ for (; this.cursor < this.queue.length && this.queue[this.cursor].time <= this.clock;) {
686
+ let e = this.queue[this.cursor++], t = r.guns[e.gun], i = r.ship?.matrix(), a = i ? K(i, t.position) : [
687
+ t.position[0],
688
+ t.position[1],
689
+ t.position[2]
690
+ ], o = this.worldDirection(t), s = this.clock - e.time, c = {
691
+ gun: e.gun,
692
+ time: e.time,
693
+ muzzle: a,
694
+ direction: o,
695
+ drift: [this.drift[0] - n[0] * s, this.drift[1] - n[1] * s],
696
+ hits: []
697
+ }, l = r.wind ?? this.flashes[0]?.options.wind, u = R(a, o, t.muzzleVelocity ?? 490, t.shot ?? {}, {
698
+ fireTime: e.time,
699
+ seaLevel: r.seaLevel ?? 0,
700
+ ...r.water ? { water: r.water } : {},
701
+ ...l ? { wind: l } : {},
702
+ ...r.targets?.length ? { targets: r.targets } : {},
703
+ ...r.ballistics ? { ballistics: r.ballistics } : {}
704
+ });
705
+ u.impact && (c.impact = u.impact), c.hits = u.hits;
706
+ for (let e of u.hits) {
707
+ let t = this.pendingHits.length;
708
+ for (; t > this.hitCursor && this.pendingHits[t - 1].time > e.time;) t--;
709
+ this.pendingHits.splice(t, 0, e);
710
+ }
711
+ this.fired.push(c);
712
+ let d = this.banks.find((e) => !e.shot);
713
+ d ||= this.banks.reduce((e, t) => e.shot.time <= t.shot.time ? e : t), d.shot = c, d.puff = this.puffOf(e.gun);
714
+ let f = this.flashes[e.gun], p = f.options, m = b(t.charge ?? 4.5, r.flashDuration ?? .04).radius * (r.flashSize ?? 1);
715
+ p.position = [
716
+ a[0] + o[0] * m * .8,
717
+ a[1] + o[1] * m * .8,
718
+ a[2] + o[2] * m * .8
719
+ ], p.emissionExtent = [
720
+ m * .45,
721
+ m * .45,
722
+ m * .45
723
+ ], p.velocity = [
724
+ o[0] * m * 4,
725
+ o[1] * m * 4,
726
+ o[2] * m * 4
727
+ ], p.spread = m * 1.5, p.size = m * 1.6, f.burst(24);
728
+ let h = t.muzzleVelocity ?? 490, g = U(t.charge ?? 4.5, r.grain ?? .003, h), _ = this.sparks[e.gun], v = _.options;
729
+ v.position = [
730
+ a[0] + o[0] * m * .5,
731
+ a[1] + o[1] * m * .5,
732
+ a[2] + o[2] * m * .5
733
+ ], v.velocity = [
734
+ o[0] * h,
735
+ o[1] * h,
736
+ o[2] * h
737
+ ], v.spread = .1 * h, v.emissionExtent = [
738
+ m * .3,
739
+ m * .3,
740
+ m * .3
741
+ ], v.lifetime = [.7 * g.burnTime, 1.3 * g.burnTime], v.overLife = { speed: g.speeds };
742
+ let y = Math.round(Math.min(2048, g.count * (r.sparks ?? 1)));
743
+ y > 0 && _.burst(y);
744
+ }
745
+ for (; this.hitCursor < this.pendingHits.length && this.pendingHits[this.hitCursor].time <= this.clock;) this.strike(this.pendingHits[this.hitCursor++]);
746
+ this.place();
747
+ }
748
+ hits() {
749
+ return this.pendingHits.slice(0, this.hitCursor);
750
+ }
751
+ strike(e) {
752
+ let t = this.options, n = e.normal, r = Math.hypot(e.velocity[0], e.velocity[1], e.velocity[2]), i = {
753
+ position: [...e.position],
754
+ emissionExtent: [
755
+ e.hole + .05,
756
+ e.hole + .05,
757
+ e.hole + .05
758
+ ],
759
+ velocity: [
760
+ 0,
761
+ 0,
762
+ 0
763
+ ],
764
+ spread: 0
765
+ };
766
+ if (e.kind === "through" || e.kind === "exit") {
767
+ let t = Math.min(150, .5 * r) / (r || 1);
768
+ i.velocity = [
769
+ e.velocity[0] * t,
770
+ e.velocity[1] * t,
771
+ e.velocity[2] * t
772
+ ], i.spread = .6 * Math.min(150, .5 * r);
773
+ } else i.velocity = [
774
+ n[0] * 15,
775
+ n[1] * 15 + 3,
776
+ n[2] * 15
777
+ ], i.spread = 10;
778
+ let a = Math.round(Math.min(2048, e.splinters * (t.splinters ?? 1)));
779
+ a > 0 && this.splinters.burst(a, i);
780
+ let o = t.targets?.[e.target];
781
+ o?.mask && e.hole > 0 && ce(o.mask, e.local, e.hole, o);
782
+ }
783
+ place() {
784
+ let e = this.options, t = this.clock, n = (e.shutter ?? .5) * this.lastDelta, r = e.flashDuration ?? .04, i = e.flashRadiance ?? 40, a = /* @__PURE__ */ new Map();
785
+ for (let n = this.fired.length - 1; n >= 0 && a.size < e.guns.length; n--) {
786
+ let e = this.fired[n];
787
+ if (a.has(e.gun) || a.set(e.gun, e), t - e.time > 1) break;
788
+ }
789
+ for (let o = 0; o < e.guns.length; o++) {
790
+ let s = a.get(o), c = s ? x(t - s.time, n, r) : 0, l = e.guns[o], u = b(l.charge ?? 4.5, r).radius * (e.flashSize ?? 1);
791
+ this.flashes[o].options.intensity = Math.min(65504, i * c / y);
792
+ let d = this.sparks[o].options;
793
+ d.intensity = e.sparkRadiance ?? 30, d.size = e.sparkSize ?? .3, d.stretch = { factor: Math.min(10, n) };
794
+ let f = this.lights[o];
795
+ if (f && s) {
796
+ let t = f.position;
797
+ t[0] = s.muzzle[0] + s.direction[0] * u, t[1] = s.muzzle[1] + s.direction[1] * u, t[2] = s.muzzle[2] + s.direction[2] * u, f.radius = u, f.range = 60 * Math.max(u, .5), f.intensity = c > 1e-4 ? Math.PI * u * u * i * c * (e.flashLight ?? 1) : 0;
798
+ }
799
+ }
800
+ this.target && this.target.setLights(this.lights);
801
+ let o = (e.extinction ?? 3) * 1e3, s = e.smokeLife ?? 600, c = Math.hypot(...this.wind()), l = e.smokeColor ?? W(0), u = this.point, d = 0, f = 0;
802
+ for (let n of this.banks) {
803
+ let r = n.shot, i = n.cloud;
804
+ if (!r || !n.puff) {
805
+ i.options.opacity = 0;
806
+ continue;
807
+ }
808
+ let a = t - r.time;
809
+ if (a < 0) {
810
+ i.options.opacity = 0;
811
+ continue;
812
+ }
813
+ if (a > s) {
814
+ n.shot = null, i.options.opacity = 0;
815
+ continue;
816
+ }
817
+ n.puff.at(a, u);
818
+ let p = w(a, c, Math.max(2, r.muzzle[1] + u.rise), e.dispersion ?? .5), m = Math.sqrt(u.radius * u.radius + 2 * p * p), h = e.guns[r.gun], g = o * _(h.charge ?? 4.5).smoke / (Math.PI * m * m), v = Math.hypot(r.direction[0], r.direction[2]) || 1, y = r.direction[0] / v, b = r.direction[2] / v, x = r.muzzle[0] + y * u.along + (this.drift[0] - r.drift[0]), S = r.muzzle[1] + u.rise, C = r.muzzle[2] + b * u.along + (this.drift[1] - r.drift[1]), T = 1 + Math.min(1.5, u.along / Math.max(.001, 4 * m));
819
+ i.matrix.set([
820
+ y * m * T,
821
+ 0,
822
+ b * m * T,
823
+ 0,
824
+ 0,
825
+ m,
826
+ 0,
827
+ 0,
828
+ -b * m,
829
+ 0,
830
+ y * m,
831
+ 0,
832
+ x,
833
+ S,
834
+ C,
835
+ 1
836
+ ]);
837
+ let E = g / Math.max(1, i.options.segments * Math.PI * .36 / (1.6 * 1.6));
838
+ i.options.opacity = Math.min(1, 1 - Math.exp(-E)), i.options.color = [
839
+ l[0],
840
+ l[1],
841
+ l[2]
842
+ ], g < .01 ? (n.shot = null, i.options.opacity = 0) : (d += Math.min(8, E), f++);
843
+ }
844
+ this.clouds.options.density = f > 0 ? d / f : 1;
845
+ let p = e.clouds ?? {};
846
+ for (let e of Object.keys(p)) this.clouds.options[e] = p[e];
847
+ }
848
+ static attach(t, n) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
849
+ if (!n || typeof n != "object") throw TypeError("Invalid Battery options; pass {guns: [{position, direction}]}");
850
+ return new e(t, n);
851
+ }
852
+ dispose() {
853
+ if (!this.disposed) {
854
+ this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0;
855
+ for (let e of this.flashes) e.dispose();
856
+ for (let e of this.sparks) e.dispose();
857
+ this.splinters.dispose(), this.clouds.dispose(), this.group ? this.group.dispose() : this.target && this.target.setLights([]);
858
+ }
859
+ }
860
+ [Symbol.dispose]() {
861
+ this.dispose();
862
+ }
863
+ };
864
+ function Y(e, t) {
865
+ return g(e, "vent area", 0, 1e4), g(t, "vent height", 0, 100), .09 * e * Math.sqrt(t);
866
+ }
867
+ function X(e, t, n) {
868
+ return g(e, "burning rate", 0, 1e5), g(t, "vent width", 0, 100, !0), g(n, "vent height", 0, 100), Math.max(0, 12.8 * Math.cbrt((e / t) ** 2) - n);
869
+ }
870
+ var Z = 16e6, Q = 24, re = 1.5, ie = .5, $ = (e) => {
871
+ let t = e?.source;
872
+ if (!t || !(t.data instanceof Uint8Array || t.data instanceof Uint8ClampedArray) || !Number.isInteger(t.width) || !Number.isInteger(t.height) || t.data.length !== t.width * t.height * 4 || typeof e.update != "function") throw TypeError("Gunfire hull mask needs a Texture of raw RGBA8 data: new Texture(hullMaskImage(), {colorSpace: \"linear\"})");
873
+ return t;
874
+ };
875
+ function ae(e = 256, t = 64) {
876
+ if (!Number.isInteger(e) || !Number.isInteger(t) || e < 4 || t < 2 || e > 4096 || t > 4096 || e % 2) throw RangeError("hullMaskImage needs an even whole width 4..4096 and a whole height 2..4096");
877
+ let n = new Uint8Array(e * t * 4);
878
+ for (let e = 3; e < n.length; e += 4) n[e] = 255;
879
+ return {
880
+ data: n,
881
+ width: e,
882
+ height: t
883
+ };
884
+ }
885
+ function oe(e, t = {}) {
886
+ if (!e || e.length % 3) throw TypeError("hullMaskUVs needs x, y, z for each vertex");
887
+ let n = g(t.length ?? 50, "hull length", 1, 500), r = g(t.deck ?? 6, "hull deck", 0, 50), i = g(t.draft ?? 5, "hull draft", 0, 50), a = new Float32Array(e.length / 3 * 2);
888
+ for (let t = 0, o = 0; t < e.length; t += 3, o += 2) {
889
+ let s = e[t], c = e[t + 1], l = e[t + 2], u = Math.min(1, Math.max(0, l / n + .5));
890
+ a[o] = .5 * u * .999 + (s >= 0 ? .5 : 0), a[o + 1] = Math.min(1, Math.max(0, (c + i) / (r + i || 1)));
891
+ }
892
+ return a;
893
+ }
894
+ var se = (e, t, n, r, i) => {
895
+ let a = e.width / 2;
896
+ return [Math.min(1, Math.max(0, t[2] / n + .5)) * a + (t[0] >= 0 ? a : 0), (t[1] + i) / (r + i || 1) * e.height];
897
+ };
898
+ function ce(e, t, n, r) {
899
+ let i = $(e), a = r.length ?? 50, o = r.height ?? 6, s = r.draft ?? 5, [c, l] = se(i, t, a, o, s), u = i.width / 2 / a, d = i.height / (o + s || 1), f = t[0] >= 0 ? i.width / 2 : 0;
900
+ for (let e = Math.floor(l - n * d - 1); e <= Math.ceil(l + n * d + 1); e++) for (let t = Math.floor(c - n * u - 1); t <= Math.ceil(c + n * u + 1); t++) e < 0 || e >= i.height || t < f || t >= f + i.width / 2 || ((t + .5 - c) / u) ** 2 + ((e + .5 - l) / d) ** 2 > n * n || (i.data[(e * i.width + t) * 4 + 2] = 255);
901
+ let p = Math.min(f + i.width / 2 - 1, Math.max(f, Math.floor(c))), m = Math.min(i.height - 1, Math.max(0, Math.floor(l)));
902
+ i.data[(m * i.width + p) * 4 + 2] = 255, e.update();
903
+ }
904
+ var le = .02;
905
+ function ue(e, t, n = 6) {
906
+ let r = g(e, "mast height", 1, 120), i = g(t, "seconds", 0, 1e7), a = g(n, "deck", 0, 50), o = Math.acos(Math.max(-1, Math.min(1, -a / r))), s = 3 * u / (2 * r), c = le, l = 0, d = 0;
907
+ for (; d < i;) {
908
+ let e = Math.min(.001, i - d), t = s * Math.sin(c), n = s * Math.sin(c + .5 * e * l), r = s * Math.sin(c + .5 * e * (l + .5 * e * t)), a = s * Math.sin(c + e * (l + .5 * e * n));
909
+ if (c += e * l + e * e / 6 * (t + n + r), l += e / 6 * (t + 2 * n + 2 * r + a), d += e, c >= o) return {
910
+ angle: o,
911
+ rate: 0,
912
+ down: !0
913
+ };
914
+ }
915
+ return {
916
+ angle: c,
917
+ rate: l,
918
+ down: !1
919
+ };
920
+ }
921
+ var de = class e {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
922
+ constructor(e, n) {
923
+ this.name = "vfx-hull-fire:" + G++, this.flameVolumes = [], this.smokeColumns = [], this.disposed = !1, this.clock = 0, this.options = n, this.checkLive();
924
+ let r = g(n.flames ?? 10, "flames", 0, 64), i = g(n.columns ?? 2, "columns", 0, 16);
925
+ if (!Number.isInteger(r) || !Number.isInteger(i)) throw RangeError("HullFire flames and columns must be whole numbers");
926
+ t(e).use(this);
927
+ let a = this.toWorld([
928
+ 0,
929
+ -1e6,
930
+ 0
931
+ ]), o = n.lights, l = o && "clustered" in o ? o.clustered : o;
932
+ for (let t = 0; t < i; t++) this.smokeColumns.push(c.attach(e, {
933
+ position: a,
934
+ heat: 0,
935
+ radius: 3,
936
+ color: W(n.soot ?? .35),
937
+ opacity: n.opacity ?? .7,
938
+ rate: 18,
939
+ lifetime: 240,
940
+ stability: 1e-4,
941
+ ...n.wind ? { wind: n.wind } : {},
942
+ ...l ? { lights: l } : {}
943
+ }));
944
+ for (let t = 0; t < r; t++) this.flameVolumes.push(s.attach(e, {
945
+ position: a,
946
+ length: 0,
947
+ footprint: [1, 1],
948
+ ...n.lights ? { lights: n.lights } : {}
949
+ }));
950
+ this.place();
951
+ }
952
+ checkLive() {
953
+ let e = this.options;
954
+ if (!e || typeof e != "object" || !e.ship) throw TypeError("HullFire needs {ship}: a Floater or a world matrix");
955
+ if (typeof e.ship.matrix != "function" && e.ship.length !== 16) throw TypeError("HullFire ship must have matrix() (a Floater) or be 16 numbers");
956
+ let t = e.hull ?? {};
957
+ g(t.length ?? 50, "hull length", 1, 500), g(t.beam ?? 13, "hull beam", .5, 100), g(t.deck ?? 6, "hull deck", 0, 50), g(e.spread ?? .03, "spread", 0, 10), g(e.burnTime ?? 2400, "burnTime", 1, 1e6), g(e.deckHeat ?? 25e4, "deckHeat", 0, 5e6), g(e.soot ?? .35, "soot", 0, 1), g(e.opacity ?? .7, "opacity", 0, 1), g(e.ignition?.time ?? 0, "ignition time", 0, 1e7), g(t.draft ?? 5, "hull draft", 0, 50), g(e.charRate ?? .5, "charRate", 0, 5), g(e.mastFall ?? 600, "mastFall", 0, 1e6), e.mask && $(e.mask);
958
+ for (let t of e.masts ?? []) {
959
+ n(t.position, 3, "HullFire mast position"), g(t.height, "mast height", 1, 120), t.falls && n(t.falls, 3, "HullFire mast falls"), t.fallAt !== void 0 && g(t.fallAt, "mast fallAt", 0, 1e7);
960
+ for (let e of t.yards ?? []) g(e.at, "yard at", 0, t.height), g(e.length, "yard length", .1, 100);
961
+ }
962
+ e.ignition?.at && n(e.ignition.at, 3, "HullFire ignition");
963
+ for (let t of e.ports ?? []) if (n(t.position, 3, "HullFire port position"), t.size) for (let e of t.size) g(e, "port size", .05, 20);
964
+ }
965
+ matrix() {
966
+ let e = this.options.ship;
967
+ return typeof e.matrix == "function" ? e.matrix() : e;
968
+ }
969
+ toWorld(e) {
970
+ return K(this.matrix(), e);
971
+ }
972
+ burning(e) {
973
+ let t = this.options, n = t.ignition?.time ?? 0, r = t.ignition?.at?.[2] ?? 0, i = t.spread ?? .03, a = i > 0 ? Math.abs(e - r) / i : Math.abs(e - r) < 1e-9 ? 0 : Infinity, o = this.clock - n - a;
974
+ if (o <= 0) return 0;
975
+ let s = t.burnTime ?? 2400;
976
+ return Math.min(1, o / (.05 * s)) * (o > s ? Math.max(0, 1 - (o - s) / (.33 * s)) : 1);
977
+ }
978
+ reach(e) {
979
+ let t = this.options, n = t.ignition?.at?.[2] ?? 0, r = t.spread ?? .03;
980
+ return r > 0 ? Math.abs(e - n) / r : Math.abs(e - n) < 1e-9 ? 0 : Infinity;
981
+ }
982
+ exposure(e) {
983
+ let t = this.options, n = this.clock - (t.ignition?.time ?? 0) - this.reach(e);
984
+ if (!(n > 0)) return 0;
985
+ let r = t.burnTime ?? 2400, i = .05 * r, a = .33 * r;
986
+ if (n <= i) return n * n / (2 * i);
987
+ if (n <= r) return i / 2 + (n - i);
988
+ let o = Math.min(n - r, a);
989
+ return i / 2 + (r - i) + o - o * o / (2 * a);
990
+ }
991
+ charDepth(e) {
992
+ n(e, 3, "HullFire char point");
993
+ let t = this.options, r = (t.charRate ?? .5) / 60, i = t.hull?.deck ?? 6, a = e[1] >= i - re ? this.exposure(e[2]) : 0;
994
+ for (let n of t.ports ?? []) {
995
+ if (Math.sign(n.position[0] || 1) !== Math.sign(e[0] || 1)) continue;
996
+ let [t, r] = n.size ?? [.8, .7], i = n.position[1] - r / 2, o = n.position[1] + r / 2 + X(Y(t * r, r), t, r), s = Math.max(0, e[1] - i);
997
+ e[1] < i || e[1] > o || Math.abs(e[2] - n.position[2]) > t / 2 + .12 * s || (a = Math.max(a, this.exposure(n.position[2])));
998
+ }
999
+ return r * a;
1000
+ }
1001
+ paint() {
1002
+ let e = this.options.mask;
1003
+ if (!e) return;
1004
+ let t = $(e), n = this.options.hull ?? {}, r = n.length ?? 50, i = n.deck ?? 6, a = n.draft ?? 5, o = t.width / 2, s = [
1005
+ 0,
1006
+ 0,
1007
+ 0
1008
+ ], c = t.data, l = !1;
1009
+ for (let e = 0; e < t.width; e++) {
1010
+ s[0] = (e < o ? -1 : 1) * .5 * (n.beam ?? 13), s[2] = ((e % o + .5) / o - .5) * r;
1011
+ let u = this.burning(s[2]);
1012
+ for (let n = 0; n < t.height; n++) {
1013
+ s[1] = (n + .5) / t.height * (i + a) - a;
1014
+ let r = this.charDepth(s), o = Math.round(255 * (1 - Math.exp(-r / ie))), d = r > 0 ? Math.round(255 * u) : 0, f = (n * t.width + e) * 4;
1015
+ (c[f] !== o || c[f + 1] !== d) && (c[f] = o, c[f + 1] = d, l = !0);
1016
+ }
1017
+ }
1018
+ l && e.update();
1019
+ }
1020
+ mastFallTime(e) {
1021
+ let t = this.options, n = t.masts?.[e];
1022
+ if (!n) throw RangeError("HullFire mast index is out of range; pass an index into masts");
1023
+ return n.fallAt ?? (t.ignition?.time ?? 0) + this.reach(n.position[2]) + (t.mastFall ?? 600);
1024
+ }
1025
+ rigging() {
1026
+ let e = this.options, t = this.matrix(), n = [];
1027
+ return (e.masts ?? []).forEach((e, r) => {
1028
+ let i = this.mastFallTime(r), a = this.clock > i ? ue(e.height, this.clock - i, Math.min(50, Math.max(0, e.position[1]))).angle : 0, o = e.falls ?? [
1029
+ 1,
1030
+ 0,
1031
+ 0
1032
+ ], s = Math.hypot(o[0], o[2]) || 1, c = o[0] / s, l = o[2] / s, u = Math.cos(a), d = Math.sin(a), f = [
1033
+ d * c,
1034
+ u,
1035
+ d * l
1036
+ ], p = [
1037
+ u * c,
1038
+ -d,
1039
+ u * l
1040
+ ], m = [
1041
+ -l,
1042
+ 0,
1043
+ c
1044
+ ], h = (e, n, r, i) => {
1045
+ let a = [
1046
+ e[0],
1047
+ e[1],
1048
+ e[2],
1049
+ 0,
1050
+ n[0],
1051
+ n[1],
1052
+ n[2],
1053
+ 0,
1054
+ r[0],
1055
+ r[1],
1056
+ r[2],
1057
+ 0,
1058
+ i[0],
1059
+ i[1],
1060
+ i[2],
1061
+ 1
1062
+ ], o = /* @__PURE__ */ new Float32Array(16);
1063
+ for (let e = 0; e < 4; e++) for (let n = 0; n < 4; n++) {
1064
+ let r = 0;
1065
+ for (let i = 0; i < 4; i++) r += t[i * 4 + n] * a[e * 4 + i];
1066
+ o[e * 4 + n] = r;
1067
+ }
1068
+ return o;
1069
+ }, g = [
1070
+ e.position[0],
1071
+ e.position[1],
1072
+ e.position[2]
1073
+ ];
1074
+ n.push({
1075
+ kind: "mast",
1076
+ mast: r,
1077
+ matrix: h(p, f, m, g),
1078
+ length: e.height,
1079
+ angle: a
1080
+ });
1081
+ for (let t of e.yards ?? []) {
1082
+ let e = [
1083
+ g[0] + f[0] * t.at,
1084
+ g[1] + f[1] * t.at,
1085
+ g[2] + f[2] * t.at
1086
+ ];
1087
+ n.push({
1088
+ kind: "yard",
1089
+ mast: r,
1090
+ matrix: h(m, f, [
1091
+ -p[0],
1092
+ -p[1],
1093
+ -p[2]
1094
+ ], e),
1095
+ length: t.length,
1096
+ angle: a
1097
+ });
1098
+ }
1099
+ }), n;
1100
+ }
1101
+ get heat() {
1102
+ return this.flames().reduce((e, t) => e + t.heat, 0);
1103
+ }
1104
+ flames() {
1105
+ let e = this.options, t = e.hull ?? {}, n = t.length ?? 50, r = t.beam ?? 13, i = t.deck ?? 6, a = e.deckHeat ?? 25e4, o = [], s = n / Q;
1106
+ for (let e = 0; e < Q; e++) {
1107
+ let t = -n / 2 + (e + .5) * s, c = this.burning(t);
1108
+ if (c <= 0) continue;
1109
+ let l = a * s * r * .8 * c, u = Math.sqrt(4 * s * r * .8 / Math.PI), d = Math.max(0, .235 * (l / 1e3) ** .4 - 1.02 * u);
1110
+ o.push({
1111
+ kind: "deck",
1112
+ position: this.toWorld([
1113
+ 0,
1114
+ i,
1115
+ t
1116
+ ]),
1117
+ length: d,
1118
+ footprint: [s, r * .8],
1119
+ heat: l
1120
+ });
1121
+ }
1122
+ for (let t of e.ports ?? []) {
1123
+ let e = this.burning(t.position[2]);
1124
+ if (e <= 0) continue;
1125
+ let [n, r] = t.size ?? [.8, .7], i = Y(n * r, r) * e, a = X(i, n, r), s = t.normal ?? [
1126
+ Math.sign(t.position[0]) || 1,
1127
+ 0,
1128
+ 0
1129
+ ], c = [
1130
+ t.position[0] + s[0] * .4,
1131
+ t.position[1] + r / 2,
1132
+ t.position[2] + s[2] * .4
1133
+ ];
1134
+ o.push({
1135
+ kind: "port",
1136
+ position: this.toWorld(c),
1137
+ length: a + r,
1138
+ footprint: [n, .6 * n],
1139
+ heat: i * Z
1140
+ });
1141
+ }
1142
+ return o;
1143
+ }
1144
+ get time() {
1145
+ return this.clock;
1146
+ }
1147
+ setup(e) {
1148
+ this.engine = e;
1149
+ }
1150
+ onPreUpdate(e) {
1151
+ this.advanceTime(e);
1152
+ }
1153
+ resetTime() {
1154
+ if (!this.disposed) {
1155
+ this.clock = 0, this.place();
1156
+ for (let e of [...this.flameVolumes, ...this.smokeColumns]) e.resetTime();
1157
+ this.paint();
1158
+ }
1159
+ }
1160
+ advanceTime(e) {
1161
+ this.disposed || (this.clock += e > 0 ? e : 0, this.checkLive(), this.place(), this.paint());
1162
+ }
1163
+ windNow() {
1164
+ return this.options.wind ?? this.smokeColumns[0]?.effect.options.wind;
1165
+ }
1166
+ place() {
1167
+ let e = this.options, t = this.flames().sort((e, t) => t.heat - e.heat), n = this.windNow(), r = this.flameVolumes;
1168
+ for (let e = 0; e < r.length; e++) {
1169
+ let i = t[e], a = r[e].options;
1170
+ if (!i) {
1171
+ a.length = 0;
1172
+ continue;
1173
+ }
1174
+ a.position = i.position, a.length = Math.min(1e3, i.length), a.footprint = i.footprint;
1175
+ let o = this.matrix();
1176
+ a.yaw = Math.atan2(-o[10], o[8]), n ? a.wind = n : delete a.wind;
1177
+ }
1178
+ let i = this.smokeColumns;
1179
+ if (i.length === 0) return;
1180
+ t.reduce((e, t) => e + t.heat, 0);
1181
+ let a = e.hull?.length ?? 50, o = e.hull?.deck ?? 6, s = W(e.soot ?? .35);
1182
+ i.forEach((n, r) => {
1183
+ let c = -a / 2 + (r + .5) * a / i.length, l = -a / 2 + r * a / i.length, u = r === i.length - 1 ? Infinity : l + a / i.length, d = t.filter((e) => {
1184
+ let t = this.localZ(e.position);
1185
+ return (r === 0 || t >= l) && t < u;
1186
+ }).reduce((e, t) => e + t.heat, 0), f = n.options;
1187
+ f.position = this.toWorld([
1188
+ 0,
1189
+ o + 4,
1190
+ c
1191
+ ]), f.heat = Math.max(0, .7 * d), f.rate = d > 0 ? 18 : .001, f.radius = Math.max(1, Math.min(30, (e.hull?.beam ?? 13) * .5)), f.color = s, f.opacity = d > 0 ? e.opacity ?? .7 : 0, e.wind !== void 0 && f.wind !== e.wind && n.setWind(e.wind);
1192
+ });
1193
+ }
1194
+ localZ(e) {
1195
+ let t = this.matrix(), n = t[8], r = t[9], i = t[10], a = n * n + r * r + i * i;
1196
+ return ((e[0] - t[12]) * n + (e[1] - t[13]) * r + (e[2] - t[14]) * i) / (a || 1);
1197
+ }
1198
+ static attach(t, n) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
1199
+ if (!n || typeof n != "object") throw TypeError("Invalid HullFire options; pass {ship}");
1200
+ return new e(t, n);
1201
+ }
1202
+ dispose() {
1203
+ if (!this.disposed) {
1204
+ this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0;
1205
+ for (let e of this.flameVolumes) e.dispose();
1206
+ for (let e of this.smokeColumns) e.dispose();
1207
+ }
1208
+ }
1209
+ [Symbol.dispose]() {
1210
+ this.dispose();
1211
+ }
1212
+ };
1213
+ //#endregion
1214
+ export { h as BLACK_POWDER, ne as Battery, y as FLASH_ALPHA, v as FLASH_DURATION, V as GRAIN_DENSITY, Z as HULL_FUEL_HEAT, de as HullFire, D as OAK, B as POWDER_BURN_RATE, C as PowderPuff, H as UNBURNT_FRACTION, x as flashExposure, ae as hullMaskImage, oe as hullMaskUVs, ue as mastTopple, b as muzzleFlash, U as powderGrains, _ as powderProducts, z as shotFlight, R as shotPath, W as smokeColor, w as smokeDispersion, T as sphereDrag, Y as ventBurningRate, X as ventFlameHeight, A as woodExitSpeed, k as woodPenetration };