最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to remove all Mesh objects from the scene in three.js? - Stack Overflow

programmeradmin1浏览0评论

I am passing the name of the 3d model add and texture name in a function and the result is the 3d model is rendered in a scene. All what am in stuck is ,I just want to remove only the 3d objects from the scene

when i use scene.children to get the objects it contains the light and camera too i just want to remove only the Meshes in the scene

I am passing the name of the 3d model add and texture name in a function and the result is the 3d model is rendered in a scene. All what am in stuck is ,I just want to remove only the 3d objects from the scene

when i use scene.children to get the objects it contains the light and camera too i just want to remove only the Meshes in the scene

Share Improve this question edited Jan 28, 2016 at 14:08 micnil 4,8152 gold badges32 silver badges41 bronze badges asked Jan 28, 2016 at 11:55 ArUnArUn 1,3372 gold badges23 silver badges40 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Maybe this solves your problem,

for (let i = scene.children.length - 1; i >= 0; i--) {
    if(scene.children[i].type === "Mesh")
        scene.remove(scene.children[i]);
}

Note that it is a reverse for loop. This is because we are removing items from the array that we are iterating, and we need to preserve the indices.

Just removing THREE objects from your scene is not enough to delete them from memory. You have to call the dispose() methods on the objects' geometries, materials and textures.

https://github./mrdoob/three.js/issues/5175

After you call your dispose and remove methods, do a diagnostic like this (where this.renderer is your THREE.Renderer):

if (this.renderer && (this.renderer.info.memory.geometries || this.renderer.info.memory.programs || this.renderer.info.memory.textures)) {
    loge("geometries=" + this.renderer.info.memory.geometries + " programs=" + this.renderer.info.memory.programs + " textures=" + this.renderer.info.memory.textures);
}

If the number of programs, geometries and textures isn't stable, you are inviting performance issues and a memory leak.

发布评论

评论列表(0)

  1. 暂无评论