在three.js中从场景中完全删除网格的正确方法是什么? [英] What is the right way to remove a mesh completely from the scene in three.js?

查看:706
本文介绍了在three.js中从场景中完全删除网格的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何完全从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.

推荐答案

在几何体和材质上使用 dispose 方法。此外,确保没有任何东西持有对这些对象的引用,因为这会阻止垃圾回收。

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;

这篇关于在three.js中从场景中完全删除网格的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆