@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/solid.d.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/** Most particles, and most vertices over all of them (the material mesh's own limit of 1,000,000 vertices). */
|
|
2
|
+
export declare const MAX_SOLID_PARTICLES = 1000000;
|
|
3
|
+
/** Most vertices over all particles of one system: the material mesh's own limit. */
|
|
4
|
+
export declare const MAX_SOLID_VERTICES = 1000000;
|
|
5
|
+
type V3 = [number, number, number];
|
|
6
|
+
/** A shape to copy into particles: positions (xyz triples), optional normals, uvs (uv pairs) and indices (default: every 3 vertices). */
|
|
7
|
+
export interface SolidShape {
|
|
8
|
+
positions: ArrayLike<number>;
|
|
9
|
+
normals?: ArrayLike<number>;
|
|
10
|
+
uvs?: ArrayLike<number>;
|
|
11
|
+
indices?: ArrayLike<number>;
|
|
12
|
+
}
|
|
13
|
+
/** The merged mesh as flat arrays. Pass it to `createMesh` of `@margoengine/materials` (it takes this shape as is). */
|
|
14
|
+
export interface SolidGeometry {
|
|
15
|
+
positions: Float32Array;
|
|
16
|
+
normals: Float32Array;
|
|
17
|
+
uvs: Float32Array;
|
|
18
|
+
/** rgba per vertex, 0 to 1. */
|
|
19
|
+
colors: Float32Array;
|
|
20
|
+
indices: Uint32Array;
|
|
21
|
+
}
|
|
22
|
+
/** A vertex range, in vertices, as `setGeometry(geometry, range)` of a material mesh takes it. */
|
|
23
|
+
export interface SolidRange {
|
|
24
|
+
start: number;
|
|
25
|
+
count: number;
|
|
26
|
+
}
|
|
27
|
+
/** One particle of the system. Edit it in `updateParticle`, then `setParticles` writes it into the merged mesh. */
|
|
28
|
+
export interface SolidParticle {
|
|
29
|
+
/** Index in the system, in creation order. */
|
|
30
|
+
readonly idx: number;
|
|
31
|
+
/** The id `addShape` or `digest` returned for the shape this particle copies. */
|
|
32
|
+
readonly shapeId: number;
|
|
33
|
+
/** World position (default [0,0,0]). */
|
|
34
|
+
position: V3;
|
|
35
|
+
/** Euler angles in radians about x (pitch), y (yaw) and z (roll), applied in the order roll, pitch, yaw. Ignored while `rotationQuaternion` is set. */
|
|
36
|
+
rotation: V3;
|
|
37
|
+
/** Unit quaternion [x, y, z, w], or null (default) to use `rotation`. Normalised on write. */
|
|
38
|
+
rotationQuaternion: [number, number, number, number] | null;
|
|
39
|
+
/** Scale per axis, applied before the rotation (default [1,1,1]). */
|
|
40
|
+
scaling: V3;
|
|
41
|
+
/** rgba, 0 to 1 (default white). */
|
|
42
|
+
color: [number, number, number, number];
|
|
43
|
+
/** The rectangle [u0, v0, u1, v1] the shape's 0 to 1 uvs map into (default [0,0,1,1]). */
|
|
44
|
+
uvs: [number, number, number, number];
|
|
45
|
+
/** Free for the caller (speed, age ...). */
|
|
46
|
+
velocity: V3;
|
|
47
|
+
props: unknown;
|
|
48
|
+
/** False: `updateParticle` is not called, and the particle is not drawn or picked (default true). */
|
|
49
|
+
alive: boolean;
|
|
50
|
+
/** False: the particle is updated but collapsed to a point, not drawn or picked (default true). */
|
|
51
|
+
isVisible: boolean;
|
|
52
|
+
/** World box of the particle as last written (a point while hidden). */
|
|
53
|
+
readonly boundingBox: {
|
|
54
|
+
min: V3;
|
|
55
|
+
max: V3;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** Options of a new Solid Particle System. */
|
|
59
|
+
export interface SolidOptions {
|
|
60
|
+
/** Called by `setParticles` for each alive particle in its range before it is written (default none). */
|
|
61
|
+
updateParticle?: (particle: SolidParticle) => void;
|
|
62
|
+
}
|
|
63
|
+
/** Options of `addShape`: how each new particle of the shape starts. */
|
|
64
|
+
export interface SolidShapeOptions {
|
|
65
|
+
/** Sets each new particle's start (`particle`, its index in the shape, its index in the system). Default none. */
|
|
66
|
+
positionFunction?: (particle: SolidParticle, indexInShape: number, indexInSystem: number) => void;
|
|
67
|
+
/** Moves each template vertex of each particle once at creation (`vertex` is edited in place). Default none. */
|
|
68
|
+
vertexFunction?: (particle: SolidParticle, vertex: V3, vertexIndex: number) => void;
|
|
69
|
+
/** Start colour of the shape's particles (default white). */
|
|
70
|
+
color?: readonly [number, number, number, number];
|
|
71
|
+
}
|
|
72
|
+
/** `number` particles, or `facetNb` triangles in each (`number` wins when both are given); default `facetNb` 1. */
|
|
73
|
+
export interface SolidDigestOptions extends SolidShapeOptions {
|
|
74
|
+
number?: number;
|
|
75
|
+
facetNb?: number;
|
|
76
|
+
}
|
|
77
|
+
/** A world-space ray for `pick`; the direction need not be unit length, but must not be zero. */
|
|
78
|
+
export interface SolidRay {
|
|
79
|
+
origin: ArrayLike<number>;
|
|
80
|
+
direction: ArrayLike<number>;
|
|
81
|
+
}
|
|
82
|
+
/** Options of `pick`. */
|
|
83
|
+
export interface SolidPickOptions {
|
|
84
|
+
/** Farthest hit in world units (default Infinity). */
|
|
85
|
+
maxDistance?: number;
|
|
86
|
+
/** Count hits on back faces too (default true; false needs counter-clockwise faces seen from the ray). */
|
|
87
|
+
backFaces?: boolean;
|
|
88
|
+
}
|
|
89
|
+
/** The nearest particle a ray hits, from `pick`. */
|
|
90
|
+
export interface SolidPick {
|
|
91
|
+
particleIndex: number;
|
|
92
|
+
shapeId: number;
|
|
93
|
+
/** Triangle number in the merged index buffer. */
|
|
94
|
+
faceId: number;
|
|
95
|
+
/** Triangle number inside the particle's own shape. */
|
|
96
|
+
localFaceId: number;
|
|
97
|
+
/** World distance along the (normalised) ray. */
|
|
98
|
+
distance: number;
|
|
99
|
+
point: V3;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Solid particle system: many small meshes (a shape per `addShape` or `digest`) merged into ONE vertex and index
|
|
103
|
+
* buffer, so the whole system is one draw. Each particle keeps a position, rotation (Euler or quaternion), scaling,
|
|
104
|
+
* colour, uv rectangle, `alive` and `isVisible`. `setParticles(start?, end?)` runs `updateParticle` over a range and
|
|
105
|
+
* rewrites only that range's vertices, reporting the vertex range to re-upload (`takeDirty`). `pick(ray)` tests the
|
|
106
|
+
* merged buffer as it was last written, so what is drawn is what is picked.
|
|
107
|
+
*
|
|
108
|
+
* Build in order: `addShape` / `digest` (any number), `build()`, then `setParticles` and `pick`. Calls out of order
|
|
109
|
+
* throw `MargoError` `'state'`; any call after `dispose()` throws `'disposed'`.
|
|
110
|
+
*/
|
|
111
|
+
export declare class SolidParticleSystem {
|
|
112
|
+
updateParticle: ((p: SolidParticle) => void) | undefined;
|
|
113
|
+
/** Every particle, in creation order. Read-only list; edit the particles in it. */
|
|
114
|
+
readonly particles: SolidParticle[];
|
|
115
|
+
private slots;
|
|
116
|
+
private shapes;
|
|
117
|
+
private data;
|
|
118
|
+
private dirty;
|
|
119
|
+
private dead;
|
|
120
|
+
private busy;
|
|
121
|
+
private vertices;
|
|
122
|
+
private triangles;
|
|
123
|
+
constructor(options?: SolidOptions);
|
|
124
|
+
private live;
|
|
125
|
+
private add;
|
|
126
|
+
private room;
|
|
127
|
+
/**
|
|
128
|
+
* Adds `count` particles that copy `shape`. Returns the shape's id.
|
|
129
|
+
* @throws TypeError / RangeError for a bad shape, count or option; MargoError `'state'` once built.
|
|
130
|
+
*/
|
|
131
|
+
addShape(shape: SolidShape, count: number, options?: SolidShapeOptions): number;
|
|
132
|
+
/**
|
|
133
|
+
* Splits `shape` into particles of whole triangles (an explosion, a shattering): `number` particles or `facetNb`
|
|
134
|
+
* triangles each (default 1). Each particle sits at the centre of its triangles' vertices, its own copy of them.
|
|
135
|
+
* Returns the shape id; `particles` gains ceil(triangles / facetNb) entries.
|
|
136
|
+
*/
|
|
137
|
+
digest(shape: SolidShape, options?: SolidDigestOptions): number;
|
|
138
|
+
/**
|
|
139
|
+
* Allocates the merged buffers and writes every particle once. Returns the geometry (also `geometry`).
|
|
140
|
+
* @throws MargoError `'state'` when already built or empty.
|
|
141
|
+
*/
|
|
142
|
+
build(): SolidGeometry;
|
|
143
|
+
/** The merged mesh. Throws `'state'` before `build()`. */
|
|
144
|
+
get geometry(): SolidGeometry;
|
|
145
|
+
/**
|
|
146
|
+
* Updates and writes particles `start` to `end` (inclusive; default all). With `update` false (default true) the
|
|
147
|
+
* `updateParticle` callback is not called and the particles are only rewritten. Writes only their vertices.
|
|
148
|
+
* @throws RangeError for a bad range or a non-finite particle value (naming the particle); MargoError `'state'` before
|
|
149
|
+
* `build()` or when called from inside `updateParticle`.
|
|
150
|
+
*/
|
|
151
|
+
setParticles(start?: number, end?: number, update?: boolean): this;
|
|
152
|
+
/** The vertex range written since the last call (for `mesh.setGeometry(geometry, range)`), or null; clears it. */
|
|
153
|
+
takeDirty(): SolidRange | null;
|
|
154
|
+
/** The box around every drawn particle (alive and visible), or null when none is. */
|
|
155
|
+
getBounds(): {
|
|
156
|
+
min: V3;
|
|
157
|
+
max: V3;
|
|
158
|
+
} | null;
|
|
159
|
+
/**
|
|
160
|
+
* The nearest triangle the ray hits among alive, visible particles as last written, or null. Each particle's box is
|
|
161
|
+
* tested first, then its triangles (Moller and Trumbore 1997).
|
|
162
|
+
*/
|
|
163
|
+
pick(ray: SolidRay, options?: SolidPickOptions): SolidPick | null;
|
|
164
|
+
/** Frees the buffers and the particles. Safe to call twice. */
|
|
165
|
+
dispose(): void;
|
|
166
|
+
[Symbol.dispose](): void;
|
|
167
|
+
}
|
|
168
|
+
export {};
|
package/dist/solid.js
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/solid.ts
|
|
3
|
+
var t = 1e6, n = 1e6, r = (e, t, n) => {
|
|
4
|
+
let r = e;
|
|
5
|
+
if (!r || typeof r != "object" || typeof r.length != "number" || r.length % n) throw TypeError(`Solid particle ${t} must be a list of ${n}-number groups; pass a Float32Array or number[]`);
|
|
6
|
+
for (let e = 0; e < r.length; e++) if (typeof r[e] != "number" || !Number.isFinite(r[e])) throw RangeError(`Solid particle ${t}[${e}] is not a finite number; pass finite values`);
|
|
7
|
+
return r;
|
|
8
|
+
};
|
|
9
|
+
function i(e, t) {
|
|
10
|
+
let n = new Float32Array(e.length);
|
|
11
|
+
for (let r = 0; r < t.length; r += 3) {
|
|
12
|
+
let [i, a, o] = [
|
|
13
|
+
t[r] * 3,
|
|
14
|
+
t[r + 1] * 3,
|
|
15
|
+
t[r + 2] * 3
|
|
16
|
+
], s = e[a] - e[i], c = e[a + 1] - e[i + 1], l = e[a + 2] - e[i + 2], u = e[o] - e[i], d = e[o + 1] - e[i + 1], f = e[o + 2] - e[i + 2], p = [
|
|
17
|
+
c * f - l * d,
|
|
18
|
+
l * u - s * f,
|
|
19
|
+
s * d - c * u
|
|
20
|
+
];
|
|
21
|
+
for (let e of [
|
|
22
|
+
i,
|
|
23
|
+
a,
|
|
24
|
+
o
|
|
25
|
+
]) for (let t = 0; t < 3; t++) n[e + t] += p[t];
|
|
26
|
+
}
|
|
27
|
+
for (let e = 0; e < n.length; e += 3) {
|
|
28
|
+
let t = Math.hypot(n[e], n[e + 1], n[e + 2]);
|
|
29
|
+
if (t) for (let r = 0; r < 3; r++) n[e + r] /= t;
|
|
30
|
+
else n[e + 1] = 1;
|
|
31
|
+
}
|
|
32
|
+
return n;
|
|
33
|
+
}
|
|
34
|
+
function a(e) {
|
|
35
|
+
if (!e || typeof e != "object") throw TypeError("Solid shape must be {positions, normals?, uvs?, indices?}; pass a geometry object");
|
|
36
|
+
let t = r(e.positions, "positions", 3), n = t.length / 3;
|
|
37
|
+
if (n < 3) throw RangeError("Solid shape needs at least one triangle; pass 3 or more vertices");
|
|
38
|
+
let a = e.indices ? r(e.indices, "indices", 3) : Array.from({ length: n - n % 3 }, (e, t) => t);
|
|
39
|
+
if (!a.length || !e.indices && n % 3) throw RangeError("Solid shape needs whole triangles; pass indices or a multiple of 3 vertices");
|
|
40
|
+
for (let e = 0; e < a.length; e++) if (!Number.isInteger(a[e]) || a[e] < 0 || a[e] >= n) throw RangeError(`Solid shape indices[${e}] is ${a[e]}; pass whole numbers below ${n}`);
|
|
41
|
+
let o = e.normals ? r(e.normals, "normals", 3) : i(t, a), s = e.uvs ? r(e.uvs, "uvs", 2) : new Float32Array(n * 2);
|
|
42
|
+
if (o.length !== t.length || s.length !== n * 2) throw RangeError("Solid shape normals and uvs need one entry per vertex, as many as positions");
|
|
43
|
+
return {
|
|
44
|
+
positions: Float32Array.from(t),
|
|
45
|
+
normals: Float32Array.from(o),
|
|
46
|
+
uvs: Float32Array.from(s),
|
|
47
|
+
indices: Uint32Array.from(a)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
var o = (e, t, n, r) => {
|
|
51
|
+
if (!Number.isInteger(e) || e < n || e > r) throw RangeError(`Solid ${t} must be a whole number from ${n} to ${r}; got ${String(e)}`);
|
|
52
|
+
return e;
|
|
53
|
+
}, s = class {
|
|
54
|
+
constructor(e = {}) {
|
|
55
|
+
if (this.particles = [], this.slots = [], this.shapes = 0, this.data = null, this.dirty = null, this.dead = !1, this.busy = !1, this.vertices = 0, this.triangles = 0, e.updateParticle !== void 0 && typeof e.updateParticle != "function") throw TypeError("Solid updateParticle must be a function; pass (particle) => void");
|
|
56
|
+
this.updateParticle = e.updateParticle;
|
|
57
|
+
}
|
|
58
|
+
live() {
|
|
59
|
+
if (this.dead) throw new e("disposed", "SolidParticleSystem is disposed; create a new one");
|
|
60
|
+
}
|
|
61
|
+
add(e, t, n, r) {
|
|
62
|
+
let i = this.particles.length, a = {
|
|
63
|
+
idx: i,
|
|
64
|
+
shapeId: t,
|
|
65
|
+
position: [
|
|
66
|
+
0,
|
|
67
|
+
0,
|
|
68
|
+
0
|
|
69
|
+
],
|
|
70
|
+
rotation: [
|
|
71
|
+
0,
|
|
72
|
+
0,
|
|
73
|
+
0
|
|
74
|
+
],
|
|
75
|
+
rotationQuaternion: null,
|
|
76
|
+
scaling: [
|
|
77
|
+
1,
|
|
78
|
+
1,
|
|
79
|
+
1
|
|
80
|
+
],
|
|
81
|
+
color: n.color ? [...n.color] : [
|
|
82
|
+
1,
|
|
83
|
+
1,
|
|
84
|
+
1,
|
|
85
|
+
1
|
|
86
|
+
],
|
|
87
|
+
uvs: [
|
|
88
|
+
0,
|
|
89
|
+
0,
|
|
90
|
+
1,
|
|
91
|
+
1
|
|
92
|
+
],
|
|
93
|
+
velocity: [
|
|
94
|
+
0,
|
|
95
|
+
0,
|
|
96
|
+
0
|
|
97
|
+
],
|
|
98
|
+
props: void 0,
|
|
99
|
+
alive: !0,
|
|
100
|
+
isVisible: !0,
|
|
101
|
+
boundingBox: {
|
|
102
|
+
min: [
|
|
103
|
+
0,
|
|
104
|
+
0,
|
|
105
|
+
0
|
|
106
|
+
],
|
|
107
|
+
max: [
|
|
108
|
+
0,
|
|
109
|
+
0,
|
|
110
|
+
0
|
|
111
|
+
]
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
if (n.vertexFunction) {
|
|
115
|
+
e = {
|
|
116
|
+
...e,
|
|
117
|
+
positions: Float32Array.from(e.positions)
|
|
118
|
+
};
|
|
119
|
+
let t = [
|
|
120
|
+
0,
|
|
121
|
+
0,
|
|
122
|
+
0
|
|
123
|
+
];
|
|
124
|
+
for (let r = 0; r < e.positions.length / 3; r++) t[0] = e.positions[r * 3], t[1] = e.positions[r * 3 + 1], t[2] = e.positions[r * 3 + 2], n.vertexFunction(a, t, r), e.positions.set(t, r * 3);
|
|
125
|
+
}
|
|
126
|
+
this.slots.push({
|
|
127
|
+
p: a,
|
|
128
|
+
t: e,
|
|
129
|
+
vertex: this.vertices,
|
|
130
|
+
tri: this.triangles
|
|
131
|
+
}), this.particles.push(a), this.vertices += e.positions.length / 3, this.triangles += e.indices.length / 3, n.positionFunction?.(a, r, i);
|
|
132
|
+
}
|
|
133
|
+
room(r, i) {
|
|
134
|
+
if (this.live(), this.data) throw new e("state", "SolidParticleSystem is built; add shapes before build()");
|
|
135
|
+
if (this.particles.length + r > 1e6 || this.vertices + i > 1e6) throw RangeError(`Solid system holds at most ${t} particles and ${n} vertices; use fewer or smaller shapes`);
|
|
136
|
+
}
|
|
137
|
+
addShape(e, n, r = {}) {
|
|
138
|
+
let i = a(e);
|
|
139
|
+
o(n, "count", 1, t), this.room(n, n * (i.positions.length / 3));
|
|
140
|
+
let s = this.shapes++;
|
|
141
|
+
for (let e = 0; e < n; e++) this.add(i, s, r, e);
|
|
142
|
+
return s;
|
|
143
|
+
}
|
|
144
|
+
digest(e, t = {}) {
|
|
145
|
+
let n = a(e), r = n.indices.length / 3, i = t.number === void 0 ? o(t.facetNb ?? 1, "digest facetNb", 1, r) : Math.ceil(r / o(t.number, "digest number", 1, r)), s = Math.ceil(r / i);
|
|
146
|
+
this.room(s, r * 3);
|
|
147
|
+
let c = this.shapes++;
|
|
148
|
+
for (let e = 0; e < s; e++) {
|
|
149
|
+
let a = e * i, o = (Math.min(r, a + i) - a) * 3, s = {
|
|
150
|
+
positions: new Float32Array(o * 3),
|
|
151
|
+
normals: new Float32Array(o * 3),
|
|
152
|
+
uvs: new Float32Array(o * 2),
|
|
153
|
+
indices: Uint32Array.from({ length: o }, (e, t) => t)
|
|
154
|
+
}, l = [
|
|
155
|
+
0,
|
|
156
|
+
0,
|
|
157
|
+
0
|
|
158
|
+
];
|
|
159
|
+
for (let e = 0; e < o; e++) {
|
|
160
|
+
let t = n.indices[a * 3 + e];
|
|
161
|
+
for (let r = 0; r < 3; r++) s.positions[e * 3 + r] = n.positions[t * 3 + r], s.normals[e * 3 + r] = n.normals[t * 3 + r], l[r] += n.positions[t * 3 + r] / o;
|
|
162
|
+
s.uvs.set(n.uvs.subarray(t * 2, t * 2 + 2), e * 2);
|
|
163
|
+
}
|
|
164
|
+
for (let e = 0; e < o; e++) for (let t = 0; t < 3; t++) s.positions[e * 3 + t] -= l[t];
|
|
165
|
+
this.add(s, c, {
|
|
166
|
+
...t,
|
|
167
|
+
positionFunction: void 0
|
|
168
|
+
}, e);
|
|
169
|
+
let u = this.particles[this.particles.length - 1];
|
|
170
|
+
u.position = [
|
|
171
|
+
l[0],
|
|
172
|
+
l[1],
|
|
173
|
+
l[2]
|
|
174
|
+
], t.positionFunction?.(u, e, u.idx);
|
|
175
|
+
}
|
|
176
|
+
return c;
|
|
177
|
+
}
|
|
178
|
+
build() {
|
|
179
|
+
if (this.live(), this.data) throw new e("state", "SolidParticleSystem is already built; build() runs once");
|
|
180
|
+
if (!this.particles.length) throw new e("state", "SolidParticleSystem has no particles; call addShape or digest first");
|
|
181
|
+
let t = this.vertices, n = this.data = {
|
|
182
|
+
positions: new Float32Array(t * 3),
|
|
183
|
+
normals: new Float32Array(t * 3),
|
|
184
|
+
uvs: new Float32Array(t * 2),
|
|
185
|
+
colors: new Float32Array(t * 4),
|
|
186
|
+
indices: new Uint32Array(this.triangles * 3)
|
|
187
|
+
};
|
|
188
|
+
for (let e of this.slots) for (let t = 0; t < e.t.indices.length; t++) n.indices[e.tri * 3 + t] = e.t.indices[t] + e.vertex;
|
|
189
|
+
return this.setParticles(0, this.particles.length - 1, !1), n;
|
|
190
|
+
}
|
|
191
|
+
get geometry() {
|
|
192
|
+
if (this.live(), !this.data) throw new e("state", "SolidParticleSystem is not built; call build() first");
|
|
193
|
+
return this.data;
|
|
194
|
+
}
|
|
195
|
+
setParticles(t = 0, n = this.particles.length - 1, r = !0) {
|
|
196
|
+
let i = this.geometry;
|
|
197
|
+
if (this.busy) throw new e("state", "setParticles is running; do not call it from updateParticle");
|
|
198
|
+
let a = this.particles.length;
|
|
199
|
+
o(t, "setParticles start", 0, a - 1), o(n, "setParticles end", t, a - 1), this.busy = !0;
|
|
200
|
+
try {
|
|
201
|
+
for (let e = t; e <= n; e++) {
|
|
202
|
+
let t = this.slots[e];
|
|
203
|
+
r && t.p.alive && this.updateParticle && this.updateParticle(t.p), c(t, i);
|
|
204
|
+
}
|
|
205
|
+
} finally {
|
|
206
|
+
this.busy = !1;
|
|
207
|
+
}
|
|
208
|
+
let s = this.slots[t].vertex, l = this.slots[n], u = l.vertex + l.t.positions.length / 3;
|
|
209
|
+
if (this.dirty) {
|
|
210
|
+
let e = Math.min(this.dirty.start, s), t = Math.max(this.dirty.start + this.dirty.count, u);
|
|
211
|
+
this.dirty = {
|
|
212
|
+
start: e,
|
|
213
|
+
count: t - e
|
|
214
|
+
};
|
|
215
|
+
} else this.dirty = {
|
|
216
|
+
start: s,
|
|
217
|
+
count: u - s
|
|
218
|
+
};
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
takeDirty() {
|
|
222
|
+
this.live();
|
|
223
|
+
let e = this.dirty;
|
|
224
|
+
return this.dirty = null, e;
|
|
225
|
+
}
|
|
226
|
+
getBounds() {
|
|
227
|
+
this.geometry;
|
|
228
|
+
let e = [
|
|
229
|
+
Infinity,
|
|
230
|
+
Infinity,
|
|
231
|
+
Infinity
|
|
232
|
+
], t = [
|
|
233
|
+
-Infinity,
|
|
234
|
+
-Infinity,
|
|
235
|
+
-Infinity
|
|
236
|
+
], n = !1;
|
|
237
|
+
for (let { p: r } of this.slots) if (r.alive && r.isVisible) {
|
|
238
|
+
n = !0;
|
|
239
|
+
for (let n = 0; n < 3; n++) e[n] = Math.min(e[n], r.boundingBox.min[n]), t[n] = Math.max(t[n], r.boundingBox.max[n]);
|
|
240
|
+
}
|
|
241
|
+
return n ? {
|
|
242
|
+
min: e,
|
|
243
|
+
max: t
|
|
244
|
+
} : null;
|
|
245
|
+
}
|
|
246
|
+
pick(e, t = {}) {
|
|
247
|
+
let n = this.geometry, i = r(e?.origin, "ray origin", 3), a = r(e?.direction, "ray direction", 3);
|
|
248
|
+
if (i.length !== 3 || a.length !== 3) throw RangeError("Solid ray origin and direction need 3 numbers each");
|
|
249
|
+
let o = Math.hypot(a[0], a[1], a[2]);
|
|
250
|
+
if (!o) throw RangeError("Solid ray direction is zero; pass a direction with length");
|
|
251
|
+
let s = t.maxDistance ?? Infinity, c = t.backFaces ?? !0;
|
|
252
|
+
if (typeof s != "number" || !(s > 0) || typeof c != "boolean") throw TypeError("Solid pick options: pass maxDistance as a number above 0 and backFaces as a boolean");
|
|
253
|
+
let l = [
|
|
254
|
+
a[0] / o,
|
|
255
|
+
a[1] / o,
|
|
256
|
+
a[2] / o
|
|
257
|
+
], u = null, p = s;
|
|
258
|
+
for (let e of this.slots) {
|
|
259
|
+
let t = e.p;
|
|
260
|
+
if (!t.alive || !t.isVisible || !d(i, l, t.boundingBox, p)) continue;
|
|
261
|
+
let r = e.t.indices.length / 3;
|
|
262
|
+
for (let a = 0; a < r; a++) {
|
|
263
|
+
let [r, o, s] = [
|
|
264
|
+
0,
|
|
265
|
+
1,
|
|
266
|
+
2
|
|
267
|
+
].map((t) => n.indices[(e.tri + a) * 3 + t] * 3), d = n.positions, m = f(i, l, d, r, o, s, c);
|
|
268
|
+
m !== null && m <= p && (p = m, u = {
|
|
269
|
+
particleIndex: t.idx,
|
|
270
|
+
shapeId: t.shapeId,
|
|
271
|
+
faceId: e.tri + a,
|
|
272
|
+
localFaceId: a,
|
|
273
|
+
distance: m,
|
|
274
|
+
point: [
|
|
275
|
+
i[0] + l[0] * m,
|
|
276
|
+
i[1] + l[1] * m,
|
|
277
|
+
i[2] + l[2] * m
|
|
278
|
+
]
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return u;
|
|
283
|
+
}
|
|
284
|
+
dispose() {
|
|
285
|
+
this.dead = !0, this.data = null, this.slots = [], this.particles.length = 0;
|
|
286
|
+
}
|
|
287
|
+
[Symbol.dispose]() {
|
|
288
|
+
this.dispose();
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
function c(e, t) {
|
|
292
|
+
let { p: n, t: r } = e, i = r.positions.length / 3, a = n.position, o = n.scaling, s = n.color, c = n.uvs;
|
|
293
|
+
for (let [e, t] of [
|
|
294
|
+
["position", a],
|
|
295
|
+
["scaling", o],
|
|
296
|
+
["color", s],
|
|
297
|
+
["uvs", c],
|
|
298
|
+
["rotation", n.rotation]
|
|
299
|
+
]) for (let r of t) if (typeof r != "number" || !Number.isFinite(r)) throw RangeError(`Solid particle ${n.idx} ${e} is not finite; set finite numbers`);
|
|
300
|
+
let d = n.rotationQuaternion;
|
|
301
|
+
if (d && (d.length !== 4 || d.some((e) => !Number.isFinite(e)) || !Math.hypot(d[0], d[1], d[2], d[3]))) throw RangeError(`Solid particle ${n.idx} rotationQuaternion must be 4 finite numbers, not all zero; use null for none`);
|
|
302
|
+
let f = n.alive && n.isVisible, p = d ? l(d) : u(n.rotation), m = n.boundingBox, h = t.positions, g = t.normals, _ = o.map((e) => 1 / (e || 1));
|
|
303
|
+
m.min = [
|
|
304
|
+
Infinity,
|
|
305
|
+
Infinity,
|
|
306
|
+
Infinity
|
|
307
|
+
], m.max = [
|
|
308
|
+
-Infinity,
|
|
309
|
+
-Infinity,
|
|
310
|
+
-Infinity
|
|
311
|
+
];
|
|
312
|
+
for (let n = 0; n < i; n++) {
|
|
313
|
+
let i = (e.vertex + n) * 3, l = r.positions[n * 3] * o[0], u = r.positions[n * 3 + 1] * o[1], d = r.positions[n * 3 + 2] * o[2];
|
|
314
|
+
for (let e = 0; e < 3; e++) h[i + e] = f ? p[e * 3] * l + p[e * 3 + 1] * u + p[e * 3 + 2] * d + a[e] : a[e], m.min[e] = Math.min(m.min[e], h[i + e]), m.max[e] = Math.max(m.max[e], h[i + e]);
|
|
315
|
+
let v = r.normals[n * 3] * _[0], y = r.normals[n * 3 + 1] * _[1], b = r.normals[n * 3 + 2] * _[2], x = [
|
|
316
|
+
0,
|
|
317
|
+
1,
|
|
318
|
+
2
|
|
319
|
+
].map((e) => p[e * 3] * v + p[e * 3 + 1] * y + p[e * 3 + 2] * b), S = Math.hypot(x[0], x[1], x[2]) || 1;
|
|
320
|
+
for (let e = 0; e < 3; e++) g[i + e] = x[e] / S;
|
|
321
|
+
let C = e.vertex + n;
|
|
322
|
+
t.uvs[C * 2] = c[0] + r.uvs[n * 2] * (c[2] - c[0]), t.uvs[C * 2 + 1] = c[1] + r.uvs[n * 2 + 1] * (c[3] - c[1]), t.colors.set(s, C * 4);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function l(e) {
|
|
326
|
+
let t = Math.hypot(e[0], e[1], e[2], e[3]), [n, r, i, a] = e.map((e) => e / t);
|
|
327
|
+
return [
|
|
328
|
+
1 - 2 * (r * r + i * i),
|
|
329
|
+
2 * (n * r - i * a),
|
|
330
|
+
2 * (n * i + r * a),
|
|
331
|
+
2 * (n * r + i * a),
|
|
332
|
+
1 - 2 * (n * n + i * i),
|
|
333
|
+
2 * (r * i - n * a),
|
|
334
|
+
2 * (n * i - r * a),
|
|
335
|
+
2 * (r * i + n * a),
|
|
336
|
+
1 - 2 * (n * n + r * r)
|
|
337
|
+
];
|
|
338
|
+
}
|
|
339
|
+
function u(e) {
|
|
340
|
+
let [t, n, r, i, a, o] = [
|
|
341
|
+
Math.cos(e[0]),
|
|
342
|
+
Math.sin(e[0]),
|
|
343
|
+
Math.cos(e[1]),
|
|
344
|
+
Math.sin(e[1]),
|
|
345
|
+
Math.cos(e[2]),
|
|
346
|
+
Math.sin(e[2])
|
|
347
|
+
];
|
|
348
|
+
return [
|
|
349
|
+
r * a + i * n * o,
|
|
350
|
+
-r * o + i * n * a,
|
|
351
|
+
i * t,
|
|
352
|
+
t * o,
|
|
353
|
+
t * a,
|
|
354
|
+
-n,
|
|
355
|
+
-i * a + r * n * o,
|
|
356
|
+
i * o + r * n * a,
|
|
357
|
+
r * t
|
|
358
|
+
];
|
|
359
|
+
}
|
|
360
|
+
function d(e, t, n, r) {
|
|
361
|
+
let i = 0, a = r;
|
|
362
|
+
for (let r = 0; r < 3; r++) {
|
|
363
|
+
if (t[r] === 0) {
|
|
364
|
+
if (e[r] < n.min[r] || e[r] > n.max[r]) return !1;
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
let o = (n.min[r] - e[r]) / t[r], s = (n.max[r] - e[r]) / t[r];
|
|
368
|
+
if (o > s && ([o, s] = [s, o]), i = Math.max(i, o), a = Math.min(a, s), i > a) return !1;
|
|
369
|
+
}
|
|
370
|
+
return !0;
|
|
371
|
+
}
|
|
372
|
+
function f(e, t, n, r, i, a, o) {
|
|
373
|
+
let s = [
|
|
374
|
+
n[i] - n[r],
|
|
375
|
+
n[i + 1] - n[r + 1],
|
|
376
|
+
n[i + 2] - n[r + 2]
|
|
377
|
+
], c = [
|
|
378
|
+
n[a] - n[r],
|
|
379
|
+
n[a + 1] - n[r + 1],
|
|
380
|
+
n[a + 2] - n[r + 2]
|
|
381
|
+
], l = [
|
|
382
|
+
t[1] * c[2] - t[2] * c[1],
|
|
383
|
+
t[2] * c[0] - t[0] * c[2],
|
|
384
|
+
t[0] * c[1] - t[1] * c[0]
|
|
385
|
+
], u = s[0] * l[0] + s[1] * l[1] + s[2] * l[2];
|
|
386
|
+
if (u === 0 || !o && u < 0) return null;
|
|
387
|
+
let d = 1 / u, f = [
|
|
388
|
+
e[0] - n[r],
|
|
389
|
+
e[1] - n[r + 1],
|
|
390
|
+
e[2] - n[r + 2]
|
|
391
|
+
], p = d * (f[0] * l[0] + f[1] * l[1] + f[2] * l[2]);
|
|
392
|
+
if (p < 0 || p > 1) return null;
|
|
393
|
+
let m = [
|
|
394
|
+
f[1] * s[2] - f[2] * s[1],
|
|
395
|
+
f[2] * s[0] - f[0] * s[2],
|
|
396
|
+
f[0] * s[1] - f[1] * s[0]
|
|
397
|
+
], h = d * (t[0] * m[0] + t[1] * m[1] + t[2] * m[2]);
|
|
398
|
+
if (h < 0 || p + h > 1) return null;
|
|
399
|
+
let g = d * (c[0] * m[0] + c[1] * m[1] + c[2] * m[2]);
|
|
400
|
+
return g > 0 ? g : null;
|
|
401
|
+
}
|
|
402
|
+
//#endregion
|
|
403
|
+
export { t as MAX_SOLID_PARTICLES, n as MAX_SOLID_VERTICES, s as SolidParticleSystem };
|
package/dist/sort.d.ts
ADDED
package/dist/sort.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { t as e } from "./frame-BuvMFBPp.js";
|
|
2
|
+
import { t } from "./extra-CgGjbojB.js";
|
|
3
|
+
//#region src/sort.ts
|
|
4
|
+
var n = e + t + "\n@group(0) @binding(1) var<storage,read> particles:array<vec4f>;\n@group(0) @binding(3) var<storage,read_write> alive:array<u32>;\n@group(0) @binding(4) var<storage,read> indirect:array<u32,5>;\n@group(0) @binding(7) var<uniform> stage:vec4u;\nfn key(id:u32)->f32 {if(id==0xffffffffu){return -1e30;}return dot(particles[id*2].xyz-extra.eye.xyz,extra.forward.xyz);}\n\n@compute @workgroup_size(256) fn clear(@builtin(global_invocation_id) id:vec3u){if(id.x>=indirect[1]&&id.x<stage.z){alive[id.x]=0xffffffffu;}}\n\n@compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u){\n let a=id.x;let b=a^stage.x;if(b<=a||b>=stage.z){return;}let ia=alive[a];let ib=alive[b];let ka=key(ia);let kb=key(ib);let descending=(a&stage.y)==0;\n if((descending&&ka<kb)||(!descending&&ka>kb)){alive[a]=ib;alive[b]=ia;}\n}", r = { create(e, t, r, i, a, o, s) {
|
|
5
|
+
let c = e.createShaderModule({ code: n }), l = (e, t) => ({
|
|
6
|
+
binding: e,
|
|
7
|
+
resource: { buffer: t }
|
|
8
|
+
}), u = [], d = e.createComputePipeline({
|
|
9
|
+
layout: "auto",
|
|
10
|
+
compute: {
|
|
11
|
+
module: c,
|
|
12
|
+
entryPoint: "clear"
|
|
13
|
+
}
|
|
14
|
+
}), f = e.createComputePipeline({
|
|
15
|
+
layout: "auto",
|
|
16
|
+
compute: {
|
|
17
|
+
module: c,
|
|
18
|
+
entryPoint: "main"
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
for (let n = 2; n <= Math.max(2, o); n *= 2) for (let i = n / 2; i >= 1; i /= 2) {
|
|
22
|
+
let c = e.createBuffer({
|
|
23
|
+
size: 16,
|
|
24
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
25
|
+
});
|
|
26
|
+
s.push(c), u.push({
|
|
27
|
+
buffer: c,
|
|
28
|
+
group: e.createBindGroup({
|
|
29
|
+
layout: f.getBindGroupLayout(0),
|
|
30
|
+
entries: [
|
|
31
|
+
l(1, t),
|
|
32
|
+
l(3, r),
|
|
33
|
+
l(6, a),
|
|
34
|
+
l(7, c)
|
|
35
|
+
]
|
|
36
|
+
})
|
|
37
|
+
}), e.queue.writeBuffer(c, 0, new Uint32Array([
|
|
38
|
+
i,
|
|
39
|
+
n,
|
|
40
|
+
o,
|
|
41
|
+
0
|
|
42
|
+
]));
|
|
43
|
+
}
|
|
44
|
+
let p = e.createBindGroup({
|
|
45
|
+
layout: d.getBindGroupLayout(0),
|
|
46
|
+
entries: [
|
|
47
|
+
l(3, r),
|
|
48
|
+
l(4, i),
|
|
49
|
+
l(7, u[0].buffer)
|
|
50
|
+
]
|
|
51
|
+
}), m = Math.ceil(r.size / 1024);
|
|
52
|
+
return { run(e) {
|
|
53
|
+
e.setPipeline(d), e.setBindGroup(0, p), e.dispatchWorkgroups(m), e.setPipeline(f);
|
|
54
|
+
for (let t of u) e.setBindGroup(0, t.group), e.dispatchWorkgroups(m);
|
|
55
|
+
} };
|
|
56
|
+
} };
|
|
57
|
+
//#endregion
|
|
58
|
+
export { r as sort };
|
package/dist/space.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { VFXExtension } from './base.js';
|
|
2
|
+
/** A rigid emitter frame. Particle buffers remain in world coordinates for collisions, fields and other draws. */
|
|
3
|
+
export interface VFXSpace {
|
|
4
|
+
/** `local` follows translation and rotation; `world` leaves existing particles independent. Default `world`. */
|
|
5
|
+
mode?: 'local' | 'world';
|
|
6
|
+
/** Column-major rigid 4x4 transform, read every step. Default identity; scale/shear are rejected. */
|
|
7
|
+
transform?: ArrayLike<number>;
|
|
8
|
+
/** Share of emitter origin velocity added at birth, 0..1. Default 0. */
|
|
9
|
+
inheritVelocity?: number;
|
|
10
|
+
/** Births per world unit travelled by the emitter origin, 0..1,000,000. Default 0. */
|
|
11
|
+
ratePerDistance?: number;
|
|
12
|
+
}
|
|
13
|
+
/** Moving local/world emitter space, inherited velocity and distance emission, without CPU particle work. */
|
|
14
|
+
export declare const space: VFXExtension<VFXSpace>;
|