我正在使用OBJLoader.js.我有一个按钮,点击从场景中删除一个3d模型,并添加一个新的模型,但每次加载模型,chrome的内存使用量增加约50 MB,而不是减少.我试过“dispose()”和null,但没有太多的用途.有一个动画功能,每秒都会使用渲染器渲染场景.
function freeMemory(model) { if (model instanceof THREE.Mesh) { var texMat = model.material; texMat.map.dispose(); texMat.map = null; texMat.dispose(); texMat = null; model.geometry.dispose(); model.geometry = null; model = null; } } function loadModel() { scene.remove(model); freeMemory(model); var loader = new THREE.OBJLoader(); loader.load('test3d.obj',function(object) { object.children[0].geometry.computeFaceNormals(); geometry = object.children[0].geometry; console.log(geometry); geometry.dynamic = true; var modelTexture = THREE.ImageUtils.loadTexture("models/tex_0.jpg"); object.traverse(function(child) { if (child instanceof THREE.Mesh) { child.material.map = modelTexture; model = child; model.scale.set(0.5,0.5,0.5); model.translateOnAxis(new THREE.Vector3(1,1).normalize(),1.5); model.rotation.x = 0; model.rotation.y = 0; model.rotation.z = 0; modelInitialPositionX = model.position.x; modelInitialPositionY = model.position.y; modelInitialPositionZ = model.position.z; scene.add(model); } }); modelTexture.dispose(); modelTexture = null; }); loader = null; }
解决方法
尝试停止动画,使用以下代码:
animate(){ this.requestId = requestAnimationFrame(this.animate.bind(this)); this.render(); } render(){ this.renderer.render(this.scene,this.camera); } stop(){ window.cancelAnimationFrame(this.requestId); }