three.js ObjLoader内存增加

前端之家收集整理的这篇文章主要介绍了three.js ObjLoader内存增加前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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;
}

我有任何最佳实践,我可以使用添加删除.obj和纹理按钮点击.

解决方法

尝试停止动画,使用以下代码
animate(){
    this.requestId = requestAnimationFrame(this.animate.bind(this));
    this.render();
}
render(){
    this.renderer.render(this.scene,this.camera);
}
stop(){
    window.cancelAnimationFrame(this.requestId);
}

猜你在找的JavaScript相关文章