copyTextureToTexture3D method

void copyTextureToTexture3D(
  1. BoundingBox sourceBox,
  2. Vector3 position,
  3. Texture srcTexture,
  4. Texture dstTexture, {
  5. int level = 0,
})

Implementation

void copyTextureToTexture3D(
  BoundingBox sourceBox,
  Vector3 position,
  Texture srcTexture,
  Texture dstTexture, {
  int level = 0,
}) {
  final width = sourceBox.max.x - sourceBox.min.x + 1;
  final height = sourceBox.max.y - sourceBox.min.y + 1;
  final depth = sourceBox.max.z - sourceBox.min.z + 1;
  final glFormat = utils.convert(dstTexture.format);
  final glType = utils.convert(dstTexture.type);
  dynamic glTarget;

  if (dstTexture is Data3DTexture) {
    textures.setTexture3D(dstTexture, 0);
    glTarget = WebGL.TEXTURE_3D;
  } else if (dstTexture is DataArrayTexture) {
    textures.setTexture2DArray(dstTexture, 0);
    glTarget = WebGL.TEXTURE_2D_ARRAY;
  } else {
    console.warning('WebGLRenderer.copyTextureToTexture3D: only supports DataTexture3D and DataTexture2DArray.');
    return;
  }

  _gl.pixelStorei(WebGL.UNPACK_FLIP_Y_WEBGL, dstTexture.flipY ? 1 : 0);
  _gl.pixelStorei(WebGL.UNPACK_PREMULTIPLY_ALPHA_WEBGL, dstTexture.premultiplyAlpha?1:0);
  _gl.pixelStorei(WebGL.UNPACK_ALIGNMENT, dstTexture.unpackAlignment);

  final unpackRowLen = _gl.getParameter(WebGL.UNPACK_ROW_LENGTH);
  final unpackImageHeight = _gl.getParameter(WebGL.UNPACK_IMAGE_HEIGHT);
  final unpackSkipPixels = _gl.getParameter(WebGL.UNPACK_SKIP_PIXELS);
  final unpackSkipRows = _gl.getParameter(WebGL.UNPACK_SKIP_ROWS);
  final unpackSkipImages = _gl.getParameter(WebGL.UNPACK_SKIP_IMAGES);

  final image = srcTexture is CompressedTexture ? srcTexture.mipmaps[0] : srcTexture.image;

  _gl.pixelStorei(WebGL.UNPACK_ROW_LENGTH, image.width);
  _gl.pixelStorei(WebGL.UNPACK_IMAGE_HEIGHT, image.height);
  _gl.pixelStorei(WebGL.UNPACK_SKIP_PIXELS, sourceBox.min.x.toInt());
  _gl.pixelStorei(WebGL.UNPACK_SKIP_ROWS, sourceBox.min.y.toInt());
  _gl.pixelStorei(WebGL.UNPACK_SKIP_IMAGES, sourceBox.min.z.toInt());

  if (srcTexture is DataTexture || srcTexture is Data3DTexture) {
    _gl.texSubImage3D(glTarget, level, position.x.toInt(), position.y.toInt(), position.z.toInt(), width.toInt(), height.toInt(), depth.toInt(), glFormat, glType, image.data);
  }
  else {
    if (srcTexture is CompressedTexture) {
      console.warning('WebGLRenderer.copyTextureToTexture3D: untested support for compressed srcTexture.');
      //_gl.compressedTexSubImage3D(glTarget, level, position.x, position.y, position.z, width, height, depth, glFormat, image.data);
    }
    else {
      _gl.texSubImage3D(glTarget, level, position.x.toInt(), position.y.toInt(), position.z.toInt(), width.toInt(), height.toInt(), depth.toInt(), glFormat, glType, image);
    }
  }

  _gl.pixelStorei(WebGL.UNPACK_ROW_LENGTH, unpackRowLen);
  _gl.pixelStorei(WebGL.UNPACK_IMAGE_HEIGHT, unpackImageHeight);
  _gl.pixelStorei(WebGL.UNPACK_SKIP_PIXELS, unpackSkipPixels);
  _gl.pixelStorei(WebGL.UNPACK_SKIP_ROWS, unpackSkipRows);
  _gl.pixelStorei(WebGL.UNPACK_SKIP_IMAGES, unpackSkipImages);

  // Generate mipmaps only when copying level 0
  if (level == 0 && dstTexture.generateMipmaps) _gl.generateMipmap(glTarget);

  state.unbindTexture();
}