I know there are plenty of SO questions asking similar things about removing everything from the scene, however everything I have tried doesn't work the way I was hoping. There also doesn't seem to be a definitive way of removing everything from the scene (in the docs or anything like that), and so I am hoping that someone can tell me the correct way to do it.
Below is the function I am currently using to remove everything from the scene:
function removeAll() {
while (scene.children.length > 0) {
scene.remove(scene.children[0]);
if (scene.children[0] == THREE.Mesh || scene.children[0] == THREE.Object3D || scene.children[0] == THREE.Group) {
scene.children[0].dispose();
scene.children[0].geometry.dispose();
scene.children[0].material.dispose();
}
}
}
This works okay, and seems to visually remove everything from the scene.
My problem is that if I call this function and then re-call the function that creates the scene, or a function that creates a new scene (see context section, I have multiple), there are more objects in the scene than there were after the first call of the scene creation function (init()
) - checked with renderer.info
in the console.
I also notice that after calling the removeAll()
function and then reloading the scene, the more I do this the laggier the scene gets, which I assume is because not everything is being removed properly.
So I ask:
What is the correct way to remove everything from the scene.
For context:
I have a HTML menu where the user can choose which scene they want to "skip" to. I need to remove everything when they do this and then reload only the objects for that particular scene. I have all of this functionality working, the only problem is with the removal of the objects before the new scene loads.