@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +2240 -2
- package/dist/base-LGs65K7K.js +709 -0
- package/dist/base.d.ts +869 -0
- package/dist/base.js +1 -0
- package/dist/blown-sand.d.ts +218 -0
- package/dist/blown-sand.js +700 -0
- package/dist/clouds.d.ts +237 -0
- package/dist/clouds.js +583 -0
- package/dist/compat-BFjXAQuu.js +14 -0
- package/dist/compat-CVSEj1Bp.js +217 -0
- package/dist/compat.d.ts +59 -0
- package/dist/curves.d.ts +11 -0
- package/dist/curves.js +59 -0
- package/dist/debris.d.ts +154 -0
- package/dist/debris.js +290 -0
- package/dist/depth-collide.d.ts +7 -0
- package/dist/depth-collide.js +96 -0
- package/dist/dust-current.d.ts +66 -0
- package/dist/dust-current.js +92 -0
- package/dist/emitter.d.ts +10 -0
- package/dist/emitter.js +125 -0
- package/dist/events.d.ts +9 -0
- package/dist/events.js +131 -0
- package/dist/extra-CgGjbojB.js +4 -0
- package/dist/extra.d.ts +2 -0
- package/dist/fields.d.ts +11 -0
- package/dist/fields.js +95 -0
- package/dist/fire-front.d.ts +610 -0
- package/dist/fire-front.js +1080 -0
- package/dist/fire.d.ts +140 -0
- package/dist/fire.js +194 -0
- package/dist/flame-ramp-VoBKpYnM.js +75 -0
- package/dist/flame-ramp.d.ts +61 -0
- package/dist/flame.d.ts +428 -0
- package/dist/flame.js +817 -0
- package/dist/flipbook.d.ts +7 -0
- package/dist/flipbook.js +23 -0
- package/dist/flocking-shaders.d.ts +48 -0
- package/dist/flocking-sim.d.ts +623 -0
- package/dist/flocking.d.ts +145 -0
- package/dist/flocking.js +1846 -0
- package/dist/fog-B-kBv7g0.js +32 -0
- package/dist/frame-BuvMFBPp.js +4 -0
- package/dist/frame.d.ts +7 -0
- package/dist/gunfire.d.ts +742 -0
- package/dist/gunfire.js +1214 -0
- package/dist/hash-Btsp6fGK.js +7 -0
- package/dist/hash.d.ts +2 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +60 -0
- package/dist/json.d.ts +53 -0
- package/dist/json.js +239 -0
- package/dist/lit-Dx2tNwP-.js +152 -0
- package/dist/lit.d.ts +18 -0
- package/dist/lit.js +1 -0
- package/dist/mesh.d.ts +16 -0
- package/dist/mesh.js +248 -0
- package/dist/node-system.d.ts +315 -0
- package/dist/node-system.js +384 -0
- package/dist/nodes.d.ts +14 -0
- package/dist/nodes.js +293 -0
- package/dist/obstacles.d.ts +3 -0
- package/dist/obstacles.js +25 -0
- package/dist/over-life.d.ts +14 -0
- package/dist/over-life.js +94 -0
- package/dist/reactive-mask-B8xbyzlk.js +69 -0
- package/dist/require-webgpu--tUXPsmA.js +7 -0
- package/dist/ribbons.d.ts +22 -0
- package/dist/ribbons.js +165 -0
- package/dist/schedule.d.ts +29 -0
- package/dist/schedule.js +47 -0
- package/dist/settings.d.ts +362 -0
- package/dist/settings.js +707 -0
- package/dist/shader.d.ts +49 -0
- package/dist/shadows.d.ts +26 -0
- package/dist/shadows.js +182 -0
- package/dist/shapes.d.ts +34 -0
- package/dist/shapes.js +81 -0
- package/dist/shared/fog.d.ts +33 -0
- package/dist/shared/pass.d.ts +53 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/skinned.d.ts +11 -0
- package/dist/skinned.js +104 -0
- package/dist/smoke.d.ts +283 -0
- package/dist/smoke.js +504 -0
- package/dist/soft.d.ts +17 -0
- package/dist/soft.js +55 -0
- package/dist/solid.d.ts +168 -0
- package/dist/solid.js +403 -0
- package/dist/sort.d.ts +3 -0
- package/dist/sort.js +58 -0
- package/dist/space.d.ts +14 -0
- package/dist/space.js +88 -0
- package/dist/stretch.d.ts +7 -0
- package/dist/stretch.js +40 -0
- package/dist/texture.d.ts +8 -0
- package/dist/texture.js +45 -0
- package/dist/view-RKprOPAy.js +30 -0
- package/dist/view-rect-DkeiiUtw.js +13 -0
- package/dist/wrap.d.ts +3 -0
- package/dist/wrap.js +5 -0
- package/package.json +198 -4
package/dist/clouds.js
ADDED
|
@@ -0,0 +1,583 @@
|
|
|
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 { 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 = 20, 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,@location(8) heap: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,@location(4) heap: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.heap=heap.xyz;
|
|
205
|
+
o.color=vec4f(tint.rgb,tint.a*near);
|
|
206
|
+
return o;
|
|
207
|
+
}
|
|
208
|
+
${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);}" : ""}
|
|
209
|
+
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));}
|
|
210
|
+
@fragment fn fragment(i:Out)->@location(0) vec4f {
|
|
211
|
+
let t=textureSample(puffTexture,puffSampler,i.uv);
|
|
212
|
+
var a=t.a*i.color.a;
|
|
213
|
+
${e ? "a*=contrast(clamp(softGap(i.clip,i.viewDepth),0.0,1.0));" : ""}
|
|
214
|
+
var rgb=i.color.rgb*t.rgb;
|
|
215
|
+
if (frame.lightColor.w>0.5) {
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
let relief=frame.right.w;
|
|
220
|
+
let lobe=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));
|
|
221
|
+
let m=lobe+i.heap;
|
|
222
|
+
let n=m*inverseSqrt(max(dot(m,m),1e-8));
|
|
223
|
+
let h=dot(n,frame.light.xyz)*0.5+0.5;
|
|
224
|
+
let direct=mix(1.0,2.0*h*h,relief)*i.shade.x;
|
|
225
|
+
|
|
226
|
+
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);
|
|
227
|
+
rgb*=vec3f(sky)+frame.lightColor.rgb*direct;
|
|
228
|
+
}
|
|
229
|
+
rgb+=i.glow*t.rgb;
|
|
230
|
+
${n ? "rgb=mix(rgb,margoFog.color.rgb,fogAmount(i.world));" : ""}
|
|
231
|
+
${r ? "rgb=encodeSrgb(max(rgb,vec3f(0.0)));" : ""}
|
|
232
|
+
return vec4f(rgb*a,a);
|
|
233
|
+
}
|
|
234
|
+
`, D = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
235
|
+
constructor(e = {}) {
|
|
236
|
+
this.name = "clouds:" + p++, this.stats = {
|
|
237
|
+
puffs: 0,
|
|
238
|
+
dropped: 0,
|
|
239
|
+
culled: 0,
|
|
240
|
+
drawn: 0
|
|
241
|
+
}, 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 = {
|
|
242
|
+
view: void 0,
|
|
243
|
+
loadOp: "load",
|
|
244
|
+
storeOp: "store"
|
|
245
|
+
}, this.depthAttachment = {
|
|
246
|
+
view: void 0,
|
|
247
|
+
depthReadOnly: !0
|
|
248
|
+
}, this.options = e, this.validate();
|
|
249
|
+
}
|
|
250
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
251
|
+
let r = new t(n);
|
|
252
|
+
return e.use(r), r;
|
|
253
|
+
}
|
|
254
|
+
get clouds() {
|
|
255
|
+
return this.list;
|
|
256
|
+
}
|
|
257
|
+
get time() {
|
|
258
|
+
return this.seconds;
|
|
259
|
+
}
|
|
260
|
+
add(e = {}) {
|
|
261
|
+
if (this.disposed) throw new c("disposed", "Clouds is disposed; create a new Clouds");
|
|
262
|
+
let t = new _(this, ++this.added, e);
|
|
263
|
+
return t.update(), this.list.push(t), t;
|
|
264
|
+
}
|
|
265
|
+
remove(e) {
|
|
266
|
+
let t = this.list.indexOf(e);
|
|
267
|
+
return t < 0 ? !1 : (this.list.splice(t, 1), e.owner = null, !0);
|
|
268
|
+
}
|
|
269
|
+
advanceTime(e) {
|
|
270
|
+
e > 0 && (this.seconds += e);
|
|
271
|
+
}
|
|
272
|
+
onPreUpdate(e) {
|
|
273
|
+
this.advanceTime(e);
|
|
274
|
+
}
|
|
275
|
+
resetTime() {
|
|
276
|
+
this.seconds = 0;
|
|
277
|
+
}
|
|
278
|
+
get fog() {
|
|
279
|
+
return this.fogSource;
|
|
280
|
+
}
|
|
281
|
+
setFog(e) {
|
|
282
|
+
return this.fogSource = r(e), this;
|
|
283
|
+
}
|
|
284
|
+
validate() {
|
|
285
|
+
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);
|
|
286
|
+
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)");
|
|
287
|
+
let r = g(e.lightDirection, [
|
|
288
|
+
.4,
|
|
289
|
+
1,
|
|
290
|
+
.3
|
|
291
|
+
], "Clouds lightDirection");
|
|
292
|
+
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");
|
|
293
|
+
return l(e.lightColor ?? [
|
|
294
|
+
1,
|
|
295
|
+
1,
|
|
296
|
+
1
|
|
297
|
+
]), {
|
|
298
|
+
limit: t,
|
|
299
|
+
range: n
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
puffs() {
|
|
303
|
+
let e = this.options, t = e.density ?? 1, n = e.relief ?? 0, r = g(e.lightDirection, [
|
|
304
|
+
.4,
|
|
305
|
+
1,
|
|
306
|
+
.3
|
|
307
|
+
], "Clouds lightDirection"), i = Math.hypot(...r), a = r.map((e) => e / i), o = [], s = this.seconds;
|
|
308
|
+
for (let e of this.list) {
|
|
309
|
+
e.update();
|
|
310
|
+
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 ?? [
|
|
311
|
+
1,
|
|
312
|
+
1,
|
|
313
|
+
1
|
|
314
|
+
]), 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;
|
|
315
|
+
for (let t = 0; t < g.length; t += 6) {
|
|
316
|
+
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;
|
|
317
|
+
o.push({
|
|
318
|
+
cloud: e,
|
|
319
|
+
centre: [
|
|
320
|
+
i[0] * n + i[4] * r + i[8] * a + i[12],
|
|
321
|
+
i[1] * n + i[5] * r + i[9] * a + i[13],
|
|
322
|
+
i[2] * n + i[6] * r + i[10] * a + i[14]
|
|
323
|
+
],
|
|
324
|
+
size: g[t + 3] * m * (1 + h),
|
|
325
|
+
angle: g[t + 4] + l * u * s,
|
|
326
|
+
light: 1,
|
|
327
|
+
sky: 1,
|
|
328
|
+
normal: [
|
|
329
|
+
0,
|
|
330
|
+
0,
|
|
331
|
+
0
|
|
332
|
+
],
|
|
333
|
+
color: _.length && !Number.isNaN(_[t]) ? [
|
|
334
|
+
_[t],
|
|
335
|
+
_[t + 1],
|
|
336
|
+
_[t + 2],
|
|
337
|
+
f
|
|
338
|
+
] : [
|
|
339
|
+
p[0],
|
|
340
|
+
p[1],
|
|
341
|
+
p[2],
|
|
342
|
+
f
|
|
343
|
+
],
|
|
344
|
+
emission: _.length ? [
|
|
345
|
+
_[t + 3],
|
|
346
|
+
_[t + 4],
|
|
347
|
+
_[t + 5]
|
|
348
|
+
] : [
|
|
349
|
+
0,
|
|
350
|
+
0,
|
|
351
|
+
0
|
|
352
|
+
],
|
|
353
|
+
fade: d
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
let b = h(r.volume, 6, "Cloud volume", 0) * m / 2;
|
|
357
|
+
for (let e = v; e < o.length; e++) {
|
|
358
|
+
let r = o[e].centre, i = 0, s = 0, c = 0, l = 0, u = 0, d = o[e].size / 2;
|
|
359
|
+
for (let f = v; f < o.length; f++) {
|
|
360
|
+
if (f === e) continue;
|
|
361
|
+
let p = o[f].centre, m = p[0] - r[0], h = p[1] - r[1], g = p[2] - r[2], _ = m * a[0] + h * a[1] + g * a[2], v = o[f].size / 2;
|
|
362
|
+
if (!(v > 0)) continue;
|
|
363
|
+
let y = m * m + h * h + g * g, x = t * (b > 0 ? v / b : 1);
|
|
364
|
+
if (_ > 0 && (i += x * Math.exp(-(y - _ * _) / (v * v))), n > 0 && h > 0 && (s += x * Math.exp(-(y - h * h) / (v * v))), n > 0) {
|
|
365
|
+
let e = 2 * x * Math.exp(-y / (v * v)) * d / (v * v);
|
|
366
|
+
c -= e * m, l -= e * h, u -= e * g;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
o[e].light = n > 0 ? y(i) : Math.exp(-i), o[e].sky = n > 0 ? y(s) : 1, n > 0 && (o[e].normal = [
|
|
370
|
+
c,
|
|
371
|
+
l,
|
|
372
|
+
u
|
|
373
|
+
]);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return o;
|
|
377
|
+
}
|
|
378
|
+
setup(e) {
|
|
379
|
+
s(e, "Clouds"), this.engine = e;
|
|
380
|
+
}
|
|
381
|
+
registerPasses(e) {
|
|
382
|
+
e.add({
|
|
383
|
+
name: this.name + ":draw",
|
|
384
|
+
order: 6,
|
|
385
|
+
execute: (e) => {
|
|
386
|
+
e.backend === "webgpu" && this.draw(e);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
collect(t) {
|
|
391
|
+
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 = [];
|
|
392
|
+
if (this.options.frustumCulled ?? !0) for (let [e, t] of [
|
|
393
|
+
[1, 0],
|
|
394
|
+
[-1, 0],
|
|
395
|
+
[1, 1],
|
|
396
|
+
[-1, 1],
|
|
397
|
+
[-1, 2]
|
|
398
|
+
]) {
|
|
399
|
+
let n = [
|
|
400
|
+
0,
|
|
401
|
+
1,
|
|
402
|
+
2,
|
|
403
|
+
3
|
|
404
|
+
].map((n) => f[n * 4 + 3] + e * f[n * 4 + t]), r = Math.hypot(n[0], n[1], n[2]);
|
|
405
|
+
r > 0 && p.push(n.map((e) => e / r));
|
|
406
|
+
}
|
|
407
|
+
let m = Math.min(i.length, r, n);
|
|
408
|
+
a.puffs = i.length, a.dropped = i.length - m;
|
|
409
|
+
let h = [];
|
|
410
|
+
for (let e = 0; e < m; e++) {
|
|
411
|
+
let t = i[e], [n, r, a] = t.centre, s = t.size * Math.SQRT1_2;
|
|
412
|
+
p.some((e) => e[0] * n + e[1] * r + e[2] * a + e[3] < -s) || h.push({
|
|
413
|
+
puff: t,
|
|
414
|
+
depth: (n - o[0]) * c + (r - o[1]) * l + (a - o[2]) * u,
|
|
415
|
+
index: e
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
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));
|
|
419
|
+
let g = this.data;
|
|
420
|
+
return h.forEach(({ puff: e }, t) => {
|
|
421
|
+
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, g.set(e.normal, t * d + 16), g[t * d + 19] = 0;
|
|
422
|
+
}), a.drawn = h.length, h.length;
|
|
423
|
+
}
|
|
424
|
+
textureFor(e) {
|
|
425
|
+
let t = this.options.texture ?? null;
|
|
426
|
+
if (this.texture && t === this.textureSource) return this.texture;
|
|
427
|
+
let n = T(t ? S(t) : x()), r = e.createTexture({
|
|
428
|
+
size: [n[0].width, n[0].height],
|
|
429
|
+
format: "rgba8unorm-srgb",
|
|
430
|
+
mipLevelCount: n.length,
|
|
431
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
|
|
432
|
+
});
|
|
433
|
+
return n.forEach((t, n) => e.queue.writeTexture({
|
|
434
|
+
texture: r,
|
|
435
|
+
mipLevel: n
|
|
436
|
+
}, t.data, { bytesPerRow: t.width * 4 }, [t.width, t.height])), this.texture?.destroy(), this.texture = r, this.textureSource = t, this.groups.clear(), r;
|
|
437
|
+
}
|
|
438
|
+
draw(e) {
|
|
439
|
+
if (this.disposed) return;
|
|
440
|
+
let t = this.collect(e);
|
|
441
|
+
if (!t) return;
|
|
442
|
+
let r = e.device, { limit: o } = this.validate();
|
|
443
|
+
(!this.instances || this.capacity < Math.min(o, t)) && (this.instances?.destroy(), this.capacity = Math.max(o, t), this.instances = r.createBuffer({
|
|
444
|
+
size: this.capacity * d * 4,
|
|
445
|
+
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
446
|
+
})), r.queue.writeBuffer(this.instances, 0, this.data, 0, t * d);
|
|
447
|
+
let s = this.options, c = this.view, u = this.values, f = g(s.lightDirection, [
|
|
448
|
+
.4,
|
|
449
|
+
1,
|
|
450
|
+
.3
|
|
451
|
+
], "Clouds lightDirection"), p = Math.hypot(...f);
|
|
452
|
+
u.set(e.camera.matrix);
|
|
453
|
+
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;
|
|
454
|
+
u[19] = s.relief ?? 0, u[31] = s.ambient ?? .55, l(s.lightColor ?? [
|
|
455
|
+
1,
|
|
456
|
+
1,
|
|
457
|
+
1
|
|
458
|
+
], u, 32), u[35] = +((s.shading ?? "scatter") === "scatter"), this.uniform ??= r.createBuffer({
|
|
459
|
+
size: 144,
|
|
460
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
461
|
+
}), r.queue.writeBuffer(this.uniform, 0, u);
|
|
462
|
+
let m = this.fogSource, h = !e.format.includes("float") && !e.format.endsWith("-srgb");
|
|
463
|
+
m && (n(m, e.camera.position, this.fogData), this.fogBuffer ??= r.createBuffer({
|
|
464
|
+
size: 48,
|
|
465
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
466
|
+
}), r.queue.writeBuffer(this.fogBuffer, 0, this.fogData));
|
|
467
|
+
let _ = s.softness ?? 0, v = 0;
|
|
468
|
+
_ > 0 && (this.softRuntime ??= a.create(r, this.owned), v = this.softRuntime.write(e, s.depth(), c, _), this.softRuntime.changed && this.groups.clear());
|
|
469
|
+
let y = this.textureFor(r), b = e.descriptor.multisample?.count ?? 1, x = e.descriptor.depthStencil.format, S = [
|
|
470
|
+
e.format,
|
|
471
|
+
x,
|
|
472
|
+
b,
|
|
473
|
+
v,
|
|
474
|
+
!!m
|
|
475
|
+
].join(":"), C = this.pipelines.get(S);
|
|
476
|
+
if (!C) {
|
|
477
|
+
let t = r.createShaderModule({ code: E(v, !!m, h) });
|
|
478
|
+
C = r.createRenderPipeline({
|
|
479
|
+
layout: "auto",
|
|
480
|
+
vertex: {
|
|
481
|
+
module: t,
|
|
482
|
+
entryPoint: "vertex",
|
|
483
|
+
buffers: [{
|
|
484
|
+
arrayStride: 80,
|
|
485
|
+
stepMode: "instance",
|
|
486
|
+
attributes: [
|
|
487
|
+
0,
|
|
488
|
+
1,
|
|
489
|
+
2,
|
|
490
|
+
3,
|
|
491
|
+
4
|
|
492
|
+
].map((e) => ({
|
|
493
|
+
shaderLocation: e,
|
|
494
|
+
offset: e * 16,
|
|
495
|
+
format: "float32x4"
|
|
496
|
+
}))
|
|
497
|
+
}]
|
|
498
|
+
},
|
|
499
|
+
fragment: {
|
|
500
|
+
module: t,
|
|
501
|
+
entryPoint: "fragment",
|
|
502
|
+
targets: [{
|
|
503
|
+
format: e.format,
|
|
504
|
+
blend: {
|
|
505
|
+
color: {
|
|
506
|
+
srcFactor: "one",
|
|
507
|
+
dstFactor: "one-minus-src-alpha"
|
|
508
|
+
},
|
|
509
|
+
alpha: {
|
|
510
|
+
srcFactor: "one",
|
|
511
|
+
dstFactor: "one-minus-src-alpha"
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}]
|
|
515
|
+
},
|
|
516
|
+
primitive: { topology: "triangle-strip" },
|
|
517
|
+
multisample: e.descriptor.multisample,
|
|
518
|
+
depthStencil: {
|
|
519
|
+
format: x,
|
|
520
|
+
depthWriteEnabled: !1,
|
|
521
|
+
depthCompare: "greater-equal"
|
|
522
|
+
}
|
|
523
|
+
}), this.pipelines.set(S, C);
|
|
524
|
+
}
|
|
525
|
+
let w = this.groups.get(C);
|
|
526
|
+
w || (this.sampler ??= r.createSampler({
|
|
527
|
+
magFilter: "linear",
|
|
528
|
+
minFilter: "linear",
|
|
529
|
+
mipmapFilter: "linear"
|
|
530
|
+
}), w = r.createBindGroup({
|
|
531
|
+
layout: C.getBindGroupLayout(0),
|
|
532
|
+
entries: [
|
|
533
|
+
{
|
|
534
|
+
binding: 0,
|
|
535
|
+
resource: { buffer: this.uniform }
|
|
536
|
+
},
|
|
537
|
+
{
|
|
538
|
+
binding: 1,
|
|
539
|
+
resource: y.createView()
|
|
540
|
+
},
|
|
541
|
+
{
|
|
542
|
+
binding: 2,
|
|
543
|
+
resource: this.sampler
|
|
544
|
+
},
|
|
545
|
+
...m ? [{
|
|
546
|
+
binding: 5,
|
|
547
|
+
resource: { buffer: this.fogBuffer }
|
|
548
|
+
}] : [],
|
|
549
|
+
...v ? this.softRuntime.entries : []
|
|
550
|
+
]
|
|
551
|
+
}), this.groups.set(C, w)), this.attachment.view = e.color, this.depthAttachment.view = e.depth, this.depthAttachment.stencilReadOnly = x.includes("stencil") ? !0 : void 0;
|
|
552
|
+
let T = e.encoder.beginRenderPass({
|
|
553
|
+
colorAttachments: [this.attachment],
|
|
554
|
+
depthStencilAttachment: this.depthAttachment
|
|
555
|
+
});
|
|
556
|
+
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++;
|
|
557
|
+
}
|
|
558
|
+
release() {
|
|
559
|
+
for (let e of [
|
|
560
|
+
this.instances,
|
|
561
|
+
this.uniform,
|
|
562
|
+
this.fogBuffer,
|
|
563
|
+
this.texture,
|
|
564
|
+
...this.owned
|
|
565
|
+
]) e?.destroy();
|
|
566
|
+
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();
|
|
567
|
+
}
|
|
568
|
+
onDeviceRestored(e) {
|
|
569
|
+
this.release(), this.setup(e);
|
|
570
|
+
}
|
|
571
|
+
dispose() {
|
|
572
|
+
if (!this.disposed) {
|
|
573
|
+
this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.release();
|
|
574
|
+
for (let e of this.list) e.owner = null;
|
|
575
|
+
this.list.length = 0, this.engine = void 0;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
579
|
+
this.dispose();
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
//#endregion
|
|
583
|
+
export { _ as Cloud, D as Clouds, x as defaultPuffTexture, v as layoutPuffs, y as scattered };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function e(e) {
|
|
2
|
+
return e?.adapterInfo?.vendor === "margo-webgl2" ? "webgl2" : "native";
|
|
3
|
+
}
|
|
4
|
+
function t(t, n) {
|
|
5
|
+
return t.backend = e(n) === "webgl2" ? "webgl2-compat" : "webgpu", t.backend === "webgl2-compat";
|
|
6
|
+
}
|
|
7
|
+
function n(t, n) {
|
|
8
|
+
return e(t) === "webgl2" ? n() : void 0;
|
|
9
|
+
}
|
|
10
|
+
function r(e) {
|
|
11
|
+
return e ? e.then(() => void 0, () => void 0) : Promise.resolve();
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { t as n, r, n as t };
|