@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,709 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const d=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
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 "./frame-BuvMFBPp.js";
7
+ import { t as a } from "./view-rect-DkeiiUtw.js";
8
+ import { n as o, r as s, t as c } from "./reactive-mask-B8xbyzlk.js";
9
+ import { r as l, t as u } from "./compat-BFjXAQuu.js";
10
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
11
+ import { finiteVector as f } from "@margoengine/math/validate";
12
+ import { MargoError as p } from "@margoengine/math/errors";
13
+ //#region ../shared/preset.js
14
+ function m(e, t) {
15
+ let n = globalThis.margoPreset?.[e];
16
+ if (!n) return t;
17
+ let r = { ...t };
18
+ for (let e in n) r[e] ??= n[e];
19
+ return r;
20
+ }
21
+ //#endregion
22
+ //#region ../shared/pass.js
23
+ function h(e, t, n, r, i) {
24
+ let a = e.includes("var<private> margoCoverage:u32");
25
+ return `\n@fragment fn transparent(${t}${a ? ",@builtin(sample_mask) margoMask:u32" : ""})->MargoTransparent {${a ? "margoCoverage=margoMask;" : ""}return margoTransparentAt(${n},${r},${i});}\n`;
26
+ }
27
+ function g() {
28
+ return {
29
+ hook: void 0,
30
+ released: void 0,
31
+ set(e, t) {
32
+ return this.hook = e, this.released = t, () => {
33
+ this.hook === e && (this.hook = this.released = void 0);
34
+ };
35
+ }
36
+ };
37
+ }
38
+ function _(e, t, n, r) {
39
+ let i = t.begin();
40
+ return i.setPipeline(n), i.setBindGroup(0, r), i.setBindGroup(1, e.createBindGroup({
41
+ layout: n.getBindGroupLayout(1),
42
+ entries: t.bindings()
43
+ })), i;
44
+ }
45
+ //#endregion
46
+ //#region src/shader.ts
47
+ var v = i + "\n@group(0) @binding(1) var<storage,read_write> particles:array<vec4f>;\n@group(0) @binding(2) var<storage,read_write> groups:array<u32>;\n@group(0) @binding(3) var<storage,read_write> alive:array<u32>;\nstruct Indirect {vertexCount:u32,instanceCount:atomic<u32>,rest:array<u32,3>}\n@group(0) @binding(4) var<storage,read_write> indirect:Indirect;\n@group(0) @binding(5) var<storage,read> births:array<u32>;\n@group(0) @binding(9) var<storage,read_write> listed:array<u32>;\n@group(0) @binding(10) var<storage,read_write> total:array<u32,5>;\nvar<workgroup> scratch:array<u32,256>;\nfn prefix(l:u32,v:u32)->u32 {scratch[l]=v;workgroupBarrier();for(var o=1u;o<256u;o=o<<1u){var a=0u;if(l>=o){a=scratch[l-o];}workgroupBarrier();scratch[l]+=a;workgroupBarrier();}return scratch[l];}\nfn dies(i:u32)->u32 {return select(0u,1u,i<f.control.x&&particles[i*2].w-f.gravityDelta.w<=0);}\nfn hash(v:u32)->u32 {var x=v;x=(x^(x>>16u))*0x7feb352du;x=(x^(x>>15u))*0x846ca68bu;return x^(x>>16u);}\nfn random(seed:u32)->f32{return f32(hash(seed)>>8u)/16777216.0;}\nfn curlNoise(p:vec3f)->vec3f {\n var curl=vec3f(0);var frequency=f.physics.y;var amplitude=1.0;\n for(var octave=0u;octave<3u;octave++){\n let q=p*frequency+f.emitterTime.w*0.3;\n\n curl+=vec3f(-sin(q.y)-cos(q.z),-sin(q.z)-cos(q.x),-sin(q.x)-cos(q.y))*amplitude;\n frequency*=2.03;amplitude*=0.5;\n }\n return curl*f.physics.x;\n}\nfn update(i:u32,ticket:u32)->u32 {\n let dt=f.gravityDelta.w;\n let at=i*2;let cold=f.control.x*2+at;var p=Particle(particles[at],particles[at+1],vec4f(0),vec4f(0));\n p.positionLife.w-=dt;\n if(p.positionLife.w<=0){\n if(ticket>=f.control.y){particles[at].w=0;return 0u;}\n let seed=hash(hash(ticket)^hash(f.control.z));\n let life=mix(f.shape.y,f.shape.z,random(seed));\n let origin=f.emitterTime.xyz+(vec3f(random(seed+5u),random(seed+6u),random(seed+7u))*2-1)*f.emissionExtent.xyz;\n p.positionLife=vec4f(origin,max(0.0,life-dt));\n p.velocitySize=vec4f(f.velocitySpread.xyz+(vec3f(random(seed+1u),random(seed+2u),random(seed+3u))*2-1)*f.velocitySpread.w,f.shape.x);\n p.color=vec4f(f.color.rgb*f.emissionExtent.w,f.color.a);p.rotation=vec4f(random(seed+4u)*6.2831853,f.physics.w,life,0);\n particles[cold]=p.color;particles[cold+1]=p.rotation;\n }\n p.velocitySize=vec4f(p.velocitySize.xyz+(f.gravityDelta.xyz+curlNoise(p.positionLife.xyz))*dt,p.velocitySize.w);\n if(f.control.w!=0u){let wind=unpack2x16float(f.control.w);let k=1.0-exp(-4.0*dt);p.velocitySize=vec4f(p.velocitySize.x+(wind.x-p.velocitySize.x)*k,p.velocitySize.y,p.velocitySize.z+(wind.y-p.velocitySize.z)*k,p.velocitySize.w);}\n p.positionLife=vec4f(p.positionLife.xyz+p.velocitySize.xyz*dt,p.positionLife.w);\n if(p.positionLife.y-p.velocitySize.w*0.5<f.shape.w){p.positionLife.y=f.shape.w+p.velocitySize.w*0.5;if(p.velocitySize.y<0){p.velocitySize.y=-p.velocitySize.y*f.physics.z;}}\n particles[at]=p.positionLife;particles[at+1]=p.velocitySize;\n if(p.positionLife.w<=0){return 0u;}\n atomicAdd(&indirect.instanceCount,1u);\n let c=f.camera*vec4f(p.positionLife.xyz,1);let m=transpose(f.camera);let r=p.velocitySize.w*1.01;\n if(any(vec4f(c.x-c.w,-c.x-c.w,c.y-c.w,-c.y-c.w)>vec4f(length(m[0].xyz-m[3].xyz),length(m[0].xyz+m[3].xyz),length(m[1].xyz-m[3].xyz),length(m[1].xyz+m[3].xyz))*r+abs(c.w)*1e-5)){return 0u;}\n return 1u;\n}\n@compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u,@builtin(local_invocation_index) l:u32,@builtin(workgroup_id) w:vec3u){\n let d=dies(id.x);let ticket=births[w.x]+prefix(l,d)-d;\n var drawn=0u;if(id.x<f.control.x){drawn=update(id.x,ticket);}\n let rank=prefix(l,drawn);if(drawn==1u){listed[w.x*256u+rank-1u]=id.x;}if(l==255u){groups[w.x]=rank;}\n}\n@compute @workgroup_size(256) fn deaths(@builtin(global_invocation_id) id:vec3u,@builtin(local_invocation_index) l:u32,@builtin(workgroup_id) w:vec3u){\n let d=prefix(l,dies(id.x));if(l==255u){groups[w.x]=d;}\n}\n@compute @workgroup_size(256) fn scan(@builtin(local_invocation_index) l:u32){\n let n=(f.control.x+255u)/256u;let per=(n+255u)/256u;let a=min(l*per,n);let b=min(a+per,n);\n var sum=0u;for(var k=a;k<b;k++){sum+=groups[k];}\n var run=prefix(l,sum)-sum;for(var k=a;k<b;k++){let v=groups[k];groups[k]=run;run+=v;}\n if(l==255u){total[1]=run;}\n}\n@compute @workgroup_size(256) fn compact(@builtin(workgroup_id) w:vec3u,@builtin(local_invocation_index) l:u32){\n let start=groups[w.x];var end=total[1];if(w.x+1u<(f.control.x+255u)/256u){end=groups[w.x+1u];}\n if(l<end-start){alive[start+l]=listed[w.x*256u+l];}\n}", y = i + "\n@group(0) @binding(1) var<storage,read> particles:array<vec4f>;\n@group(0) @binding(2) var<storage,read> alive:array<u32>;\nstruct Out {@builtin(position) clip:vec4f,@location(0) uv:vec2f,@location(1) color:vec4f}\n@vertex fn vertex(@builtin(vertex_index) vertex:u32,@builtin(instance_index) instance:u32)->Out {\n let corners=array<vec2f,3>(vec2f(-1.7320509,-1),vec2f(1.7320509,-1),vec2f(0,2));let corner=corners[vertex];let at=alive[instance]*2;let cold=f.control.x*2+at;let p=Particle(particles[at],particles[at+1],particles[cold],particles[cold+1]);\n let angle=p.rotation.x+p.rotation.y*(p.rotation.z-p.positionLife.w);let cs=cos(angle);let sn=sin(angle);let rotated=vec2f(corner.x*cs-corner.y*sn,corner.x*sn+corner.y*cs);\n\n\n let pixels=p.velocitySize.w*f.up.w/max(abs((f.camera*vec4f(p.positionLife.xyz,1)).w),1e-6);let grow=select(1.0,max(1.0,1.5/pixels),pixels>0);\n let position=p.positionLife.xyz+(f.right.xyz*rotated.x+f.up.xyz*rotated.y)*p.velocitySize.w*0.5*grow;\n var o:Out;o.clip=f.camera*vec4f(position,1);o.uv=corner;o.color=vec4f(p.color.rgb,p.color.a*clamp(p.positionLife.w/p.rotation.z,0.0,1.0)/(grow*grow));return o;\n}\n@fragment fn fragment(i:Out)->@location(0) vec4f {\n let radius=dot(i.uv,i.uv);if(radius>1){discard;}return vec4f(i.color.rgb,i.color.a*(1-radius)*(1-radius));\n}", b = (e) => e.replace("@fragment fn fragment(i:Out)->@location(0) vec4f {", "struct Samples {@location(0) color:vec4f,@builtin(sample_mask) mask:u32}\n@fragment fn fragment(i:Out,@builtin(sample_mask) covered:u32)->Samples {\n let dx=dpdx(i.uv);let dy=dpdy(i.uv);let offsets=array<vec2f,4>(vec2f(-0.125,-0.375),vec2f(0.375,-0.125),vec2f(-0.375,0.125),vec2f(0.125,0.375));var mask=0u;\n for(var s=0u;s<4u;s++){let q=abs(i.uv+dx*offsets[s].x+dy*offsets[s].y);if(max(q.x,q.y)<=1){mask|=1u<<s;}}\n return Samples(shade(i),covered&mask);\n}\nfn shade(i:Out)->vec4f {"), x = (e) => {
48
+ let t = "@fragment fn fragment(i:Out)->@location(0) vec4f {";
49
+ if (e.split(t).length !== 2) throw new p("unsupported", "VFX reactive masks need the billboard shader they were written for");
50
+ return e.replace(t, "fn margoParticle(i:Out)->vec4f {") + "\n@fragment fn reactive(i:Out)->@location(0) vec4f {return vec4f(clamp(margoParticle(i).a,0.0,1.0),0,0,1);}\n";
51
+ }, S = [
52
+ ["@location(0) uv:vec2f,", "@location(0) uv:vec2f,@location(3) margoDepth:f32,"],
53
+ ["o.clip=f.camera*vec4f(position,1);", "o.clip=f.camera*vec4f(position,1);o.margoDepth=-dot(cross(f.right.xyz,f.up.xyz),position)-f.right.w;"],
54
+ ["@fragment fn fragment(i:Out)->@location(0) vec4f {", "fn margoShade(i:Out)->vec4f {"]
55
+ ], C = (e, t) => {
56
+ let n = e;
57
+ for (let [e, t] of S) {
58
+ if (n.split(e).length !== 2) throw new p("unsupported", "VFX transparency needs the billboard shader it was written for; update its edits");
59
+ n = n.replace(e, t);
60
+ }
61
+ return t + n + h(t, "i:Out", "margoShade(i)", "i.clip", "i.margoDepth");
62
+ }, w = [
63
+ ["@location(0) uv:vec2f,", "@location(0) uv:vec2f,@location(7) fogWorld:vec3f,"],
64
+ ["o.clip=f.camera*vec4f(position,1);", "o.clip=f.camera*vec4f(position,1);o.fogWorld=position;"],
65
+ ["@fragment fn fragment(i:Out)->@location(0) vec4f {", "@fragment fn fragment(i:Out)->@location(0) vec4f {return fogged(i,unfogged(i));}\nfn unfogged(i:Out)->vec4f {"]
66
+ ], T = (e) => {
67
+ let n = e;
68
+ for (let [e, t] of w) {
69
+ if (n.split(e).length !== 2) throw new p("unsupported", "VFX fog needs the billboard shader it was written for; update the fog edits");
70
+ n = n.replace(e, t);
71
+ }
72
+ return n + t(0, 5) + "fn fogged(i:Out,c:vec4f)->vec4f {return vec4f(mix(c.rgb,margoFog.color.rgb,fogAmount(i.fogWorld)),c.a);}\n";
73
+ }, E = /* @__PURE__ */ Symbol.for("margoengine.ambientVeils");
74
+ function D(e, t) {
75
+ e[E] || (e[E] = /* @__PURE__ */ new Set());
76
+ let n = e[E];
77
+ return n.add(t), () => {
78
+ n.delete(t);
79
+ };
80
+ }
81
+ //#endregion
82
+ //#region src/base.ts
83
+ var O = 16776960, k = 65504;
84
+ function A(e) {
85
+ return Math.min(O, Math.floor(Math.min(e.maxStorageBufferBindingSize, e.maxBufferSize) / 64), e.maxComputeWorkgroupsPerDimension * 256);
86
+ }
87
+ var j = [
88
+ 0,
89
+ 1,
90
+ 0
91
+ ], M = [
92
+ 0,
93
+ 3,
94
+ 0
95
+ ], N = [
96
+ 0,
97
+ -9.81,
98
+ 0
99
+ ], P = [
100
+ 1,
101
+ .5,
102
+ .1,
103
+ 1
104
+ ], F = [1, 3], I = /* @__PURE__ */ new Float32Array(1), L = new Uint32Array(I.buffer);
105
+ function R(e) {
106
+ I[0] = e;
107
+ let t = L[0], n = t >>> 16 & 32768, r = (t >>> 23 & 255) - 112, i = t & 8388607;
108
+ return r <= 0 ? r < -10 ? n : (i = (i | 8388608) >> 1 - r, n | i + 4096 >> 13) : n | (r << 10) + (i + 4096 >> 13);
109
+ }
110
+ function z(e, t) {
111
+ return (R(t) << 16 | R(e)) >>> 0;
112
+ }
113
+ var B = (e, t, n) => {
114
+ let r = "sample" in e ? e.sample(t[0], t[1], t[2], n) : e, i = r[0], a = "sample" in e ? r[2] : r[1];
115
+ if (!(Math.abs(i) <= 1e3 && Math.abs(a) <= 1e3)) throw RangeError("VFX wind must give finite x and z of at most 1,000 m/s; check the wind option.");
116
+ return [i, a];
117
+ }, V = [
118
+ "ribbons",
119
+ "mesh",
120
+ "events",
121
+ "lit",
122
+ "depthCollide",
123
+ "stretch",
124
+ "fields",
125
+ "skinned",
126
+ "flipbook",
127
+ "shadows",
128
+ "nodes",
129
+ "space",
130
+ "overLife",
131
+ "schedule"
132
+ ], H = 120, U = (e) => ({
133
+ ...e.position && { position: Array.from(e.position) },
134
+ ...e.velocity && { velocity: Array.from(e.velocity) },
135
+ ...e.spread !== void 0 && { spread: e.spread },
136
+ ...e.emissionExtent && { emissionExtent: Array.from(e.emissionExtent) }
137
+ }), W = 0, G = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
138
+ constructor(e = {}, t = {}) {
139
+ this.options = e, this.features = t, this.attachment = {
140
+ view: void 0,
141
+ loadOp: "load",
142
+ storeOp: "store"
143
+ }, this.depthAttachment = {
144
+ view: void 0,
145
+ depthReadOnly: !0
146
+ }, this.presentation = {
147
+ colorAttachments: [this.attachment],
148
+ depthStencilAttachment: this.depthAttachment
149
+ }, this.reactiveAttachments = [], this.veiled = {}, this.ambientVeil = { veil: (e, t) => this.veil(e, t) }, this.reactivePass = {
150
+ colorAttachments: this.reactiveAttachments,
151
+ depthStencilAttachment: this.depthAttachment
152
+ }, this.renderFormat = "", this.renderDepth = "", this.renderKey = "", this.name = "vfx:" + W++, this.renderGroups = /* @__PURE__ */ new Map(), this.renders = /* @__PURE__ */ new Map(), this.billboard = "", this.extraValues = /* @__PURE__ */ new Float32Array(524), this.spriteEntries = [], this.transparency = g(), this.runtimes = [], this.owned = [], this.fogSource = null, this.fogData = /* @__PURE__ */ new Float32Array(12), this.values = /* @__PURE__ */ new Float32Array(56), this.ints = new Uint32Array(this.values.buffer), this.view = /* @__PURE__ */ new Float32Array(16), this.seconds = 0, this.remainder = 0, this.frames = 0, this.burstCount = 0, this.done = 0, this.spawnQueue = [], this.tick = {
153
+ delta: 0,
154
+ time: 0,
155
+ budget: 0,
156
+ frame: 0
157
+ }, this.still = {
158
+ delta: 0,
159
+ time: 0,
160
+ budget: 0,
161
+ frame: 0
162
+ }, this.ticked = !1, this.clocked = !1, this.warm = [], this.held = [], this.routeLoading = !1, this.routeReady = Promise.resolve(), this.disposed = !1;
163
+ let n = m("vfx", e);
164
+ this.options = (n.capacity ?? 0) < (e.initialCount ?? 0) ? e : n, this.capacity = this.options.capacity ?? 1e6, this.validate();
165
+ let r = e.texture;
166
+ if (r) {
167
+ if (!t.texture) throw TypeError("VFX texture needs textureFeature from @margoengine/vfx/texture: pass {texture: textureFeature}");
168
+ this.sprite = t.texture.copy(r);
169
+ }
170
+ }
171
+ static attach(e, n = {}, r = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
172
+ let i = new t(n, r);
173
+ return d(e).use(i), i;
174
+ }
175
+ validate() {
176
+ let e = this.options;
177
+ if (!Number.isInteger(e.initialCount ?? 0) || (e.initialCount ?? 0) < 0 || (e.initialCount ?? 0) > this.capacity) throw RangeError("initialCount must be a whole number up to capacity; pass a smaller count.");
178
+ if (e.emissionExtent) {
179
+ f(e.emissionExtent, 3, "Emission extent");
180
+ for (let t = 0; t < 3; t++) if (e.emissionExtent[t] < 0) throw RangeError("Emission extent must be nonnegative; pass half-sizes of 0 or more.");
181
+ }
182
+ if (e.clock !== void 0 && typeof e.clock?.ticks != "function") throw TypeError("A VFX clock needs ticks(rate); pass a FilmClock from @margoengine/math/clock.");
183
+ if (!Number.isFinite(e.intensity ?? 1) || (e.intensity ?? 1) < 0 || (e.intensity ?? 1) > k) throw RangeError("Particle intensity must be between 0 and 65,504; pass a value in range.");
184
+ if (!Number.isInteger(this.capacity) || this.capacity < 1 || this.capacity > O || e.capacity !== void 0 && e.capacity !== this.capacity || !Number.isFinite(e.rate ?? this.capacity / 2) || (e.rate ?? this.capacity / 2) < 0 || !Number.isFinite(Math.fround(e.ground ?? 0)) || !Number.isFinite(Math.fround(e.angularVelocity ?? 0)) || !Number.isFinite(e.bounce ?? .3) || (e.bounce ?? .3) < 0 || (e.bounce ?? .3) > 1 || !Number.isFinite(Math.fround(e.size ?? .1)) || (e.size ?? .1) <= 0) throw RangeError("VFX capacity, rate, bounce, size or ground is out of range; check the options.");
185
+ for (let t = 0; t < 3; t++) {
186
+ let n = t === 0 ? e.spread ?? 1 : t === 1 ? e.turbulence ?? .5 : e.frequency ?? 1;
187
+ if (!Number.isFinite(Math.fround(n)) || n < 0) throw RangeError("VFX spread, turbulence and frequency must be 0 or more; check the options.");
188
+ }
189
+ if (e.wind !== void 0) {
190
+ if (typeof e.wind != "object" || e.wind === null || !("sample" in e.wind) && !Array.isArray(e.wind)) throw TypeError("VFX wind must be [x, z] in m/s or an object with sample(x, y, z, t); check the wind option.");
191
+ "sample" in e.wind || B(e.wind, j, 0);
192
+ }
193
+ f(e.position ?? j, 3, "Emitter position"), f(e.velocity ?? M, 3, "Emitter velocity"), f(e.gravity ?? N, 3, "Particle gravity"), f(e.color ?? P, 4, "Particle color");
194
+ let t = e.color ?? P;
195
+ for (let e = 0; e < 4; e++) if (t[e] < 0 || t[e] > 1) throw RangeError("Particle color channels must be between 0 and 1; pass [r, g, b, a].");
196
+ let n = e.lifetime ?? F;
197
+ if (f(n, 2, "Particle lifetime"), n[0] <= 0 || n[1] < n[0]) throw RangeError("Particle lifetime must be [min, max] seconds, 0 < min <= max; pass both.");
198
+ if (e.sort !== void 0 && typeof e.sort != "boolean" || e.wrap !== void 0 && typeof e.wrap != "boolean" || e.wrap && !e.emissionExtent?.every((e) => e > 0)) throw RangeError("VFX sort and wrap must be booleans; wrap needs emissionExtent above 0.");
199
+ if (e.reactive !== void 0 && typeof e.reactive != "boolean") throw TypeError("VFX reactive must be true or false; pass a boolean");
200
+ if (e.softness !== void 0 && (!Number.isFinite(Math.fround(e.softness)) || e.softness < 0)) throw RangeError("VFX softness must be a world distance of 0 or more; pass e.g. 0.5, or 0 to turn it off.");
201
+ if (e.depth !== void 0 && typeof e.depth != "function") throw TypeError("VFX depth must be a function returning the scene depth texture; pass () => depth.texture");
202
+ if (e.softness) {
203
+ if (!this.features.soft) throw TypeError("VFX softness needs soft from @margoengine/vfx/soft: pass {soft}");
204
+ if (!e.depth) throw TypeError("VFX softness needs scene depth: add DepthTexture from @margoengine/post/depth and pass depth: () => depth.texture");
205
+ }
206
+ for (let t of [
207
+ "obstacles",
208
+ "wrap",
209
+ "sort",
210
+ "curves",
211
+ "emitter"
212
+ ]) if (e[t] && !this.features[t]) throw TypeError(`VFX ${t} needs ${t} from @margoengine/vfx/${t}`);
213
+ if (e.curves && (this.features.curves.validate(e.curves), !e.curves.size && !e.curves.alpha && !e.curves.color)) throw TypeError("VFX curves needs at least one of size, alpha or color; pass e.g. {size: [0, 1, 0]}");
214
+ if (e.emitter) {
215
+ if (this.features.emitter.validate(e.emitter, !this.emitterShape), this.emitterShape && e.emitter.shape !== this.emitterShape) throw TypeError(`VFX emitter shape is fixed at attach (${this.emitterShape}); attach a new VFX to change it`);
216
+ } else if (this.emitterShape) throw TypeError("VFX emitter was set at attach and cannot be removed; attach a new VFX to drop it");
217
+ e.obstacles && this.features.obstacles.write(e.obstacles, this.extraValues);
218
+ for (let t of V) {
219
+ let n = e[t], r = this.features[t];
220
+ if (n !== void 0) {
221
+ if (!r) throw TypeError(`VFX ${t} needs ${t} from @margoengine/vfx/${t.replace(/[A-Z]/g, (e) => "-" + e.toLowerCase())}: pass {${t}}`);
222
+ r.validate(n, e);
223
+ }
224
+ }
225
+ }
226
+ get fog() {
227
+ return this.fogSource;
228
+ }
229
+ setFog(e) {
230
+ return this.fogSource = r(e), this;
231
+ }
232
+ burst(e, t) {
233
+ if (this.disposed) throw new p("disposed", "VFX is disposed; attach a new VFX to burst it");
234
+ if (!Number.isInteger(e) || e < 0 || e > this.capacity) throw RangeError("Particle burst must be a whole number up to capacity; pass a smaller count.");
235
+ return t ? e > 0 && this.spawnQueue.push({
236
+ count: e,
237
+ spawn: U(t)
238
+ }) : this.burstCount = Math.min(this.capacity, this.burstCount + e), this;
239
+ }
240
+ reset() {
241
+ if (this.disposed) throw new p("disposed", "VFX is disposed; attach a new VFX to reset it");
242
+ this.seconds = this.remainder = this.frames = this.burstCount = this.done = 0, this.spawnQueue.length = 0;
243
+ for (let e of [this.tick, this.still]) e.delta = e.time = e.budget = e.frame = 0;
244
+ if (this.ticked = this.clocked = !1, this.warm = [], this.held.length = 0, !this.device) return this;
245
+ this.burstCount = this.options.initialCount ?? 0;
246
+ for (let e of [this.particles, this.alive]) this.device.queue.writeBuffer(e, 0, new Uint8Array(e.size));
247
+ for (let e of [this.indirect, this.visible]) this.device.queue.writeBuffer(e, 0, new Uint32Array([
248
+ 3,
249
+ 0,
250
+ 0,
251
+ 0,
252
+ 0
253
+ ]));
254
+ for (let e of this.runtimes) e.reset?.();
255
+ return this;
256
+ }
257
+ resetTime() {
258
+ this.disposed || this.reset();
259
+ }
260
+ setWind(e) {
261
+ let t = this.options.wind;
262
+ this.options.wind = e;
263
+ try {
264
+ this.validate();
265
+ } catch (e) {
266
+ throw this.options.wind = t, e;
267
+ }
268
+ return this;
269
+ }
270
+ setup(e) {
271
+ if (this.disposed || !e.gpu) throw new p(this.disposed ? "disposed" : "unsupported", "VFX requires WebGPU and a live effect; attach a new VFX to a WebGPU engine; see https://margoengine.com/errors#webgpu");
272
+ this.engine = e, this.device = e.gpu, this.leaveVeils = D(this.device, this.ambientVeil);
273
+ let t = this.device.limits;
274
+ if (this.capacity * 64 > t.maxStorageBufferBindingSize || this.capacity * 64 > t.maxBufferSize || Math.ceil(this.capacity / 256) > t.maxComputeWorkgroupsPerDimension || this.options.sort && 2 ** Math.ceil(Math.log2(this.capacity)) / 256 > t.maxComputeWorkgroupsPerDimension || t.maxComputeInvocationsPerWorkgroup < 256 || t.maxComputeWorkgroupSizeX < 256) throw RangeError(`VFX capacity ${this.capacity} exceeds the device limits (64 bytes a slot: at most ${A(t)} here); reduce capacity.`);
275
+ let n = (e, t) => this.device.createBuffer({
276
+ size: e,
277
+ usage: t | GPUBufferUsage.COPY_DST
278
+ }), r = this.options, i = this.features;
279
+ this.uniform = n(224, GPUBufferUsage.UNIFORM), this.particles = n(this.capacity * 64, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC);
280
+ let a = r.sort ? 2 ** Math.ceil(Math.log2(this.capacity)) : this.capacity;
281
+ (r.obstacles || r.sort) && (this.extra = n(2096, GPUBufferUsage.UNIFORM));
282
+ let o = v;
283
+ r.obstacles && (o = i.obstacles.patch(o)), r.wrap && (o = i.wrap.patch(o)), r.emitter && (o = i.emitter.patch(o, r.emitter), this.emitterRuntime = i.emitter.create(this.device, r.emitter, this.owned), this.emitterShape = r.emitter.shape), this.billboard = this.sprite ? i.texture.patch(y, this.sprite) : y, r.curves && (this.billboard = i.curves.patch(this.billboard, r.curves), this.curveRuntime = i.curves.create(this.device, this.owned));
284
+ let s = V.flatMap((e) => r[e] === void 0 ? [] : [[i[e], r[e]]]);
285
+ for (let [e, t] of s) e.simulation && (o = e.simulation(o, t)), e.billboard && (this.billboard = e.billboard(this.billboard, t));
286
+ this.alive = n(a * 4, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC), this.indirect = n(20, GPUBufferUsage.STORAGE | GPUBufferUsage.INDIRECT | GPUBufferUsage.COPY_SRC), this.respawn = n(20, GPUBufferUsage.STORAGE), this.visible = n(20, GPUBufferUsage.STORAGE | GPUBufferUsage.INDIRECT | GPUBufferUsage.COPY_SRC);
287
+ for (let e of [this.indirect, this.visible]) this.device.queue.writeBuffer(e, 0, new Uint32Array([
288
+ 3,
289
+ 0,
290
+ 0,
291
+ 0,
292
+ 0
293
+ ]));
294
+ let c = Math.ceil(this.capacity / 256), d = (e) => {
295
+ let t = n(e, GPUBufferUsage.STORAGE);
296
+ return this.owned.push(t), t;
297
+ }, f = d(c * 4), m = d(c * 4), h = d(c * 1024), g = this.device.createShaderModule({ code: o }), _ = this.uniform, b = (e, t, n = []) => {
298
+ let r = this.device.createComputePipeline({
299
+ layout: "auto",
300
+ compute: {
301
+ module: g,
302
+ entryPoint: e
303
+ }
304
+ });
305
+ return [r, this.device.createBindGroup({
306
+ layout: r.getBindGroupLayout(0),
307
+ entries: [...t.flatMap((e, t) => e ? [{
308
+ binding: t,
309
+ resource: { buffer: e }
310
+ }] : []), ...n]
311
+ })];
312
+ }, x = {
313
+ device: this.device,
314
+ capacity: this.capacity,
315
+ uniform: _,
316
+ particles: this.particles,
317
+ alive: this.alive,
318
+ visible: this.visible,
319
+ indirect: this.indirect,
320
+ module: g,
321
+ options: r,
322
+ stats: this.engine.stats,
323
+ engine: this.engine
324
+ };
325
+ this.runtimes = s.flatMap(([e, t]) => e.create ? [e.create(x, t, this.owned)] : []);
326
+ let S = this.runtimes.flatMap((e) => e.compute ?? []);
327
+ [this.simulation, this.computeGroup] = b("main", [
328
+ _,
329
+ this.particles,
330
+ m,
331
+ 0,
332
+ this.indirect,
333
+ f,
334
+ 0,
335
+ 0,
336
+ 0,
337
+ h
338
+ ], [
339
+ ...r.obstacles ? [{
340
+ binding: 6,
341
+ resource: { buffer: this.extra }
342
+ }] : [],
343
+ ...this.emitterRuntime?.entries ?? [],
344
+ ...S
345
+ ]), this.births = [b("deaths", [
346
+ _,
347
+ this.particles,
348
+ f
349
+ ]), b("scan", [
350
+ _,
351
+ 0,
352
+ f,
353
+ 0,
354
+ 0,
355
+ 0,
356
+ 0,
357
+ 0,
358
+ 0,
359
+ 0,
360
+ this.respawn
361
+ ])], this.listing = [b("scan", [
362
+ _,
363
+ 0,
364
+ m,
365
+ 0,
366
+ 0,
367
+ 0,
368
+ 0,
369
+ 0,
370
+ 0,
371
+ 0,
372
+ this.visible
373
+ ]), b("compact", [
374
+ _,
375
+ 0,
376
+ m,
377
+ this.alive,
378
+ 0,
379
+ 0,
380
+ 0,
381
+ 0,
382
+ 0,
383
+ h,
384
+ this.visible
385
+ ])], r.sort && (this.sorter = i.sort.create(this.device, this.particles, this.alive, this.visible, this.extra, a, this.owned)), this.sprite && (this.spriteEntries = i.texture.create(this.device, this.sprite, this.owned)), this.burst(this.options.initialCount ?? 0);
386
+ let C = this.particles, w = u(this.device, () => import("./compat-CVSEj1Bp.js"));
387
+ w && (this.routeLoading = !0), w?.then((e) => {
388
+ this.particles === C && (this.route = {
389
+ kernels: new e.SimulationRoute(this.device, o, this.capacity, {
390
+ uniform: _,
391
+ particles: C,
392
+ alive: this.alive,
393
+ indirect: this.indirect,
394
+ visible: this.visible,
395
+ events: this.runtimes.find((e) => e.eventList)?.eventList
396
+ }, [
397
+ ...r.obstacles ? [{
398
+ binding: 6,
399
+ resource: { buffer: this.extra }
400
+ }] : [],
401
+ ...this.emitterRuntime?.entries ?? [],
402
+ ...S
403
+ ], this.owned),
404
+ billboard: e.countedBillboard,
405
+ binding: e.COUNT_BINDING
406
+ }, this.renders.clear(), this.renderGroups.clear());
407
+ }).finally(() => {
408
+ this.particles === C && (this.routeLoading = !1);
409
+ }), this.routeReady = l(w);
410
+ }
411
+ get ready() {
412
+ return this.routeReady;
413
+ }
414
+ registerPasses(e) {
415
+ e.add({
416
+ name: this.name + ":simulate",
417
+ order: -8,
418
+ execute: (e) => {
419
+ e.backend === "webgpu" && this.simulate(e);
420
+ }
421
+ }), e.add({
422
+ name: this.name + ":draw",
423
+ order: 6,
424
+ execute: (e) => {
425
+ e.backend === "webgpu" && this.draw(e);
426
+ }
427
+ });
428
+ }
429
+ get time() {
430
+ return this.seconds;
431
+ }
432
+ advanceTime(e) {
433
+ if (!this.disposed) {
434
+ if (this.validate(), !(e > 0) || this.options.clock) this.clocked = !0;
435
+ else {
436
+ this.warm.length = 0;
437
+ for (let e of this.runtimes) e.warmup?.((e) => this.warm.push(this.countStep(e, {})));
438
+ this.takeSpawns(), this.countStep(e, this.tick), this.ticked = this.clocked = !0;
439
+ }
440
+ }
441
+ }
442
+ takeSpawns() {
443
+ for (let { count: e, spawn: t } of this.spawnQueue) this.warm.push({
444
+ delta: 0,
445
+ time: this.seconds,
446
+ budget: e,
447
+ frame: this.frames++,
448
+ spawn: t
449
+ });
450
+ this.spawnQueue.length = 0;
451
+ }
452
+ onPreUpdate(e) {
453
+ this.advanceTime(e);
454
+ }
455
+ countStep(e, t, n = !1, r) {
456
+ let i = this.options;
457
+ this.seconds = r ?? this.seconds + e;
458
+ let a = this.runtimes.reduce((t, n) => t + (n.emission?.(e) ?? 0), 0), o = (i.rate ?? this.capacity / 2) * e + this.remainder;
459
+ return this.remainder = o % 1, t.budget = Math.min(this.capacity, Math.floor(o) + this.burstCount + a), this.burstCount = 0, t.delta = e, t.time = this.seconds, t.frame = n && !t.budget ? this.frames : this.frames++, t;
460
+ }
461
+ simulate(e) {
462
+ if (this.validate(), this.options.clock) this.countTicks(this.options.clock);
463
+ else if (this.clocked) this.ticked || (this.takeSpawns(), this.countStep(0, this.still, !0));
464
+ else {
465
+ this.warm.length = 0;
466
+ for (let e of this.runtimes) e.warmup?.((e) => this.warm.push(this.countStep(e, {})));
467
+ this.takeSpawns(), this.countStep(e.delta, this.tick), this.ticked = !0;
468
+ }
469
+ if (this.routeLoading && (this.warm.length || this.held.length)) {
470
+ this.held.push(...this.warm), this.warm.length = 0, this.ticked ? this.held.push({ ...this.tick }) : this.still.budget && this.held.push({ ...this.still }), this.ticked = !1;
471
+ return;
472
+ }
473
+ this.held.length && (this.warm.unshift(...this.held), this.held.length = 0);
474
+ for (let t of this.warm) {
475
+ let n = e.device.createCommandEncoder();
476
+ this.simulateStep({
477
+ ...e,
478
+ delta: t.delta,
479
+ encoder: n
480
+ }, t), e.device.queue.submit([n.finish()]);
481
+ }
482
+ this.warm.length = 0;
483
+ let t = e.delta, n = this.ticked, r = n ? this.tick : this.still;
484
+ this.ticked = !1, (this.options.clock || !n) && (e.delta = r.delta);
485
+ try {
486
+ this.simulateStep(e, r);
487
+ } finally {
488
+ e.delta = t;
489
+ }
490
+ }
491
+ countTicks(e) {
492
+ let t = e.ticks(120);
493
+ if (!Number.isSafeInteger(t) || t < 0) throw RangeError(`A VFX clock gives whole ticks from 0; got ${t}. Pass a FilmClock.`);
494
+ t < this.done && this.reset(), this.warm.length = 0;
495
+ for (let e of this.runtimes) e.warmup?.((e) => this.warm.push(this.countStep(e, {})));
496
+ if (this.takeSpawns(), t === this.done) this.countStep(0, this.still, !0);
497
+ else {
498
+ for (; this.done < t - 1;) this.warm.push(this.countTick({}));
499
+ this.countTick(this.tick), this.ticked = !0;
500
+ }
501
+ }
502
+ countTick(e) {
503
+ this.done++;
504
+ let t = this.options, n = this.done / 120;
505
+ return t.tick ? (t.tick(n), this.countStep(1 / 120, e, !1, n), e.spawn = U({
506
+ position: t.position ?? j,
507
+ velocity: t.velocity ?? M,
508
+ spread: t.spread ?? 1,
509
+ emissionExtent: t.emissionExtent ?? [
510
+ 0,
511
+ 0,
512
+ 0
513
+ ]
514
+ }), e) : (e.spawn = void 0, this.countStep(1 / 120, e, !1, n));
515
+ }
516
+ simulateStep(t, n) {
517
+ let r = this.options, i = n.budget;
518
+ this.values.set(t.camera.matrix), e(this.view, t.camera);
519
+ let a = n.spawn, o = a?.position ?? r.position ?? j;
520
+ for (let e = 0; e < 3; e++) this.values[16 + e] = this.view[e * 4], this.values[20 + e] = this.view[e * 4 + 1], this.values[48 + e] = a?.emissionExtent?.[e] ?? r.emissionExtent?.[e] ?? 0;
521
+ this.values[19] = this.transparency.hook ? this.view[14] : 0;
522
+ let s = this.values, c = s[0] * s[20] + s[4] * s[21] + s[8] * s[22], l = s[1] * s[20] + s[5] * s[21] + s[9] * s[22];
523
+ s[23] = Math.sqrt(c * c + l * l) * t.height * .5, this.values.set(r.gravity ?? N, 24), this.values[27] = n.delta, this.values.set(o, 28), this.values[31] = n.time, this.values.set(a?.velocity ?? r.velocity ?? M, 32), this.values[35] = a?.spread ?? r.spread ?? 1, this.values.set(r.color ?? P, 36);
524
+ let u = r.lifetime ?? F;
525
+ if (this.values[40] = r.size ?? .1, this.values[41] = u[0], this.values[42] = u[1], this.values[43] = r.ground ?? 0, this.values[44] = r.turbulence ?? .5, this.values[45] = r.frequency ?? 1, this.values[46] = r.bounce ?? .3, this.values[47] = r.angularVelocity ?? 0, this.values[51] = r.intensity ?? 1, this.ints[52] = this.capacity, this.ints[53] = i, this.ints[54] = n.frame, r.wind) {
526
+ let [e, t] = B(r.wind, [
527
+ o[0],
528
+ o[1],
529
+ o[2]
530
+ ], n.time);
531
+ this.ints[55] = z(e, t);
532
+ } else this.ints[55] = 0;
533
+ if (t.device.queue.writeBuffer(this.uniform, 0, this.values), this.extra) {
534
+ let e = this.extraValues;
535
+ e.set(t.camera.position);
536
+ for (let t = 0; t < 3; t++) e[4 + t] = -this.view[t * 4 + 2];
537
+ t.device.queue.writeBuffer(this.extra, 0, e);
538
+ }
539
+ r.curves && this.curveRuntime.write(t.device.queue, r.curves), r.emitter && this.emitterRuntime.write(t.device.queue, r.emitter);
540
+ for (let e of this.runtimes) e.write?.(t);
541
+ this.route || t.encoder.clearBuffer(this.indirect, 4, 4);
542
+ let d = t.encoder.beginComputePass(), f = Math.ceil(this.capacity / 256), p = ([e, t], n) => {
543
+ d.setPipeline(e), d.setBindGroup(0, t), d.dispatchWorkgroups(n);
544
+ }, [m, h] = this.births, [g, _] = this.listing, v = i > 0 || this.runtimes.some((e) => e.births);
545
+ this.route ? this.route.kernels.encode(d, v) : (v && (p(m, f), p(h, 1)), p([this.simulation, this.computeGroup], f), p(g, 1), p(_, f)), this.sorter?.run(d);
546
+ for (let e of this.runtimes) this.route && e.eventList || e.run?.(d);
547
+ d.end();
548
+ }
549
+ prepare(e, t, r = !1, i) {
550
+ (this.renderFormat !== e.format || this.renderDepth !== e.descriptor.depthStencil.format) && (this.renderFormat = e.format, this.renderDepth = e.descriptor.depthStencil.format, this.renderKey = this.renderFormat + ":" + this.renderDepth);
551
+ let a = this.fogSource;
552
+ a && (n(a, e.camera.position, this.fogData, !e.format.includes("float") && !e.format.endsWith("-srgb")), this.fogBuffer ??= e.device.createBuffer({
553
+ size: 48,
554
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
555
+ }), e.device.queue.writeBuffer(this.fogBuffer, 0, this.fogData));
556
+ let o = this.options.softness ?? 0, s = 0;
557
+ o > 0 && (this.softRuntime ??= this.features.soft.create(e.device, this.owned), s = this.softRuntime.write(e, this.options.depth(), this.view, o), this.softRuntime.changed && this.renderGroups.clear());
558
+ let l = t?.surface(1), u = t ? 2 : 1, d = this.renderKey + ":" + e.descriptor.multisample?.count + (a ? ":fog" : "") + (s ? ":soft" + s : "") + (l ?? "") + (r ? ":reactive" : "") + (i ? ":veil:" + i : "");
559
+ for (let e of this.runtimes) e.key && (d += e.key(u));
560
+ let f = this.renders.get(d);
561
+ if (!f) {
562
+ let n = s ? this.features.soft.patch(this.billboard, s) : this.billboard, o = a ? T(n) : n, p = this.route ? this.route.billboard(o) : o, m = r ? p : l ? C(p, l) : (e.descriptor.multisample?.count ?? 1) > 1 ? b(p) : p;
563
+ for (let e of this.runtimes) e.shade && (m = e.shade(m, u));
564
+ r && (m = x(m));
565
+ let h = e.device.createShaderModule({ code: m });
566
+ f = e.device.createRenderPipeline({
567
+ layout: "auto",
568
+ vertex: {
569
+ module: h,
570
+ entryPoint: "vertex"
571
+ },
572
+ fragment: {
573
+ module: h,
574
+ entryPoint: r ? "reactive" : t ? "transparent" : "fragment",
575
+ ...t?.blends ? { constants: { margoKind: 0 } } : {},
576
+ targets: r ? [{
577
+ format: "r8unorm",
578
+ blend: c
579
+ }] : t ? [...t.targets] : [{
580
+ format: i ?? e.format,
581
+ blend: {
582
+ color: {
583
+ srcFactor: i ? "zero" : "src-alpha",
584
+ dstFactor: "one-minus-src-alpha"
585
+ },
586
+ alpha: {
587
+ srcFactor: "one",
588
+ dstFactor: "one-minus-src-alpha"
589
+ }
590
+ }
591
+ }]
592
+ },
593
+ multisample: e.descriptor.multisample,
594
+ depthStencil: {
595
+ format: e.descriptor.depthStencil.format,
596
+ depthWriteEnabled: !1,
597
+ depthCompare: "greater-equal"
598
+ }
599
+ }), this.renders.set(d, f);
600
+ }
601
+ let p = this.renderGroups.get(f);
602
+ return p || (p = e.device.createBindGroup({
603
+ layout: f.getBindGroupLayout(0),
604
+ entries: [
605
+ ...[
606
+ this.uniform,
607
+ this.particles,
608
+ this.alive
609
+ ].map((e, t) => ({
610
+ binding: t,
611
+ resource: { buffer: e }
612
+ })),
613
+ ...this.spriteEntries,
614
+ ...this.curveRuntime?.entries ?? [],
615
+ ...this.runtimes.flatMap((e) => e.render ?? []),
616
+ ...a ? [{
617
+ binding: 5,
618
+ resource: { buffer: this.fogBuffer }
619
+ }] : [],
620
+ ...s ? this.softRuntime.entries : [],
621
+ ...this.route ? [{
622
+ binding: this.route.binding,
623
+ resource: { buffer: this.visible }
624
+ }] : []
625
+ ]
626
+ }), this.renderGroups.set(f, p)), [f, p];
627
+ }
628
+ draw(e) {
629
+ this.runtimes.some((e) => e.hides?.()) || (this.transparency.hook ? this.visible && this.drawReactive(e) : this.billboards(e));
630
+ for (let t of this.runtimes) t.draw?.(e);
631
+ }
632
+ billboards(e) {
633
+ let [t, n] = this.prepare(e);
634
+ this.attachment.view = e.color, this.depthAttachment.view = e.depth, this.depthAttachment.stencilReadOnly = e.descriptor.depthStencil.format.includes("stencil") ? !0 : void 0;
635
+ let r = e.encoder.beginRenderPass(this.presentation);
636
+ r.setPipeline(t), r.setBindGroup(0, n);
637
+ for (let e of this.runtimes) e.bind?.(r, t, 1);
638
+ a(r, e), this.route ? r.draw(3, this.capacity) : r.drawIndirect(this.visible, 0), r.end(), this.engine.stats.drawCalls++;
639
+ let i = this.veiled;
640
+ i.encoder = e.encoder, i.depth = e.depth, i.view = e.view, this.drawReactive(e);
641
+ }
642
+ veil(e, t) {
643
+ let n = this.veiled;
644
+ if (!this.visible || e.encoder !== n.encoder || e.depth !== n.depth || e.view !== n.view) return !1;
645
+ let [r, i] = this.prepare(e, void 0, !1, t);
646
+ this.attachment.view = e.color, this.depthAttachment.view = e.depth, this.depthAttachment.stencilReadOnly = e.descriptor.depthStencil.format.includes("stencil") ? !0 : void 0;
647
+ let o = e.encoder.beginRenderPass(this.presentation);
648
+ o.setPipeline(r), o.setBindGroup(0, i);
649
+ for (let e of this.runtimes) e.bind?.(o, r, 1);
650
+ return a(o, e), this.route ? o.draw(3, this.capacity) : o.drawIndirect(this.visible, 0), o.end(), !0;
651
+ }
652
+ drawReactive(e) {
653
+ if (!(this.options.reactive ?? o(e, this))) return;
654
+ let [t, n] = this.prepare(e, void 0, !0);
655
+ this.reactiveAttachments[0] = s(e), this.depthAttachment.view = e.depth, this.depthAttachment.stencilReadOnly = e.descriptor.depthStencil.format.includes("stencil") ? !0 : void 0;
656
+ let r = e.encoder.beginRenderPass(this.reactivePass);
657
+ r.setPipeline(t), r.setBindGroup(0, n);
658
+ for (let e of this.runtimes) e.bind?.(r, t, 1);
659
+ a(r, e), this.route ? r.draw(3, this.capacity) : r.drawIndirect(this.visible, 0), r.end(), this.engine.stats.drawCalls++;
660
+ }
661
+ setTransparency(e, t) {
662
+ return this.transparency.set(e, t);
663
+ }
664
+ drawTransparent(e, t) {
665
+ if (!this.visible || this.runtimes.some((e) => e.hides?.())) return;
666
+ let [n, r] = this.prepare(e, t), i = _(e.device, t, n, r);
667
+ for (let e of this.runtimes) e.bind?.(i, n, 2);
668
+ this.route ? i.draw(3, this.capacity) : i.drawIndirect(this.visible, 0), this.engine.stats.drawCalls++;
669
+ }
670
+ get gpuState() {
671
+ if (!this.indirect || !this.particles || !this.alive || !this.visible) throw new p("state", "VFX is not attached; attach it to an engine first");
672
+ return {
673
+ particles: this.particles,
674
+ indirect: this.indirect,
675
+ alive: this.alive,
676
+ visible: this.visible
677
+ };
678
+ }
679
+ release() {
680
+ this.leaveVeils?.(), this.leaveVeils = this.veiled.encoder = this.veiled.depth = this.veiled.view = void 0;
681
+ for (let e of this.runtimes) e.dispose?.();
682
+ for (let e of [
683
+ this.uniform,
684
+ this.particles,
685
+ this.alive,
686
+ this.indirect,
687
+ this.visible,
688
+ this.respawn,
689
+ this.extra,
690
+ this.fogBuffer,
691
+ ...this.owned
692
+ ]) e?.destroy();
693
+ this.fogBuffer = void 0, this.softRuntime = void 0, this.owned = [], this.spriteEntries = [], this.curveRuntime = this.emitterRuntime = void 0, this.extra = this.sorter = this.route = void 0, this.held.length = 0, this.routeLoading = !1, this.runtimes = [], this.uniform = this.particles = this.alive = this.indirect = this.visible = this.respawn = void 0, this.computeGroup = this.births = this.listing = void 0, this.renderGroups.clear(), this.renders.clear(), this.simulation = void 0;
694
+ }
695
+ onDeviceRestored(e) {
696
+ this.release(), this.setup(e);
697
+ }
698
+ dispose() {
699
+ this.disposed || (this.disposed = !0, this.transparency.released?.(), this.engine?.sceneOf(this)?.removePlugin(this.name), this.release(), this.engine = this.device = void 0);
700
+ }
701
+ [Symbol.dispose ?? Symbol()]() {
702
+ this.dispose();
703
+ }
704
+ destroy() {
705
+ this.dispose();
706
+ }
707
+ };
708
+ //#endregion
709
+ export { H as n, G as t };