(
gl: WebGL2RenderingContext, texture: WebGLTexture,
dims: Dimensions)
| 57 | * @param dims |
| 58 | */ |
| 59 | export function downloadTextureData( |
| 60 | gl: WebGL2RenderingContext, texture: WebGLTexture, |
| 61 | dims: Dimensions): Uint8Array { |
| 62 | const {width, height, depth} = dims; |
| 63 | const pixels = new Uint8Array(width * height * depth); |
| 64 | |
| 65 | if (!fboCache.has(gl)) { |
| 66 | fboCache.set(gl, createFrameBuffer(gl)); |
| 67 | } |
| 68 | const fbo = fboCache.get(gl); |
| 69 | |
| 70 | webgl_util.callAndCheck(gl, () => { |
| 71 | gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); |
| 72 | }); |
| 73 | |
| 74 | webgl_util.callAndCheck(gl, () => { |
| 75 | gl.activeTexture(gl.TEXTURE0); |
| 76 | gl.bindTexture(gl.TEXTURE_2D, texture); |
| 77 | }); |
| 78 | |
| 79 | webgl_util.callAndCheck(gl, () => { |
| 80 | const level = 0; |
| 81 | gl.framebufferTexture2D( |
| 82 | gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, level); |
| 83 | }); |
| 84 | |
| 85 | webgl_util.callAndCheck(gl, () => { |
| 86 | const format = depth === 3 ? gl.RGB : gl.RGBA; |
| 87 | const x = 0; |
| 88 | const y = 0; |
| 89 | gl.readPixels(x, y, width, height, format, gl.UNSIGNED_BYTE, pixels); |
| 90 | }); |
| 91 | |
| 92 | // Unbind framebuffer |
| 93 | webgl_util.callAndCheck(gl, () => { |
| 94 | gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
| 95 | }); |
| 96 | return pixels; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Upload image data to a texture. |
no test coverage detected
searching dependent graphs…