Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threejs remove all together object from scene

I tried to make a function to remove all object from scene in one shoot but it removes only one object for invocation.

GeometryModel.prototype.clearScene = function(scene) {
var i;
for(i=0; i < scene.children.length; i++){
     obj = scene.children[i];
     scene.remove(obj);
  }
}

another solution I tried and that works is this:

scene.children={};

but I am not sure if it is correct.

like image 429
Stefano Maglione Avatar asked Apr 02 '15 16:04

Stefano Maglione


People also ask

How to remove object threejs?

Whenever you create an instance of Texture, three. js internally creates an instance of WebGLTexture. Similar to buffers, this object can only be deleted by calling Texture. dispose().

What are three js scenes?

Scenes allow you to set up what and where is to be rendered by three. js. This is where you place objects, lights and cameras.


1 Answers

You have to do the opposite:

for( var i = scene.children.length - 1; i >= 0; i--) { 
     obj = scene.children[i];
     scene.remove(obj); 
}

because in each iteration the .children array changes once you do a .remove() from the start and the indexing of that array changes.

If you want to understand it better, unroll the for loop and follow what the index into the array is.

like image 181
gaitat Avatar answered Sep 28 '22 10:09

gaitat