To effectively clean up your scene, you can iterate through the objects in the scene and selectively remove certain Meshes based on their attributes. For example, you can set a specific attribute or name on the Meshes you want to keep, and then check for that attribute when removing objects.
Here is an Example function clearScene()
that removes all Meshes from the scene which have the keepMe
attribute set to false or do not have this attribute at all:
floor = new THREE.Mesh( /* ... */ );
floor.userData = { keepMe: true };
// ...
function clearScene() {
var to_remove = [];
scene.traverse ( function( child ) {
if ( child instanceof THREE.Mesh && !child.userData.keepMe === true ) {
to_remove.push( child );
}
} );
for ( var i = 0; i < to_remove.length; i++ ) {
scene.remove( to_remove[i] );
}
}
Jsfiddle: http://jsfiddle.net/L0rdzbej/138/
A fix for the removal bug mentioned in this answer by @gaitat