readRenderTargetPixels method

void readRenderTargetPixels(
  1. WebGLRenderTarget renderTarget,
  2. int x,
  3. int y,
  4. int width,
  5. int height,
  6. NativeArray<num> buffer, [
  7. dynamic activeCubeFaceIndex,
])

Implementation

void readRenderTargetPixels(WebGLRenderTarget renderTarget, int x, int y, int width, int height, NativeArray buffer, [activeCubeFaceIndex]) {
  dynamic framebuffer = properties.get(renderTarget)["__webglFramebuffer"]; //can be Map or int

  if (renderTarget.isWebGLCubeRenderTarget && activeCubeFaceIndex != null) {
    framebuffer = framebuffer?[activeCubeFaceIndex];
  }

  if (framebuffer != null) {
    state.bindFramebuffer(WebGL.FRAMEBUFFER, framebuffer);

    try {
      final texture = renderTarget.texture;
      final textureFormat = texture.format;
      final textureType = texture.type;

      if (textureFormat != RGBAFormat &&
          utils.convert(textureFormat) != _gl.getParameter(WebGL.IMPLEMENTATION_COLOR_READ_FORMAT)) {
        console.warning('WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.');
        return;
      }

      final halfFloatSupportedByExt = textureType == HalfFloatType &&
          (extensions.get('EXT_color_buffer_half_float') ||
              (capabilities.isWebGL2 && extensions.get('EXT_color_buffer_float')));

      if (textureType != UnsignedByteType &&
          utils.convert(textureType) !=
              _gl.getParameter(WebGL.IMPLEMENTATION_COLOR_READ_TYPE) && // IE11, Edge and Chrome Mac < 52 (#9513)
          !(textureType == FloatType &&
              (capabilities.isWebGL2 ||
                  extensions.get('OES_texture_float') ||
                  extensions.get('WEBGL_color_buffer_float'))) && // Chrome Mac >= 52 and Firefox
          !halfFloatSupportedByExt) {
        console.warning('WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.');
        return;
      }

      // the following if statement ensures valid read requests (no out-of-bounds pixels, see #8604)

      if ((x >= 0 && x <= (renderTarget.width - width)) && (y >= 0 && y <= (renderTarget.height - height))) {
        _gl.readPixels(x, y, width, height, utils.convert(textureFormat), utils.convert(textureType), buffer);
      }
    } finally {
      final framebuffer = (_currentRenderTarget != null) ? properties.get(_currentRenderTarget)["__webglFramebuffer"] : null;
      state.bindFramebuffer(WebGL.FRAMEBUFFER, framebuffer);
    }
  }
}