处理对象并恢复它们 [英] Disposing objects and restoring them

查看:19
本文介绍了处理对象并恢复它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Three.js 项目,该项目允许用户切换包含不同网格对象的场景.它们都在页面加载的同时加载到项目中,并且所有这些对象都具有严重影响性能的纹理.

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.

为了解决这个问题,我想暂时移除用户选择的所有场景中的所有对象.因此,对于 10 个场景,如果用户例如选择查看场景 4 中的内容(这将是 currentScene),那么场景 1 到 3 和 5 到 10 应该没有放置不必要的对象对性能造成压力.这也应该考虑页面加载的时间.只有当用户再次选择上述场景之一时,它才会还原场景的对象.

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.

这个 FIDDLE 是我的项目的一个示例,其中包含基本代码.我正在使用 remove()dispose() 方法来实现它.

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?

另外,如果这是一种方法,我该如何选择除了 currentScene 之外的所有场景?

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

PS:我很清楚它会在场景之间切换加载时间,但我更喜欢这样.

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

推荐答案

如果我移除和处置对象,我该如何恢复它们?- 如果你正确处理对象,你必须重新加载它,这是丑陋的

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

这是一个很好的方法吗?它甚至可能吗,和/或是否有更好的方法来实现这一目标?- 如果你打算再次显示对象,最好设置obj.visible = false;重新加载和配置是性能最差的方法.稍后加载对象后,某些东西(如雾等)无法正确应用.

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.

另外,如果这是一种方法,我该如何选择除 currentScene 之外的所有场景?- 不确定问题,为此提出一个新问题.(我猜是通过控制鼠标移动)

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.

请始终只问一个问题.

这篇关于处理对象并恢复它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆