How do we handle webgl context lost event in Three.js

前端 未结 1 1307
灰色年华
灰色年华 2021-01-05 02:05

I want to handle a lost context event in Three.js. There is a nice documentation about that here but unfortunately it doesn\'t work when I apply it to my renderer.domE

相关标签:
1条回答
  • 2021-01-05 02:28

    You should be able to do something like this about the renderer was initialized and assuming of course that the variable you stored the renderer into is named 'renderer'.

    renderer.context.canvas.addEventListener("webglcontextlost", function(event) {
        event.preventDefault();
        // animationID would have been set by your call to requestAnimationFrame
        cancelAnimationFrame(animationID); 
    }, false);
    
    renderer.context.canvas.addEventListener("webglcontextrestored", function(event) {
       // Do something 
    }, false);
    

    BTW - loseContext is not defined by Three.JS and it isn't a standard method as of this time. You can simulate it by doing the following.

    Load this script before Three.JS

    https://github.com/vorg/webgl-debug

    Then after you've started your renderer you can hook the loseContext to the canvas.

    renderer.context.canvas = WebGLDebugUtils.makeLostContextSimulatingCanvas(renderer.context.canvas);
    

    To trigger the loseContext you would do this.

    renderer.context.canvas.loseContext();
    

    And you can then also have it fail after a set number of calls by doing this.

    renderer.context.canvas.loseContextInNCalls(5);
    
    0 讨论(0)
提交回复
热议问题