flutter3d
Showcase Changelog 62 packages API reference

Plan a frame before drawing: the source

since 0.7.0 Scene and geometry

Regions the guide quotes: scene (line 31), settings (line 61), plan (line 66), controls (line 81), check (line 96).

1/// A dry-run frame graph compared with the frame that followed it.
2library;
3
4import 'package:flutter3d/flutter3d.dart';
5import 'package:flutter3d_showcase/src/demo/demo.dart';
6
7/// The `frame-graph` page: an emissive torus and bright metal, the frame
8/// planned dry before it is drawn. Its check is that the plan names the same
9/// passes the frame ran.
10final class FrameGraphDemo extends ShowcaseDemo {
11 /// Whether the bloom pass runs, at an intensity of 0.65; off sets it to 0.
12 bool bloom = true;
14 /// Whether the antialias pass runs.
15 bool antiAlias = true;
17 late final Scene _scene;
18 List<String> _planned = const <String>[];
19 List<String> _skipped = const <String>[];
21 @override
22 void configureView(DemoContext context) {
23 context.orbit
24 ..distance = 6.5
25 ..pitch = 0.25
26 ..yaw = 0.45;
27 }
29 @override
30 Scene build(DemoContext context) {
31 final RenderMaterial bright = RenderMaterial(
32 name: 'bright metal',
33 baseColor: LinearColor.fromSrgb(0.7, 0.2, 0.08, 1.0),
34 emissive: LinearColor(1.0, 0.16, 0.03),
35 emissiveStrength: 2.5 * RenderMaterial.defaultEmissiveStrength,
36 metallic: 0.62,
37 roughness: 0.22,
38 );
39 _scene = Scene()
40 ..ambientColor = LinearColor(0.38, 0.46, 0.64)
41 ..ambientIntensity = 0.12 * LightNode.legacyUnit
42 ..add(
43 MeshNode(
44 DeviceMesh.upload(
45 context.device,
46 const TorusShape(radius: 1.15, tubeRadius: 0.38).build(),
47 ),
48 bright,
49 name: 'emissive torus',
50 ),
51 )
52 ..add(
53 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
54 ..setLocalForward(Vector3(-0.45, -0.82, -0.35)),
55 );
56 return _scene;
57 }
59 @override
60 RenderSettings settings(DemoContext context) {
61 final RenderSettings next = RenderSettings(
62 bloom: BloomSettings(intensity: bloom ? 0.65 : 0.0),
63 antiAlias: AntiAliasSettings(enabled: antiAlias),
64 );
66 final CompiledFrameGraph graph = context.renderer.planFrame(
67 scene: _scene,
68 views: <CameraView>[context.view],
69 settings: next,
70 );
71 _planned = <String>[
72 for (final FrameGraphNode node in graph.order) node.name,
73 ];
74 _skipped = <String>[
75 for (final SkippedPass pass in graph.skipped) pass.name,
76 ];
77 return next;
78 }
80 @override
81 List<DemoControl> controls(DemoContext context) => <DemoControl>[
82 ToggleControl(
83 'Bloom pass',
84 value: () => bloom,
85 onChanged: (bool value) => bloom = value,
86 ),
87 ToggleControl(
88 'Antialias pass',
89 value: () => antiAlias,
90 onChanged: (bool value) => antiAlias = value,
91 ),
92 ];
94 @override
95 void verify(Scene scene, FrameResult frame) {
96 final List<String> drawn = <String>[
97 for (final FramePass pass in frame.passes) pass.name,
98 ];
99 if (_planned.isEmpty ||
100 !_planned.contains('scene') ||
101 !_planned.contains('composite') ||
102 bloom && !_planned.contains('bloom') ||
103 antiAlias && !_planned.contains('antialias') ||
104 !_sameNames(_planned, drawn) ||
105 _skipped.any(drawn.contains)) {
106 throw StateError('the planned graph did not match the drawn frame');
107 }
108 }
111bool _sameNames(List<String> left, List<String> right) {
112 if (left.length != right.length) return false;
113 for (var i = 0; i < left.length; i++) {
114 if (left[i] != right[i]) return false;
115 }
116 return true;