@vfx-js/effects 1.4.2 → 1.4.4
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/lib/cjs/ascii.js +63 -73
- package/lib/cjs/ascii.js.map +1 -1
- package/lib/cjs/bad-jpeg.js +7 -5
- package/lib/cjs/bad-jpeg.js.map +1 -1
- package/lib/cjs/bloom.js +68 -78
- package/lib/cjs/bloom.js.map +1 -1
- package/lib/cjs/chromatic.js +1 -0
- package/lib/cjs/chromatic.js.map +1 -1
- package/lib/cjs/colored-edges.js +5 -4
- package/lib/cjs/colored-edges.js.map +1 -1
- package/lib/cjs/datamosh.js +176 -186
- package/lib/cjs/datamosh.js.map +1 -1
- package/lib/cjs/dither.js +8 -5
- package/lib/cjs/dither.js.map +1 -1
- package/lib/cjs/duotone.js +4 -2
- package/lib/cjs/duotone.js.map +1 -1
- package/lib/cjs/fluid.js +62 -74
- package/lib/cjs/fluid.js.map +1 -1
- package/lib/cjs/glitch.js +1 -0
- package/lib/cjs/glitch.js.map +1 -1
- package/lib/cjs/gradient-map.js +6 -3
- package/lib/cjs/gradient-map.js.map +1 -1
- package/lib/cjs/halftone.js +3 -0
- package/lib/cjs/halftone.js.map +1 -1
- package/lib/cjs/hue-shift.js +1 -0
- package/lib/cjs/hue-shift.js.map +1 -1
- package/lib/cjs/index.d.ts +33 -33
- package/lib/cjs/index.js +68 -68
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/jpeg-glitch.js +274 -278
- package/lib/cjs/jpeg-glitch.js.map +1 -1
- package/lib/cjs/light-streak.js +34 -47
- package/lib/cjs/light-streak.js.map +1 -1
- package/lib/cjs/matrix.js +48 -59
- package/lib/cjs/matrix.js.map +1 -1
- package/lib/cjs/package.json +2 -1
- package/lib/cjs/particle-explode.js +135 -141
- package/lib/cjs/particle-explode.js.map +1 -1
- package/lib/cjs/particle.js +265 -270
- package/lib/cjs/particle.js.map +1 -1
- package/lib/cjs/pattern-refraction.js +3 -2
- package/lib/cjs/pattern-refraction.js.map +1 -1
- package/lib/cjs/pixel-stretch.js +3 -2
- package/lib/cjs/pixel-stretch.js.map +1 -1
- package/lib/cjs/pixelate.js +1 -0
- package/lib/cjs/pixelate.js.map +1 -1
- package/lib/cjs/pixelsort.js +103 -122
- package/lib/cjs/pixelsort.js.map +1 -1
- package/lib/cjs/rainbow.js +1 -0
- package/lib/cjs/rainbow.js.map +1 -1
- package/lib/cjs/rgb-glitch.js +1 -0
- package/lib/cjs/rgb-glitch.js.map +1 -1
- package/lib/cjs/rgb-shift.js +1 -0
- package/lib/cjs/rgb-shift.js.map +1 -1
- package/lib/cjs/scanline.js +1 -0
- package/lib/cjs/scanline.js.map +1 -1
- package/lib/cjs/sinewave.js +1 -0
- package/lib/cjs/sinewave.js.map +1 -1
- package/lib/cjs/slice-shift.js +3 -2
- package/lib/cjs/slice-shift.js.map +1 -1
- package/lib/cjs/tile-pixelate.js +4 -1
- package/lib/cjs/tile-pixelate.js.map +1 -1
- package/lib/cjs/tritone.d.ts +1 -1
- package/lib/cjs/tritone.js +5 -2
- package/lib/cjs/tritone.js.map +1 -1
- package/lib/cjs/vignette.js +1 -0
- package/lib/cjs/vignette.js.map +1 -1
- package/lib/cjs/voronoi.js +4 -1
- package/lib/cjs/voronoi.js.map +1 -1
- package/lib/cjs/warp.js +3 -2
- package/lib/cjs/warp.js.map +1 -1
- package/lib/esm/ascii.js +63 -73
- package/lib/esm/ascii.js.map +1 -1
- package/lib/esm/bad-jpeg.js +7 -5
- package/lib/esm/bad-jpeg.js.map +1 -1
- package/lib/esm/bloom.js +67 -77
- package/lib/esm/bloom.js.map +1 -1
- package/lib/esm/chromatic.js +1 -0
- package/lib/esm/chromatic.js.map +1 -1
- package/lib/esm/colored-edges.js +2 -1
- package/lib/esm/colored-edges.js.map +1 -1
- package/lib/esm/datamosh.js +173 -183
- package/lib/esm/datamosh.js.map +1 -1
- package/lib/esm/dither.js +7 -4
- package/lib/esm/dither.js.map +1 -1
- package/lib/esm/duotone.js +4 -2
- package/lib/esm/duotone.js.map +1 -1
- package/lib/esm/fluid.js +62 -74
- package/lib/esm/fluid.js.map +1 -1
- package/lib/esm/glitch.js +1 -0
- package/lib/esm/glitch.js.map +1 -1
- package/lib/esm/gradient-map.js +5 -2
- package/lib/esm/gradient-map.js.map +1 -1
- package/lib/esm/halftone.js +3 -0
- package/lib/esm/halftone.js.map +1 -1
- package/lib/esm/hue-shift.js +1 -0
- package/lib/esm/hue-shift.js.map +1 -1
- package/lib/esm/index.d.ts +33 -33
- package/lib/esm/index.js +33 -33
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/jpeg-glitch.js +273 -277
- package/lib/esm/jpeg-glitch.js.map +1 -1
- package/lib/esm/light-streak.js +34 -47
- package/lib/esm/light-streak.js.map +1 -1
- package/lib/esm/matrix.js +48 -59
- package/lib/esm/matrix.js.map +1 -1
- package/lib/esm/package.json +2 -1
- package/lib/esm/particle-explode.js +124 -130
- package/lib/esm/particle-explode.js.map +1 -1
- package/lib/esm/particle.js +255 -260
- package/lib/esm/particle.js.map +1 -1
- package/lib/esm/pattern-refraction.js +2 -1
- package/lib/esm/pattern-refraction.js.map +1 -1
- package/lib/esm/pixel-stretch.js +2 -1
- package/lib/esm/pixel-stretch.js.map +1 -1
- package/lib/esm/pixelate.js +1 -0
- package/lib/esm/pixelate.js.map +1 -1
- package/lib/esm/pixelsort.js +103 -122
- package/lib/esm/pixelsort.js.map +1 -1
- package/lib/esm/rainbow.js +1 -0
- package/lib/esm/rainbow.js.map +1 -1
- package/lib/esm/rgb-glitch.js +1 -0
- package/lib/esm/rgb-glitch.js.map +1 -1
- package/lib/esm/rgb-shift.js +1 -0
- package/lib/esm/rgb-shift.js.map +1 -1
- package/lib/esm/scanline.js +1 -0
- package/lib/esm/scanline.js.map +1 -1
- package/lib/esm/sinewave.js +1 -0
- package/lib/esm/sinewave.js.map +1 -1
- package/lib/esm/slice-shift.js +2 -1
- package/lib/esm/slice-shift.js.map +1 -1
- package/lib/esm/tile-pixelate.js +4 -1
- package/lib/esm/tile-pixelate.js.map +1 -1
- package/lib/esm/tritone.d.ts +1 -1
- package/lib/esm/tritone.js +5 -2
- package/lib/esm/tritone.js.map +1 -1
- package/lib/esm/vignette.js +1 -0
- package/lib/esm/vignette.js.map +1 -1
- package/lib/esm/voronoi.js +4 -1
- package/lib/esm/voronoi.js.map +1 -1
- package/lib/esm/warp.js +2 -1
- package/lib/esm/warp.js.map +1 -1
- package/package.json +4 -3
package/lib/esm/particle.js
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
-
};
|
|
12
|
-
var _ParticleEffect_instances, _ParticleEffect_params, _ParticleEffect_posTex, _ParticleEffect_colorTex, _ParticleEffect_velTex, _ParticleEffect_stampTex, _ParticleEffect_trail, _ParticleEffect_initialized, _ParticleEffect_stateSize, _ParticleEffect_stateCapacity, _ParticleEffect_spawnUniform, _ParticleEffect_spawnRawTex, _ParticleEffect_spawnTexHandle, _ParticleEffect_spawnGeometry, _ParticleEffect_particleGeometry, _ParticleEffect_gl, _ParticleEffect_contextRestoredUnsub, _ParticleEffect_nextSlot, _ParticleEffect_birthAccumulator, _ParticleEffect_screenBirthAccumulator, _ParticleEffect_lastMouseUv, _ParticleEffect_lastMoveTime, _ParticleEffect_allocStateRTs, _ParticleEffect_disposeStateRTs, _ParticleEffect_createSpawnTex, _ParticleEffect_allocSpawnTextures, _ParticleEffect_uploadSpawnTextures, _ParticleEffect_scheduleSpawns, _ParticleEffect_cap;
|
|
13
|
-
import { GLSL_CURL_NOISE, LIFE_JITTER_MAX, LIFE_JITTER_MIN, } from "./_curl-noise";
|
|
14
|
-
import { clampDt, FRAG_PARTICLE, FRAG_TRAIL_COMPOSITE, GLSL_HASH, hexToRgb, QUAD_VERTS, sanitizeCount, stateSizeFromCount, } from "./_particle-common";
|
|
1
|
+
import { GLSL_CURL_NOISE, LIFE_JITTER_MAX, LIFE_JITTER_MIN, } from "./_curl-noise.js";
|
|
2
|
+
import { clampDt, FRAG_PARTICLE, FRAG_TRAIL_COMPOSITE, GLSL_HASH, hexToRgb, QUAD_VERTS, sanitizeCount, stateSizeFromCount, } from "./_particle-common.js";
|
|
15
3
|
// State texture footprint: 4 internal buffers total (pos persistent =
|
|
16
4
|
// 2 internal, color + vel single = 1 each) × stateSize² × 16 B =
|
|
17
5
|
// 64 MB at 1024 (RGBA32F) / 32 MB at RGBA16F fallback at the 1M cap.
|
|
@@ -265,7 +253,9 @@ void main() {
|
|
|
265
253
|
vec4 c = texture(src, clamp(vSpawn.yz, 0.0, 1.0));
|
|
266
254
|
float h = hash21(vSpawn.yz + vec2(vSpawn.x) * 1.7);
|
|
267
255
|
float lifeJitter = mix(lifeJitterRange.x, lifeJitterRange.y, h);
|
|
268
|
-
|
|
256
|
+
// Un-premultiply; the particle shader multiplies by alpha.
|
|
257
|
+
vec3 rgb = c.a > 0.0 ? c.rgb / c.a : vec3(0.0);
|
|
258
|
+
outColor = vec4(mix(rgb, color, colorMix), lifeJitter);
|
|
269
259
|
}
|
|
270
260
|
`;
|
|
271
261
|
// Writes the radial direction (unit vector in pixel space) per spawn
|
|
@@ -307,49 +297,48 @@ const DEFAULT_PARAMS = {
|
|
|
307
297
|
blend: "add",
|
|
308
298
|
};
|
|
309
299
|
export class ParticleEffect {
|
|
300
|
+
#params;
|
|
310
301
|
/** Read-only view of the current params. Use `setParam(key, value)`
|
|
311
302
|
* to mutate so values like `count` are sanitized. (External tools
|
|
312
303
|
* such as tweakpane bind directly to the underlying object and
|
|
313
304
|
* skip the validation; the type marker is for programmatic use.) */
|
|
314
305
|
get params() {
|
|
315
|
-
return
|
|
306
|
+
return this.#params;
|
|
316
307
|
}
|
|
308
|
+
#posTex = null;
|
|
309
|
+
#colorTex = null;
|
|
310
|
+
#velTex = null;
|
|
311
|
+
#stampTex = null;
|
|
312
|
+
#trail = null;
|
|
313
|
+
#initialized = false;
|
|
314
|
+
#stateSize;
|
|
315
|
+
#stateCapacity;
|
|
316
|
+
#spawnUniform = new Float32Array(MAX_SPAWNS_PER_FRAME * 4);
|
|
317
|
+
#spawnRawTex = null;
|
|
318
|
+
#spawnTexHandle = null;
|
|
319
|
+
#spawnGeometry = null;
|
|
320
|
+
#particleGeometry = null;
|
|
321
|
+
#gl = null;
|
|
322
|
+
#contextRestoredUnsub = null;
|
|
323
|
+
#nextSlot = 0;
|
|
324
|
+
#birthAccumulator = 0;
|
|
325
|
+
#screenBirthAccumulator = 0;
|
|
326
|
+
#lastMouseUv = null;
|
|
327
|
+
#lastMoveTime = -Infinity;
|
|
317
328
|
constructor(initial = {}) {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
_ParticleEffect_velTex.set(this, null);
|
|
323
|
-
_ParticleEffect_stampTex.set(this, null);
|
|
324
|
-
_ParticleEffect_trail.set(this, null);
|
|
325
|
-
_ParticleEffect_initialized.set(this, false);
|
|
326
|
-
_ParticleEffect_stateSize.set(this, void 0);
|
|
327
|
-
_ParticleEffect_stateCapacity.set(this, void 0);
|
|
328
|
-
_ParticleEffect_spawnUniform.set(this, new Float32Array(MAX_SPAWNS_PER_FRAME * 4));
|
|
329
|
-
_ParticleEffect_spawnRawTex.set(this, null);
|
|
330
|
-
_ParticleEffect_spawnTexHandle.set(this, null);
|
|
331
|
-
_ParticleEffect_spawnGeometry.set(this, null);
|
|
332
|
-
_ParticleEffect_particleGeometry.set(this, null);
|
|
333
|
-
_ParticleEffect_gl.set(this, null);
|
|
334
|
-
_ParticleEffect_contextRestoredUnsub.set(this, null);
|
|
335
|
-
_ParticleEffect_nextSlot.set(this, 0);
|
|
336
|
-
_ParticleEffect_birthAccumulator.set(this, 0);
|
|
337
|
-
_ParticleEffect_screenBirthAccumulator.set(this, 0);
|
|
338
|
-
_ParticleEffect_lastMouseUv.set(this, null);
|
|
339
|
-
_ParticleEffect_lastMoveTime.set(this, -Infinity);
|
|
340
|
-
__classPrivateFieldSet(this, _ParticleEffect_params, { ...DEFAULT_PARAMS, ...initial }, "f");
|
|
341
|
-
__classPrivateFieldGet(this, _ParticleEffect_params, "f").count = sanitizeCount(__classPrivateFieldGet(this, _ParticleEffect_params, "f").count);
|
|
342
|
-
__classPrivateFieldSet(this, _ParticleEffect_stateSize, stateSizeFromCount(__classPrivateFieldGet(this, _ParticleEffect_params, "f").count), "f");
|
|
343
|
-
__classPrivateFieldSet(this, _ParticleEffect_stateCapacity, __classPrivateFieldGet(this, _ParticleEffect_stateSize, "f") * __classPrivateFieldGet(this, _ParticleEffect_stateSize, "f"), "f");
|
|
329
|
+
this.#params = { ...DEFAULT_PARAMS, ...initial };
|
|
330
|
+
this.#params.count = sanitizeCount(this.#params.count);
|
|
331
|
+
this.#stateSize = stateSizeFromCount(this.#params.count);
|
|
332
|
+
this.#stateCapacity = this.#stateSize * this.#stateSize;
|
|
344
333
|
}
|
|
345
334
|
get maxCount() {
|
|
346
|
-
return
|
|
335
|
+
return this.#stateCapacity;
|
|
347
336
|
}
|
|
348
337
|
/** Validated bulk setter. Pass any subset of params; bad inputs
|
|
349
338
|
* (NaN / negative `count`, etc) are coerced to valid values
|
|
350
339
|
* before applying. */
|
|
351
340
|
setParam(updates) {
|
|
352
|
-
const target =
|
|
341
|
+
const target = this.#params;
|
|
353
342
|
for (const [k, v] of Object.entries(updates)) {
|
|
354
343
|
if (v === undefined) {
|
|
355
344
|
continue;
|
|
@@ -358,68 +347,136 @@ export class ParticleEffect {
|
|
|
358
347
|
}
|
|
359
348
|
}
|
|
360
349
|
init(ctx) {
|
|
361
|
-
|
|
362
|
-
|
|
350
|
+
this.#allocStateRTs(ctx);
|
|
351
|
+
this.#stampTex = ctx.createRenderTarget({
|
|
363
352
|
float: false,
|
|
364
353
|
wrap: "clamp",
|
|
365
354
|
filter: "linear",
|
|
366
|
-
})
|
|
367
|
-
|
|
355
|
+
});
|
|
356
|
+
this.#trail = ctx.createRenderTarget({
|
|
368
357
|
float: false,
|
|
369
358
|
persistent: true,
|
|
370
359
|
wrap: "clamp",
|
|
371
360
|
filter: "linear",
|
|
372
|
-
})
|
|
373
|
-
|
|
361
|
+
});
|
|
362
|
+
this.#particleGeometry = {
|
|
374
363
|
attributes: { position: QUAD_VERTS },
|
|
375
|
-
}
|
|
376
|
-
|
|
364
|
+
};
|
|
365
|
+
this.#spawnGeometry = {
|
|
377
366
|
mode: "points",
|
|
378
367
|
attributes: {
|
|
379
368
|
position: { data: SPAWN_VERTEX_INDICES, itemSize: 1 },
|
|
380
369
|
},
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
})
|
|
370
|
+
};
|
|
371
|
+
this.#gl = ctx.gl;
|
|
372
|
+
this.#allocSpawnTextures(ctx);
|
|
373
|
+
this.#contextRestoredUnsub = ctx.onContextRestored(() => {
|
|
374
|
+
this.#gl = ctx.gl;
|
|
375
|
+
this.#allocSpawnTextures(ctx);
|
|
376
|
+
this.#initialized = false;
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
#allocStateRTs(ctx) {
|
|
380
|
+
const stateOpts = {
|
|
381
|
+
size: [this.#stateSize, this.#stateSize],
|
|
382
|
+
float: true,
|
|
383
|
+
wrap: "clamp",
|
|
384
|
+
filter: "nearest",
|
|
385
|
+
};
|
|
386
|
+
// pos uses auto-ping-pong (`persistent: true`); advect runs
|
|
387
|
+
// with `swap: false` so the subsequent spawn pass lands in the
|
|
388
|
+
// same internal buffer. color/vel are single-buffer — only the
|
|
389
|
+
// spawn pass writes them, other texels persist naturally.
|
|
390
|
+
this.#posTex = ctx.createRenderTarget({
|
|
391
|
+
...stateOpts,
|
|
392
|
+
persistent: true,
|
|
393
|
+
});
|
|
394
|
+
this.#colorTex = ctx.createRenderTarget(stateOpts);
|
|
395
|
+
this.#velTex = ctx.createRenderTarget(stateOpts);
|
|
396
|
+
}
|
|
397
|
+
#disposeStateRTs() {
|
|
398
|
+
this.#posTex?.dispose();
|
|
399
|
+
this.#colorTex?.dispose();
|
|
400
|
+
this.#velTex?.dispose();
|
|
401
|
+
this.#posTex = null;
|
|
402
|
+
this.#colorTex = null;
|
|
403
|
+
this.#velTex = null;
|
|
404
|
+
}
|
|
405
|
+
#createSpawnTex(ctx) {
|
|
406
|
+
const gl = ctx.gl;
|
|
407
|
+
const tex = gl.createTexture();
|
|
408
|
+
if (!tex) {
|
|
409
|
+
throw new Error("[ParticleEffect] Failed to create spawn texture");
|
|
410
|
+
}
|
|
411
|
+
gl.bindTexture(gl.TEXTURE_2D, tex);
|
|
412
|
+
// Counteract the global UNPACK_FLIP_Y_WEBGL=true that the
|
|
413
|
+
// framework sets for DOM source uploads — our spawn data is a
|
|
414
|
+
// raw float buffer with row 0 at the top, no flip wanted.
|
|
415
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
416
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, SPAWN_TEX_SIZE, SPAWN_TEX_SIZE, 0, gl.RGBA, gl.FLOAT, null);
|
|
417
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
418
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
419
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
420
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
421
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
422
|
+
const handle = ctx.wrapTexture(tex, {
|
|
423
|
+
size: [SPAWN_TEX_SIZE, SPAWN_TEX_SIZE],
|
|
424
|
+
filter: "nearest",
|
|
425
|
+
wrap: "clamp",
|
|
426
|
+
});
|
|
427
|
+
return { raw: tex, handle };
|
|
428
|
+
}
|
|
429
|
+
#allocSpawnTextures(ctx) {
|
|
430
|
+
const a = this.#createSpawnTex(ctx);
|
|
431
|
+
this.#spawnRawTex = a.raw;
|
|
432
|
+
this.#spawnTexHandle = a.handle;
|
|
433
|
+
}
|
|
434
|
+
#uploadSpawnTextures(ctx) {
|
|
435
|
+
if (!this.#spawnRawTex) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
const gl = ctx.gl;
|
|
439
|
+
// Reset framework's global UNPACK_FLIP_Y_WEBGL=true (set for
|
|
440
|
+
// DOM source uploads) so row 0 of our raw float buffer maps to
|
|
441
|
+
// row 0 of the texture.
|
|
442
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
443
|
+
gl.bindTexture(gl.TEXTURE_2D, this.#spawnRawTex);
|
|
444
|
+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, SPAWN_TEX_SIZE, SPAWN_TEX_SIZE, gl.RGBA, gl.FLOAT, this.#spawnUniform);
|
|
445
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
389
446
|
}
|
|
390
447
|
render(ctx) {
|
|
391
|
-
if (!
|
|
392
|
-
!
|
|
393
|
-
!
|
|
394
|
-
!
|
|
395
|
-
!
|
|
396
|
-
!
|
|
397
|
-
!
|
|
398
|
-
!
|
|
399
|
-
!
|
|
448
|
+
if (!this.#posTex ||
|
|
449
|
+
!this.#colorTex ||
|
|
450
|
+
!this.#velTex ||
|
|
451
|
+
!this.#stampTex ||
|
|
452
|
+
!this.#trail ||
|
|
453
|
+
!this.#particleGeometry ||
|
|
454
|
+
!this.#spawnGeometry ||
|
|
455
|
+
!this.#spawnTexHandle ||
|
|
456
|
+
!this.#spawnRawTex) {
|
|
400
457
|
return;
|
|
401
458
|
}
|
|
402
459
|
// Resize state RTs if `params.count` crossed a power-of-two
|
|
403
460
|
// boundary. dispose-then-create keeps peak GPU memory minimal;
|
|
404
461
|
// the FRAG_INIT pass below seeds the fresh RTs.
|
|
405
462
|
const newSize = stateSizeFromCount(this.params.count);
|
|
406
|
-
if (newSize !==
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
463
|
+
if (newSize !== this.#stateSize) {
|
|
464
|
+
this.#disposeStateRTs();
|
|
465
|
+
this.#stateSize = newSize;
|
|
466
|
+
this.#stateCapacity = newSize * newSize;
|
|
467
|
+
this.#allocStateRTs(ctx);
|
|
468
|
+
this.#nextSlot = 0;
|
|
469
|
+
this.#initialized = false;
|
|
413
470
|
}
|
|
414
|
-
if (!
|
|
471
|
+
if (!this.#initialized) {
|
|
415
472
|
// posTex's "stale" half gets overwritten on the first
|
|
416
473
|
// advect+swap, so a single init draw is enough. color/vel
|
|
417
474
|
// are single-buffer; one clear gives every dead slot known
|
|
418
475
|
// contents until the spawn pass writes it.
|
|
419
|
-
ctx.draw({ frag: FRAG_INIT_POS, target:
|
|
420
|
-
ctx.draw({ frag: FRAG_INIT_COLOR, target:
|
|
421
|
-
ctx.draw({ frag: FRAG_INIT_COLOR, target:
|
|
422
|
-
|
|
476
|
+
ctx.draw({ frag: FRAG_INIT_POS, target: this.#posTex });
|
|
477
|
+
ctx.draw({ frag: FRAG_INIT_COLOR, target: this.#colorTex });
|
|
478
|
+
ctx.draw({ frag: FRAG_INIT_COLOR, target: this.#velTex });
|
|
479
|
+
this.#initialized = true;
|
|
423
480
|
}
|
|
424
481
|
const dt = clampDt(ctx.deltaTime);
|
|
425
482
|
const elementPixel = [
|
|
@@ -427,12 +484,12 @@ export class ParticleEffect {
|
|
|
427
484
|
ctx.dims.elementPixel[1],
|
|
428
485
|
];
|
|
429
486
|
const stateSizeVec = [
|
|
430
|
-
|
|
431
|
-
|
|
487
|
+
this.#stateSize,
|
|
488
|
+
this.#stateSize,
|
|
432
489
|
];
|
|
433
|
-
const nSpawn =
|
|
490
|
+
const nSpawn = this.#scheduleSpawns(ctx, dt, elementPixel);
|
|
434
491
|
if (nSpawn > 0) {
|
|
435
|
-
|
|
492
|
+
this.#uploadSpawnTextures(ctx);
|
|
436
493
|
}
|
|
437
494
|
// advect skips its swap when a spawn pass follows, so both
|
|
438
495
|
// passes write into the same internal buffer of the persistent
|
|
@@ -442,9 +499,9 @@ export class ParticleEffect {
|
|
|
442
499
|
ctx.draw({
|
|
443
500
|
frag: FRAG_ADVECT_POS,
|
|
444
501
|
uniforms: {
|
|
445
|
-
posTex:
|
|
446
|
-
colorTex:
|
|
447
|
-
velTex:
|
|
502
|
+
posTex: this.#posTex,
|
|
503
|
+
colorTex: this.#colorTex,
|
|
504
|
+
velTex: this.#velTex,
|
|
448
505
|
elementPixel,
|
|
449
506
|
time: ctx.time,
|
|
450
507
|
dt,
|
|
@@ -456,12 +513,12 @@ export class ParticleEffect {
|
|
|
456
513
|
speedDecay: this.params.speedDecay,
|
|
457
514
|
life: this.params.life,
|
|
458
515
|
},
|
|
459
|
-
target:
|
|
516
|
+
target: this.#posTex,
|
|
460
517
|
swap: advectSwap,
|
|
461
518
|
});
|
|
462
519
|
if (nSpawn > 0) {
|
|
463
520
|
const spawnUniforms = {
|
|
464
|
-
uSpawnTex:
|
|
521
|
+
uSpawnTex: this.#spawnTexHandle,
|
|
465
522
|
uSpawnTexSize: SPAWN_TEX_SIZE_VEC,
|
|
466
523
|
uSpawnCount: nSpawn,
|
|
467
524
|
stateSize: stateSizeVec,
|
|
@@ -469,19 +526,19 @@ export class ParticleEffect {
|
|
|
469
526
|
ctx.draw({
|
|
470
527
|
vert: VERT_SPAWN,
|
|
471
528
|
frag: FRAG_SPAWN_POS,
|
|
472
|
-
geometry:
|
|
529
|
+
geometry: this.#spawnGeometry,
|
|
473
530
|
uniforms: {
|
|
474
531
|
...spawnUniforms,
|
|
475
532
|
src: ctx.src,
|
|
476
533
|
alphaThreshold: this.params.alphaThreshold,
|
|
477
534
|
},
|
|
478
|
-
target:
|
|
535
|
+
target: this.#posTex,
|
|
479
536
|
blend: "none",
|
|
480
537
|
});
|
|
481
538
|
ctx.draw({
|
|
482
539
|
vert: VERT_SPAWN,
|
|
483
540
|
frag: FRAG_SPAWN_COLOR,
|
|
484
|
-
geometry:
|
|
541
|
+
geometry: this.#spawnGeometry,
|
|
485
542
|
uniforms: {
|
|
486
543
|
...spawnUniforms,
|
|
487
544
|
src: ctx.src,
|
|
@@ -489,27 +546,27 @@ export class ParticleEffect {
|
|
|
489
546
|
colorMix: this.params.colorMix,
|
|
490
547
|
lifeJitterRange: [LIFE_JITTER_MIN, LIFE_JITTER_MAX],
|
|
491
548
|
},
|
|
492
|
-
target:
|
|
549
|
+
target: this.#colorTex,
|
|
493
550
|
blend: "none",
|
|
494
551
|
});
|
|
495
552
|
ctx.draw({
|
|
496
553
|
vert: VERT_SPAWN,
|
|
497
554
|
frag: FRAG_SPAWN_VEL,
|
|
498
|
-
geometry:
|
|
555
|
+
geometry: this.#spawnGeometry,
|
|
499
556
|
uniforms: spawnUniforms,
|
|
500
|
-
target:
|
|
557
|
+
target: this.#velTex,
|
|
501
558
|
blend: "none",
|
|
502
559
|
});
|
|
503
560
|
}
|
|
504
|
-
const cap =
|
|
505
|
-
|
|
506
|
-
ctx.clear(
|
|
561
|
+
const cap = this.#cap();
|
|
562
|
+
this.#particleGeometry.instanceCount = cap;
|
|
563
|
+
ctx.clear(this.#stampTex);
|
|
507
564
|
ctx.draw({
|
|
508
565
|
vert: VERT_PARTICLE,
|
|
509
566
|
frag: FRAG_PARTICLE,
|
|
510
567
|
uniforms: {
|
|
511
|
-
posTex:
|
|
512
|
-
colorTex:
|
|
568
|
+
posTex: this.#posTex,
|
|
569
|
+
colorTex: this.#colorTex,
|
|
513
570
|
stateSize: stateSizeVec,
|
|
514
571
|
pointSize: this.params.pointSize,
|
|
515
572
|
elementPixel,
|
|
@@ -519,188 +576,126 @@ export class ParticleEffect {
|
|
|
519
576
|
fadeIn: this.params.fadeIn,
|
|
520
577
|
fog: this.params.fog,
|
|
521
578
|
},
|
|
522
|
-
geometry:
|
|
523
|
-
target:
|
|
579
|
+
geometry: this.#particleGeometry,
|
|
580
|
+
target: this.#stampTex,
|
|
524
581
|
blend: this.params.blend === "normal" ? "premultiplied" : "additive",
|
|
525
582
|
});
|
|
526
583
|
ctx.draw({
|
|
527
584
|
frag: FRAG_TRAIL_COMPOSITE,
|
|
528
585
|
uniforms: {
|
|
529
|
-
trailPrev:
|
|
530
|
-
particleStamp:
|
|
586
|
+
trailPrev: this.#trail,
|
|
587
|
+
particleStamp: this.#stampTex,
|
|
531
588
|
trailFade: this.params.trailFade,
|
|
532
589
|
blendMode: this.params.blend === "normal" ? 1 : 0,
|
|
533
590
|
},
|
|
534
|
-
target:
|
|
591
|
+
target: this.#trail,
|
|
535
592
|
});
|
|
536
593
|
ctx.draw({
|
|
537
594
|
frag: FRAG_OUTPUT,
|
|
538
595
|
uniforms: {
|
|
539
596
|
src: ctx.src,
|
|
540
|
-
trail:
|
|
597
|
+
trail: this.#trail,
|
|
541
598
|
srcOpacity: this.params.srcOpacity,
|
|
542
599
|
},
|
|
543
600
|
target: ctx.target,
|
|
544
601
|
});
|
|
545
602
|
}
|
|
603
|
+
#scheduleSpawns(ctx, dt, elementPixel) {
|
|
604
|
+
const mouseUv = [
|
|
605
|
+
ctx.mouse[0] / Math.max(1, elementPixel[0]),
|
|
606
|
+
ctx.mouse[1] / Math.max(1, elementPixel[1]),
|
|
607
|
+
];
|
|
608
|
+
const moved = !this.#lastMouseUv ||
|
|
609
|
+
Math.abs(mouseUv[0] - this.#lastMouseUv[0]) > 1e-6 ||
|
|
610
|
+
Math.abs(mouseUv[1] - this.#lastMouseUv[1]) > 1e-6;
|
|
611
|
+
if (moved) {
|
|
612
|
+
this.#lastMoveTime = ctx.time;
|
|
613
|
+
}
|
|
614
|
+
this.#lastMouseUv = mouseUv;
|
|
615
|
+
const visible = ctx.intersection > 0;
|
|
616
|
+
const recent = ctx.time - this.#lastMoveTime < IDLE_THRESHOLD;
|
|
617
|
+
const mouseActive = visible && (recent || this.params.spawnOnIdle);
|
|
618
|
+
// Cap accumulators at the per-frame budget so a birthRate that
|
|
619
|
+
// exceeds MAX_SPAWNS_PER_FRAME × fps doesn't queue indefinitely
|
|
620
|
+
// and bleed spawns past when the user expects (e.g., still
|
|
621
|
+
// emitting after the cursor stops on a high birthRate setting).
|
|
622
|
+
if (mouseActive) {
|
|
623
|
+
this.#birthAccumulator = Math.min(this.#birthAccumulator + this.params.birthRate * dt, MAX_SPAWNS_PER_FRAME);
|
|
624
|
+
}
|
|
625
|
+
else {
|
|
626
|
+
this.#birthAccumulator = 0;
|
|
627
|
+
}
|
|
628
|
+
if (visible) {
|
|
629
|
+
this.#screenBirthAccumulator = Math.min(this.#screenBirthAccumulator + this.params.screenBirthRate * dt, MAX_SPAWNS_PER_FRAME);
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
this.#screenBirthAccumulator = 0;
|
|
633
|
+
}
|
|
634
|
+
// Mouse takes priority for the per-frame slot budget — when the
|
|
635
|
+
// user is dragging fast we'd rather show that than the ambient.
|
|
636
|
+
const nMouse = Math.min(MAX_SPAWNS_PER_FRAME, Math.floor(this.#birthAccumulator));
|
|
637
|
+
const nScreen = Math.min(MAX_SPAWNS_PER_FRAME - nMouse, Math.floor(this.#screenBirthAccumulator));
|
|
638
|
+
this.#birthAccumulator -= nMouse;
|
|
639
|
+
this.#screenBirthAccumulator -= nScreen;
|
|
640
|
+
const total = nMouse + nScreen;
|
|
641
|
+
if (total === 0) {
|
|
642
|
+
return 0;
|
|
643
|
+
}
|
|
644
|
+
const cap = this.#cap();
|
|
645
|
+
const elemPxX = Math.max(1, elementPixel[0]);
|
|
646
|
+
const elemPxY = Math.max(1, elementPixel[1]);
|
|
647
|
+
const buf = this.#spawnUniform;
|
|
648
|
+
let i = 0;
|
|
649
|
+
// .w = theta in [0, 2π) for mouse spawns. FRAG_SPAWN_VEL turns
|
|
650
|
+
// it into a unit (cosθ, sinθ) in pixel space; FRAG_ADVECT_POS
|
|
651
|
+
// divides by elementPixel for circular symmetry on non-square
|
|
652
|
+
// elements.
|
|
653
|
+
for (; i < nMouse; i++) {
|
|
654
|
+
const r = Math.sqrt(Math.random()) * this.params.radius;
|
|
655
|
+
const theta = Math.random() * Math.PI * 2;
|
|
656
|
+
const dx = Math.cos(theta) * r;
|
|
657
|
+
const dy = Math.sin(theta) * r;
|
|
658
|
+
const o = i * 4;
|
|
659
|
+
buf[o + 0] = this.#nextSlot;
|
|
660
|
+
buf[o + 1] = mouseUv[0] + dx / elemPxX;
|
|
661
|
+
buf[o + 2] = mouseUv[1] + dy / elemPxY;
|
|
662
|
+
buf[o + 3] = theta;
|
|
663
|
+
this.#nextSlot = (this.#nextSlot + 1) % cap;
|
|
664
|
+
}
|
|
665
|
+
// .w = -1 sentinel: no radial impulse (curl-noise only).
|
|
666
|
+
for (let j = 0; j < nScreen; j++, i++) {
|
|
667
|
+
const o = i * 4;
|
|
668
|
+
buf[o + 0] = this.#nextSlot;
|
|
669
|
+
buf[o + 1] = Math.random();
|
|
670
|
+
buf[o + 2] = Math.random();
|
|
671
|
+
buf[o + 3] = -1;
|
|
672
|
+
this.#nextSlot = (this.#nextSlot + 1) % cap;
|
|
673
|
+
}
|
|
674
|
+
return total;
|
|
675
|
+
}
|
|
676
|
+
#cap() {
|
|
677
|
+
return Math.max(1, Math.min(this.#stateCapacity, Math.floor(this.params.count)));
|
|
678
|
+
}
|
|
546
679
|
dispose() {
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
if (
|
|
550
|
-
|
|
680
|
+
this.#contextRestoredUnsub?.();
|
|
681
|
+
this.#contextRestoredUnsub = null;
|
|
682
|
+
if (this.#gl && this.#spawnRawTex) {
|
|
683
|
+
this.#gl.deleteTexture(this.#spawnRawTex);
|
|
551
684
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
685
|
+
this.#spawnRawTex = null;
|
|
686
|
+
this.#spawnTexHandle = null;
|
|
687
|
+
this.#spawnGeometry = null;
|
|
688
|
+
this.#gl = null;
|
|
689
|
+
this.#disposeStateRTs();
|
|
690
|
+
this.#stampTex?.dispose();
|
|
691
|
+
this.#trail?.dispose();
|
|
692
|
+
this.#stampTex = null;
|
|
693
|
+
this.#trail = null;
|
|
694
|
+
this.#particleGeometry = null;
|
|
695
|
+
this.#initialized = false;
|
|
563
696
|
}
|
|
564
697
|
outputRect(dims) {
|
|
565
698
|
return dims.canvasRect;
|
|
566
699
|
}
|
|
567
700
|
}
|
|
568
|
-
_ParticleEffect_params = new WeakMap(), _ParticleEffect_posTex = new WeakMap(), _ParticleEffect_colorTex = new WeakMap(), _ParticleEffect_velTex = new WeakMap(), _ParticleEffect_stampTex = new WeakMap(), _ParticleEffect_trail = new WeakMap(), _ParticleEffect_initialized = new WeakMap(), _ParticleEffect_stateSize = new WeakMap(), _ParticleEffect_stateCapacity = new WeakMap(), _ParticleEffect_spawnUniform = new WeakMap(), _ParticleEffect_spawnRawTex = new WeakMap(), _ParticleEffect_spawnTexHandle = new WeakMap(), _ParticleEffect_spawnGeometry = new WeakMap(), _ParticleEffect_particleGeometry = new WeakMap(), _ParticleEffect_gl = new WeakMap(), _ParticleEffect_contextRestoredUnsub = new WeakMap(), _ParticleEffect_nextSlot = new WeakMap(), _ParticleEffect_birthAccumulator = new WeakMap(), _ParticleEffect_screenBirthAccumulator = new WeakMap(), _ParticleEffect_lastMouseUv = new WeakMap(), _ParticleEffect_lastMoveTime = new WeakMap(), _ParticleEffect_instances = new WeakSet(), _ParticleEffect_allocStateRTs = function _ParticleEffect_allocStateRTs(ctx) {
|
|
569
|
-
const stateOpts = {
|
|
570
|
-
size: [__classPrivateFieldGet(this, _ParticleEffect_stateSize, "f"), __classPrivateFieldGet(this, _ParticleEffect_stateSize, "f")],
|
|
571
|
-
float: true,
|
|
572
|
-
wrap: "clamp",
|
|
573
|
-
filter: "nearest",
|
|
574
|
-
};
|
|
575
|
-
// pos uses auto-ping-pong (`persistent: true`); advect runs
|
|
576
|
-
// with `swap: false` so the subsequent spawn pass lands in the
|
|
577
|
-
// same internal buffer. color/vel are single-buffer — only the
|
|
578
|
-
// spawn pass writes them, other texels persist naturally.
|
|
579
|
-
__classPrivateFieldSet(this, _ParticleEffect_posTex, ctx.createRenderTarget({
|
|
580
|
-
...stateOpts,
|
|
581
|
-
persistent: true,
|
|
582
|
-
}), "f");
|
|
583
|
-
__classPrivateFieldSet(this, _ParticleEffect_colorTex, ctx.createRenderTarget(stateOpts), "f");
|
|
584
|
-
__classPrivateFieldSet(this, _ParticleEffect_velTex, ctx.createRenderTarget(stateOpts), "f");
|
|
585
|
-
}, _ParticleEffect_disposeStateRTs = function _ParticleEffect_disposeStateRTs() {
|
|
586
|
-
__classPrivateFieldGet(this, _ParticleEffect_posTex, "f")?.dispose();
|
|
587
|
-
__classPrivateFieldGet(this, _ParticleEffect_colorTex, "f")?.dispose();
|
|
588
|
-
__classPrivateFieldGet(this, _ParticleEffect_velTex, "f")?.dispose();
|
|
589
|
-
__classPrivateFieldSet(this, _ParticleEffect_posTex, null, "f");
|
|
590
|
-
__classPrivateFieldSet(this, _ParticleEffect_colorTex, null, "f");
|
|
591
|
-
__classPrivateFieldSet(this, _ParticleEffect_velTex, null, "f");
|
|
592
|
-
}, _ParticleEffect_createSpawnTex = function _ParticleEffect_createSpawnTex(ctx) {
|
|
593
|
-
const gl = ctx.gl;
|
|
594
|
-
const tex = gl.createTexture();
|
|
595
|
-
if (!tex) {
|
|
596
|
-
throw new Error("[ParticleEffect] Failed to create spawn texture");
|
|
597
|
-
}
|
|
598
|
-
gl.bindTexture(gl.TEXTURE_2D, tex);
|
|
599
|
-
// Counteract the global UNPACK_FLIP_Y_WEBGL=true that the
|
|
600
|
-
// framework sets for DOM source uploads — our spawn data is a
|
|
601
|
-
// raw float buffer with row 0 at the top, no flip wanted.
|
|
602
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
603
|
-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, SPAWN_TEX_SIZE, SPAWN_TEX_SIZE, 0, gl.RGBA, gl.FLOAT, null);
|
|
604
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
605
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
606
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
607
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
608
|
-
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
609
|
-
const handle = ctx.wrapTexture(tex, {
|
|
610
|
-
size: [SPAWN_TEX_SIZE, SPAWN_TEX_SIZE],
|
|
611
|
-
filter: "nearest",
|
|
612
|
-
wrap: "clamp",
|
|
613
|
-
});
|
|
614
|
-
return { raw: tex, handle };
|
|
615
|
-
}, _ParticleEffect_allocSpawnTextures = function _ParticleEffect_allocSpawnTextures(ctx) {
|
|
616
|
-
const a = __classPrivateFieldGet(this, _ParticleEffect_instances, "m", _ParticleEffect_createSpawnTex).call(this, ctx);
|
|
617
|
-
__classPrivateFieldSet(this, _ParticleEffect_spawnRawTex, a.raw, "f");
|
|
618
|
-
__classPrivateFieldSet(this, _ParticleEffect_spawnTexHandle, a.handle, "f");
|
|
619
|
-
}, _ParticleEffect_uploadSpawnTextures = function _ParticleEffect_uploadSpawnTextures(ctx) {
|
|
620
|
-
if (!__classPrivateFieldGet(this, _ParticleEffect_spawnRawTex, "f")) {
|
|
621
|
-
return;
|
|
622
|
-
}
|
|
623
|
-
const gl = ctx.gl;
|
|
624
|
-
// Reset framework's global UNPACK_FLIP_Y_WEBGL=true (set for
|
|
625
|
-
// DOM source uploads) so row 0 of our raw float buffer maps to
|
|
626
|
-
// row 0 of the texture.
|
|
627
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
628
|
-
gl.bindTexture(gl.TEXTURE_2D, __classPrivateFieldGet(this, _ParticleEffect_spawnRawTex, "f"));
|
|
629
|
-
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, SPAWN_TEX_SIZE, SPAWN_TEX_SIZE, gl.RGBA, gl.FLOAT, __classPrivateFieldGet(this, _ParticleEffect_spawnUniform, "f"));
|
|
630
|
-
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
631
|
-
}, _ParticleEffect_scheduleSpawns = function _ParticleEffect_scheduleSpawns(ctx, dt, elementPixel) {
|
|
632
|
-
const mouseUv = [
|
|
633
|
-
ctx.mouse[0] / Math.max(1, elementPixel[0]),
|
|
634
|
-
ctx.mouse[1] / Math.max(1, elementPixel[1]),
|
|
635
|
-
];
|
|
636
|
-
const moved = !__classPrivateFieldGet(this, _ParticleEffect_lastMouseUv, "f") ||
|
|
637
|
-
Math.abs(mouseUv[0] - __classPrivateFieldGet(this, _ParticleEffect_lastMouseUv, "f")[0]) > 1e-6 ||
|
|
638
|
-
Math.abs(mouseUv[1] - __classPrivateFieldGet(this, _ParticleEffect_lastMouseUv, "f")[1]) > 1e-6;
|
|
639
|
-
if (moved) {
|
|
640
|
-
__classPrivateFieldSet(this, _ParticleEffect_lastMoveTime, ctx.time, "f");
|
|
641
|
-
}
|
|
642
|
-
__classPrivateFieldSet(this, _ParticleEffect_lastMouseUv, mouseUv, "f");
|
|
643
|
-
const visible = ctx.intersection > 0;
|
|
644
|
-
const recent = ctx.time - __classPrivateFieldGet(this, _ParticleEffect_lastMoveTime, "f") < IDLE_THRESHOLD;
|
|
645
|
-
const mouseActive = visible && (recent || this.params.spawnOnIdle);
|
|
646
|
-
// Cap accumulators at the per-frame budget so a birthRate that
|
|
647
|
-
// exceeds MAX_SPAWNS_PER_FRAME × fps doesn't queue indefinitely
|
|
648
|
-
// and bleed spawns past when the user expects (e.g., still
|
|
649
|
-
// emitting after the cursor stops on a high birthRate setting).
|
|
650
|
-
if (mouseActive) {
|
|
651
|
-
__classPrivateFieldSet(this, _ParticleEffect_birthAccumulator, Math.min(__classPrivateFieldGet(this, _ParticleEffect_birthAccumulator, "f") + this.params.birthRate * dt, MAX_SPAWNS_PER_FRAME), "f");
|
|
652
|
-
}
|
|
653
|
-
else {
|
|
654
|
-
__classPrivateFieldSet(this, _ParticleEffect_birthAccumulator, 0, "f");
|
|
655
|
-
}
|
|
656
|
-
if (visible) {
|
|
657
|
-
__classPrivateFieldSet(this, _ParticleEffect_screenBirthAccumulator, Math.min(__classPrivateFieldGet(this, _ParticleEffect_screenBirthAccumulator, "f") + this.params.screenBirthRate * dt, MAX_SPAWNS_PER_FRAME), "f");
|
|
658
|
-
}
|
|
659
|
-
else {
|
|
660
|
-
__classPrivateFieldSet(this, _ParticleEffect_screenBirthAccumulator, 0, "f");
|
|
661
|
-
}
|
|
662
|
-
// Mouse takes priority for the per-frame slot budget — when the
|
|
663
|
-
// user is dragging fast we'd rather show that than the ambient.
|
|
664
|
-
const nMouse = Math.min(MAX_SPAWNS_PER_FRAME, Math.floor(__classPrivateFieldGet(this, _ParticleEffect_birthAccumulator, "f")));
|
|
665
|
-
const nScreen = Math.min(MAX_SPAWNS_PER_FRAME - nMouse, Math.floor(__classPrivateFieldGet(this, _ParticleEffect_screenBirthAccumulator, "f")));
|
|
666
|
-
__classPrivateFieldSet(this, _ParticleEffect_birthAccumulator, __classPrivateFieldGet(this, _ParticleEffect_birthAccumulator, "f") - nMouse, "f");
|
|
667
|
-
__classPrivateFieldSet(this, _ParticleEffect_screenBirthAccumulator, __classPrivateFieldGet(this, _ParticleEffect_screenBirthAccumulator, "f") - nScreen, "f");
|
|
668
|
-
const total = nMouse + nScreen;
|
|
669
|
-
if (total === 0) {
|
|
670
|
-
return 0;
|
|
671
|
-
}
|
|
672
|
-
const cap = __classPrivateFieldGet(this, _ParticleEffect_instances, "m", _ParticleEffect_cap).call(this);
|
|
673
|
-
const elemPxX = Math.max(1, elementPixel[0]);
|
|
674
|
-
const elemPxY = Math.max(1, elementPixel[1]);
|
|
675
|
-
const buf = __classPrivateFieldGet(this, _ParticleEffect_spawnUniform, "f");
|
|
676
|
-
let i = 0;
|
|
677
|
-
// .w = theta in [0, 2π) for mouse spawns. FRAG_SPAWN_VEL turns
|
|
678
|
-
// it into a unit (cosθ, sinθ) in pixel space; FRAG_ADVECT_POS
|
|
679
|
-
// divides by elementPixel for circular symmetry on non-square
|
|
680
|
-
// elements.
|
|
681
|
-
for (; i < nMouse; i++) {
|
|
682
|
-
const r = Math.sqrt(Math.random()) * this.params.radius;
|
|
683
|
-
const theta = Math.random() * Math.PI * 2;
|
|
684
|
-
const dx = Math.cos(theta) * r;
|
|
685
|
-
const dy = Math.sin(theta) * r;
|
|
686
|
-
const o = i * 4;
|
|
687
|
-
buf[o + 0] = __classPrivateFieldGet(this, _ParticleEffect_nextSlot, "f");
|
|
688
|
-
buf[o + 1] = mouseUv[0] + dx / elemPxX;
|
|
689
|
-
buf[o + 2] = mouseUv[1] + dy / elemPxY;
|
|
690
|
-
buf[o + 3] = theta;
|
|
691
|
-
__classPrivateFieldSet(this, _ParticleEffect_nextSlot, (__classPrivateFieldGet(this, _ParticleEffect_nextSlot, "f") + 1) % cap, "f");
|
|
692
|
-
}
|
|
693
|
-
// .w = -1 sentinel: no radial impulse (curl-noise only).
|
|
694
|
-
for (let j = 0; j < nScreen; j++, i++) {
|
|
695
|
-
const o = i * 4;
|
|
696
|
-
buf[o + 0] = __classPrivateFieldGet(this, _ParticleEffect_nextSlot, "f");
|
|
697
|
-
buf[o + 1] = Math.random();
|
|
698
|
-
buf[o + 2] = Math.random();
|
|
699
|
-
buf[o + 3] = -1;
|
|
700
|
-
__classPrivateFieldSet(this, _ParticleEffect_nextSlot, (__classPrivateFieldGet(this, _ParticleEffect_nextSlot, "f") + 1) % cap, "f");
|
|
701
|
-
}
|
|
702
|
-
return total;
|
|
703
|
-
}, _ParticleEffect_cap = function _ParticleEffect_cap() {
|
|
704
|
-
return Math.max(1, Math.min(__classPrivateFieldGet(this, _ParticleEffect_stateCapacity, "f"), Math.floor(this.params.count)));
|
|
705
|
-
};
|
|
706
701
|
//# sourceMappingURL=particle.js.map
|