清理 Threejs WebGl 上下文 [英] Clean up Threejs WebGl contexts
问题描述
我在清理 WebGl 场景时遇到问题.我正在使用 Three.js 和 WebGlRenderer.在我的应用程序中,我必须经常更改视图,因此需要一直渲染新场景.直到现在我销毁并重新初始化整个 Threejs 场景.切换场景约 15 - 20 次后,我收到以下警告:
I have a problem while cleaning up my WebGl-Scenes. I'm using Three.js with a WebGlRenderer. In my application I have to change the views quite often and therefore need to render new scenes all the time. Uptil now I destroy and reinitialize the entire Threejs scene. After switching the scenes about 15 - 20 times I get following warning:
警告:活动的 WebGL 上下文过多.最旧的上下文将丢失.
多次切换后,上下文完全丢失,应用程序崩溃.
After switching a couple of times more the context is lost completly and the application crashes.
有没有办法在清理时破坏当前的 WebGl 上下文?还是 WebGlRenderer 在实例化时总是创建一个新的 WebGl 上下文?
Is there a way to destroy the current WebGl context, when cleaning up? Or does the WebGlRenderer always create a new WebGl context when being instantiated?
我使用 Three.js R64.
I'm using Three.js R64.
推荐答案
您可以为不同的场景保留相同的渲染器.渲染器并不关心它将渲染什么场景.如果您愿意,您可以在每次调用 render()
时提供不同的 Scene
.
You can keep the same renderer for different scenes. The renderer does not care what scene it will render. You can supply a different Scene
everytime you call render()
if you like.
// instantiate only once and keep it
var renderer = new THREE.WebGLRenderer();
// current scene and camera. Switch whenever you like
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(...);
fillScene(scene);
// rendering always uses current scene
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
}
/* ...
* somewhere in your application
* ...
*/
if(condition) {
// switch scene
scene = new THREE.Scene();
fillOtherScene(scene);
}
这篇关于清理 Threejs WebGl 上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!