How to toggle preserveDrawingBuffer in three.js?

后端 未结 2 699
無奈伤痛
無奈伤痛 2020-12-01 23:46

Basically, I want the setup where I could go to preserveDrawingBuffer=true, render the scene once, grab the screenshot, and go back. However, this poses two problems:

<
2条回答
  •  我在风中等你
    2020-12-02 00:16

    You don't need preserveDrawingBuffer: true to take a screenshot. What you need is to take the screenshot immediately after rendering. A screenshot is guaranteed to work as long as you take it after rendering but before exiting the current event.

    So for example this will always work

    renderer.render( scene, camera );
    var screenshot = renderer.domElement.toDataURL();
    

    Whereas this will only work randomly if you're luck

    someElement.addEventListener('click', function() {
       // this is not immediately after rendering. you have no
       // idea when it is relative to rendering.
       var screenshot = renderer.domElement.toDataURL();
    });
    

    Most of the THREE.js examples have a render function if you need to take a screenshot when the user requests one you could do this

    someElement.addEventListener('click', function() {
       render();
       var screenshot = renderer.domElement.toDataURL();
    });
    

    Or you could do this

    var takeScreenshot;
    
    function render() {
       ...
       if (takeScreenshot) {
         takeScreenshot = false;
         var screenshot = renderer.domElement.toDataURL();
       }
    }
    
    someElement.addEventListener('click', function() {
       takeScreenshot = true;
    });
    

    Or any number of other ways to just make sure you take the screenshot immediately after rendering.

提交回复
热议问题