setRenderTarget method

void setRenderTarget(
  1. RenderTarget? renderTarget, [
  2. int activeCubeFace = 0,
  3. int activeMipmapLevel = 0
])

Implementation

void setRenderTarget(RenderTarget? renderTarget, [int activeCubeFace = 0, int activeMipmapLevel = 0]) {
  _currentRenderTarget = renderTarget;
  _currentActiveCubeFace = activeCubeFace;
  _currentActiveMipmapLevel = activeMipmapLevel;
  bool useDefaultFramebuffer = true;

  if (renderTarget != null) {
    var renderTargetProperties = properties.get(renderTarget);

    if (renderTargetProperties["__useDefaultFramebuffer"] != null) {
      // We need to make sure to rebind the framebuffer.
      state.bindFramebuffer(_gl.FRAMEBUFFER, null);
      useDefaultFramebuffer = false;
    } else if (renderTargetProperties["__webglFramebuffer"] == null) {
      textures.setupRenderTarget(renderTarget);
    } else if (renderTargetProperties["__hasExternalTextures"] == true) {
      // Color and depth texture must be rebound in order for the swapchain to update.
      textures.rebindTextures(renderTarget, properties.get(renderTarget.texture)["__webglTexture"],
          properties.get(renderTarget.depthTexture)["__webglTexture"]);
    }
  }

  var framebuffer;
  var isCube = false;
  var isRenderTarget3D = false;

  if (renderTarget != null) {
    var texture = renderTarget.texture;

    if (texture is Data3DTexture || texture is DataArrayTexture) {
      isRenderTarget3D = true;
    }

    var webglFramebuffer = properties.get(renderTarget)["__webglFramebuffer"];

    if (renderTarget.isWebGLCubeRenderTarget) {
      framebuffer = webglFramebuffer[activeCubeFace];
      isCube = true;
    } else if ((capabilities.isWebGL2 && renderTarget.samples > 0) &&
        textures.useMultisampledRenderToTexture(renderTarget) == false) {
      framebuffer = properties.get(renderTarget)["__webglMultisampledFramebuffer"];
    } else {
      framebuffer = webglFramebuffer;
    }

    _currentViewport.copy(renderTarget.viewport);
    _currentScissor.copy(renderTarget.scissor);
    _currentScissorTest = renderTarget.scissorTest;
  } else {
    _currentViewport.copy(_viewport).multiplyScalar(_pixelRatio).floor();
    _currentScissor.copy(_scissor).multiplyScalar(_pixelRatio).floor();
    _currentScissorTest = _scissorTest;
  }

  var framebufferBound = state.bindFramebuffer(_gl.FRAMEBUFFER, framebuffer);

  if (framebufferBound && capabilities.drawBuffers && useDefaultFramebuffer) {
    state.drawBuffers(renderTarget, framebuffer);
  }

  state.viewport(_currentViewport);
  state.scissor(_currentScissor);
  state.setScissorTest(_currentScissorTest!);

  if (isCube) {
    var textureProperties = properties.get(renderTarget!.texture);
    _gl.framebufferTexture2D(_gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + activeCubeFace,
        textureProperties["__webglTexture"], activeMipmapLevel);
  } else if (isRenderTarget3D) {
    var textureProperties = properties.get(renderTarget!.texture);
    var layer = activeCubeFace;
    _gl.framebufferTextureLayer(
        _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureProperties["__webglTexture"], activeMipmapLevel, layer);
  }

  _currentMaterialId = -1; // reset current material to ensure correct uniform bindings
}