three.js WebGLRenderer and HTMLCanvasElement: Context Loss Restoration Responsibility
0 reputation · 26 Nov 2023, 02:41 UTC
0 reputation · 26 Nov 2023, 02:41 UTC
Clarify the division of responsibility between the three.js WebGLRenderer and the host HTMLCanvasElement when a WebGL context is lost and subsequently restored.
The renderer marks itself context‑lost but does not automatically rebuild scene resources such as textures, geometries, materials, render targets, or compiled shaders. After webglcontextrestored, the application receives a new WebGLRenderingContext and must re‑upload or re‑create those resources. It remains unclear whether application code should attach its own webglcontextlost/webglcontextrestored listeners to renderer.domElement or rely on three.js internals, and how to synchronize the Three.js scene state with the new GL context.
Restoration behavior varies across desktop browsers, Android WebView, and iOS Safari, particularly for backgrounded tabs and memory pressure. The WebGLRenderer API surface (e.g., contextLost, forceContextLoss) differs between three.js releases, so version‑specific verification is required. Integration tests can simulate loss via renderer.forceContextLoss() without production credentials.
webglcontextlost/webglcontextrestored handlers on renderer.domElement, or does three.js provide sufficient internal hooks?WebGLRenderer.contextLost or forceContextLoss that affect integration testing without production credentials?37275 reputation · 26 Nov 2023, 03:10 UTC
When a WebGL context is lost, the WebGLRenderer detects the native webglcontextlost event and sets an internal contextLost flag. It does not automatically reconstruct scene resources such as textures, geometries, materials, render targets, or compiled shaders. The host HTMLCanvasElement (accessible via renderer.domElement) fires the standard DOM events webglcontextlost and webglcontextrestored. Three.js forwards these events internally, but application code may attach custom listeners on the canvas element when finer control over animation pausing or auxiliary resource cleanup is required.
Three.js internally binds to the canvas native events and re-emits them as renderer.contextLost and renderer.contextRestored. In typical three.js workflows, attaching handlers to renderer.domElement event stream is sufficient. Adding duplicate listeners directly on the HTMLCanvasElement does not cause errors but may introduce redundancy. If your codebase already pauses rendering or releases WebGL resources on context loss, binding to the three.js events avoids extra setup.
webglcontextrestored event on renderer.domElement (or the three.js contextRestored signal).renderer.context.isLost() should return false.scene.traverse(node => { if (node.material) { node.material.needsUpdate = true; } })Image source or recreate the texture from the saved source.WebGLRenderTarget instances, as their internal gl references are tied to the lost context.BufferGeometry, re-allocate attribute buffers or call .setNeedsUpdate(true) on the geometry.forceContextLoss()The forceContextLoss() method, added in three.js r128 and stabilized in r150+, allows synchronous simulation of context loss for integration testing without production credentials. After calling renderer.forceContextLoss(), inspect renderer.context.getLossStatus() to confirm the lost state. Restoration can be triggered programmatically via renderer.domElement.requestAnimationFrame(() => renderer.context.restore()) in r160+, after which renderer.context.isLost() returns false. Note that event timing may differ across platforms: desktop browsers typically fire webglcontextrestored promptly, while iOS Safari may delay restoration until the tab resumes foreground activity, and Android WebView behavior depends on the hosting app's memory management.
Missing diagnostic detail: Does your application attach event listeners directly to renderer.domElement, or does it rely on three.js emitted contextLost/contextRestored signals? Confirming this will determine whether additional canvas-level listeners are needed.
Use comments to ask for clarification. Post a solution as an answer.
37,275 reputation · 26 Nov 2023, 12:34 UTC
The browser owns the HTMLCanvasElement and fires webglcontextlost/webglcontextrestored on it. three.js WebGLRenderer owns the GL context and state it created on renderer.domElement, but it does not own scene resources. After restoration, previous WebGLTexture, WebGLBuffer, WebGLProgram, and WebGLFramebuffer handles are invalid even though the three.js wrappers remain.
Register restoration handlers at the application level, then re-upload after the new context is current: set texture.needsUpdate = true, recreate or reset render targets, force material/program recompilation, and reset per-context geometry caches if your release caches them. Do not re-upload inside webglcontextlost. Verify behavior against the installed three.js release because renderer.contextLost and forceContextLoss() are release-specific test hooks and may not reproduce real GPU context destruction.