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) {
    final renderTargetProperties = properties.get(renderTarget);

    if (renderTargetProperties["__useDefaultFramebuffer"] != null) {
      // We need to make sure to rebind the framebuffer.
      state.bindFramebuffer(WebGL.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"]);
    }
  }

  Framebuffer? framebuffer;
  bool isCube = false;
  bool isRenderTarget3D = false;

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

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

    final 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.setFrom(renderTarget.viewport);
    _currentScissor.setFrom(renderTarget.scissor);
    _currentScissorTest = renderTarget.scissorTest;
  }
  else {
    _currentViewport.setFrom(_viewport).scale(_pixelRatio).floor();
    _currentScissor.setFrom(_scissor).scale(_pixelRatio).floor();
    _currentScissorTest = _scissorTest;
  }

  final framebufferBound = state.bindFramebuffer(WebGL.FRAMEBUFFER, framebuffer);

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

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

  if (isCube) {
    final textureProperties = properties.get(renderTarget!.texture);
    _gl.framebufferTexture2D(WebGL.FRAMEBUFFER, WebGL.COLOR_ATTACHMENT0, WebGL.TEXTURE_CUBE_MAP_POSITIVE_X + activeCubeFace, textureProperties["__webglTexture"], activeMipmapLevel);
  }
  else if (isRenderTarget3D) {
    console.warning('framebufferTextureLayer not supported yet');
    // final textureProperties = properties.get(renderTarget!.texture);
    // final layer = activeCubeFace;
    // _gl.gl.glFramebufferTextureLayer( WebGL.FRAMEBUFFER, WebGL.COLOR_ATTACHMENT0, textureProperties["__webglTexture"], activeMipmapLevel, layer);
  }

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