@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
package/dist/flame.js ADDED
@@ -0,0 +1,817 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { t as e } from "./view-RKprOPAy.js";
5
+ import { n as t, r as n, t as r } from "./fog-B-kBv7g0.js";
6
+ import { t as i } from "./view-rect-DkeiiUtw.js";
7
+ import { n as a, r as o, t as s } from "./reactive-mask-B8xbyzlk.js";
8
+ import { soft as c, softDepthWgsl as l } from "./soft.js";
9
+ import { a as u, c as d, i as f, n as p, o as m, r as h, s as g, t as _ } from "./flame-ramp-VoBKpYnM.js";
10
+ import { t as v } from "./require-webgpu--tUXPsmA.js";
11
+ import { finiteVector as y } from "@margoengine/math/validate";
12
+ import { MargoError as b } from "@margoengine/math/errors";
13
+ //#region ../shared/noise-wgsl.js
14
+ var x = "fn hash1(n:u32)->f32 {var x=n*747796405u+2891336453u;x=((x>>((x>>28u)+4u))^x)*277803737u;x=(x>>22u)^x;return f32(x)/4294967295.0;}\nfn hash3(p:vec3i)->f32 {let q=bitcast<vec3u>(p);return hash1((q.x*73856093u)^(q.y*19349663u)^(q.z*83492791u));}\nfn vnoise3(p:vec3f)->f32 {\n let i=vec3i(floor(p));let f=p-floor(p);let s=f*f*(3.0-2.0*f);\n let a=mix(mix(hash3(i),hash3(i+vec3i(1,0,0)),s.x),mix(hash3(i+vec3i(0,1,0)),hash3(i+vec3i(1,1,0)),s.x),s.y);\n let b=mix(mix(hash3(i+vec3i(0,0,1)),hash3(i+vec3i(1,0,1)),s.x),mix(hash3(i+vec3i(0,1,1)),hash3(i+vec3i(1,1,1)),s.x),s.y);\n return mix(a,b,s.z);\n}", S = 9.80665, C = .5, w = .25;
15
+ function T(e) {
16
+ if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Fireline intensity must be a finite kW/m of 0 or more");
17
+ return .0775 * e ** .46;
18
+ }
19
+ function ee(e) {
20
+ if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Flame length must be a finite number of metres, 0 or more");
21
+ return (e / .0775) ** (1 / .46);
22
+ }
23
+ function E(e, t) {
24
+ if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Heat release must be a finite number of watts, 0 or more");
25
+ if (!(Number.isFinite(t) && t >= 0)) throw RangeError("Fire diameter must be a finite number of metres, 0 or more");
26
+ return Math.max(.235 * (e / 1e3) ** .4 - 1.02 * t, 0);
27
+ }
28
+ var D = Object.freeze({
29
+ itemMass: .567,
30
+ burnTime: 600,
31
+ heatOfCombustion: 247e5,
32
+ smoke: Object.freeze([
33
+ .97,
34
+ .97,
35
+ .97
36
+ ])
37
+ });
38
+ function te(e) {
39
+ let { fuel: t, area: n, burnTime: r, heatOfCombustion: i } = e, a = e.spread ?? 0, o = (e) => Number.isFinite(e) && e > 0;
40
+ if (!o(t)) throw RangeError("burningBed fuel must be a finite number of kilograms above 0");
41
+ if (!o(n)) throw RangeError("burningBed area must be a finite number of square metres above 0");
42
+ if (!o(r)) throw RangeError("burningBed burnTime must be a finite number of seconds above 0");
43
+ if (!o(i)) throw RangeError("burningBed heatOfCombustion must be a finite number of J/kg above 0");
44
+ if (!(Number.isFinite(a) && a >= 0)) throw RangeError("burningBed spread must be a finite number of seconds, 0 or more");
45
+ let s = t * i / r, c = Math.sqrt(4 * n / Math.PI), l = (e) => a > 0 ? Math.min(Math.max(e / a, 0), 1) : +(e >= 0), u = (e) => {
46
+ if (!Number.isFinite(e)) throw RangeError("burningBed time must be a finite number of seconds");
47
+ return s * Math.max(l(e) - l(e - r), 0);
48
+ };
49
+ return Object.freeze({
50
+ duration: a + r,
51
+ diameter: c,
52
+ power: u,
53
+ flameLength: (e) => E(u(e), c)
54
+ });
55
+ }
56
+ var O = 1.5;
57
+ function k(e) {
58
+ if (!(Number.isFinite(e) && e > 0)) throw RangeError("Fire diameter must be a finite number of metres above 0");
59
+ return O / Math.sqrt(e);
60
+ }
61
+ function A(e) {
62
+ let [t, n] = e;
63
+ return Math.max(Math.min(Math.sqrt(4 * t * n / Math.PI), 2 * Math.min(t, n)), .05);
64
+ }
65
+ var j = 5.95, ne = 5.96, re = .5, M = 1.35, N = 2, ie = 18, P = 64, F = 256, ae = .03, oe = 5.588238, I = 5e-4, se = .15, L = 3, ce = 4, R = 16, z = .5, B = .3;
66
+ function V(e, t, n = B) {
67
+ if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Heat release must be a finite number of watts, 0 or more");
68
+ if (!(Number.isFinite(n) && n >= 0 && n <= 1)) throw RangeError("Radiant fraction must be a number from 0 to 1");
69
+ return n * e * u(t);
70
+ }
71
+ function H(e, t) {
72
+ if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Flame length must be a finite number of metres, 0 or more");
73
+ if (!(Number.isFinite(t) && t >= 0)) throw RangeError("Fire diameter must be a finite number of metres, 0 or more");
74
+ return 1e3 * ((e + 1.02 * t) / .235) ** 2.5;
75
+ }
76
+ var U = 5.670374419e-8, W = 1e3, le = u(5772);
77
+ function G(e) {
78
+ let { heat: t, area: n, kelvin: r } = e, i = e.noon ?? Math.PI, a = e.radiantFraction ?? .3;
79
+ if (!(Number.isFinite(t) && t >= 0)) throw RangeError("Flame heat must be a finite number of watts, 0 or more");
80
+ if (!(Number.isFinite(n) && n > 0)) throw RangeError("Flame area must be a finite number of square metres above 0");
81
+ if (d(r, "core", 800, 3e3), !(Number.isFinite(i) && i > 0 && i <= 1e7)) throw RangeError("Flame noon must be the noon sun's engine intensity, above 0 and at most 1e7; pass e.g. Math.PI");
82
+ if (!(Number.isFinite(a) && a >= 0 && a <= 1)) throw RangeError("Radiant fraction must be a number from 0 to 1");
83
+ let o = Math.min(a * t / n, U * r ** 4) * u(r) / Math.PI, [s, c, l] = h(r);
84
+ return o * i / (W * le) / (.2126 * s + .7152 * c + .0722 * l);
85
+ }
86
+ function K(e, t) {
87
+ return 2 * Math.max(t[0], t[1]) * e + t[0] * t[1];
88
+ }
89
+ var q = (e) => {
90
+ let t = Math.imul(e >>> 0, 747796405) + 2891336453 >>> 0;
91
+ return t = Math.imul((t >>> (t >>> 28) + 4 ^ t) >>> 0, 277803737) >>> 0, t = (t >>> 22 ^ t) >>> 0, t / 4294967295;
92
+ }, J = (e) => {
93
+ let t = Math.floor(e), n = e - t, r = n * n * (3 - 2 * n);
94
+ return q(t | 0) + (q(t + 1 | 0) - q(t | 0)) * r;
95
+ }, ue = (e) => Math.min(Math.max(e, 0), 1), de = 8;
96
+ function fe(e) {
97
+ if (typeof e != "object" || !e || e.length !== 64) throw RangeError("Flame relief must hold 64 heights (8 x 8 over the footprint)");
98
+ for (let t = 0; t < e.length; t++) d(e[t], "relief", -1e3, 1e3);
99
+ }
100
+ function Y(e, t, n) {
101
+ if (!e) return 0;
102
+ let r = Math.min(Math.max((t * .5 + .5) * 7, 0), 7), i = Math.min(Math.max((n * .5 + .5) * 7, 0), 7), a = Math.min(Math.floor(r), 6), o = Math.min(Math.floor(i), 6), s = r - a, c = i - o, l = (t, n) => e[n * 8 + t];
103
+ return (1 - c) * ((1 - s) * l(a, o) + s * l(a + 1, o)) + c * ((1 - s) * l(a, o + 1) + s * l(a + 1, o + 1));
104
+ }
105
+ var pe = 196, me = 112, he = 116, ge = 120, _e = 124, ve = 128, ye = 132, X = /* @__PURE__ */ new WeakMap(), be = 0, Z = /* @__PURE__ */ new WeakMap(), Q = /* @__PURE__ */ new WeakMap();
106
+ function xe(e, t) {
107
+ let n = t.descriptor.multisample?.count ?? 1;
108
+ if (!e.mask || e.device !== t.device || e.width !== t.width || e.height !== t.height || e.samples !== n) {
109
+ e.mask?.destroy(), e.multi?.destroy();
110
+ let r = [t.width, t.height];
111
+ e.device = t.device, e.width = t.width, e.height = t.height, e.samples = n, e.mask = t.device.createTexture({
112
+ label: "flame coverage mask",
113
+ size: r,
114
+ format: "r8unorm",
115
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC
116
+ }), e.multi = n > 1 ? t.device.createTexture({
117
+ label: "flame coverage mask (multisampled)",
118
+ size: r,
119
+ format: "r8unorm",
120
+ sampleCount: n,
121
+ usage: GPUTextureUsage.RENDER_ATTACHMENT
122
+ }) : void 0, e.maskView = e.mask.createView(), e.multiView = e.multi?.createView(), e.drawn = -1;
123
+ }
124
+ let r = e.drawn !== e.frame || e.view !== t.view;
125
+ return e.drawn = e.frame, e.view = t.view, {
126
+ view: e.multiView ?? e.maskView,
127
+ resolveTarget: e.multi ? e.maskView : void 0,
128
+ loadOp: r ? "clear" : "load",
129
+ clearValue: [
130
+ 0,
131
+ 0,
132
+ 0,
133
+ 0
134
+ ],
135
+ storeOp: "store"
136
+ };
137
+ }
138
+ function Se(e) {
139
+ if (!e || typeof e.use != "function") throw TypeError("watchFlameCoverage needs an engine; pass the engine the flames are attached to");
140
+ let t = Q.get(e);
141
+ t || Q.set(e, t = {
142
+ watchers: 0,
143
+ frame: 0,
144
+ drawn: -1,
145
+ view: void 0,
146
+ width: 0,
147
+ height: 0,
148
+ samples: 0
149
+ });
150
+ let n = t;
151
+ n.watchers++ === 0 && e.use({coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
152
+ name: "flame-coverage",
153
+ setup() {},
154
+ onPreUpdate() {
155
+ n.frame++;
156
+ },
157
+ dispose() {
158
+ n.mask?.destroy(), n.multi?.destroy(), n.mask = n.multi = n.maskView = n.multiView = void 0, n.watchers = 0;
159
+ }
160
+ });
161
+ let r = !0, i = () => {
162
+ r && (r = !1, --n.watchers === 0 && e.removePlugin("flame-coverage"));
163
+ };
164
+ return {
165
+ get texture() {
166
+ return r && n.mask && n.drawn === n.frame ? n.mask : void 0;
167
+ },
168
+ dispose: i,
169
+ [Symbol.dispose]: i
170
+ };
171
+ }
172
+ var $ = (e, n, r, i, a = i) => `
173
+ struct F {camera:mat4x4f,eye:vec4f,base:vec4f,shape:vec4f,turn:vec4f,motion:vec4f,look:vec4f,box:vec4f,forward:vec4f,ramp:array<vec4f,${R}>,extra:vec4f,ground:vec4f,glow:vec4f,outer:vec4f,relief:vec4f,grid:array<vec4f,16>}
174
+ @group(0) @binding(0) var<uniform> u:F;
175
+ ${n ? t(0, 5) : ""}
176
+ ${a ? l(e) : ""}
177
+ struct Out {@builtin(position) clip:vec4f,@location(0) world:vec3f}
178
+
179
+
180
+ fn toWorld(p:vec3f)->vec3f {
181
+ var s=vec3f(p.x+u.shape.z*p.y,p.y,p.z+u.shape.w*p.y);
182
+ s.y+=u.ground.x*s.x+u.ground.y*s.z;
183
+ return u.base.xyz+vec3f(u.turn.x*s.x+u.turn.y*s.z,s.y,-u.turn.y*s.x+u.turn.x*s.z);
184
+ }
185
+ fn toLocal(v:vec3f)->vec3f {
186
+ var r=vec3f(u.turn.x*v.x-u.turn.y*v.z,v.y,u.turn.y*v.x+u.turn.x*v.z);
187
+ r.y-=u.ground.x*r.x+u.ground.y*r.z;
188
+ return vec3f(r.x-u.shape.z*r.y,r.y,r.z-u.shape.w*r.y);
189
+ }
190
+
191
+
192
+ fn cell(i:u32,j:u32)->f32 {let k=j*8u+i;return u.grid[k>>2u][k&3u];}
193
+ fn reliefAt(x:f32,z:f32)->f32 {
194
+ let n=7.0;
195
+ let fu=clamp((x/max(u.relief.z,1e-6)*0.5+0.5)*n,0.0,n);let fv=clamp((z/max(u.relief.w,1e-6)*0.5+0.5)*n,0.0,n);
196
+ let i=min(u32(fu),6u);let j=min(u32(fv),6u);let a=fu-f32(i);let b=fv-f32(j);
197
+ return mix(mix(cell(i,j),cell(i+1u,j),a),mix(cell(i,j+1u),cell(i+1u,j+1u),a),b);
198
+ }
199
+
200
+
201
+
202
+ fn overGround(q:vec3f)->vec3f {let g=reliefAt(q.x+u.shape.z*q.y,q.z+u.shape.w*q.y);return vec3f(q.x+u.shape.z*g,q.y-g,q.z+u.shape.w*g);}
203
+
204
+ fn reliefPad()->f32 {return max(abs(u.shape.z),abs(u.shape.w))*max(abs(u.relief.x),abs(u.relief.y));}
205
+ fn corner(v:u32,margin:f32,top:f32)->Out {
206
+ var corners=array<u32,36>(0u,2u,1u,1u,2u,3u,4u,5u,6u,5u,7u,6u,0u,1u,4u,1u,5u,4u,2u,6u,3u,3u,6u,7u,0u,4u,2u,2u,4u,6u,1u,3u,5u,3u,7u,5u);
207
+ let k=corners[v];
208
+ let c=vec3f(f32(k&1u),f32((k>>1u)&1u),f32((k>>2u)&1u));
209
+
210
+ let pad=margin+reliefPad();
211
+ let w=toWorld(vec3f((c.x*2.0-1.0)*(u.shape.x+pad),mix(u.box.z+u.relief.x,top+u.relief.y,c.y),(c.z*2.0-1.0)*(u.shape.y+pad)));
212
+ var o:Out;o.clip=u.camera*vec4f(w,1.0);o.world=w;return o;
213
+ }
214
+
215
+ @vertex fn vertex(@builtin(vertex_index) v:u32)->Out {return corner(v,u.box.x,u.box.y);}
216
+ @vertex fn glowVertex(@builtin(vertex_index) v:u32)->Out {return corner(v,u.outer.x,u.outer.y);}
217
+ ${x}
218
+ fn vnoise1(v:f32)->f32 {let i=floor(v);let f=v-i;let s=f*f*(3.0-2.0*f);let a=bitcast<u32>(i32(i));return mix(hash1(a),hash1(a+1u),s);}
219
+ fn fbm(p:vec3f)->f32 {return 0.5161*vnoise3(p)+0.2581*vnoise3(p*2.03+vec3f(17.1,3.7,9.2))+0.129*vnoise3(p*4.07+vec3f(5.3,11.9,2.1))+0.0645*vnoise3(p*8.11+vec3f(9.7,1.3,13.4))+0.0323*vnoise3(p*16.3+vec3f(3.1,7.7,5.9));}
220
+ fn table(x:f32)->vec3f {let t=clamp(x,0.0,1.0)*15.0;let i=u32(floor(t));let j=min(i+1u,15u);return mix(u.ramp[i].rgb,u.ramp[j].rgb,t-floor(t));}
221
+
222
+
223
+
224
+
225
+ fn trees(q:vec3f,h0:f32)->vec2f {
226
+ let S=u.extra.y;let c=floor(q.xz/S);var best=vec2f(-9.0,h0);
227
+ for(var j=-1;j<=1;j++){for(var i=-1;i<=1;i++){
228
+ let id=vec2i(c)+vec2i(i,j);let at=(vec2f(id)+0.2+0.6*vec2f(hash3(vec3i(id,7)),hash3(vec3i(id,13))))*S;
229
+ let h=(h0+0.3*hash3(vec3i(id,29)))/(0.6+0.8*hash3(vec3i(id,41)));
230
+ let r=0.34*pow(clamp(1.0-h/${M.toFixed(2)},0.0,1.0),0.7)*smoothstep(-0.08,0.12,h);
231
+ let v=2.6*(r-length(q.xz-at)/S);
232
+ if(v>best.x){best=vec2f(v,h);}
233
+ }}
234
+ return best;
235
+ }
236
+ fn zone(q:vec3f)->vec2f {
237
+ let L=u.base.w;var h=q.y/L;
238
+ if(h<u.box.z/L||h>${N.toFixed(2)}){return vec2f(0.0);}
239
+ let along=select(q.z,q.x,u.shape.x>=u.shape.y);
240
+
241
+
242
+
243
+ h/=1.0+select(0.6,0.0,u.extra.y>0.0)*smoothstep(0.5*L,2.0*L,max(u.shape.x,u.shape.y))*(vnoise1(along/(0.8*L)+3.7*u.look.w+0.15*u.turn.z*u.eye.w)-0.5);
244
+
245
+ var column=0.0;
246
+ if(u.extra.y>0.0){
247
+ let t=trees(q,h);column=t.x;h=max(t.y,0.0);
248
+ if(column<-1.2){return vec2f(0.0);}
249
+ }
250
+ let puff=sin(6.2831853*(u.turn.z*u.eye.w-q.y/u.turn.w+vnoise1(along/u.look.z+u.look.w)));
251
+ let taper=pow(clamp(1.0-h/${M.toFixed(2)},0.0,1.0),0.6);
252
+ let narrow=min(u.shape.x,u.shape.y)+0.12*L;
253
+ let ext=max(vec2f(u.shape.x,u.shape.y)+0.12*L-narrow*(1.0-taper)-0.2*L*smoothstep(0.0,1.3,h),vec2f(0.0));
254
+ let d=abs(q.xz)-ext;
255
+ var inner=-(length(max(d,vec2f(0.0)))+min(max(d.x,d.y),0.0))/(0.35*L+0.5*narrow);
256
+ if(u.extra.y>0.0){inner=min(inner+0.6,column);}
257
+ inner+=0.3*u.motion.z*puff*smoothstep(0.1,0.9,h);
258
+ if(inner<-1.2){return vec2f(0.0);}
259
+ let s=u.motion.y;let rise=u.motion.x*u.eye.w;
260
+ let warp=vec3f(vnoise3(vec3f(q.x/s,(q.y-rise)/(2.2*s),q.z/s)+vec3f(31.7,0.0,7.3)),0.0,vnoise3(vec3f(q.x/s,(q.y-rise)/(2.2*s),q.z/s)+vec3f(2.9,0.0,43.1)))-vec3f(0.5,0.0,0.5);
261
+ let p=vec3f(q.x/s+1.4*warp.x,(q.y-rise)/(2.6*s),q.z/s+1.4*warp.z);
262
+ let n=fbm(p);
263
+ let amp=mix(0.35,1.25,smoothstep(0.0,1.1,h));
264
+ var v=inner+amp*(n-0.5)*2.0-0.55*smoothstep(0.45,1.55,h)+0.25;
265
+
266
+
267
+ if(v>-0.35&&v<0.35){
268
+ let w=vec3f(p.x*4.0+2.0*warp.x,p.y*1.4,p.z*4.0+2.0*warp.z);
269
+ v+=(0.24*(vnoise3(w)-0.5)+0.14*(vnoise3(w*2.0+vec3f(7.3,1.1,3.9))-0.5))*2.0*smoothstep(0.0,0.6,h+0.2);
270
+ }
271
+
272
+
273
+ let density=smoothstep(0.0,0.03,v)*smoothstep(0.0,0.08,h+0.3*(n-0.5));
274
+
275
+ let heat=clamp(2.0*v+0.3-0.7*h+0.25*(1.0-smoothstep(0.0,0.15,h)),0.0,1.0);
276
+ return vec2f(density,heat);
277
+ }
278
+ fn encodeSrgb(c:vec3f)->vec3f {return select(1.055*pow(c,vec3f(1.0/2.4))-0.055,c*12.92,c<=vec3f(0.0031308));}
279
+
280
+ fn boxHit(o:vec3f,d:vec3f,lo:vec3f,hi:vec3f)->vec2f {
281
+ let inv=1.0/select(d,vec3f(1e-6),abs(d)<vec3f(1e-6));
282
+ let ta=(lo-o)*inv;let tb=(hi-o)*inv;
283
+ let tn=min(ta,tb);let tf=max(ta,tb);
284
+ return vec2f(max(max(tn.x,tn.y),tn.z),min(min(tf.x,tf.y),tf.z));
285
+ }
286
+
287
+
288
+ struct Ray {o:vec3f,d:vec3f,rd:vec3f,span:vec4f}
289
+ fn rayOf(i:Out,margin:f32,top:f32,glow:bool)->Ray {
290
+ var r:Ray;
291
+ let ray=i.world-u.eye.xyz;let along=length(ray);r.rd=ray/along;
292
+ r.o=toLocal(u.eye.xyz-u.base.xyz);r.d=toLocal(r.rd);
293
+ let m=vec2f(u.shape.x,u.shape.y)+margin+reliefPad();
294
+ let box=boxHit(r.o,r.d,vec3f(-m.x,u.box.z+u.relief.x,-m.y),vec3f(m.x,top+u.relief.y,m.y));
295
+ let enter=box.x;let leave=box.y;
296
+
297
+
298
+ ${i ? "let mine=!(abs(along-leave)>abs(along-max(enter,0.0))&&enter>0.0);" : "let mine=select(!(abs(along-enter)>abs(along-leave)||enter<0.0),abs(along-leave)<=abs(along-enter),glow&&enter<0.0);"}
299
+ var end=leave;
300
+ ${a ? "if(textureLoad(sceneDepth,vec2i(i.clip.xy),0)>0.0){end=min(end,softGap(i.clip,0.0)/max(dot(r.rd,u.forward.xyz),1e-4));}" : ""}
301
+
302
+
303
+ var ground=-1.0;
304
+ let floor=select(0.0,u.box.z+0.16*u.base.w,u.extra.y>0.0);
305
+
306
+ if(r.d.y<0.0){
307
+ var tg=(floor-r.o.y)/r.d.y;
308
+ for(var k=0;k<4;k++){let p=r.o+r.d*tg;tg=(floor+reliefAt(p.x+u.shape.z*p.y,p.z+u.shape.w*p.y)-r.o.y)/r.d.y;}
309
+ if(tg>max(enter,0.0)&&tg<end){end=tg;ground=tg;}
310
+ }
311
+ r.span=vec4f(max(enter,0.0),end,ground,select(0.0,1.0,mine&&end>max(enter,0.0)));
312
+ return r;
313
+ }
314
+
315
+
316
+ struct March {glow:vec3f,trans:f32,middle:f32,surface:f32}
317
+ fn march(o:vec3f,d:vec3f,t0:f32,t1:f32,pixel:vec2f,stopAt:f32)->March {
318
+ var m:March;m.glow=vec3f(0.0);m.trans=1.0;m.middle=t0;m.surface=-1.0;
319
+
320
+
321
+ let dt=min((t1-t0)/${P}.0,${ae}*u.base.w);
322
+
323
+
324
+ let px=pixel+${oe}*u.extra.x;let dither=fract(52.9829189*fract(0.06711056*px.x+0.00583715*px.y));
325
+ var t=t0+dt*dither;
326
+ let k=u.box.w/u.base.w;
327
+ var last=t;var entered=false;
328
+ for(var n=0;n<${F};n++){
329
+ if(t>t1){break;}
330
+ var z=zone(overGround(o+d*t));
331
+
332
+
333
+ if(z.x>0.0&&!entered){
334
+ entered=true;var a0=last;var b0=t;
335
+ for(var r=0;r<5;r++){let mid=0.5*(a0+b0);if(zone(overGround(o+d*mid)).x>0.0){b0=mid;}else{a0=mid;}}
336
+ t=b0;z=zone(overGround(o+d*t));
337
+ }
338
+ last=t;
339
+ if(z.x>0.0){
340
+
341
+ let a=1.0-exp(-z.x*(0.2+0.8*z.y)*k*dt);
342
+ m.glow+=m.trans*a*table(z.y);
343
+ if(m.trans>0.5){m.middle=t;}
344
+ m.trans*=1.0-a;
345
+ if(stopAt>0.0&&m.trans<stopAt){m.surface=t;break;}
346
+ if(m.trans<0.004){break;}
347
+ }
348
+ t+=select(dt,2.0*dt,z.x<=0.0);
349
+ }
350
+ return m;
351
+ }
352
+
353
+
354
+
355
+ fn glowAt(e:vec3f,rd:vec3f,ta:f32,tb:f32,tg:f32)->vec3f {
356
+ let n=i32(u.extra.w);
357
+ if(n<=0||tb<=ta){return vec3f(0.0);}
358
+ let L=u.base.w;let R=u.extra.z;let a2=0.0625*L*L;let kR=1.0/(R*R+a2);
359
+ let long=u.shape.x>=u.shape.y;let span=1.6*max(u.shape.x,u.shape.y);
360
+ var normal=normalize(vec3f(-u.outer.z,1.0,-u.outer.w));
361
+
362
+ if(tg>0.0){
363
+ let q=toLocal(e+rd*tg-u.base.xyz);let px=q.x+u.shape.z*q.y;let pz=q.z+u.shape.w*q.y;
364
+ let hx=u.relief.z/7.0+1e-3;let hz=u.relief.w/7.0+1e-3;
365
+ let du=(reliefAt(px+hx,pz)-reliefAt(px-hx,pz))/(2.0*hx);let dv=(reliefAt(px,pz+hz)-reliefAt(px,pz-hz))/(2.0*hz);
366
+ normal=normalize(vec3f(-(u.outer.z+du*u.turn.x+dv*u.turn.y),1.0,-(u.outer.w-du*u.turn.y+dv*u.turn.x)));
367
+ }
368
+ var air=0.0;var lit=0.0;
369
+ for(var k=0;k<n;k++){
370
+ let a=select(0.0,(f32(k)/f32(max(n-1,1))-0.5)*span,n>1);
371
+ let s=toWorld(select(vec3f(0.0,u.glow.w+reliefAt(0.0,a),a),vec3f(a,u.glow.w+reliefAt(a,0.0),0.0),long));
372
+ let es=s-e;let tc=dot(es,rd);let h2=max(dot(es,es)-tc*tc,0.0);
373
+ let c2=R*R-h2;
374
+ if(c2>0.0){
375
+ let c=sqrt(c2);let x0=clamp(ta-tc,-c,c);let x1=clamp(tb-tc,-c,c);let b=sqrt(h2+a2);
376
+ air+=max((atan(x1/b)-atan(x0/b))/b-(x1-x0)*kR,0.0);
377
+ }
378
+ if(tg>0.0){
379
+ let sv=s-(e+rd*tg);let dd=dot(sv,sv);
380
+ if(dd<R*R){lit+=max(dot(normal,sv),0.0)*inverseSqrt(max(dd,1e-6))*(1.0/(dd+a2)-kR);}
381
+ }
382
+ }
383
+ return u.glow.rgb*(u.ground.w*0.07957747*air+u.ground.z*0.31830989*lit);
384
+ }
385
+ fn flame(i:Out)->vec4f {
386
+ let r=rayOf(i,u.box.x,u.box.y,false);
387
+ if(r.span.w<=0.0){discard;}
388
+ let m=march(r.o,r.d,r.span.x,r.span.y,i.clip.xy,0.0);
389
+
390
+ let alpha=(1.0-m.trans*m.trans)*u.motion.w;
391
+ if(alpha<=0.0){discard;}
392
+
393
+
394
+ var c=u.look.x*m.glow/max(1.0-m.trans,1e-5);
395
+ ${n ? "c=mix(c,margoFog.color.rgb,fogAmount(u.eye.xyz+r.rd*m.middle));" : ""}
396
+ ${r ? "c=encodeSrgb(max(c,vec3f(0.0)));" : ""}
397
+ return vec4f(c*alpha,alpha);
398
+ }
399
+ @fragment fn fragment(i:Out)->@location(0) vec4f {return flame(i);}
400
+
401
+ @fragment fn reactiveFragment(i:Out)->@location(0) vec4f {return vec4f(clamp(flame(i).a,0.0,1.0),0,0,1);}
402
+
403
+
404
+ @fragment fn coverageFragment(i:Out)->@location(0) vec4f {return vec4f(select(0.0,1.0,flame(i).a>0.0),0,0,1);}
405
+
406
+
407
+ struct Glow {@location(0) color:vec4f,@builtin(frag_depth) depth:f32}
408
+ @fragment fn glowFragment(i:Out)->Glow {
409
+ let r=rayOf(i,u.outer.x,u.outer.y,true);
410
+ if(r.span.w<=0.0){discard;}
411
+ var add=glowAt(u.eye.xyz,r.rd,r.span.x,r.span.y,r.span.z);
412
+ ${n ? "add*=1.0-fogAmount(u.eye.xyz+r.rd*r.span.y);" : ""}
413
+ if(max(add.x,max(add.y,add.z))<=0.0){discard;}
414
+ ${r ? "add=encodeSrgb(max(add,vec3f(0.0)));" : ""}
415
+ var o:Glow;o.color=vec4f(add,0.0);o.depth=1.0;
416
+ if(r.span.x>0.0){let p=u.camera*vec4f(u.eye.xyz+r.rd*r.span.x,1.0);o.depth=clamp(p.z/p.w,0.0,1.0);}
417
+ return o;
418
+ }
419
+
420
+
421
+ struct Depth {@builtin(frag_depth) depth:f32}
422
+ @fragment fn depthFragment(i:Out)->Depth {
423
+ let r=rayOf(i,u.box.x,u.box.y,false);
424
+ if(r.span.w<=0.0||u.motion.w<1.0){discard;}
425
+ let m=march(r.o,r.d,r.span.x,r.span.y,i.clip.xy,${re});
426
+ if(m.surface<0.0){discard;}
427
+ let p=u.camera*vec4f(u.eye.xyz+r.rd*m.surface,1.0);
428
+ var o:Depth;o.depth=clamp(p.z/p.w,0.0,1.0);return o;
429
+ }
430
+ `, Ce = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
431
+ constructor(e, t) {
432
+ this.disposed = !1, this.clock = 0, this.current = 0, this.key = [], this.values = new Float32Array(pe), this.view = /* @__PURE__ */ new Float32Array(16), this.heat = {
433
+ position: [
434
+ 0,
435
+ 0,
436
+ 0
437
+ ],
438
+ length: 0,
439
+ footprint: [0, 0],
440
+ yaw: 0,
441
+ lean: [0, 0],
442
+ time: 0
443
+ }, this.fogData = /* @__PURE__ */ new Float32Array(12), this.steady = [], this.samples = 0, this.owned = [], this.groups = /* @__PURE__ */ new Map(), this.attachment = {
444
+ view: void 0,
445
+ loadOp: "load",
446
+ storeOp: "store"
447
+ }, this.depthAttachment = {
448
+ view: void 0,
449
+ depthReadOnly: !0
450
+ }, this.reactiveAttachments = [], this.name = "vfx-flame:" + be++, this.seed = t * 7.31 % 97, this.options = e, this.check();
451
+ let n = e.lights, r = n && "names" in n && "clustered" in n ? n : void 0, i = r ? r.clustered : n && "maxLights" in n ? n : void 0;
452
+ if (n && !i) throw TypeError("Invalid Flame lights; pass a Clustered from @margoengine/clustered or its LightGroups");
453
+ let a = e.castShadow === !0;
454
+ if (a && i && !i.shadow) throw new b("state", "A Flame that casts shadows needs LocalShadows attached to its Clustered first; attach it, or pass castShadow: false");
455
+ let o = [];
456
+ if (i) {
457
+ for (let e = 0; e < (a ? 1 : 4); e++) o.push({
458
+ position: [
459
+ 0,
460
+ -1e6,
461
+ 0
462
+ ],
463
+ range: 1,
464
+ intensity: 0,
465
+ color: [
466
+ 1,
467
+ .5,
468
+ .2
469
+ ],
470
+ radius: 0,
471
+ castShadow: a
472
+ });
473
+ if (r) {
474
+ let e = "flame";
475
+ for (let t = 2; r.names.includes(e); t++) e = `flame ${t}`;
476
+ this.target = this.group = r.group(e);
477
+ } else this.target = i;
478
+ }
479
+ this.lights = o, this.write(!0);
480
+ }
481
+ length() {
482
+ let e = this.options;
483
+ if (e.length !== void 0) return e.length;
484
+ if (e.intensity !== void 0) return T(e.intensity);
485
+ let [t, n] = e.footprint ?? [0, 0];
486
+ return E(e.heat ?? 0, Math.sqrt(4 * t * n / Math.PI));
487
+ }
488
+ check() {
489
+ let e = this.options;
490
+ if (!e || typeof e != "object") throw TypeError("Flame options must be an object with a position and a length, intensity or heat");
491
+ if (y(e.position, 3, "Flame position"), e.length === void 0 && e.intensity === void 0 && e.heat === void 0) throw TypeError("Flame needs a length (m), an intensity (kW/m) or a heat (W)");
492
+ if (d(this.length(), "length", 0, 1e3), e.footprint && (d(e.footprint[0], "footprint x", 0, 1e5), d(e.footprint[1], "footprint z", 0, 1e5)), d(e.opacity ?? 1, "opacity", 0, 1), d(e.flicker ?? 1, "flicker", 0, 3), d(e.tongues ?? 1, "tongues", .25, 4), d(e.crowns ?? 0, "crowns", 0, 1e3), e.slope !== void 0 && (y(e.slope, 2, "Flame slope"), d(e.slope[0], "slope x", -10, 10), d(e.slope[1], "slope z", -10, 10)), e.relief !== void 0 && fe(e.relief), d(e.glow ?? 1, "glow", 0, 8), e.castShadow !== void 0 && typeof e.castShadow != "boolean") throw TypeError("Flame castShadow must be true or false; pass a boolean");
493
+ if (e.reactive !== void 0 && typeof e.reactive != "boolean") throw TypeError("Flame reactive must be true or false; pass a boolean");
494
+ d(e.haze ?? I, "haze", 0, 1), d(e.groundAlbedo ?? 0, "groundAlbedo", 0, 1), d(e.glowRadius ?? L, "glowRadius", .5, 10), d(e.yaw ?? 0, "yaw", -1e3, 1e3), d(e.lightPower ?? 1, "lightPower", 0, 1e6), e.keyDistance !== void 0 && d(e.keyDistance, "keyDistance", .01, 1e4), e.noon !== void 0 && d(e.noon, "noon", 1e-9, 1e7), e.heat !== void 0 && d(e.heat, "heat", 0, 0x9184e72a000);
495
+ }
496
+ lookOf() {
497
+ let e = this.options, t = this.look, n = (e, n) => t && typeof t[e] == "number" ? t[e] : n, r = t?.["flames.edgeColor"] ?? [
498
+ .55,
499
+ .12,
500
+ .04
501
+ ];
502
+ return {
503
+ core: n("flames.core", e.core ?? 2200),
504
+ edge: n("flames.edge", e.edge ?? 1e3),
505
+ peak: n("flames.peak", e.peak ?? 1),
506
+ flicker: n("flames.flicker", 1) * (e.flicker ?? 1),
507
+ tongues: n("flames.tongues", 1) * (e.tongues ?? 1),
508
+ size: n("flames.size", 1),
509
+ radiance: n("flames.radiance", 1),
510
+ opacity: n("flames.opacity", 1) * (e.opacity ?? 1),
511
+ ramp: n("flames.ramp", 0),
512
+ glow: n("flames.glow", 1) * (e.glow ?? 1),
513
+ haze: n("flames.haze", 1) * (e.haze ?? I),
514
+ edgeColor: r
515
+ };
516
+ }
517
+ write(e = !1) {
518
+ let t = this.options, n = this.lookOf(), r = [
519
+ n.core,
520
+ n.edge,
521
+ t.cooling ?? 1.2,
522
+ t.exposure ?? .3,
523
+ n.ramp,
524
+ n.edgeColor[0],
525
+ n.edgeColor[1],
526
+ n.edgeColor[2]
527
+ ];
528
+ if (!e && r.every((e, t) => e === this.key[t])) return;
529
+ for (let e = 0; e < r.length; e++) this.key[e] = r[e];
530
+ let i = d(n.core, "core", 800, 3e3), a = d(Math.min(n.edge, i), "edge", 600, 3e3), o = d(t.cooling ?? 1.2, "cooling", .2, 5), s = d(t.exposure ?? .3, "exposure", 0, 1), c = m(i)[1], l = this.values;
531
+ for (let e = 0; e < R; e++) {
532
+ let t = e / 15, r = a + (i - a) * t ** o, u = Math.min(1, (m(r)[1] / c) ** s), d = h(r);
533
+ for (let r = 0; r < 3; r++) l[48 + e * 4 + r] = d[r] * u + (n.edgeColor[r] - d[r] * u) * n.ramp * (1 - t);
534
+ l[48 + e * 4 + 3] = 1;
535
+ }
536
+ }
537
+ setFlameLook(e) {
538
+ return this.look = { ...e }, this.write(), this;
539
+ }
540
+ get peakRadiance() {
541
+ let e = this.lookOf();
542
+ return e.peak * e.radiance * this.coreRadiance(e);
543
+ }
544
+ get heatRelease() {
545
+ let e = this.options, [t, n] = e.footprint ?? [.5 * this.length(), .5 * this.length()];
546
+ return e.heat === void 0 ? e.intensity === void 0 ? H(this.length(), Math.sqrt(4 * t * n / Math.PI)) : e.intensity * 1e3 * t : e.heat;
547
+ }
548
+ coreRadiance(e) {
549
+ let t = this.options, n = Math.max(this.length(), .001), r = t.footprint ?? [.5 * n, .5 * n], i = [Math.max(r[0] * e.size, .001), Math.max(r[1] * e.size, .001)];
550
+ return G({
551
+ heat: this.heatRelease,
552
+ area: K(n, i),
553
+ kelvin: d(e.core, "core", 800, 3e3),
554
+ ...t.noon === void 0 ? {} : { noon: t.noon }
555
+ });
556
+ }
557
+ get plume() {
558
+ let e = this.options, t = this.heat, n = this.values;
559
+ return t.position[0] = e.position[0], t.position[1] = e.position[1], t.position[2] = e.position[2], t.length = this.current, t.footprint[0] = 2 * n[24], t.footprint[1] = 2 * n[25], t.yaw = e.yaw ?? 0, t.time = this.clock, t;
560
+ }
561
+ get flameLength() {
562
+ return this.current;
563
+ }
564
+ get time() {
565
+ return this.clock;
566
+ }
567
+ get puffing() {
568
+ let e = this.length();
569
+ return k(A(this.options.footprint ?? [.5 * e, .5 * e]));
570
+ }
571
+ setup(e) {
572
+ this.engine = e;
573
+ }
574
+ registerPasses(e) {
575
+ e.add({
576
+ name: this.name + ":draw",
577
+ order: j,
578
+ execute: (e) => {
579
+ e.backend === "webgpu" && this.draw(e, !1);
580
+ }
581
+ }), e.add({
582
+ name: this.name + ":depth",
583
+ order: ne,
584
+ execute: (e) => {
585
+ e.backend === "webgpu" && this.draw(e, !0);
586
+ }
587
+ });
588
+ }
589
+ lean(e, t, n) {
590
+ let r = this.options, i = r.wind, a = 0, o = 0;
591
+ if (i && typeof i == "object" && "sample" in i) {
592
+ let e = i.sample(r.position[0], r.position[1] + .5 * this.current, r.position[2], this.clock);
593
+ a = e[0] ?? 0, o = e[2] ?? 0;
594
+ } else i && (a = i[0], o = i[1]);
595
+ let s = Math.cos(r.yaw ?? 0), c = Math.sin(r.yaw ?? 0), l = Math.max(-3, Math.min(3, a / e)), u = Math.max(-3, Math.min(3, o / e));
596
+ this.heat.lean[0] = l, this.heat.lean[1] = u, t[n] = s * l - c * u, t[n + 1] = c * l + s * u;
597
+ }
598
+ advanceTime(e) {
599
+ this.disposed || (this.clock += e > 0 ? e : 0);
600
+ }
601
+ resetTime() {
602
+ this.clock = this.current = 0;
603
+ }
604
+ onPreUpdate(e) {
605
+ if (this.disposed) return;
606
+ this.advanceTime(e), this.check(), this.write();
607
+ let t = this.options, n = this.length(), r = this.lookOf(), i = t.footprint ?? [.5 * n, .5 * n], a = this.values;
608
+ this.current = n, d(r.peak, "peak", 1e-6, 1e3);
609
+ let o = Math.max(n, .001), s = C * Math.sqrt(S * o), c = A(i), l = k(c), u = t.yaw ?? 0;
610
+ a[19] = this.clock, a[20] = t.position[0], a[21] = n > 0 ? t.position[1] : -1e6, a[22] = t.position[2], a[23] = o, a[24] = i[0] / 2 * r.size, a[25] = i[1] / 2 * r.size, this.lean(s, a, 26), a[28] = Math.cos(u), a[29] = Math.sin(u), a[30] = l, a[31] = s / l, a[32] = s, a[33] = .22 * o * r.tongues, a[34] = r.flicker, a[35] = ue(r.opacity), a[36] = r.peak * r.radiance * this.coreRadiance(r), a[37] = 0, a[38] = c, a[39] = this.seed, a[40] = .2 * o + .3 * Math.min(i[0], i[1]), a[41] = N * o, a[42] = (t.crowns ? -.46 : -.16) * o, a[43] = n > 0 ? ie : 0;
611
+ let f = t.relief, p = 0, m = 0;
612
+ for (let e = 0; e < 64; e++) {
613
+ let t = f ? f[e] : 0;
614
+ a[ye + e] = t, p = Math.min(p, t), m = Math.max(m, t);
615
+ }
616
+ a[ve] = p, a[129] = m, a[130] = i[0] / 2, a[131] = i[1] / 2, a[me] = Math.floor(this.clock * 240) % 4096, a[113] = t.crowns ?? 0, this.glow(o, i, r, s, a), this.light(o, i, l, c, r.flicker, a[39]);
617
+ }
618
+ lightTotal(e, t, n) {
619
+ let r = this.options, i = z * n.peak * n.radiance * this.coreRadiance(n) * e * (.5 * (t[0] + t[1]) + .24 * e);
620
+ if (r.keyDistance !== void 0) {
621
+ let [e, t, a] = h(Math.max(1e3, Math.min(n.core * .8, 3e3)));
622
+ i = Math.PI * r.keyDistance ** 2 / (.2126 * e + .7152 * t + .0722 * a);
623
+ }
624
+ return i * (r.lightPower ?? 1);
625
+ }
626
+ glow(e, t, n, r, i) {
627
+ let a = this.options, o = Math.cos(a.yaw ?? 0), s = Math.sin(a.yaw ?? 0), c = a.slope?.[0] ?? 0, l = a.slope?.[1] ?? 0, u = c * o - l * s, d = c * s + l * o, f = Math.min(ce, Math.max(1, Math.round(Math.max(t[0], t[1]) / e))), p = this.current > 0 && n.glow > 0, m = p ? n.glow * this.lightTotal(e, t, n) : 0, g = h(Math.max(1e3, Math.min(n.core * .8, 3e3))), _ = (a.glowRadius ?? L) * e, v = Math.max(Math.abs(i[26]), Math.abs(i[27])), y = Math.max(Math.abs(u), Math.abs(d)), b = a.groundAlbedo ?? (a.lights ? 0 : se);
628
+ i[114] = _, i[115] = p ? f : 0, i[he] = u, i[117] = d, i[118] = b, i[119] = n.haze;
629
+ for (let e = 0; e < 3; e++) i[ge + e] = m / f * g[e];
630
+ i[123] = .35 * e, i[_e] = p ? Math.max(i[40], _ * (1 + y + v)) : i[40], i[125] = p ? Math.max(i[41], .35 * e + _ * (1 + y)) : i[41], i[126] = c, i[127] = l;
631
+ }
632
+ light(e, t, n, r, i, a) {
633
+ if (!this.target || this.target.isDisposed) return;
634
+ let o = this.options, s = this.lights, c = t[0] >= t[1], l = c ? t[0] : t[1], u = Math.min(s.length, Math.max(1, Math.round(l / Math.max(e, .001)))), d = this.current > 0, f = this.lookOf(), p = d ? this.lightTotal(e, t, f) : 0, m = Math.cos(o.yaw ?? 0), g = Math.sin(o.yaw ?? 0), _ = h(Math.max(1e3, Math.min(f.core * .8, 3e3)));
635
+ for (let d = 0; d < s.length; d++) {
636
+ let f = s[d], h = u > 1 ? (d / (u - 1) - .5) * l * .8 : 0, v = c ? h : 0, y = c ? 0 : h, b = 6.2831853 * (n * this.clock - .35 * e / (C * Math.sqrt(S * e) / n) + J(h / r + a)), x = Math.max(.15, 1 + i * (.3 * Math.sin(b) + .25 * (J(this.clock * n * 2.7 + d * 5.1 + a) - .5))), w = f.position;
637
+ w[0] = o.position[0] + m * v + g * y, w[1] = o.position[1] + .35 * e + Y(o.relief, v / Math.max(t[0] / 2, 1e-6), y / Math.max(t[1] / 2, 1e-6)), w[2] = o.position[2] - g * v + m * y, f.range = 30 * e, f.radius = .25 * Math.min(e, Math.max(t[0], t[1]) / u + .2 * e), f.intensity = d < u ? p / u * x : 0, f.color = _, this.steady[d] = d < u ? p / u : 0;
638
+ }
639
+ this.target.setLights(s);
640
+ }
641
+ draw(t, l) {
642
+ if (this.disposed) return;
643
+ let u = t.device, d = this.options, f = this.values, p = e(this.view, t.camera);
644
+ if (!l) {
645
+ f.set(t.camera.matrix);
646
+ for (let e = 0; e < 3; e++) f[16 + e] = t.camera.position[e], f[44 + e] = -p[e * 4 + 2];
647
+ this.uniform ??= u.createBuffer({
648
+ size: 784,
649
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
650
+ }), u.queue.writeBuffer(this.uniform, 0, f);
651
+ }
652
+ if (!this.uniform) return;
653
+ let m = r(d.fog ?? null), h = !t.format.includes("float") && !t.format.endsWith("-srgb");
654
+ m && !l && (n(m, t.camera.position, this.fogData), this.fogBuffer ??= u.createBuffer({
655
+ size: 48,
656
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
657
+ }), u.queue.writeBuffer(this.fogBuffer, 0, this.fogData));
658
+ let g = 0, _ = d.depth;
659
+ _ && (this.softRuntime ??= c.create(u, this.owned), l ? g = this.samples : (g = this.samples = this.softRuntime.write(t, _(), p, 1), this.softRuntime.changed && this.groups.clear()));
660
+ let v = t.descriptor.multisample?.count ?? 1, y = t.descriptor.depthStencil.format, b = [
661
+ t.format,
662
+ y,
663
+ v,
664
+ g,
665
+ !!m,
666
+ !!_
667
+ ].join(":"), x = X.get(u);
668
+ x || X.set(u, x = /* @__PURE__ */ new Map());
669
+ let S = x.get(b);
670
+ if (!S) {
671
+ let e = u.createShaderModule({ code: $(g, !!m, h, !!_) }), n = _ ? u.createShaderModule({ code: $(g, !!m, h, !0, !1) }) : e, r = {
672
+ color: {
673
+ srcFactor: "one",
674
+ dstFactor: "one-minus-src-alpha"
675
+ },
676
+ alpha: {
677
+ srcFactor: "one",
678
+ dstFactor: "one-minus-src-alpha"
679
+ }
680
+ }, i = (n, r, i, a, o = e) => u.createRenderPipeline({
681
+ layout: "auto",
682
+ vertex: {
683
+ module: o,
684
+ entryPoint: n
685
+ },
686
+ fragment: {
687
+ module: o,
688
+ entryPoint: r,
689
+ targets: [i]
690
+ },
691
+ primitive: { topology: "triangle-list" },
692
+ multisample: t.descriptor.multisample,
693
+ depthStencil: {
694
+ format: y,
695
+ ...a
696
+ }
697
+ });
698
+ S = [
699
+ i("glowVertex", "glowFragment", {
700
+ format: t.format,
701
+ blend: r
702
+ }, {
703
+ depthWriteEnabled: !1,
704
+ depthCompare: "greater-equal"
705
+ }),
706
+ i("vertex", "fragment", {
707
+ format: t.format,
708
+ blend: r
709
+ }, {
710
+ depthWriteEnabled: !1,
711
+ depthCompare: _ ? "always" : "greater-equal"
712
+ }),
713
+ i("vertex", "depthFragment", {
714
+ format: t.format,
715
+ writeMask: 0
716
+ }, {
717
+ depthWriteEnabled: !0,
718
+ depthCompare: "greater-equal"
719
+ }, n),
720
+ i("vertex", "reactiveFragment", {
721
+ format: "r8unorm",
722
+ blend: s
723
+ }, {
724
+ depthWriteEnabled: !1,
725
+ depthCompare: _ ? "always" : "greater-equal"
726
+ }),
727
+ i("vertex", "coverageFragment", {
728
+ format: "r8unorm",
729
+ blend: s
730
+ }, {
731
+ depthWriteEnabled: !1,
732
+ depthCompare: _ ? "always" : "greater-equal"
733
+ })
734
+ ], x.set(b, S);
735
+ }
736
+ let C = l ? [S[2]] : f[115] > 0 ? [S[0], S[1]] : [S[1]];
737
+ for (let e of S) this.groups.has(e) || this.groups.set(e, u.createBindGroup({
738
+ layout: e.getBindGroupLayout(0),
739
+ entries: [
740
+ {
741
+ binding: 0,
742
+ resource: { buffer: this.uniform }
743
+ },
744
+ ...m && e !== S[2] ? [{
745
+ binding: 5,
746
+ resource: { buffer: this.fogBuffer }
747
+ }] : [],
748
+ ...g && e !== S[2] ? this.softRuntime.entries : []
749
+ ]
750
+ }));
751
+ if (!(this.current > 0) || l && !((this.options.opacity ?? 1) >= 1 && this.lookOf().opacity >= 1)) return;
752
+ this.attachment.view = t.color, this.depthAttachment.view = t.depth, this.depthAttachment.depthReadOnly = !l || void 0, l ? (this.depthAttachment.depthLoadOp = "load", this.depthAttachment.depthStoreOp = "store") : (delete this.depthAttachment.depthLoadOp, delete this.depthAttachment.depthStoreOp), this.depthAttachment.stencilReadOnly = y.includes("stencil") ? !0 : void 0;
753
+ let w = t.encoder.beginRenderPass({
754
+ colorAttachments: [this.attachment],
755
+ depthStencilAttachment: this.depthAttachment
756
+ });
757
+ i(w, t);
758
+ for (let e of C) w.setPipeline(e), w.setBindGroup(0, this.groups.get(e)), w.draw(36), this.engine && this.engine.stats.drawCalls++;
759
+ if (w.end(), l) return;
760
+ (d.reactive ?? a(t, this)) && this.drawMask(t, S[3], o(t));
761
+ let T = this.engine && Q.get(this.engine);
762
+ T && T.watchers > 0 && this.drawMask(t, S[4], xe(T, t));
763
+ }
764
+ drawMask(e, t, n) {
765
+ this.reactiveAttachments[0] = n;
766
+ let r = e.encoder.beginRenderPass({
767
+ colorAttachments: this.reactiveAttachments,
768
+ depthStencilAttachment: this.depthAttachment
769
+ });
770
+ i(r, e), r.setPipeline(t), r.setBindGroup(0, this.groups.get(t)), r.draw(36), r.end(), this.engine && this.engine.stats.drawCalls++;
771
+ }
772
+ setLength(e) {
773
+ return d(e, "length", 0, 1e3), this.options.length = e, this;
774
+ }
775
+ setWind(e) {
776
+ return this.options.wind = e, this;
777
+ }
778
+ get steadyIntensities() {
779
+ return this.steady;
780
+ }
781
+ static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
782
+ v(e, "Flame");
783
+ let r = Z.get(e) ?? 0, i = new t(n, r);
784
+ return e.use(i), Z.set(e, r + 1), i;
785
+ }
786
+ release() {
787
+ for (let e of [
788
+ this.uniform,
789
+ this.fogBuffer,
790
+ ...this.owned
791
+ ]) e?.destroy();
792
+ this.uniform = this.fogBuffer = this.softRuntime = void 0, this.owned = [], this.groups.clear();
793
+ }
794
+ onDeviceRestored(e) {
795
+ this.release(), this.setup(e);
796
+ }
797
+ dispose() {
798
+ this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0, this.group ? this.group.dispose() : this.target && !this.target.isDisposed && this.target.setLights([]), this.release());
799
+ }
800
+ [Symbol.dispose]() {
801
+ this.dispose();
802
+ }
803
+ };
804
+ function we(e, t, n) {
805
+ let r = Math.sqrt(Math.max(t / w, 1)), i = structuredClone(e);
806
+ return i.size *= r, i.rate *= r, i.capacity = Math.ceil(i.capacity * r), i.lifetime = [i.lifetime[0] * r, i.lifetime[1] * r], i.velocity = i.velocity.map((e) => e * r), i.gravity = i.gravity.map((e) => e * r), i.turbulence *= r, i.frequency /= r, {
807
+ ...i,
808
+ emissionExtent: [
809
+ n[0] / 2,
810
+ .02 * t,
811
+ n[1] / 2
812
+ ],
813
+ schedule: { prewarm: i.lifetime[1] }
814
+ };
815
+ }
816
+ //#endregion
817
+ export { C as FLAME_FROUDE, z as FLAME_LIGHT_FILL, _ as FLAME_PEAK, B as FLAME_RADIANT_FRACTION, de as FLAME_RELIEF, Ce as Flame, w as HEARTH_FLAME_LENGTH, D as INCENDIARY_4LB, O as PUFFING_CONSTANT, p as blackbodyChromaticity, h as blackbodyColor, f as blackbodyLuminanceRatio, u as blackbodyLuminousEfficacy, te as burningBed, T as byramFlameLength, ee as byramIntensity, K as flameArea, G as flameCoreRadiance, V as flameLuminousFlux, g as flameRamp, Y as flameReliefAt, E as heskestadFlameLength, H as heskestadHeat, A as puffingDiameter, k as puffingFrequency, we as scaleHearthEmbers, Se as watchFlameCoverage };