WebGL - reading pixel data from render buffer

前端 未结 3 2217
天涯浪人
天涯浪人 2020-12-14 23:08

Is there a way to get the raw pixel data from a WebGL render buffer or frame buffer that is off screen?

I\'m using WebGL to do some image processing, e.g. blurring a

相关标签:
3条回答
  • 2020-12-14 23:24

    readPixels() should do what you want. Read more in the WebGL spec at http://www.khronos.org/registry/webgl/specs/latest/

    0 讨论(0)
  • 2020-12-14 23:30

    This is very old question, but I have looked for the same in three.js recently. There is no direct way to render to frame buffer, but actually it is done by render to texture (RTT) process. I check the framework source code and figure out the following code:

    renderer.render( rttScene, rttCamera, rttTexture, true );
    
    // ...
    
    var width = rttTexture.width;
    var height = rttTexture.height;
    var pixels = new Uint8Array(4 * width * height); // be careful - allocate memory only once
    
    // ...
    
    var gl = renderer.context;
    var framebuffer = rttTexture.__webglFramebuffer;
    gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);        
    gl.viewport(0, 0, width, height);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    
    0 讨论(0)
  • 2020-12-14 23:32

    Yes, you can read raw pixel data. Set preserveDrawingBuffer as true while getting webgl context and afterwards make use of readPixels by WebGL.

    var context = canvasElement.getContext("webgl", {preserveDrawingBuffer: true}
    var pixels = new Uint8Array(4 * width * height);
    context.readPixels(x, y, width, height, context.RGBA, context.UNSIGNED_BYTE, pixels)
    
    0 讨论(0)
提交回复
热议问题