Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to remove a mesh completely from the scene in three.js?

How can I remove meshes from three.js scene completely without causing memory leaks. I could find that loading the same models again and again causes the browser to crash, so it seems like the memory is not being deallocated.

like image 286
sreesreenu Avatar asked Nov 19 '16 15:11

sreesreenu


1 Answers

Use the dispose method on the geometry and material. Also, ensure that nothing is holding references to these objects, as that will prevent garbage collection.

var myMesh = new THREE.Mesh(geo, mat);
scene.add(myMesh);
//...
scene.remove(myMesh);
myMesh.geometry.dispose();
myMesh.material.dispose();
myMesh = undefined;
like image 100
TheJim01 Avatar answered Oct 28 '22 00:10

TheJim01