简体   繁体   English

three.js在单页webapp中正确重启场景

[英]three.js properly restart scene in single page webapp

Right now I'm trying to do a single page app with multiple “slides” as different divs which are hidden unless they are the currently visible slide. 现在,我正在尝试制作一个具有多个“幻灯片”作为不同div的单页应用程序,除非它们是当前可见的幻灯片,否则它们将被隐藏。

One slide contains a canvas element and a ThreeJS scene. 一张幻灯片包含一个画布元素和一个ThreeJS场景。 This scene is loaded as soon as the containing div (->slide) is visible. 包含div(-> slide)可见后立即加载此场景。

The user is able to proceed to the next slide which doesn't contain a threejs scene. 用户可以继续进行到不包含threejs场景的下一张幻灯片。

The user is able to get back to the slide with the threejs scene but it is not necessary to save the state of the scene, the scene should reload completely. 用户可以使用threejs场景返回幻灯片,但不必保存场景状态,场景应完全重新加载。

The scene is now all set up but actually I did not find any suitable information on my problem. 现在场景已经全部设置好了,但是实际上我找不到关于我的问题的任何合适的信息。 I'm wondering what the best approach would be firstly memory-wise and secondly to reset the scene completely. 我想知道最好的方法是首先在内存方面,其次完全重置场景。

  • Should I remove every object from the scene and add them again if necessary? 我是否应该从场景中删除所有对象,并在必要时再次添加它们? If so, what would be the best method to do that? 如果是这样,最好的方法是什么?
  • What exactly is happening on .dispose()? .dispose()究竟发生了什么?
  • Is there some kind of routine to remove every object/group/texture/material/whatever from a scene at once? 是否有某种例程可以一次删除场景中的每个对象/组/纹理/材质/任何东西? And dispose any memories? 并留下任何回忆?
  • is there anything else i should consider when resetting a scene? 重置场景时我还应该考虑什么?

A lot of questions, I know. 我知道很多问题。 But a lot of answers to these questions relate to old threejs versions or somehow don't work anymore. 但是,这些问题的很多答案都与旧的threejs版本有关,或者某种程度上不再起作用了。 Any answer is really appreciated! 任何答案都非常感谢!

A few ideas that might give you hints... 一些可能会给您提示的想法...

About "hiding" the scene 关于“隐藏”场景

I would simply stop rendering (calling the render/animate loop) when container div is not visible. 当容器div不可见时,我只是停止rendering (调用render / animate循环)。

About "showing" the scene again 关于再次“显示”场景

Reloading external ressources (textures, models...) seems pointless and, depending on your scene, reloading everything could be a real pain for users. 重新加载外部资源(纹理,模型...)似乎毫无意义,并且根据您的场景,重新加载所有内容对于用户而言可能是真正的痛苦。 What is loaded should remain available to your renderer. 加载的内容应仍可用于渲染器。 If you need to reset your scene though, you should wrap it in an init() function, to be called again when container becomes visible again. 但是,如果需要重置场景,则应将其包装在init()函数中,以便在容器再次可见时再次调用它。

Using workers 使用工人

Depending on your precise use case, using workers could be a good idea too. 根据您的确切用例,使用工人也是一个好主意。 If your scene is made of loads of heavy files, then making sure the rest of the page is available to the user (no freeze/stutter) while loading is a good idea. 如果您的场景是由大量文件组成的,则确保在加载时页面的其余部分对用户可用(不冻结/不结结)是个好主意。 Here are a few examples 这里有一些例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM