Skip to content

Commit

Permalink
Prototype fix to remove unused pool elements from THREE.js scene graph
Browse files Browse the repository at this point in the history
  • Loading branch information
diarmidmackenzie committed Dec 17, 2022
1 parent 1ed1b1e commit ef55c88
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/components/scene/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ module.exports.Component = registerComponent('pool', {
el.pause();
this.container.appendChild(el);
this.availableEls.push(el);

var usedEls = this.usedEls;
el.addEventListener('loaded', function () {
if (usedEls.indexOf(el) !== -1) { return; }
el.object3DParent = el.object3D.parent;
el.object3D.parent.remove(el.object3D);
});
},

/**
Expand Down Expand Up @@ -94,6 +101,9 @@ module.exports.Component = registerComponent('pool', {
}
el = this.availableEls.shift();
this.usedEls.push(el);
if (el.object3DParent) {
el.object3DParent.add(el.object3D);
}
el.object3D.visible = true;
return el;
},
Expand All @@ -110,6 +120,8 @@ module.exports.Component = registerComponent('pool', {
this.usedEls.splice(index, 1);
this.availableEls.push(el);
el.object3D.visible = false;
el.object3DParent = el.object3D.parent;
el.object3D.parent.remove(el.object3D);
el.pause();
return el;
}
Expand Down

0 comments on commit ef55c88

Please sign in to comment.