setupDepthTexture method

void setupDepthTexture(
  1. dynamic framebuffer,
  2. RenderTarget renderTarget
)

Implementation

void setupDepthTexture(framebuffer, RenderTarget renderTarget) {
  final isCube = (renderTarget.isWebGLCubeRenderTarget);
  if (isCube) {
    throw ('Depth Texture with cube render targets is not supported');
  }

  state.bindFramebuffer(WebGL.FRAMEBUFFER, framebuffer);

  if (!(renderTarget.depthTexture != null && renderTarget.depthTexture is DepthTexture)) {
    throw ('renderTarget.depthTexture must be an instance of three.DepthTexture');
  }

  // upload an empty depth texture with framebuffer size
  final depthTexture = renderTarget.depthTexture!;
  if (properties.get(depthTexture)["__webglTexture"] == null ||
      depthTexture.image.width != renderTarget.width ||
      depthTexture.image.height != renderTarget.height) {
    depthTexture.image.width = renderTarget.width;
    depthTexture.image.height = renderTarget.height;
    depthTexture.needsUpdate = true;
  }

  setTexture2D(depthTexture, 0);

  final webglDepthTexture = properties.get(depthTexture)["__webglTexture"];
  final samples = getRenderTargetSamples(renderTarget);

  if (depthTexture.format == DepthFormat) {
    if (useMultisampledRenderToTexture(renderTarget)) {
      multisampledRenderToTextureExtension.framebufferTexture2DMultisampleEXT(
          WebGL.FRAMEBUFFER, WebGL.DEPTH_ATTACHMENT, WebGL.TEXTURE_2D, webglDepthTexture, 0, samples);
    } else {
      gl.framebufferTexture2D(WebGL.FRAMEBUFFER, WebGL.DEPTH_ATTACHMENT, WebGL.TEXTURE_2D, webglDepthTexture, 0);
    }
  } else if (depthTexture.format == DepthStencilFormat) {
    if (useMultisampledRenderToTexture(renderTarget)) {
      multisampledRenderToTextureExtension.framebufferTexture2DMultisampleEXT(
          WebGL.FRAMEBUFFER, WebGL.DEPTH_STENCIL_ATTACHMENT, WebGL.TEXTURE_2D, webglDepthTexture, 0, samples);
    } else {
      _gl.framebufferTexture2D(WebGL.FRAMEBUFFER, WebGL.DEPTH_STENCIL_ATTACHMENT, WebGL.TEXTURE_2D, webglDepthTexture, 0);
    }
  } else {
    throw ('Unknown depthTexture format');
  }
}