setupRenderTarget method

void setupRenderTarget(
  1. RenderTarget renderTarget
)

Implementation

void setupRenderTarget(RenderTarget renderTarget) {
  var texture = renderTarget.texture;

  var renderTargetProperties = properties.get(renderTarget);
  var textureProperties = properties.get(renderTarget.texture);

  renderTarget.addEventListener('dispose', onRenderTargetDispose);

  if (renderTarget.isWebGLMultipleRenderTargets != true) {
    textureProperties["__webglTexture"] = gl.createTexture();
    textureProperties["__version"] = texture.version;
    info.memory["textures"] = info.memory["textures"]! + 1;
  }

  var isCube = (renderTarget.isWebGLCubeRenderTarget == true);
  var isMultipleRenderTargets =
      (renderTarget.isWebGLMultipleRenderTargets == true);
  var isMultisample = (renderTarget.isWebGLMultisampleRenderTarget == true);
  var supportsMips = isPowerOfTwo(renderTarget) || isWebGL2;

  // Setup framebuffer

  if (isCube) {
    renderTargetProperties["__webglFramebuffer"] = [];

    for (var i = 0; i < 6; i++) {
      // renderTargetProperties["__webglFramebuffer"][ i ] = gl.createFramebuffer();

      renderTargetProperties["__webglFramebuffer"]
          .add(gl.createFramebuffer());
    }
  } else {
    renderTargetProperties["__webglFramebuffer"] = gl.createFramebuffer();

    if (isMultipleRenderTargets) {
      if (capabilities.drawBuffers) {
        var textures = renderTarget.texture;

        for (var i = 0, il = textures.length; i < il; i++) {
          var attachmentProperties = properties.get(textures[i]);

          if (attachmentProperties["__webglTexture"] == null) {
            attachmentProperties["__webglTexture"] = gl.createTexture();

            info.memory["textures"] = info.memory["textures"]! + 1;
          }
        }
      } else {
        print(
            'THREE.WebGLRenderer: WebGLMultipleRenderTargets can only be used with WebGL2 or WEBGL_draw_buffers extension.');
      }
    } else if ((isWebGL2 && renderTarget.samples > 0) &&
        useMultisampledRenderToTexture(renderTarget) == false) {
      renderTargetProperties["__webglMultisampledFramebuffer"] =
          _gl.createFramebuffer();
      renderTargetProperties["__webglColorRenderbuffer"] =
          _gl.createRenderbuffer();

      _gl.bindRenderbuffer(_gl.RENDERBUFFER,
          renderTargetProperties["__webglColorRenderbuffer"]);

      var glFormat = utils.convert(texture.format, texture.encoding);
      var glType = utils.convert(texture.type);
      var glInternalFormat = getInternalFormat(
          texture.internalFormat, glFormat, glType, texture.encoding);
      var samples = getRenderTargetSamples(renderTarget);
      _gl.renderbufferStorageMultisample(
          _gl.RENDERBUFFER,
          samples,
          glInternalFormat,
          renderTarget.width.toInt(),
          renderTarget.height.toInt());

      state.bindFramebuffer(_gl.FRAMEBUFFER,
          renderTargetProperties["__webglMultisampledFramebuffer"]);
      _gl.framebufferRenderbuffer(
          _gl.FRAMEBUFFER,
          _gl.COLOR_ATTACHMENT0,
          _gl.RENDERBUFFER,
          renderTargetProperties["__webglColorRenderbuffer"]);
      _gl.bindRenderbuffer(_gl.RENDERBUFFER, null);

      if (renderTarget.depthBuffer) {
        renderTargetProperties["__webglDepthRenderbuffer"] =
            _gl.createRenderbuffer();
        setupRenderBufferStorage(
            renderTargetProperties["__webglDepthRenderbuffer"],
            renderTarget,
            true);
      }

      state.bindFramebuffer(_gl.FRAMEBUFFER, null);
    }
  }

  // Setup color buffer

  if (isCube) {
    state.bindTexture(
        gl.TEXTURE_CUBE_MAP, textureProperties["__webglTexture"]);
    setTextureParameters(gl.TEXTURE_CUBE_MAP, texture, supportsMips);

    for (var i = 0; i < 6; i++) {
      setupFrameBufferTexture(
          renderTargetProperties["__webglFramebuffer"][i],
          renderTarget,
          texture,
          gl.COLOR_ATTACHMENT0,
          gl.TEXTURE_CUBE_MAP_POSITIVE_X + i);
    }

    if (textureNeedsGenerateMipmaps(texture, supportsMips)) {
      generateMipmap(gl.TEXTURE_CUBE_MAP);
    }

    state.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  } else if (isMultipleRenderTargets) {
    var textures = renderTarget.texture;

    for (var i = 0, il = textures.length; i < il; i++) {
      var attachment = textures[i];
      var attachmentProperties = properties.get(attachment);

      state.bindTexture(
          gl.TEXTURE_2D, attachmentProperties["__webglTexture"]);
      setTextureParameters(gl.TEXTURE_2D, attachment, supportsMips);
      setupFrameBufferTexture(renderTargetProperties["__webglFramebuffer"],
          renderTarget, attachment, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D);

      if (textureNeedsGenerateMipmaps(attachment, supportsMips)) {
        generateMipmap(gl.TEXTURE_2D);
      }
    }

    state.bindTexture(gl.TEXTURE_2D, null);
  } else {
    var glTextureType = gl.TEXTURE_2D;

    if (renderTarget is WebGL3DRenderTarget ||
        renderTarget is WebGLArrayRenderTarget) {
      if (isWebGL2) {
        glTextureType = renderTarget is WebGL3DRenderTarget
            ? _gl.TEXTURE_3D
            : _gl.TEXTURE_2D_ARRAY;
      } else {
        print(
            'THREE.DataTexture3D and THREE.DataTexture2DArray only supported with WebGL2.');
      }
    }

    state.bindTexture(glTextureType, textureProperties["__webglTexture"]);
    setTextureParameters(glTextureType, texture, supportsMips);
    setupFrameBufferTexture(renderTargetProperties["__webglFramebuffer"],
        renderTarget, texture, gl.COLOR_ATTACHMENT0, glTextureType);

    if (textureNeedsGenerateMipmaps(texture, supportsMips)) {
      generateMipmap(glTextureType);
    }

    state.bindTexture(glTextureType, null);
  }

  // Setup depth and stencil buffers

  if (renderTarget.depthBuffer) {
    setupDepthRenderbuffer(renderTarget);
  }
}