我正在使用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, 0, 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;
}
我有任何最佳实践,可以在单击按钮时添加和删除.obj和纹理。
最佳答案
尝试使用以下代码停止动画:
animate(){
this.requestId = requestAnimationFrame(this.animate.bind(this));
this.render();
}
render(){
this.renderer.render(this.scene, this.camera);
}
stop(){
window.cancelAnimationFrame(this.requestId);
}
关于three.js ObjLoader内存增加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21355901/