setupRenderTarget method

void setupRenderTarget(
  1. RenderTarget renderTarget
)

Implementation

void setupRenderTarget(RenderTarget renderTarget) {

  final texture = renderTarget.texture;

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

  final isCube = (renderTarget.isWebGLCubeRenderTarget == true);
  final isMultipleRenderTargets = (renderTarget.isWebGLMultipleRenderTargets == true);
  final supportsMips = isPowerOfTwo(renderTarget) || isWebGL2;

  // Setup framebuffer

  if (isCube) {
    renderTargetProperties["__webglFramebuffer"] = [];
    for (int i = 0; i < 6; i++) {
      renderTargetProperties["__webglFramebuffer"].add(gl.createFramebuffer());
    }
  } else {
    renderTargetProperties["__webglFramebuffer"] = gl.createFramebuffer();

    if (isMultipleRenderTargets) {
      if (capabilities.drawBuffers) {
        final textures = renderTarget.texture;
        if(textures is GroupTexture){
          final children = textures.children;
          for (int i = 0, il = children.length; i < il; i++) {
            final attachmentProperties = properties.get(children[i]);

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

              info.memory["textures"] = info.memory["textures"]! + 1;
            }
          }
        }
      } else {
        console.error('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(WebGL.RENDERBUFFER, renderTargetProperties["__webglColorRenderbuffer"]);

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

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

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

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

  // Setup color buffer

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

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

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

    state.bindTexture(WebGL.TEXTURE_CUBE_MAP, null);
  } else if (isMultipleRenderTargets) {
    final textures = renderTarget.texture;
    if(textures is GroupTexture){
      for (int i = 0, il = textures.children.length; i < il; i++) {
        final attachment = textures.children[i];
        final attachmentProperties = properties.get(attachment);

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

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

    state.bindTexture(WebGL.TEXTURE_2D, null);
  } else {
    dynamic glTextureType = WebGL.TEXTURE_2D;

    if (renderTarget is WebGL3DRenderTarget || renderTarget is WebGLArrayRenderTarget) {
      if (isWebGL2) {
        glTextureType = renderTarget is WebGL3DRenderTarget ? WebGL.TEXTURE_3D : WebGL.TEXTURE_2D_ARRAY;
      } else {
        console.info('3D and three.DataTexture2DArray only supported with WebGL2.');
      }
    }

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

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

    state.bindTexture(glTextureType, null);
  }

  // Setup depth and stencil buffers

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