How to capture binary data from a Webgl context?

Asked

Viewed 58 times

5

I created two HTML clients that communicate through a websocket server. One of the clients draws a 3D model on a canvas using the Three.js library and then sends the Webgl context binary file from the canvas to the other client via websocket. Virtually a canvas stream between multiple clients.

The basis of 3D rendering code

The problem is that the Webgl library is quite inefficient, to the point that the only way I was able to send the binary data was by using the method gl.readPixels() which is VERY slow (about 3 seconds). Being that I need to stay as fluid as possible.

Below is the way I capture the webGL context and send by websocket.

function animate() {

    requestAnimationFrame( animate );

    render();

    var ctx = renderer.getContext();        

    var byteArray = new Uint8Array(1280 * 720 * 4);

    ctx.readPixels(0,0,1280, 720, ctx.RGBA, ctx.UNSIGNED_BYTE, byteArray);

    socket.send(byteArray.buffer);

}
  • 2

    You want to send a 1280x720 image by a socket to each animation frame?! Assuming 24 frames per second, you would need a network connection of 88 megabytes per second at least... About readPixels, in fact it is a relatively slow operation, but 3 seconds is too much! (I hear about 10ms or something) Which browser and operating system are you using? Are you sure the bottleneck is the readPixels, and not some other operation in your code?

  • You can send instructions using JSON to the other client and rebuild a 3D model to reproduce instructions here.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.