What a frame reports: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: geometry (line 27), light (line 72), totals (line 97), check (line 108).
1/// A small mixed scene whose result exposes useful frame counters.
2library;
4import 'package:flutter3d/flutter3d.dart';
5import 'package:flutter3d_showcase/src/demo/demo.dart';
7/// The `frame-stats` page: a cube, a torus and a floor under a sun. Its
8/// check is that the frame's counters describe that scene.
9final class FrameStatsDemo extends ShowcaseDemo {
10 /// Whether the sun casts shadows, which adds the shadow pass to the
11 /// counters.
12 bool shadows = true;
14 late final LightNode _sun;
17 void configureView(DemoContext context) {
18 context.orbit
19 ..distance = 8.0
20 ..pitch = 0.3
21 ..yaw = 0.5;
22 context.orbit.target.setValues(0.0, 0.7, 0.0);
23 }
26 Scene build(DemoContext context) {
27 final RenderMaterial matte = RenderMaterial(
28 name: 'matte',
29 baseColor: LinearColor.fromSrgb(0.24, 0.62, 0.82, 1.0),
30 roughness: 0.72,
31 );
32 final RenderMaterial metal = RenderMaterial(
33 name: 'metal',
34 baseColor: LinearColor.fromSrgb(0.82, 0.43, 0.18, 1.0),
35 metallic: 0.76,
36 roughness: 0.24,
37 );
38 final Scene scene = Scene()
39 ..ambientColor = LinearColor(0.42, 0.5, 0.68)
40 ..ambientIntensity = 0.12 * LightNode.legacyUnit
41 ..add(
42 MeshNode(
43 DeviceMesh.upload(
44 context.device,
45 const PlaneShape(width: 8.0, depth: 7.0).build(),
46 ),
47 matte.copy()..doubleSided = true,
48 name: 'floor',
49 )..castsShadow = false,
50 )
51 ..add(
52 MeshNode(
53 DeviceMesh.upload(
54 context.device,
55 CuboidShape(size: Vector3.all(1.5)).build(),
56 ),
57 matte,
58 name: 'cube',
59 )..setPosition(-1.25, 0.75, 0.0),
60 )
61 ..add(
62 MeshNode(
63 DeviceMesh.upload(
64 context.device,
65 const TorusShape(radius: 0.72, tubeRadius: 0.28).build(),
66 ),
67 metal,
68 name: 'torus',
69 )..setPosition(1.35, 1.0, 0.0),
70 );
72 _sun = LightNode(
73 name: 'sun',
74 intensity: 3.2 * LightNode.legacyUnit,
75 castsShadow: shadows,
76 )..setLocalForward(Vector3(-0.45, -0.82, -0.35));
77 scene.add(_sun);
78 return scene;
79 }
82 void update(DemoContext context, double dt) {
83 _sun.castsShadow = shadows;
84 }
87 List<DemoControl> controls(DemoContext context) => <DemoControl>[
88 ToggleControl(
89 'Shadow pass',
90 value: () => shadows,
91 onChanged: (bool value) => shadows = value,
92 ),
93 ];
96 void verify(Scene scene, FrameResult frame) {
97 final FramePass scenePass = frame.passes.firstWhere(
98 (FramePass pass) => pass.name == 'scene',
99 );
100 final FramePass compositePass = frame.passes.firstWhere(
101 (FramePass pass) => pass.name == 'composite',
102 );
103 final int passDraws = frame.passes.fold<int>(
104 0,
105 (int total, FramePass pass) => total + pass.drawCalls,
106 );
108 if (scenePass.drawCalls < 3 ||
109 compositePass.drawCalls != 1 ||
110 frame.drawCalls != passDraws ||
111 frame.triangles <= 0 ||
112 frame.pipelineSwitches <= 0 ||
113 frame.cpuMicros < frame.submitMicros) {
114 throw StateError('the frame counters do not describe the rendered scene');
115 }
116 }
117}