Disposing objects and restoring them

落花浮王杯 提交于 2019-12-24 07:27:47

问题


I am working on a three.js project that allows the user to switch through scenes that hold different mesh objects. They're all loaded into the project at the same time when the page has loaded, and all of these objects have textures which weigh heavily on the performance.

So to fix this, I want to temporarily remove all the objects in all of the scenes that the user has not selected. So, with 10 scenes, should the user for example select to see what's in scene 4 (which would be the currentScene), then scene 1 through 3 and 5 through 10 should have no objects that put an unnecessary strain on the performance. This should account as well for when the page has loaded. Only when the user selects one of those aforementioned scenes again, then it restores the scenes' objects.

This FIDDLE is an example of my project that encompasses the essential code. I am using the remove() and dispose() methods to achieve it.

//scene that's to be removed and disposed of
var destroyScenes = scenes[1];

//removes and disposes all objects in above scene

for (i = destroyScenes.children.length - 1; i >= 0; i--) {
            obj = destroyScenes.children[i];
            destroyScenes.remove(obj);
            obj.geometry.dispose();
            obj.material.dispose();
            obj.texture.
    }

My concrete question(s) is this: How do I restore the objects if I remove and dispose of them? Is that a good approach to it, is it even possible, and/or is there a better way of achieving this?

Also, if that is a way to do it, how do I select all of the scenes except for the currentScene?

PS: I'm well aware that it'll put a loading time switching between scenes, but I'd still much more prefer that.


回答1:


How do I restore the objects if I remove and dispose of them? - If you dispose object right, you must load it again, which is ugly

Is that a good approach to it, is it even possible, and/or is there a better way of achieving this? - If you plan to show object again, it is better to set obj.visible = false; Loading and disposing again is the worst performance method. Some things (as is fog etc..) cannot be applied correctly after loading object later.

Also, if that is a way to do it, how do I select all of the scenes except for the currentScene? - Not sure about question, make a new question for that. (by controlling mouse move i guess)

I'm well aware that it'll put a loading time switching between scenes, but I'd still much more prefer that. - It's not a way how it works. Scenes are running at the same time, you can stop animation frame for inactive scenes. But scenes will load on initialisation, not during switching them.

Ask always only one question pls.



来源:https://stackoverflow.com/questions/41412666/disposing-objects-and-restoring-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!