Skip to content

Commit

Permalink
add removeAll for particles, fix bug where particle transitions weren…
Browse files Browse the repository at this point in the history
…'t properly cleaned up
  • Loading branch information
aeroheim committed Apr 5, 2021
1 parent 67bf5fa commit 6e186ee
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/effects/particles.ts
Expand Up @@ -126,14 +126,10 @@ class Particles {
* @param {ParticleGroupConfig | ParticleGroupConfig[]} config - a single or array of particle group configurations.
*/
generate(configs: ParticleGroupConfig | ParticleGroupConfig[]): void {
configs = Array.isArray(configs) ? configs : [configs];

// cleanup previous configs and objects
this._positions = [];
this._groups = {};
this._particles.geometry.dispose();
(this._particles.material as ShaderMaterial).dispose();
this.removeAll();

configs = Array.isArray(configs) ? configs : [configs];
let index = 0;
for (const config of configs) {
const {
Expand Down Expand Up @@ -192,6 +188,23 @@ class Particles {
this._particles.material = material;
}

/**
* Removes all particle groups.
*/
removeAll(): void {
for (const group in this._groups) {
// stop any ongoing transitions
this._groups[group].positionTransition.stop();
this._groups[group].swayTransition.stop();
}

// reset particles to empty
this._positions = [];
this._groups = {};
this._particles.geometry.dispose();
(this._particles.material as ShaderMaterial).dispose();
}

/**
* Calculates a new position based off an existing position and optional offset. Will wrap around boundaries.
* @param {Vector2} position - the current position.
Expand Down Expand Up @@ -377,7 +390,7 @@ class Particles {
* Updates the positions of the particles. Should be called on every render frame.
*/
update(): void {
const { attributes } = (this._particles.geometry as BufferGeometry);
const { attributes } = this._particles.geometry;
const {
position: positions,
size: sizes,
Expand Down

0 comments on commit 6e186ee

Please sign in to comment.