@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/emitter.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/emitter.ts
|
|
3
|
+
var t = 262144, n = "\nstruct Emitter {a:vec4f,b:vec4f,count:vec4u}\n@group(0) @binding(7) var<uniform> em:Emitter;\n", r = "@group(0) @binding(8) var<storage,read> tris:array<vec4f>;\n", i = "let origin=f.emitterTime.xyz+(vec3f(random(seed+5u),random(seed+6u),random(seed+7u))*2-1)*f.emissionExtent.xyz;", a = "f.velocitySpread.xyz+(vec3f(random(seed+1u),random(seed+2u),random(seed+3u))*2-1)*f.velocitySpread.w", o = {
|
|
4
|
+
sphere: "\n let su=random(seed+8u);let sv=random(seed+9u);let sw=random(seed+10u);\n let sz=1.0-2.0*sv;let sr=sqrt(max(0.0,1.0-sz*sz));let sa=6.2831853*su;\n shapeDir=vec3f(sr*cos(sa),sz,sr*sin(sa));\n origin+=shapeDir*em.a.x*select(pow(sw,0.3333333),1.0,em.a.y>0.5);",
|
|
5
|
+
cone: "\n let axis=normalize(em.b.xyz);let helper=select(vec3f(0,1,0),vec3f(1,0,0),abs(axis.y)>0.99);\n let tx=normalize(cross(helper,axis));let ty=cross(axis,tx);\n let ca=random(seed+8u)*6.2831853;let cv=random(seed+9u);let cd=random(seed+10u)*6.2831853;let cw=random(seed+11u);\n let cosT=mix(1.0,em.a.w,cv);let sinT=sqrt(max(0.0,1.0-cosT*cosT));\n shapeDir=axis*cosT+(tx*cos(ca)+ty*sin(ca))*sinT;\n origin+=(tx*cos(cd)+ty*sin(cd))*sqrt(cw)*em.a.x;",
|
|
6
|
+
mesh: "\n let share=random(seed+8u);var lo=0u;var hi=em.count.x-1u;\n loop{if(lo>=hi){break;}let mid=(lo+hi)/2u;if(tris[mid*3u].w<share){lo=mid+1u;}else{hi=mid;}}\n let ta=tris[lo*3u].xyz;let tb=tris[lo*3u+1u].xyz;let tc=tris[lo*3u+2u].xyz;\n let r1=sqrt(random(seed+9u));let r2=random(seed+10u);\n origin+=ta*(1.0-r1)+tb*(r1*(1.0-r2))+tc*(r1*r2);\n shapeDir=normalize(cross(tb-ta,tc-ta));"
|
|
7
|
+
};
|
|
8
|
+
function s(t, n, r) {
|
|
9
|
+
if (t.split(n).length !== 2) throw new e("unsupported", "VFX emitter needs the simulation shader it was written for; update the emitter edits");
|
|
10
|
+
return t.replace(n, r);
|
|
11
|
+
}
|
|
12
|
+
var c = (e, t, n, r) => {
|
|
13
|
+
if (typeof e != "number" || !Number.isFinite(e) || e < n || e > r) throw RangeError(`VFX emitter ${t} must be a number from ${n} to ${r}; pass a finite value in range`);
|
|
14
|
+
};
|
|
15
|
+
function l(e) {
|
|
16
|
+
let t = e.scale ?? 1, n = e.positions, r = e.indices ?? Array.from({ length: n.length / 3 }, (e, t) => t), i = [], a = [], o = (e) => [
|
|
17
|
+
n[e * 3] * t,
|
|
18
|
+
n[e * 3 + 1] * t,
|
|
19
|
+
n[e * 3 + 2] * t
|
|
20
|
+
];
|
|
21
|
+
for (let e = 0; e < r.length; e += 3) {
|
|
22
|
+
let t = o(r[e]), n = o(r[e + 1]), s = o(r[e + 2]), c = [
|
|
23
|
+
n[0] - t[0],
|
|
24
|
+
n[1] - t[1],
|
|
25
|
+
n[2] - t[2]
|
|
26
|
+
], l = [
|
|
27
|
+
s[0] - t[0],
|
|
28
|
+
s[1] - t[1],
|
|
29
|
+
s[2] - t[2]
|
|
30
|
+
], u = .5 * Math.hypot(c[1] * l[2] - c[2] * l[1], c[2] * l[0] - c[0] * l[2], c[0] * l[1] - c[1] * l[0]);
|
|
31
|
+
u > 0 && (i.push([
|
|
32
|
+
...t,
|
|
33
|
+
...n,
|
|
34
|
+
...s
|
|
35
|
+
]), a.push(u));
|
|
36
|
+
}
|
|
37
|
+
if (!i.length) throw RangeError("VFX mesh emitter has no triangle with an area; pass positions and indices of a real surface");
|
|
38
|
+
let s = a.reduce((e, t) => e + t, 0), c = new Float32Array(i.length * 12), l = 0;
|
|
39
|
+
return i.forEach((e, t) => {
|
|
40
|
+
l += a[t], c.set([
|
|
41
|
+
e[0],
|
|
42
|
+
e[1],
|
|
43
|
+
e[2],
|
|
44
|
+
t === i.length - 1 ? 1 : l / s
|
|
45
|
+
], t * 12), c.set([
|
|
46
|
+
e[3],
|
|
47
|
+
e[4],
|
|
48
|
+
e[5],
|
|
49
|
+
0
|
|
50
|
+
], t * 12 + 4), c.set([
|
|
51
|
+
e[6],
|
|
52
|
+
e[7],
|
|
53
|
+
e[8],
|
|
54
|
+
0
|
|
55
|
+
], t * 12 + 8);
|
|
56
|
+
}), c;
|
|
57
|
+
}
|
|
58
|
+
var u = {
|
|
59
|
+
validate(e, n = !0) {
|
|
60
|
+
if (!e || typeof e != "object" || e.shape !== "sphere" && e.shape !== "cone" && e.shape !== "mesh") throw TypeError("VFX emitter needs a shape of \"sphere\", \"cone\" or \"mesh\"; pass e.g. {shape: \"sphere\", radius: 1}");
|
|
61
|
+
if (e.speed !== void 0 && c(e.speed, "speed", -1e3, 1e3), e.shape === "sphere") {
|
|
62
|
+
if (e.radius !== void 0 && c(e.radius, "radius", 0, 1e6), e.shell !== void 0 && typeof e.shell != "boolean") throw TypeError("VFX emitter shell must be true or false; pass a boolean");
|
|
63
|
+
} else if (e.shape === "cone") {
|
|
64
|
+
if (e.radius !== void 0 && c(e.radius, "radius", 0, 1e6), e.angle !== void 0 && c(e.angle, "angle", 0, Math.PI / 2), e.axis !== void 0) {
|
|
65
|
+
if (!Array.isArray(e.axis) || e.axis.length !== 3) throw TypeError("VFX emitter axis needs [x, y, z]; pass three numbers");
|
|
66
|
+
if (e.axis.forEach((e, t) => c(e, `axis[${t}]`, -1e6, 1e6)), !Math.hypot(...e.axis)) throw RangeError("VFX emitter axis must not be zero; pass a direction");
|
|
67
|
+
}
|
|
68
|
+
} else if (n) {
|
|
69
|
+
let { positions: n, indices: r } = e;
|
|
70
|
+
if (!n || typeof n.length != "number" || n.length < 9 || n.length % 3) throw TypeError("VFX mesh emitter needs positions as xyz triples, at least one triangle; pass a Float32Array");
|
|
71
|
+
for (let e = 0; e < n.length; e++) if (!Number.isFinite(n[e])) throw RangeError("VFX mesh emitter positions must be finite; remove NaN and Infinity");
|
|
72
|
+
let i = r ? r.length : n.length / 3;
|
|
73
|
+
if (i % 3 || i < 3) throw TypeError("VFX mesh emitter indices must be triangle triples; pass a multiple of 3 indices");
|
|
74
|
+
if (i / 3 > 262144) throw RangeError(`VFX mesh emitter has more than ${t} triangles; pass a simpler mesh`);
|
|
75
|
+
if (r) {
|
|
76
|
+
for (let e = 0; e < r.length; e++) if (!Number.isInteger(r[e]) || r[e] < 0 || r[e] >= n.length / 3) throw RangeError("VFX mesh emitter index points past the positions; check the index buffer");
|
|
77
|
+
}
|
|
78
|
+
e.scale !== void 0 && c(e.scale, "scale", 1e-6, 1e6), l(e);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
patch(e, t) {
|
|
82
|
+
if (t.shape !== "sphere" && t.shape !== "cone" && t.shape !== "mesh") throw TypeError("VFX emitter needs the shapes entry for this shape; pass {emitter: shapes}");
|
|
83
|
+
let c = s(e, "fn hash(v:u32)", n + (t.shape === "mesh" ? r : "") + "fn hash(v:u32)");
|
|
84
|
+
return c = s(c, i, "var origin=" + i.slice(11) + "\n var shapeDir=vec3f(0);" + o[t.shape]), s(c, a, "f.velocitySpread.xyz+shapeDir*em.a.z+(" + a.slice(22));
|
|
85
|
+
},
|
|
86
|
+
create(e, t, n) {
|
|
87
|
+
let r = e.createBuffer({
|
|
88
|
+
size: 48,
|
|
89
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
90
|
+
}), i = /* @__PURE__ */ new Float32Array(12), a = new Uint32Array(i.buffer);
|
|
91
|
+
n.push(r);
|
|
92
|
+
let o = [{
|
|
93
|
+
binding: 7,
|
|
94
|
+
resource: { buffer: r }
|
|
95
|
+
}];
|
|
96
|
+
if (t.shape === "mesh") {
|
|
97
|
+
let r = l(t), i = e.createBuffer({
|
|
98
|
+
size: r.byteLength,
|
|
99
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
100
|
+
});
|
|
101
|
+
n.push(i), e.queue.writeBuffer(i, 0, r), a[8] = r.length / 12, o.push({
|
|
102
|
+
binding: 8,
|
|
103
|
+
resource: { buffer: i }
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
entries: o,
|
|
108
|
+
write(e, t) {
|
|
109
|
+
if (i[2] = t.speed ?? 0, t.shape === "sphere") i[0] = t.radius ?? 1, i[1] = +!!t.shell;
|
|
110
|
+
else if (t.shape === "cone") {
|
|
111
|
+
let e = t.axis ?? [
|
|
112
|
+
0,
|
|
113
|
+
1,
|
|
114
|
+
0
|
|
115
|
+
], n = Math.hypot(...e);
|
|
116
|
+
i[0] = t.radius ?? 0, i[3] = Math.cos(t.angle ?? Math.PI / 6);
|
|
117
|
+
for (let t = 0; t < 3; t++) i[4 + t] = e[t] / n;
|
|
118
|
+
}
|
|
119
|
+
e.writeBuffer(r, 0, i);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
//#endregion
|
|
125
|
+
export { t as MAX_EMITTER_TRIANGLES, u as emitter };
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { VFXEvents, VFXExtension } from './base.js';
|
|
2
|
+
/**
|
|
3
|
+
* The `events` feature (sub-emitters): a particle that is born, dies, or hits the ground faster than `speed`, raises an event on
|
|
4
|
+
* the GPU, and the next frame `count` new particles are born at it into the free slots of the same effect, with their
|
|
5
|
+
* own velocity, life, size and colour. Events and their births are listed in slot order, so the same frames give the
|
|
6
|
+
* same particles. Born particles never raise events. Pass it as `{events}` to `VFX` from `@margoengine/vfx/base`; the
|
|
7
|
+
* root `VFX` has it. `on` is read at attach; the rest every frame.
|
|
8
|
+
*/
|
|
9
|
+
export declare const events: VFXExtension<VFXEvents>;
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/events.ts
|
|
3
|
+
var t = "\n@group(0) @binding(15) var<storage,read_write> events:array<u32>;\n@group(0) @binding(16) var<storage,read_write> eventData:array<vec4f>;\nstruct Events {control:vec4u,shape:vec4f,velocity:vec4f,color:vec4f}\n@group(0) @binding(17) var<uniform> ev:Events;\nvar<private> eventHere:u32;\nvar<private> eventPosition:vec3f;\nvar<private> eventVelocity:vec3f;\nfn eventGroups()->u32 {return (f.control.x+255u)/256u;}\nfn raiseEvent(p:Particle){eventPosition=p.positionLife.xyz;eventVelocity=p.velocitySize.xyz;eventHere=1u;}\nfn storeEvents(i:u32){if(eventHere==1u){let at=(f.control.z%2u)*f.control.x*2u+i*2u;eventData[at]=vec4f(eventPosition,0);eventData[at+1u]=vec4f(eventVelocity,0);}}\n", n = "\n@compute @workgroup_size(256) fn eventScan(@builtin(local_invocation_index) l:u32){\n let n=eventGroups();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+=events[4u+k];}\n var run=prefix(l,sum)-sum;for(var k=a;k<b;k++){let v=events[4u+k];events[4u+k]=run;run+=v;}\n if(l==255u){events[1]=run;}\n}\n@compute @workgroup_size(256) fn eventCompact(@builtin(workgroup_id) w:vec3u,@builtin(local_invocation_index) l:u32){\n let n=eventGroups();let start=events[4u+w.x];var end=events[1];if(w.x+1u<n){end=events[4u+w.x+1u];}\n if(l<end-start){events[4u+n*257u+start+l]=events[4u+n+w.x*256u+l];}\n}", r = {
|
|
4
|
+
generation: ["let at=i*2;let cold=f.control.x*2+at;", "let at=i*2;let cold=f.control.x*2+at;var generation=particles[cold+1].w;"],
|
|
5
|
+
ticket: ["if(ticket>=f.control.y){particles[at].w=0;return 0u;}", "var child=0u;if(ticket>=f.control.y){let e=(ticket-f.control.y)/ev.control.x;if(e>=events[1]){particles[at].w=0;return 0u;}child=events[4u+eventGroups()*257u+e]+1u;}"],
|
|
6
|
+
birth: ["particles[cold]=p.color;particles[cold+1]=p.rotation;", "if(child>0u){let source=((f.control.z+1u)%2u)*f.control.x*2u+(child-1u)*2u;let childLife=mix(ev.shape.x,ev.shape.y,random(seed));\n p.positionLife=vec4f(eventData[source].xyz,max(0.0,childLife-dt));\n p.velocitySize=vec4f(eventData[source+1u].xyz*ev.shape.w+ev.velocity.xyz+(vec3f(random(seed+1u),random(seed+2u),random(seed+3u))*2-1)*ev.velocity.w,ev.shape.z);\n p.color=vec4f(ev.color.rgb*f.emissionExtent.w,ev.color.a);p.rotation=vec4f(p.rotation.x,p.rotation.y,childLife,1);}\n particles[cold]=p.color;particles[cold+1]=p.rotation;generation=p.rotation.w;"],
|
|
7
|
+
death: [" p.positionLife.w-=dt;", " p.positionLife.w-=dt;if(p.positionLife.w<=0&&particles[at].w>0&&generation==0){raiseEvent(p);}"],
|
|
8
|
+
collision: ["if(p.velocitySize.y<0){p.velocitySize.y=-p.velocitySize.y*f.physics.z;}", "if(p.velocitySize.y<0){if(-p.velocitySize.y>bitcast<f32>(ev.control.z)&&generation==0){raiseEvent(p);}p.velocitySize.y=-p.velocitySize.y*f.physics.z;}"],
|
|
9
|
+
list: ["if(l==255u){groups[w.x]=rank;}", "if(l==255u){groups[w.x]=rank;}\n storeEvents(id.x);let er=prefix(l,eventHere);if(eventHere==1u){events[4u+eventGroups()+w.x*256u+er-1u]=id.x;}if(l==255u){events[4u+w.x]=er;}"]
|
|
10
|
+
};
|
|
11
|
+
function i(t, [n, r]) {
|
|
12
|
+
if (t.split(n).length !== 2) throw new e("unsupported", "VFX events need the simulation shader they were written for; update the event edits");
|
|
13
|
+
return t.replace(n, r);
|
|
14
|
+
}
|
|
15
|
+
var a = (e, t, n, r) => {
|
|
16
|
+
if (typeof e != "number" || !Number.isFinite(e) || e < n || e > r) throw RangeError(`VFX events ${t} must be a number from ${n} to ${r}; pass a finite value in range`);
|
|
17
|
+
}, o = {
|
|
18
|
+
validate(e) {
|
|
19
|
+
if (!e || typeof e != "object" || e.on !== "death" && e.on !== "collision" && e.on !== "birth") throw TypeError("VFX events needs on: 'birth', 'death' or 'collision'; pass e.g. {on: 'death', count: 8}");
|
|
20
|
+
let t = e.count ?? 8;
|
|
21
|
+
if (!Number.isInteger(t) || t < 1 || t > 64) throw RangeError(`VFX events count is ${t}; pass a whole number of births per event from 1 to 64`);
|
|
22
|
+
let n = e.lifetime ?? [.5, 1];
|
|
23
|
+
if (!Array.isArray(n) || n.length !== 2) throw TypeError("VFX events lifetime needs [min, max] seconds");
|
|
24
|
+
a(n[0], "lifetime[0]", 1e-6, 1e6), a(n[1], "lifetime[1]", n[0], 1e6), a(e.size ?? .05, "size", 1e-6, 1e6), a(e.spread ?? 1, "spread", 0, 1e4), a(e.inherit ?? 0, "inherit", -10, 10), a(e.speed ?? .5, "speed", 0, 1e4);
|
|
25
|
+
let r = e.velocity ?? [
|
|
26
|
+
0,
|
|
27
|
+
0,
|
|
28
|
+
0
|
|
29
|
+
];
|
|
30
|
+
if (!Array.isArray(r) || r.length !== 3) throw TypeError("VFX events velocity needs [x, y, z]");
|
|
31
|
+
r.forEach((e, t) => a(e, `velocity[${t}]`, -1e4, 1e4));
|
|
32
|
+
let i = e.color ?? [
|
|
33
|
+
1,
|
|
34
|
+
1,
|
|
35
|
+
1,
|
|
36
|
+
1
|
|
37
|
+
];
|
|
38
|
+
if (!Array.isArray(i) || i.length !== 4) throw TypeError("VFX events color needs linear [r, g, b, a]");
|
|
39
|
+
i.forEach((e, t) => a(e, `color[${t}]`, 0, 1));
|
|
40
|
+
},
|
|
41
|
+
simulation(e, a) {
|
|
42
|
+
let o = e.replace("fn update(", t + "fn update(");
|
|
43
|
+
for (let e of [
|
|
44
|
+
"generation",
|
|
45
|
+
"ticket",
|
|
46
|
+
"birth",
|
|
47
|
+
"list"
|
|
48
|
+
]) o = i(o, r[e]);
|
|
49
|
+
return o = a.on === "birth" ? i(o, ["generation=p.rotation.w;", "generation=p.rotation.w;if(child==0u){raiseEvent(p);}"]) : i(o, r[a.on]), o + n;
|
|
50
|
+
},
|
|
51
|
+
create(e, t, n) {
|
|
52
|
+
let { device: r, capacity: i, options: a } = e, o = Math.ceil(i / 256), s = r.createBuffer({
|
|
53
|
+
size: (4 + o * 257 + i) * 4,
|
|
54
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
55
|
+
});
|
|
56
|
+
n.push(s);
|
|
57
|
+
let c = r.createBuffer({
|
|
58
|
+
size: i * 64,
|
|
59
|
+
usage: GPUBufferUsage.STORAGE
|
|
60
|
+
});
|
|
61
|
+
n.push(c);
|
|
62
|
+
let l = r.createBuffer({
|
|
63
|
+
size: 64,
|
|
64
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
65
|
+
});
|
|
66
|
+
n.push(l);
|
|
67
|
+
let u = t.on, d = (t) => {
|
|
68
|
+
let n = r.createComputePipeline({
|
|
69
|
+
layout: "auto",
|
|
70
|
+
compute: {
|
|
71
|
+
module: e.module,
|
|
72
|
+
entryPoint: t
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return [n, r.createBindGroup({
|
|
76
|
+
layout: n.getBindGroupLayout(0),
|
|
77
|
+
entries: [{
|
|
78
|
+
binding: 0,
|
|
79
|
+
resource: { buffer: e.uniform }
|
|
80
|
+
}, {
|
|
81
|
+
binding: 15,
|
|
82
|
+
resource: { buffer: s }
|
|
83
|
+
}]
|
|
84
|
+
})];
|
|
85
|
+
}, f = d("eventScan"), p = d("eventCompact"), m = /* @__PURE__ */ new Float32Array(16), h = new Uint32Array(m.buffer);
|
|
86
|
+
return {
|
|
87
|
+
births: !0,
|
|
88
|
+
eventList: s,
|
|
89
|
+
compute: [
|
|
90
|
+
{
|
|
91
|
+
binding: 15,
|
|
92
|
+
resource: { buffer: s }
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
binding: 16,
|
|
96
|
+
resource: { buffer: c }
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
binding: 17,
|
|
100
|
+
resource: { buffer: l }
|
|
101
|
+
}
|
|
102
|
+
],
|
|
103
|
+
reset: () => r.queue.writeBuffer(s, 0, /* @__PURE__ */ new Uint32Array(4)),
|
|
104
|
+
write(e) {
|
|
105
|
+
let t = a.events;
|
|
106
|
+
if (!t || t.on !== u) throw TypeError("VFX events and their on are fixed at attach; attach a new VFX to change them");
|
|
107
|
+
let n = t.lifetime ?? [.5, 1];
|
|
108
|
+
h[0] = t.count ?? 8, m[2] = t.speed ?? .5, m.set([
|
|
109
|
+
n[0],
|
|
110
|
+
n[1],
|
|
111
|
+
t.size ?? .05,
|
|
112
|
+
t.inherit ?? 0
|
|
113
|
+
], 4), m.set(t.velocity ?? [
|
|
114
|
+
0,
|
|
115
|
+
0,
|
|
116
|
+
0
|
|
117
|
+
], 8), m[11] = t.spread ?? 1, m.set(t.color ?? [
|
|
118
|
+
1,
|
|
119
|
+
1,
|
|
120
|
+
1,
|
|
121
|
+
1
|
|
122
|
+
], 12), e.device.queue.writeBuffer(l, 0, m);
|
|
123
|
+
},
|
|
124
|
+
run(e) {
|
|
125
|
+
for (let [[t, n], r] of [[f, 1], [p, o]]) e.setPipeline(t), e.setBindGroup(0, n), e.dispatchWorkgroups(r);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
131
|
+
export { o as events };
|
package/dist/extra.d.ts
ADDED
package/dist/fields.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { VFXExtension, VFXFields } from './base.js';
|
|
2
|
+
/** Most attractors of an effect. */
|
|
3
|
+
export declare const MAX_ATTRACTORS = 16;
|
|
4
|
+
/** Most cells of a vector field (128^3), 16 bytes each on the GPU. */
|
|
5
|
+
export declare const MAX_FIELD_CELLS = 2097152;
|
|
6
|
+
/**
|
|
7
|
+
* The `fields` feature: up to 16 point attractors (or repellers) and one 3D vector field over a box, added to each
|
|
8
|
+
* particle's acceleration. Pass it as `{fields}` to `VFX` from `@margoengine/vfx/base`; the root `VFX` has it. The
|
|
9
|
+
* field's cells are read at attach (and its presence is fixed); attractors, the box and the strengths every frame.
|
|
10
|
+
*/
|
|
11
|
+
export declare const fields: VFXExtension<VFXFields>;
|
package/dist/fields.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/fields.ts
|
|
3
|
+
var t = 16, n = 2097152, r = "f.gravityDelta.xyz+curlNoise(p.positionLife.xyz)", i = (e) => `
|
|
4
|
+
struct Fields {count:vec4u,center:vec4f,half:vec4f,attractors:array<vec4f,32>}
|
|
5
|
+
@group(0) @binding(19) var<uniform> fields:Fields;
|
|
6
|
+
${e ? "@group(0) @binding(20) var<storage,read> fieldCells:array<vec4f>;\nfn fieldCell(c:vec3u)->vec3f {let n=fields.count.yzw;return fieldCells[c.x+n.x*(c.y+n.y*c.z)].xyz;}" : ""}
|
|
7
|
+
fn fieldForce(x:vec3f)->vec3f {
|
|
8
|
+
var a=vec3f(0);
|
|
9
|
+
for(var k=0u;k<fields.count.x;k++){let d=fields.attractors[k*2].xyz-x;let s=fields.attractors[k*2+1].x;let q=dot(d,d)+s*s;a+=d*fields.attractors[k*2].w*inverseSqrt(q*q*q);}
|
|
10
|
+
${e ? "let local=(x-fields.center.xyz)/fields.half.xyz;\n if(all(abs(local)<=vec3f(1))){\n let n=fields.count.yzw;let g=clamp((local*0.5+0.5)*vec3f(n)-0.5,vec3f(0),vec3f(n-1u));let i=min(vec3u(g),n-1u);let j=min(i+1u,n-1u);let t=g-vec3f(i);\n let x0=mix(mix(fieldCell(i),fieldCell(vec3u(j.x,i.y,i.z)),t.x),mix(fieldCell(vec3u(i.x,j.y,i.z)),fieldCell(vec3u(j.x,j.y,i.z)),t.x),t.y);\n let x1=mix(mix(fieldCell(vec3u(i.x,i.y,j.z)),fieldCell(vec3u(j.x,i.y,j.z)),t.x),mix(fieldCell(vec3u(i.x,j.y,j.z)),fieldCell(j),t.x),t.y);\n a+=mix(x0,x1,t.z)*fields.half.w;\n }" : ""}
|
|
11
|
+
return a;
|
|
12
|
+
}
|
|
13
|
+
`, a = (e, t, n) => {
|
|
14
|
+
if (!Array.isArray(e) || e.length !== t || !e.every((e) => typeof e == "number" && Number.isFinite(e))) throw TypeError(`VFX fields ${n} needs ${t} finite numbers`);
|
|
15
|
+
return e;
|
|
16
|
+
}, o = {
|
|
17
|
+
validate(e) {
|
|
18
|
+
if (!e || typeof e != "object") throw TypeError("VFX fields must be an object; pass {attractors} or {field}");
|
|
19
|
+
let t = e.attractors ?? [];
|
|
20
|
+
if (!Array.isArray(t) || t.length > 16) throw RangeError("VFX fields attractors must be an array of at most 16");
|
|
21
|
+
t.forEach((e, t) => {
|
|
22
|
+
if (a(e?.position, 3, `attractors[${t}].position`), typeof e.strength != "number" || !Number.isFinite(e.strength)) throw TypeError(`VFX fields attractors[${t}].strength needs a finite number (negative pushes)`);
|
|
23
|
+
if (e.radius !== void 0 && !(e.radius > 0 && Number.isFinite(e.radius))) throw RangeError(`VFX fields attractors[${t}].radius must be above 0; pass e.g. 0.5`);
|
|
24
|
+
});
|
|
25
|
+
let r = e.field;
|
|
26
|
+
if (r !== void 0) {
|
|
27
|
+
let [e, t, i] = a(r?.size, 3, "field.size");
|
|
28
|
+
if (![
|
|
29
|
+
e,
|
|
30
|
+
t,
|
|
31
|
+
i
|
|
32
|
+
].every((e) => Number.isInteger(e) && e >= 1) || e * t * i > 2097152) throw RangeError(`VFX fields field.size needs whole cell counts, at most ${n} cells in all`);
|
|
33
|
+
if (!(r.data instanceof Float32Array) || r.data.length !== e * t * i * 3 || !r.data.every(Number.isFinite)) throw RangeError("VFX fields field.data needs a Float32Array of finite xyz vectors, 3 per cell");
|
|
34
|
+
if (a(r.center ?? [
|
|
35
|
+
0,
|
|
36
|
+
0,
|
|
37
|
+
0
|
|
38
|
+
], 3, "field.center"), !a(r.halfSize, 3, "field.halfSize").every((e) => e > 0)) throw RangeError("VFX fields field.halfSize must be above 0 on every axis");
|
|
39
|
+
if (r.strength !== void 0 && !Number.isFinite(r.strength)) throw TypeError("VFX fields field.strength needs a finite number");
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
simulation(t, n) {
|
|
43
|
+
if (t.split(r).length !== 2) throw new e("unsupported", "VFX fields need the simulation shader they were written for; update the edit");
|
|
44
|
+
return t.replace("fn update(", i(!!n.field) + "fn update(").replace(r, r + "+fieldForce(p.positionLife.xyz)");
|
|
45
|
+
},
|
|
46
|
+
create(e, t, n) {
|
|
47
|
+
let { device: r, options: i } = e, a = r.createBuffer({
|
|
48
|
+
size: 560,
|
|
49
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
50
|
+
});
|
|
51
|
+
n.push(a);
|
|
52
|
+
let o = [{
|
|
53
|
+
binding: 19,
|
|
54
|
+
resource: { buffer: a }
|
|
55
|
+
}], s = t.field, c = s ? [...s.size] : [
|
|
56
|
+
0,
|
|
57
|
+
0,
|
|
58
|
+
0
|
|
59
|
+
];
|
|
60
|
+
if (s) {
|
|
61
|
+
let e = new Float32Array(s.data.length / 3 * 4);
|
|
62
|
+
for (let t = 0; t < s.data.length / 3; t++) e.set(s.data.subarray(t * 3, t * 3 + 3), t * 4);
|
|
63
|
+
let t = r.createBuffer({
|
|
64
|
+
size: e.byteLength,
|
|
65
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
66
|
+
});
|
|
67
|
+
n.push(t), r.queue.writeBuffer(t, 0, e), o.push({
|
|
68
|
+
binding: 20,
|
|
69
|
+
resource: { buffer: t }
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
let l = /* @__PURE__ */ new Float32Array(140), u = new Uint32Array(l.buffer);
|
|
73
|
+
return {
|
|
74
|
+
compute: o,
|
|
75
|
+
write(e) {
|
|
76
|
+
let t = i.fields;
|
|
77
|
+
if (!t || !!t.field != !!s || s && t.field.data !== s.data) throw TypeError("VFX fields and their field cells are fixed at attach; change attractors and strengths in place");
|
|
78
|
+
let n = t.attractors ?? [];
|
|
79
|
+
l.fill(0), u[0] = n.length, u[1] = c[0], u[2] = c[1], u[3] = c[2], t.field && (l.set(t.field.center ?? [
|
|
80
|
+
0,
|
|
81
|
+
0,
|
|
82
|
+
0
|
|
83
|
+
], 4), l.set(t.field.halfSize, 8), l[11] = t.field.strength ?? 1), n.forEach((e, t) => {
|
|
84
|
+
l.set([
|
|
85
|
+
...e.position,
|
|
86
|
+
e.strength,
|
|
87
|
+
e.radius ?? .5
|
|
88
|
+
], 12 + t * 8);
|
|
89
|
+
}), e.device.queue.writeBuffer(a, 0, l);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
//#endregion
|
|
95
|
+
export { t as MAX_ATTRACTORS, n as MAX_FIELD_CELLS, o as fields };
|