Capturing a frame: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: probe (line 22), capture (line 45), scene (line 58), check (line 84).
1/// A frame recorded pass by pass, with the pixels each pass wrote.
2library;
4import 'package:flutter3d/flutter3d.dart';
5import 'package:flutter3d_showcase/src/demo/demo.dart';
7/// The `frame-capture` page: a torus and a probe cube recorded pass by pass.
8/// Its check is that the capture holds a lit scene pass.
9final class FrameCaptureDemo extends ShowcaseDemo {
10 late final FrameCapture _capture;
13 void configureView(DemoContext context) {
14 context.orbit
15 ..distance = 5.0
16 ..pitch = 0.2
17 ..yaw = 0.4;
18 }
21 Future<void> prepare(DemoContext context) async {
22 final Scene probe = Scene()
23 ..add(
24 MeshNode(
25 DeviceMesh.upload(
26 context.device,
27 CuboidShape(size: Vector3.all(1.2)).build(),
28 ),
29 RenderMaterial(
30 name: 'probe',
31 baseColor: LinearColor.fromSrgb(0.85, 0.42, 0.18, 1.0),
32 ),
33 name: 'probe cube',
34 ),
35 )
36 ..add(
37 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
38 ..setLocalForward(Vector3(-0.4, -0.8, -0.3)),
39 );
40 final CameraNode probeCamera = CameraNode()
41 ..setPosition(0.0, 0.0, 4.0)
42 ..lookAt(Vector3.zero());
43 probe.add(probeCamera);
45 final Future<FrameCapture> pending = context.renderer.captureNextFrame();
46 context.renderer.render(
47 width: 96,
48 height: 96,
49 scene: probe,
50 views: <CameraView>[CameraView(camera: probeCamera)],
51 settings: const RenderSettings(),
52 );
53 _capture = await pending;
54 }
57 Scene build(DemoContext context) {
58 final Scene scene = Scene()
59 ..ambientColor = LinearColor(0.42, 0.48, 0.64)
60 ..ambientIntensity = 0.16 * LightNode.legacyUnit
61 ..add(
62 MeshNode(
63 DeviceMesh.upload(
64 context.device,
65 const TorusShape(radius: 1.0, tubeRadius: 0.4).build(),
66 ),
67 RenderMaterial(
68 name: 'torus',
69 baseColor: LinearColor.fromSrgb(0.28, 0.62, 0.9, 1.0),
70 roughness: 0.4,
71 ),
72 name: 'torus',
73 ),
74 )
75 ..add(
76 LightNode(name: 'sun', intensity: 3.2 * LightNode.legacyUnit)
77 ..setLocalForward(Vector3(-0.42, -0.8, -0.34)),
78 );
79 return scene;
80 }
83 void verify(Scene scene, FrameResult frame) {
84 final CapturedPass? scenePass = _capture.passNamed('scene');
85 final CapturedImage? color = scenePass?.imageOf(
86 FrameResourceIds.hdrColor.name,
87 );
88 if (scenePass == null ||
89 !scenePass.active ||
90 color == null ||
91 color.isBlack ||
92 frame.drawCalls < 1) {
93 throw StateError('the captured frame did not record a lit scene pass');
94 }
95 }
96}