render method

dynamic render(
  1. Scene scene,
  2. dynamic camera
)

Implementation

render(Scene scene, camera) {
  // @TODO: move this to animation loop

  this._nodes.updateFrame();

  if (scene.autoUpdate == true) scene.updateMatrixWorld();

  if (camera.parent == null) camera.updateMatrixWorld();

  if (this._info.autoReset == true) this._info.reset();

  _projScreenMatrix.multiplyMatrices(
      camera.projectionMatrix, camera.matrixWorldInverse);
  _frustum.setFromProjectionMatrix(_projScreenMatrix);

  this._currentRenderList = this._renderLists.get(scene, camera);
  this._currentRenderList.init();

  this._projectObject(scene, camera, 0);

  this._currentRenderList.finish();

  if (this.sortObjects == true) {
    this._currentRenderList.sort(this._opaqueSort, this._transparentSort);
  }

  // prepare render pass descriptor

  var colorAttachment = this._renderPassDescriptor.colorAttachments;
  var depthStencilAttachment =
      this._renderPassDescriptor.depthStencilAttachment;

  var renderTarget = this._renderTarget;

  if (renderTarget != null) {
    // @TODO: Support RenderTarget with antialiasing.

    var renderTargetProperties = this._properties.get(renderTarget);

    GPUTexture colorTextureGPU = renderTargetProperties["colorTextureGPU"];

    if (this._parameters["antialias"] == true) {
      GPUTexture colorTextureGPUWithSamples = renderTargetProperties["colorTextureGPUWithSamples"];

      colorAttachment.view = colorTextureGPUWithSamples.createView();
      colorAttachment.resolveTarget = colorTextureGPU.createView();
    } else {
      colorAttachment.view = colorTextureGPU.createView();
    }

    var depthTextureGPU = renderTargetProperties["depthTextureGPU"];
    depthStencilAttachment.view = depthTextureGPU.createView();
  } else {
    if (this._parameters["antialias"] == true) {
      colorAttachment.view = this._colorBuffer!.createView();
      colorAttachment.resolveTarget =
          this._context.getCurrentTexture().createView();
    } else {
      colorAttachment.view = this._context.getCurrentTexture().createView();
      colorAttachment.resolveTarget = null;
    }

    depthStencilAttachment.view = this._depthBuffer!.createView();
  }

  this._background.update(scene);

  // start render pass

  var device = this._device;
  var cmdEncoder = device!.createCommandEncoder();

  GPURenderPassEncoder passEncoder =
      cmdEncoder.beginRenderPass(this._renderPassDescriptor);

  // global rasterization settings for all renderable objects

  var vp = this._viewport;

  if (vp != null) {
    var width = Math.floor(vp["width"] * this._pixelRatio);
    var height = Math.floor(vp["height"] * this._pixelRatio);

    passEncoder.setViewport(
        vp["x"].toDouble(),
        vp["y"].toDouble(),
        width.toDouble(),
        height.toDouble(),
        vp["minDepth"].toDouble(),
        vp["maxDepth"].toDouble());
  }

  var sc = this._scissor;

  if (sc != null) {
    var width = Math.floor(sc["width"] * this._pixelRatio);
    var height = Math.floor(sc["height"] * this._pixelRatio);

    passEncoder.setScissorRect(sc["x"], sc["y"], width, height);
  }

  // process render lists

  var opaqueObjects = this._currentRenderList.opaque;
  var transparentObjects = this._currentRenderList.transparent;

  if (opaqueObjects.length > 0)
    this._renderObjects(opaqueObjects, camera, passEncoder);
  if (transparentObjects.length > 0)
    this._renderObjects(transparentObjects, camera, passEncoder);

  // finish render pass

  passEncoder.end();
  device.queue.submit(cmdEncoder.finish(GPUCommandBufferDescriptor()));
}