render method

dynamic render(
  1. dynamic renderer,
  2. dynamic writeBuffer,
  3. dynamic readBuffer, {
  4. num? deltaTime,
  5. bool? maskActive,
})
override

Implementation

render(renderer, writeBuffer, readBuffer,
    {num? deltaTime, bool? maskActive}) {
  if (this.sampleRenderTarget == null) {
    this.sampleRenderTarget = new WebGLRenderTarget(
        readBuffer.width,
        readBuffer.height,
        WebGLRenderTargetOptions({
          "minFilter": LinearFilter,
          "magFilter": LinearFilter,
          "format": RGBAFormat
        }));
    this.sampleRenderTarget!.texture.name = 'SSAARenderPass.sample';
  }

  var jitterOffsets =
      _JitterVectors[Math.max(0, Math.min(this.sampleLevel, 5))];

  var autoClear = renderer.autoClear;
  renderer.autoClear = false;

  renderer.getClearColor(this._oldClearColor);
  var oldClearAlpha = renderer.getClearAlpha();

  var baseSampleWeight = 1.0 / jitterOffsets.length;
  var roundingRange = 1 / 32;
  this.copyUniforms['tDiffuse']["value"] = this.sampleRenderTarget!.texture;

  var viewOffset = {
    "fullWidth": readBuffer.width,
    "fullHeight": readBuffer.height,
    "offsetX": 0,
    "offsetY": 0,
    "width": readBuffer.width,
    "height": readBuffer.height
  };

  Map<String, dynamic> originalViewOffset =
      jsonDecode(jsonEncode(this.camera.view ?? {}));

  if (originalViewOffset["enabled"] == true)
    viewOffset.addAll(originalViewOffset);

  // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  for (var i = 0; i < jitterOffsets.length; i++) {
    var jitterOffset = jitterOffsets[i];

    if (this.camera.type == "PerspectiveCamera") {
      (this.camera as PerspectiveCamera).setViewOffset(
          viewOffset["fullWidth"],
          viewOffset["fullHeight"],
          viewOffset["offsetX"] + jitterOffset[0] * 0.0625,
          viewOffset["offsetY"] + jitterOffset[1] * 0.0625, // 0.0625 = 1 / 16

          viewOffset["width"],
          viewOffset["height"]);
    } else if (this.camera.type == "OrthographicCamera") {
      (this.camera as OrthographicCamera).setViewOffset(
          viewOffset["fullWidth"],
          viewOffset["fullHeight"],
          viewOffset["offsetX"] + jitterOffset[0] * 0.0625,
          viewOffset["offsetY"] + jitterOffset[1] * 0.0625, // 0.0625 = 1 / 16

          viewOffset["width"],
          viewOffset["height"]);
    }

    var sampleWeight = baseSampleWeight;

    if (this.unbiased) {
      // the theory is that equal weights for each sample lead to an accumulation of rounding errors.
      // The following equation varies the sampleWeight per sample so that it is uniformly distributed
      // across a range of values whose rounding errors cancel each other out.

      var uniformCenteredDistribution =
          (-0.5 + (i + 0.5) / jitterOffsets.length);
      sampleWeight += roundingRange * uniformCenteredDistribution;
    }

    this.copyUniforms['opacity']["value"] = sampleWeight;
    renderer.setClearColor(this.clearColor, alpha: this.clearAlpha);
    renderer.setRenderTarget(this.sampleRenderTarget);
    renderer.clear(true, true, true);
    renderer.render(this.scene, this.camera);

    renderer.setRenderTarget(this.renderToScreen ? null : writeBuffer);

    if (i == 0) {
      renderer.setClearColor(Color.fromHex(0x000000), alpha: 0.0);
      renderer.clear(true, true, true);
    }

    this.fsQuad.render(renderer);
  }

  if (this.camera.type == "OrthographicCamera" &&
      originalViewOffset["enabled"] == true) {
    (this.camera as OrthographicCamera).setViewOffset(
        originalViewOffset["fullWidth"],
        originalViewOffset["fullHeight"],
        originalViewOffset["offsetX"],
        originalViewOffset["offsetY"],
        originalViewOffset["width"],
        originalViewOffset["height"]);
  } else if (this.camera.type == "PerspectiveCamera" &&
      originalViewOffset["enabled"] == true) {
    (this.camera as PerspectiveCamera).setViewOffset(
        originalViewOffset["fullWidth"],
        originalViewOffset["fullHeight"],
        originalViewOffset["offsetX"],
        originalViewOffset["offsetY"],
        originalViewOffset["width"],
        originalViewOffset["height"]);
  } else if (this.camera.type == "PerspectiveCamera") {
    (this.camera as PerspectiveCamera).clearViewOffset();
  } else if (this.camera.type == "OrthographicCamera") {
    (this.camera as OrthographicCamera).clearViewOffset();
  }

  renderer.autoClear = autoClear;
  renderer.setClearColor(this._oldClearColor, alpha: oldClearAlpha);
}