@margoengine/vfx 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1642 -3
  4. package/dist/base-C9upJytz.js +658 -0
  5. package/dist/base.d.ts +767 -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 +229 -0
  10. package/dist/clouds.js +566 -0
  11. package/dist/compat-CVSEj1Bp.js +217 -0
  12. package/dist/compat.d.ts +59 -0
  13. package/dist/curves.d.ts +11 -0
  14. package/dist/curves.js +59 -0
  15. package/dist/debris.d.ts +111 -0
  16. package/dist/debris.js +258 -0
  17. package/dist/depth-collide.d.ts +7 -0
  18. package/dist/depth-collide.js +96 -0
  19. package/dist/emitter.d.ts +10 -0
  20. package/dist/emitter.js +125 -0
  21. package/dist/events.d.ts +9 -0
  22. package/dist/events.js +131 -0
  23. package/dist/extra-CgGjbojB.js +4 -0
  24. package/dist/extra.d.ts +2 -0
  25. package/dist/fields.d.ts +11 -0
  26. package/dist/fields.js +95 -0
  27. package/dist/fire-front.d.ts +539 -0
  28. package/dist/fire-front.js +840 -0
  29. package/dist/fire.d.ts +119 -0
  30. package/dist/fire.js +173 -0
  31. package/dist/flame-ramp-VoBKpYnM.js +75 -0
  32. package/dist/flame-ramp.d.ts +61 -0
  33. package/dist/flame.d.ts +260 -0
  34. package/dist/flame.js +605 -0
  35. package/dist/flipbook.d.ts +7 -0
  36. package/dist/flipbook.js +23 -0
  37. package/dist/fog-B-kBv7g0.js +32 -0
  38. package/dist/frame-BuvMFBPp.js +4 -0
  39. package/dist/frame.d.ts +7 -0
  40. package/dist/gunfire.d.ts +696 -0
  41. package/dist/gunfire.js +1204 -0
  42. package/dist/hash-Btsp6fGK.js +7 -0
  43. package/dist/hash.d.ts +2 -0
  44. package/dist/index.d.ts +17 -0
  45. package/dist/index.js +60 -0
  46. package/dist/json.d.ts +53 -0
  47. package/dist/json.js +237 -0
  48. package/dist/lit.d.ts +11 -0
  49. package/dist/lit.js +105 -0
  50. package/dist/mesh.d.ts +14 -0
  51. package/dist/mesh.js +225 -0
  52. package/dist/node-system.d.ts +315 -0
  53. package/dist/node-system.js +384 -0
  54. package/dist/nodes.d.ts +14 -0
  55. package/dist/nodes.js +293 -0
  56. package/dist/obstacles.d.ts +3 -0
  57. package/dist/obstacles.js +25 -0
  58. package/dist/over-life.d.ts +14 -0
  59. package/dist/over-life.js +93 -0
  60. package/dist/require-webgpu--tUXPsmA.js +7 -0
  61. package/dist/ribbons.d.ts +20 -0
  62. package/dist/ribbons.js +151 -0
  63. package/dist/schedule.d.ts +29 -0
  64. package/dist/schedule.js +47 -0
  65. package/dist/settings.d.ts +362 -0
  66. package/dist/settings.js +707 -0
  67. package/dist/shader.d.ts +44 -0
  68. package/dist/shadows.d.ts +26 -0
  69. package/dist/shadows.js +182 -0
  70. package/dist/shapes.d.ts +34 -0
  71. package/dist/shapes.js +81 -0
  72. package/dist/shared/fog.d.ts +33 -0
  73. package/dist/shared/pass.d.ts +53 -0
  74. package/dist/shared/settings.d.ts +89 -0
  75. package/dist/skinned.d.ts +11 -0
  76. package/dist/skinned.js +104 -0
  77. package/dist/smoke.d.ts +215 -0
  78. package/dist/smoke.js +280 -0
  79. package/dist/soft.d.ts +17 -0
  80. package/dist/soft.js +55 -0
  81. package/dist/solid.d.ts +168 -0
  82. package/dist/solid.js +403 -0
  83. package/dist/sort.d.ts +3 -0
  84. package/dist/sort.js +58 -0
  85. package/dist/space.d.ts +14 -0
  86. package/dist/space.js +88 -0
  87. package/dist/stretch.d.ts +7 -0
  88. package/dist/stretch.js +40 -0
  89. package/dist/texture.d.ts +8 -0
  90. package/dist/texture.js +45 -0
  91. package/dist/view-RKprOPAy.js +30 -0
  92. package/dist/view-rect-DkeiiUtw.js +13 -0
  93. package/dist/wrap.d.ts +3 -0
  94. package/dist/wrap.js +5 -0
  95. package/package.json +192 -6
package/dist/clouds.js ADDED
@@ -0,0 +1,566 @@
1
+ // @margo/core-built 0.1.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 { soft as a, softDepthWgsl as o } from "./soft.js";
8
+ import { t as s } from "./require-webgpu--tUXPsmA.js";
9
+ import { MargoError as c } from "@margoengine/math/errors";
10
+ import { parseColor as l } from "@margoengine/math/color";
11
+ import { createRandom as u } from "@margoengine/math/mathutils";
12
+ //#region src/clouds.ts
13
+ var d = 16, f = 36, p = 0, m = (e) => {
14
+ throw RangeError(e);
15
+ }, h = (e, t, n, r, i = Infinity) => {
16
+ let a = e === void 0 ? t : e;
17
+ return (typeof a != "number" || !Number.isFinite(a) || a < r || a > i) && m(`${n} is ${String(a)}; pass a finite number from ${r} to ${i}`), a;
18
+ }, g = (e, t, n) => {
19
+ let r = e ?? t;
20
+ if (!Array.isArray(r) || r.length !== 3 || !r.every((e) => typeof e == "number" && Number.isFinite(e))) throw TypeError(`${n} must be three finite numbers; pass [x, y, z]`);
21
+ return r;
22
+ }, _ = class {
23
+ constructor(e, t, n) {
24
+ this.matrix = new Float32Array([
25
+ 1,
26
+ 0,
27
+ 0,
28
+ 0,
29
+ 0,
30
+ 1,
31
+ 0,
32
+ 0,
33
+ 0,
34
+ 0,
35
+ 1,
36
+ 0,
37
+ 0,
38
+ 0,
39
+ 0,
40
+ 1
41
+ ]), this.layout = /* @__PURE__ */ new Float32Array(), this.tints = /* @__PURE__ */ new Float32Array(), this.key = "", this.distribute = null, this.owner = e, this.number = t, this.options = n;
42
+ }
43
+ remove() {
44
+ this.owner?.remove(this);
45
+ }
46
+ update() {
47
+ let e = this.options, t = h(e.segments, 20, "Cloud segments", 0, 1 << 20), n = g(e.bounds, [
48
+ 5,
49
+ 1,
50
+ 1
51
+ ], "Cloud bounds"), r = e.concentrate ?? "inside", i = h(e.volume, 6, "Cloud volume", 0), a = h(e.smallestVolume, .25, "Cloud smallestVolume", 0, 1), o = h(e.seed, this.number, "Cloud seed", -Infinity);
52
+ Number.isInteger(t) || m(`Cloud segments is ${t}; pass a whole number`), [
53
+ "inside",
54
+ "outside",
55
+ "random"
56
+ ].includes(r) || m(`Cloud concentrate is ${String(r)}; pass 'inside', 'outside' or 'random'`), n.some((e) => e < 0) && m("Cloud bounds has a negative size; pass sizes of 0 or more");
57
+ let s = e.distribute ?? null;
58
+ if (s !== null && typeof s != "function") throw TypeError("Cloud distribute must be a function or null; pass (index, random) => ({point, volume})");
59
+ let c = [
60
+ t,
61
+ n.join(),
62
+ r,
63
+ i,
64
+ a,
65
+ o
66
+ ].join("|");
67
+ (c !== this.key || s !== this.distribute) && (this.key = c, this.distribute = s, this.tints = new Float32Array(s ? t * 6 : 0), this.layout = v(t, n, r, i, a, o, s, this.tints));
68
+ }
69
+ };
70
+ function v(e, t, n, r, i, a, o = null, s) {
71
+ let c = u(a), l = new Float32Array(e * 6);
72
+ for (let a = 0; a < e; a++) {
73
+ let e = c() * 2 - 1, u = c() * 2 - 1, d = c() * 2 - 1, f = c(), p = c() * Math.PI * 2, m = c() * 2 - 1;
74
+ if (o) {
75
+ let t = o(a, c), n = g(t?.point, [
76
+ 0,
77
+ 0,
78
+ 0
79
+ ], "Cloud distribute point");
80
+ if ([e, u, d] = n, f = h(t.volume, 1, "Cloud distribute volume", 0, 1), s) {
81
+ let e = b(t.color, "Cloud distribute color"), n = b(t.emission, "Cloud distribute emission");
82
+ s.set([...e ?? [
83
+ NaN,
84
+ NaN,
85
+ NaN
86
+ ], ...n ?? [
87
+ 0,
88
+ 0,
89
+ 0
90
+ ]], a * 6);
91
+ }
92
+ } else {
93
+ let t = Math.max(Math.abs(e), Math.abs(u), Math.abs(d));
94
+ f = Math.max(i, n === "inside" ? 1 - t : n === "outside" ? t : f);
95
+ }
96
+ l.set([
97
+ e * t[0] / 2,
98
+ u * t[1] / 2,
99
+ d * t[2] / 2,
100
+ f * r,
101
+ p,
102
+ m
103
+ ], a * 6);
104
+ }
105
+ return l;
106
+ }
107
+ var y = (e) => (Math.exp(-e) + .5 * Math.exp(-.25 * e) + .25 * Math.exp(-.0625 * e)) / 1.75, b = (e, t) => {
108
+ if (e === void 0) return null;
109
+ if (!Array.isArray(e) || e.length !== 3 || !e.every((e) => typeof e == "number" && Number.isFinite(e) && e >= 0)) throw TypeError(`${t} must be three linear RGB numbers of 0 or more; pass [r, g, b]`);
110
+ return e;
111
+ };
112
+ function x(e = 128) {
113
+ let t = new Uint8Array(e * e * 4), n = u(7), r = /* @__PURE__ */ new Float32Array(256);
114
+ for (let e = 0; e < r.length; e++) r[e] = n();
115
+ let i = (e) => e * e * (3 - 2 * e), a = (e, t) => {
116
+ let n = Math.floor(e), a = Math.floor(t), o = i(e - n), s = i(t - a), c = (e, t) => r[(t % 16 + 16) % 16 * 16 + (e % 16 + 16) % 16], l = c(n, a) + (c(n + 1, a) - c(n, a)) * o;
117
+ return l + (c(n, a + 1) + (c(n + 1, a + 1) - c(n, a + 1)) * o - l) * s;
118
+ };
119
+ for (let n = 0; n < e; n++) for (let r = 0; r < e; r++) {
120
+ let i = (r + .5) / e * 2 - 1, o = (n + .5) / e * 2 - 1, s = Math.hypot(i, o), c = 0;
121
+ for (let t = 0, i = .5, o = 4; t < 4; t++, i /= 2, o *= 2) c += i * a((r + .5) / e * o, (n + .5) / e * o);
122
+ let l = s >= 1 ? 0 : (1 - s * s) ** 2, u = Math.min(1, l * (.55 + .9 * c)), d = (n * e + r) * 4;
123
+ t[d] = t[d + 1] = t[d + 2] = 255, t[d + 3] = Math.round(u * 255);
124
+ }
125
+ return {
126
+ width: e,
127
+ height: e,
128
+ data: t
129
+ };
130
+ }
131
+ function S(e) {
132
+ if ("data" in e && e.data) {
133
+ let { width: t, height: n, data: r } = e;
134
+ return (!Number.isInteger(t) || !Number.isInteger(n) || t < 1 || n < 1 || t > 4096 || n > 4096) && m(`Clouds texture is ${t} x ${n}; pass whole sizes from 1 to 4096`), r.length !== t * n * 4 && m(`Clouds texture data has ${r.length} bytes for ${t} x ${n}; pass width * height * 4 RGBA bytes`), {
135
+ width: t,
136
+ height: n,
137
+ data: r
138
+ };
139
+ }
140
+ let t = e, n = t.naturalWidth || t.width, r = t.naturalHeight || t.height;
141
+ if (typeof OffscreenCanvas != "function") throw new c("unsupported", "Clouds texture image sources need OffscreenCanvas; pass {width, height, data}");
142
+ let i = new OffscreenCanvas(n, r).getContext("2d");
143
+ return i.drawImage(t, 0, 0), S(i.getImageData(0, 0, n, r));
144
+ }
145
+ var C = (/* @__PURE__ */ new Float32Array(256)).map((e, t) => {
146
+ let n = t / 255;
147
+ return n <= .04045 ? n / 12.92 : ((n + .055) / 1.055) ** 2.4;
148
+ }), w = (e) => Math.round(255 * (e <= .0031308 ? e * 12.92 : 1.055 * e ** (1 / 2.4) - .055));
149
+ function T(e) {
150
+ let t = [e];
151
+ for (let n = e; n.width > 1 || n.height > 1;) {
152
+ let e = Math.max(1, n.width >> 1), r = Math.max(1, n.height >> 1), i = new Uint8Array(e * r * 4);
153
+ for (let t = 0; t < r; t++) for (let r = 0; r < e; r++) {
154
+ let a = [
155
+ 0,
156
+ 0,
157
+ 0,
158
+ 0
159
+ ];
160
+ for (let e = 0; e < 2; e++) for (let i = 0; i < 2; i++) {
161
+ let o = (Math.min(t * 2 + e, n.height - 1) * n.width + Math.min(r * 2 + i, n.width - 1)) * 4;
162
+ for (let e = 0; e < 3; e++) a[e] += C[n.data[o + e]];
163
+ a[3] += n.data[o + 3];
164
+ }
165
+ let o = (t * e + r) * 4;
166
+ for (let e = 0; e < 3; e++) i[o + e] = w(a[e] / 4);
167
+ i[o + 3] = Math.round(a[3] / 4);
168
+ }
169
+ t.push(n = {
170
+ width: e,
171
+ height: r,
172
+ data: i
173
+ });
174
+ }
175
+ return t;
176
+ }
177
+ var E = (e, n, r) => `
178
+ struct Frame {camera:mat4x4f,right:vec4f,up:vec4f,eye:vec4f,light:vec4f,lightColor:vec4f}
179
+ @group(0) @binding(0) var<uniform> frame:Frame;
180
+ @group(0) @binding(1) var puffTexture:texture_2d<f32>;
181
+ @group(0) @binding(2) var puffSampler:sampler;
182
+ ${e ? o(e) : ""}${n ? t(0, 5) : ""}
183
+ struct Out {@builtin(position) clip:vec4f,@location(0) uv:vec2f,@location(1) color:vec4f,@location(2) world:vec3f,@location(3) viewDepth:f32,@location(4) offset:vec2f,@location(5) back:vec3f,@location(6) shade:vec2f,@location(7) glow:vec3f}
184
+ @vertex fn vertex(@builtin(vertex_index) v:u32,@location(0) centre:vec4f,@location(1) tint:vec4f,@location(2) extra:vec4f,@location(3) glow:vec4f)->Out {
185
+ let corner=vec2f(f32(v&1u),f32(v>>1u))*2.0-1.0;
186
+ let c=cos(extra.x);let s=sin(extra.x);
187
+ let offset=vec2f(c*corner.x-s*corner.y,s*corner.x+c*corner.y);
188
+ let r=offset*(centre.w*0.5);
189
+ let world=centre.xyz+frame.right.xyz*r.x+frame.up.xyz*r.y;
190
+ var o:Out;
191
+ o.clip=frame.camera*vec4f(world,1);
192
+ o.uv=vec2f(corner.x*0.5+0.5,0.5-corner.y*0.5);
193
+ o.world=world;
194
+ ${e ? "o.viewDepth=dot(soft.plane.xyz,world)+soft.plane.w;" : "o.viewDepth=0.0;"}
195
+ let toPuff=centre.xyz-frame.eye.xyz;let distance=length(toPuff);
196
+
197
+ let near=select(1.0,smoothstep(0.0,extra.z,distance),extra.z>0.0);
198
+
199
+ let cosine=select(0.0,dot(toPuff/max(distance,1e-6),frame.light.xyz),distance>0.0);
200
+ o.offset=offset;
201
+ o.back=-toPuff/max(distance,1e-6);
202
+ o.shade=vec2f(extra.y*0.75*(1.0+cosine*cosine),extra.w);
203
+ o.glow=glow.rgb;
204
+ o.color=vec4f(tint.rgb,tint.a*near);
205
+ return o;
206
+ }
207
+ ${e ? "\nfn contrast(x:f32)->f32 {let y=0.5*pow(2.0*select(x,1.0-x,x>0.5),2.0);return select(y,1.0-y,x>0.5);}" : ""}
208
+ 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));}
209
+ @fragment fn fragment(i:Out)->@location(0) vec4f {
210
+ let t=textureSample(puffTexture,puffSampler,i.uv);
211
+ var a=t.a*i.color.a;
212
+ ${e ? "a*=contrast(clamp(softGap(i.clip,i.viewDepth),0.0,1.0));" : ""}
213
+ var rgb=i.color.rgb*t.rgb;
214
+ if (frame.lightColor.w>0.5) {
215
+
216
+
217
+ let relief=frame.right.w;
218
+ let n=normalize(frame.right.xyz*i.offset.x+frame.up.xyz*i.offset.y+i.back*sqrt(max(1.0-dot(i.offset,i.offset),0.0)));
219
+ let h=dot(n,frame.light.xyz)*0.5+0.5;
220
+ let direct=mix(1.0,2.0*h*h,relief)*i.shade.x;
221
+
222
+ let sky=frame.light.w*mix(1.0,(0.4+0.6*(n.y+1.0))*mix(1.0,i.shade.y,0.5+0.5*max(n.y,0.0)),relief);
223
+ rgb*=vec3f(sky)+frame.lightColor.rgb*direct;
224
+ }
225
+ rgb+=i.glow*t.rgb;
226
+ ${n ? "rgb=mix(rgb,margoFog.color.rgb,fogAmount(i.world));" : ""}
227
+ ${r ? "rgb=encodeSrgb(max(rgb,vec3f(0.0)));" : ""}
228
+ return vec4f(rgb*a,a);
229
+ }
230
+ `, D = class t {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
231
+ constructor(e = {}) {
232
+ this.name = "clouds:" + p++, this.stats = {
233
+ puffs: 0,
234
+ dropped: 0,
235
+ culled: 0,
236
+ drawn: 0
237
+ }, this.list = [], this.added = 0, this.seconds = 0, this.capacity = 0, this.textureSource = void 0, this.owned = [], this.pipelines = /* @__PURE__ */ new Map(), this.groups = /* @__PURE__ */ new Map(), this.fogSource = null, this.fogData = /* @__PURE__ */ new Float32Array(12), this.values = new Float32Array(f), this.view = /* @__PURE__ */ new Float32Array(16), this.data = /* @__PURE__ */ new Float32Array(), this.disposed = !1, this.attachment = {
238
+ view: void 0,
239
+ loadOp: "load",
240
+ storeOp: "store"
241
+ }, this.depthAttachment = {
242
+ view: void 0,
243
+ depthReadOnly: !0
244
+ }, this.options = e, this.validate();
245
+ }
246
+ static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
247
+ let r = new t(n);
248
+ return e.use(r), r;
249
+ }
250
+ get clouds() {
251
+ return this.list;
252
+ }
253
+ get time() {
254
+ return this.seconds;
255
+ }
256
+ add(e = {}) {
257
+ if (this.disposed) throw new c("disposed", "Clouds is disposed; create a new Clouds");
258
+ let t = new _(this, ++this.added, e);
259
+ return t.update(), this.list.push(t), t;
260
+ }
261
+ remove(e) {
262
+ let t = this.list.indexOf(e);
263
+ return t < 0 ? !1 : (this.list.splice(t, 1), e.owner = null, !0);
264
+ }
265
+ advanceTime(e) {
266
+ e > 0 && (this.seconds += e);
267
+ }
268
+ onPreUpdate(e) {
269
+ this.advanceTime(e);
270
+ }
271
+ resetTime() {
272
+ this.seconds = 0;
273
+ }
274
+ get fog() {
275
+ return this.fogSource;
276
+ }
277
+ setFog(e) {
278
+ return this.fogSource = r(e), this;
279
+ }
280
+ validate() {
281
+ let e = this.options, t = h(e.limit, 200, "Clouds limit", 1, 1 << 20), n = e.range === void 0 ? Infinity : h(e.range, 0, "Clouds range", 0);
282
+ if (Number.isInteger(t) || m(`Clouds limit is ${t}; pass a whole number`), n !== Infinity && !Number.isInteger(n) && m(`Clouds range is ${n}; pass a whole number`), h(e.ambient, .55, "Clouds ambient", 0), h(e.density, 1, "Clouds density", 0), h(e.relief, 0, "Clouds relief", 0, 1), h(e.softness, 0, "Clouds softness", 0) > 0 && typeof e.depth != "function") throw TypeError("Clouds softness needs the scene depth; pass depth: () => depthTexture.texture (DepthTexture from @margoengine/post/depth)");
283
+ let r = g(e.lightDirection, [
284
+ .4,
285
+ 1,
286
+ .3
287
+ ], "Clouds lightDirection");
288
+ if (Math.hypot(...r) || m("Clouds lightDirection is zero; pass a direction toward the light"), e.shading !== void 0 && e.shading !== "scatter" && e.shading !== "unlit" && m(`Clouds shading is ${String(e.shading)}; pass 'scatter' or 'unlit'`), e.frustumCulled !== void 0 && typeof e.frustumCulled != "boolean") throw TypeError("Clouds frustumCulled must be true or false; pass a boolean");
289
+ return l(e.lightColor ?? [
290
+ 1,
291
+ 1,
292
+ 1
293
+ ]), {
294
+ limit: t,
295
+ range: n
296
+ };
297
+ }
298
+ puffs() {
299
+ let e = this.options, t = e.density ?? 1, n = e.relief ?? 0, r = g(e.lightDirection, [
300
+ .4,
301
+ 1,
302
+ .3
303
+ ], "Clouds lightDirection"), i = Math.hypot(...r), a = r.map((e) => e / i), o = [], s = this.seconds;
304
+ for (let e of this.list) {
305
+ e.update();
306
+ let r = e.options, i = e.matrix, c = h(r.growth, 4, "Cloud growth", Number.MIN_VALUE), u = h(r.speed, 0, "Cloud speed", -Infinity), d = h(r.fade, 10, "Cloud fade", 0), f = h(r.opacity, 1, "Cloud opacity", 0, 1), p = l(r.color ?? [
307
+ 1,
308
+ 1,
309
+ 1
310
+ ]), m = (Math.hypot(i[0], i[1], i[2]) + Math.hypot(i[4], i[5], i[6]) + Math.hypot(i[8], i[9], i[10])) / 3, g = e.layout, _ = e.tints, v = o.length;
311
+ for (let t = 0; t < g.length; t += 6) {
312
+ let n = g[t], r = g[t + 1], a = g[t + 2], l = g[t + 5], h = u ? Math.sin(l * Math.PI + u * s) / c : 0;
313
+ o.push({
314
+ cloud: e,
315
+ centre: [
316
+ i[0] * n + i[4] * r + i[8] * a + i[12],
317
+ i[1] * n + i[5] * r + i[9] * a + i[13],
318
+ i[2] * n + i[6] * r + i[10] * a + i[14]
319
+ ],
320
+ size: g[t + 3] * m * (1 + h),
321
+ angle: g[t + 4] + l * u * s,
322
+ light: 1,
323
+ sky: 1,
324
+ color: _.length && !Number.isNaN(_[t]) ? [
325
+ _[t],
326
+ _[t + 1],
327
+ _[t + 2],
328
+ f
329
+ ] : [
330
+ p[0],
331
+ p[1],
332
+ p[2],
333
+ f
334
+ ],
335
+ emission: _.length ? [
336
+ _[t + 3],
337
+ _[t + 4],
338
+ _[t + 5]
339
+ ] : [
340
+ 0,
341
+ 0,
342
+ 0
343
+ ],
344
+ fade: d
345
+ });
346
+ }
347
+ let b = h(r.volume, 6, "Cloud volume", 0) * m / 2;
348
+ for (let e = v; e < o.length; e++) {
349
+ let r = o[e].centre, i = 0, s = 0;
350
+ for (let c = v; c < o.length; c++) {
351
+ if (c === e) continue;
352
+ let l = o[c].centre, u = l[0] - r[0], d = l[1] - r[1], f = l[2] - r[2], p = u * a[0] + d * a[1] + f * a[2], m = o[c].size / 2;
353
+ if (!(m > 0)) continue;
354
+ let h = u * u + d * d + f * f, g = t * (b > 0 ? m / b : 1);
355
+ p > 0 && (i += g * Math.exp(-(h - p * p) / (m * m))), n > 0 && d > 0 && (s += g * Math.exp(-(h - d * d) / (m * m)));
356
+ }
357
+ o[e].light = n > 0 ? y(i) : Math.exp(-i), o[e].sky = n > 0 ? y(s) : 1;
358
+ }
359
+ }
360
+ return o;
361
+ }
362
+ setup(e) {
363
+ s(e, "Clouds"), this.engine = e;
364
+ }
365
+ registerPasses(e) {
366
+ e.add({
367
+ name: this.name + ":draw",
368
+ order: 6,
369
+ execute: (e) => {
370
+ e.backend === "webgpu" && this.draw(e);
371
+ }
372
+ });
373
+ }
374
+ collect(t) {
375
+ let { limit: n, range: r } = this.validate(), i = this.puffs(), a = this.stats, o = t.camera.position, s = e(this.view, t.camera), c = -s[2], l = -s[6], u = -s[10], f = t.camera.matrix, p = [];
376
+ if (this.options.frustumCulled ?? !0) for (let [e, t] of [
377
+ [1, 0],
378
+ [-1, 0],
379
+ [1, 1],
380
+ [-1, 1],
381
+ [-1, 2]
382
+ ]) {
383
+ let n = [
384
+ 0,
385
+ 1,
386
+ 2,
387
+ 3
388
+ ].map((n) => f[n * 4 + 3] + e * f[n * 4 + t]), r = Math.hypot(n[0], n[1], n[2]);
389
+ r > 0 && p.push(n.map((e) => e / r));
390
+ }
391
+ let m = Math.min(i.length, r, n);
392
+ a.puffs = i.length, a.dropped = i.length - m;
393
+ let h = [];
394
+ for (let e = 0; e < m; e++) {
395
+ let t = i[e], [n, r, a] = t.centre, s = t.size * Math.SQRT1_2;
396
+ p.some((e) => e[0] * n + e[1] * r + e[2] * a + e[3] < -s) || h.push({
397
+ puff: t,
398
+ depth: (n - o[0]) * c + (r - o[1]) * l + (a - o[2]) * u,
399
+ index: e
400
+ });
401
+ }
402
+ a.culled = m - h.length, h.sort((e, t) => t.depth - e.depth || e.index - t.index), this.data.length < h.length * d && (this.data = new Float32Array(Math.max(n, h.length) * d));
403
+ let g = this.data;
404
+ return h.forEach(({ puff: e }, t) => {
405
+ g.set(e.centre, t * d), g[t * d + 3] = e.size, g.set(e.color, t * d + 4), g[t * d + 8] = e.angle, g[t * d + 9] = e.light, g[t * d + 10] = e.fade, g[t * d + 11] = e.sky, g.set(e.emission, t * d + 12), g[t * d + 15] = 0;
406
+ }), a.drawn = h.length, h.length;
407
+ }
408
+ textureFor(e) {
409
+ let t = this.options.texture ?? null;
410
+ if (this.texture && t === this.textureSource) return this.texture;
411
+ let n = T(t ? S(t) : x()), r = e.createTexture({
412
+ size: [n[0].width, n[0].height],
413
+ format: "rgba8unorm-srgb",
414
+ mipLevelCount: n.length,
415
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
416
+ });
417
+ return n.forEach((t, n) => e.queue.writeTexture({
418
+ texture: r,
419
+ mipLevel: n
420
+ }, t.data, { bytesPerRow: t.width * 4 }, [t.width, t.height])), this.texture?.destroy(), this.texture = r, this.textureSource = t, this.groups.clear(), r;
421
+ }
422
+ draw(e) {
423
+ if (this.disposed) return;
424
+ let t = this.collect(e);
425
+ if (!t) return;
426
+ let r = e.device, { limit: o } = this.validate();
427
+ (!this.instances || this.capacity < Math.min(o, t)) && (this.instances?.destroy(), this.capacity = Math.max(o, t), this.instances = r.createBuffer({
428
+ size: this.capacity * d * 4,
429
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
430
+ })), r.queue.writeBuffer(this.instances, 0, this.data, 0, t * d);
431
+ let s = this.options, c = this.view, u = this.values, f = g(s.lightDirection, [
432
+ .4,
433
+ 1,
434
+ .3
435
+ ], "Clouds lightDirection"), p = Math.hypot(...f);
436
+ u.set(e.camera.matrix);
437
+ for (let t = 0; t < 3; t++) u[16 + t] = c[t * 4], u[20 + t] = c[t * 4 + 1], u[24 + t] = e.camera.position[t], u[28 + t] = f[t] / p;
438
+ u[19] = s.relief ?? 0, u[31] = s.ambient ?? .55, l(s.lightColor ?? [
439
+ 1,
440
+ 1,
441
+ 1
442
+ ], u, 32), u[35] = +((s.shading ?? "scatter") === "scatter"), this.uniform ??= r.createBuffer({
443
+ size: 144,
444
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
445
+ }), r.queue.writeBuffer(this.uniform, 0, u);
446
+ let m = this.fogSource, h = !e.format.includes("float") && !e.format.endsWith("-srgb");
447
+ m && (n(m, e.camera.position, this.fogData), this.fogBuffer ??= r.createBuffer({
448
+ size: 48,
449
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
450
+ }), r.queue.writeBuffer(this.fogBuffer, 0, this.fogData));
451
+ let _ = s.softness ?? 0, v = 0;
452
+ _ > 0 && (this.softRuntime ??= a.create(r, this.owned), v = this.softRuntime.write(e, s.depth(), c, _), this.softRuntime.changed && this.groups.clear());
453
+ let y = this.textureFor(r), b = e.descriptor.multisample?.count ?? 1, x = e.descriptor.depthStencil.format, S = [
454
+ e.format,
455
+ x,
456
+ b,
457
+ v,
458
+ !!m
459
+ ].join(":"), C = this.pipelines.get(S);
460
+ if (!C) {
461
+ let t = r.createShaderModule({ code: E(v, !!m, h) });
462
+ C = r.createRenderPipeline({
463
+ layout: "auto",
464
+ vertex: {
465
+ module: t,
466
+ entryPoint: "vertex",
467
+ buffers: [{
468
+ arrayStride: 64,
469
+ stepMode: "instance",
470
+ attributes: [
471
+ 0,
472
+ 1,
473
+ 2,
474
+ 3
475
+ ].map((e) => ({
476
+ shaderLocation: e,
477
+ offset: e * 16,
478
+ format: "float32x4"
479
+ }))
480
+ }]
481
+ },
482
+ fragment: {
483
+ module: t,
484
+ entryPoint: "fragment",
485
+ targets: [{
486
+ format: e.format,
487
+ blend: {
488
+ color: {
489
+ srcFactor: "one",
490
+ dstFactor: "one-minus-src-alpha"
491
+ },
492
+ alpha: {
493
+ srcFactor: "one",
494
+ dstFactor: "one-minus-src-alpha"
495
+ }
496
+ }
497
+ }]
498
+ },
499
+ primitive: { topology: "triangle-strip" },
500
+ multisample: e.descriptor.multisample,
501
+ depthStencil: {
502
+ format: x,
503
+ depthWriteEnabled: !1,
504
+ depthCompare: "greater-equal"
505
+ }
506
+ }), this.pipelines.set(S, C);
507
+ }
508
+ let w = this.groups.get(C);
509
+ w || (this.sampler ??= r.createSampler({
510
+ magFilter: "linear",
511
+ minFilter: "linear",
512
+ mipmapFilter: "linear"
513
+ }), w = r.createBindGroup({
514
+ layout: C.getBindGroupLayout(0),
515
+ entries: [
516
+ {
517
+ binding: 0,
518
+ resource: { buffer: this.uniform }
519
+ },
520
+ {
521
+ binding: 1,
522
+ resource: y.createView()
523
+ },
524
+ {
525
+ binding: 2,
526
+ resource: this.sampler
527
+ },
528
+ ...m ? [{
529
+ binding: 5,
530
+ resource: { buffer: this.fogBuffer }
531
+ }] : [],
532
+ ...v ? this.softRuntime.entries : []
533
+ ]
534
+ }), this.groups.set(C, w)), this.attachment.view = e.color, this.depthAttachment.view = e.depth, this.depthAttachment.stencilReadOnly = x.includes("stencil") ? !0 : void 0;
535
+ let T = e.encoder.beginRenderPass({
536
+ colorAttachments: [this.attachment],
537
+ depthStencilAttachment: this.depthAttachment
538
+ });
539
+ i(T, e), T.setPipeline(C), T.setBindGroup(0, w), T.setVertexBuffer(0, this.instances), T.draw(4, t), T.end(), this.engine && this.engine.stats.drawCalls++;
540
+ }
541
+ release() {
542
+ for (let e of [
543
+ this.instances,
544
+ this.uniform,
545
+ this.fogBuffer,
546
+ this.texture,
547
+ ...this.owned
548
+ ]) e?.destroy();
549
+ this.instances = this.uniform = this.fogBuffer = this.texture = this.sampler = this.softRuntime = void 0, this.textureSource = void 0, this.capacity = 0, this.owned = [], this.pipelines.clear(), this.groups.clear();
550
+ }
551
+ onDeviceRestored(e) {
552
+ this.release(), this.setup(e);
553
+ }
554
+ dispose() {
555
+ if (!this.disposed) {
556
+ this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.release();
557
+ for (let e of this.list) e.owner = null;
558
+ this.list.length = 0, this.engine = void 0;
559
+ }
560
+ }
561
+ [Symbol.dispose ?? Symbol()]() {
562
+ this.dispose();
563
+ }
564
+ };
565
+ //#endregion
566
+ export { _ as Cloud, D as Clouds, x as defaultPuffTexture, v as layoutPuffs, y as scattered };