我正在处理的页面有 ajax-navigation,所以当用户导航离开我使用 WebGL 的部分时,我会停止我的 RequestAnimationFrame-loop,然后当用户重新访问该页面时,我会重新运行所有 WebGL设置代码来重新创建场景。这在我使用普通渲染器时效果很好,但使用 EffectComposer 时,它会中断。
这是代码。
初始化:
composerScene = new THREE.EffectComposer(renderer, new THREE.WebGLRenderTarget(width, height, { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat, stencilBuffer: true }));
renderModel = new THREE.RenderPass(scene, camera);
renderModel.renderToScreen = false;
renderModel.clear = true;
filmPass = new THREE.FilmPass(filmgrainOpacity, scanlineIntensity, scanlineCount, filmpassGrayscale);
filmPass.renderToScreen = true;
composerScene.addPass(renderModel);
composerScene.addPass(filmPass);
渲染:
composerScene.render(deltaTime);
当我离开然后回到页面时,错误控制台显示:
WebGL: INVALID_OPERATION: bindBuffer: object not from this context
GL_INVALID_OPERATION : glDrawXXX: attempt to access out of range vertices
WebGL: too many errors, no more errors will be reported to the console for this context.
并且没有渲染发生。如果我注释掉composerScene.addPass(filmPass);
错误不再发生。