Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handling WebGL loss in three js

I can't figure out what to do in case of webgl loss in my application (written with electron js) with three js. We have these two functions

// renderer is THREE.WebGLRenderer
renderer.context.canvas.addEventListener("webglcontextlost", contextLostFunction);
renderer.context.canvas.addEventListener("webglcontextrestored", contextRestoredFunction);

When I simulate context loss using something like this

var canvas = document.getElementById("playground").childNodes[0].childNodes[0];
var gl = canvas.getContext("webgl");
var WEBGL_lose_context = gl.getExtension('WEBGL_lose_context');
WEBGL_lose_context.loseContext();

Then webglcontextrestored event fires and everything restores as should be.

When webgl is killed for real or using something like this

renderer.context.getExtension( 'WEBGL_lose_context' ).loseContext();

Then this event webglcontextrestored never has been fired. What is going ? What to do to catch that context has been lost.

Thanks for any ideas.

like image 629
Bill Lumbert Avatar asked Feb 01 '16 17:02

Bill Lumbert


People also ask

Why WebGL context lost?

A "WebGL context is lost" error is returned when navigating through files in BIM 360. After the error appears, views for file are longer available. The error most frequently occurs when either opening large model files or changing the zoom repeatedly.

Does Three Js use WebGL?

js. Three. js is a cross-browser JavaScript library and application programming interface (API) used to create and display animated 3D computer graphics in a web browser using WebGL.

What is WebGL renderer?

WebGL enables web content to use an API based on OpenGL ES 2.0 to perform 2D and 3D rendering in an HTML canvas in browsers that support it without the use of plug-ins. WebGL programs consist of control code written in JavaScript and shader code (GLSL) that is executed on a computer's Graphics Processing Unit (GPU).

What is three Js and WebGL?

Three. js is an open source JavaScript library that is used to display the graphics, 3D and 2D objects on the web browser. It uses WebGL API behind the scene. Three. js allow you to use your GPU(Graphics Processing Unit) to render the Graphics and 3D objects on a canvas in the web browser.


1 Answers

You should use same extension reference you use to loose the context to restore the context with the restoreContext() method of the object:

var canvas = document.getElementById("playground").childNodes[0].childNodes[0];
var gl = canvas.getContext("webgl");
var WEBGL_lose_context = gl.getExtension('WEBGL_lose_context');
WEBGL_lose_context.loseContext();

window.setTimeout(()=> {
WEBGL_lose_context.restoreContext();
}, 2000);

you can also do it from the inspector to simulate it in an iterative way...

example of iterative test

like image 197
gabry Avatar answered Oct 19 '22 05:10

gabry