render method

void render(
  1. Camera camera,
  2. Canvas canvas, {
  3. Rect? viewport,
})

Renders the current state of this Scene onto the given ui.Canvas using the specified Camera.

The Camera provides the perspective from which the scene is viewed, and the ui.Canvas is the drawing surface onto which this Scene will be rendered.

Optionally, a ui.Rect can be provided to define a viewport, limiting the rendering area on the canvas. If no ui.Rect is specified, the entire canvas will be rendered.

Implementation

void render(Camera camera, ui.Canvas canvas, {ui.Rect? viewport}) {
  if (!_readyToRender) {
    debugPrint('Flutter Scene is not ready to render. Skipping frame.');
    debugPrint(
        'You may wait on the Future returned by Scene.initializeStaticResources() before rendering.');
    return;
  }

  final drawArea = viewport ?? canvas.getLocalClipBounds();
  if (drawArea.isEmpty) {
    return;
  }
  final enableMsaa = _antiAliasingMode == AntiAliasingMode.msaa;
  final gpu.RenderTarget renderTarget =
      surface.getNextRenderTarget(drawArea.size, enableMsaa);

  final env = environment.environmentMap.isEmpty()
      ? environment.withNewEnvironmentMap(Material.getDefaultEnvironmentMap())
      : environment;

  final encoder = SceneEncoder(renderTarget, camera, drawArea.size, env);
  root.render(encoder, Matrix4.identity());
  encoder.finish();

  final gpu.Texture texture = enableMsaa
      ? renderTarget.colorAttachments[0].resolveTexture!
      : renderTarget.colorAttachments[0].texture;
  final image = texture.asImage();
  canvas.drawImage(image, drawArea.topLeft, ui.Paint());
}