I am very new to the WebGL subject. What I want to do is to calculate the average color of 6 different framebuffers like the ones below in the picture. Now I am wondering what the best way to go about it would be? I tried to do
gl.readPixels(0, 0, 256, 256, gl.RGBA, gl.UNSIGNED_BYTE, pixelValues);
but that seems to be very slow... Is there a way that this can happen on the graphics card?

this is how the FBO is set up - I have this from a tutorial:
...
You have two options
- Write a fragment shader that runs once per row (per texture) by rendering a quad (1 x textureheight) and runs through all pixels in the row and averages them. Repeat this process on the averaged rows and you will have the average of the entire image. This is called stream reduction
- Call glGenerateMipMap on your FBOs and then access the highest level mipmap (get the params of this mipmap by glGetTexLevelParameter). Now you can use either the ReadPixels method on the much-reduced and averaged mip-image or use GLSL and this ought to be much faster.
Off the top of my head
- Make 6 fbos.
- Make sure each fbo is made with a texture attachment, not a renderbuffer.
- Render the 6 scenes to the 6 fbos
- For each texture call gl.generateMipmap(...)
- Write a shader that takes 6 textures and averages them using texture2D with the bias option. Set the bias to the number of levels in your textures. This is to tell the GPU to use the smallest level.
- Render a unit quad with that shader to a 1 pixel fbo (or 1 pixel in your backbuffer).
- Call gl.readpixels on that 1 pixel.
I think the shader would look something like this
--fragment shader--
precision mediump float;
uniform sampler2D u_textures[6];
uniform float u_bias;
void main() {
// since we know we are rendering only with the last mip
// then there is only 1 texel.
vec2 center = vec2(0.5, 0.5);
vec4 sum =
texture2D(u_textures[0], center, u_bias) +
texture2D(u_textures[1], center, u_bias) +
texture2D(u_textures[2], center, u_bias) +
texture2D(u_textures[3], center, u_bias) +
texture2D(u_textures[4], center, u_bias) +
texture2D(u_textures[5], center, u_bias);
gl_FragColor = sum / 6.0;
}
来源:https://stackoverflow.com/questions/13866438/is-there-a-fast-way-to-the-average-colors-from-several-framebuffers-in-webgl-j