flutter3d
Showcase Changelog 62 packages API reference

Debug views and render stats: the source

since 1.0.0-rc.1 Post-processing

Regions the guide quotes: channels (line 29), glow (line 46), view (line 76), stats (line 107).

1/// A material channel in place of the light, wiped across the frame, and
2/// what the frame says it cost.
3///
4/// Quoted by `debug_views.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_core/flutter3d_core.dart' show textureBytes;
9import 'package:flutter3d_showcase/pages/post/post_stage.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `debug-views` page: a lantern with a material channel wiped across
13/// the lit frame. Its check is that a channel is drawn, the pass costs add up
14/// to the frame, and the targets hold at least the picture.
15final class DebugViewsDemo extends ShowcaseDemo {
16 /// An index into [_channels].
17 int channel = 0;
19 /// The share of the frame, from the left, that stays lit; the rest shows the
20 /// channel. 0 to 1.
21 double split = 0.5;
23 /// Whether the page steps through the channels on its own.
24 bool stepping = true;
26 double _shown = 0.0;
27 late DebugViewSettings _debug;
29 /// Every channel there is, in the order the page steps through them.
30 static const List<DebugView> _channels = <DebugView>[
31 DebugView.albedo,
32 DebugView.normal,
33 DebugView.roughness,
34 DebugView.metallic,
35 DebugView.occlusion,
36 DebugView.emissive,
37 DebugView.uv,
38 DebugView.nonFinite,
39 ];
41 @override
42 void configureView(DemoContext context) => PostStage.frame(context);
44 @override
45 Scene build(DemoContext context) {
46 // Something that gives off light, so the emission channel has more to
47 // show than black.
48 final MeshNode lantern = MeshNode(
49 DeviceMesh.upload(
50 context.device,
51 CuboidShape(size: Vector3.all(0.4)).build(),
52 ),
53 RenderMaterial(
54 name: 'lantern',
55 baseColor: LinearColor.fromSrgb(0.2, 0.2, 0.2, 1.0),
56 emissive: LinearColor(0.3, 0.9, 0.5),
57 emissiveStrength: 2.0 * RenderMaterial.defaultEmissiveStrength,
58 ),
59 name: 'lantern',
60 )..setPosition(0.0, 0.2, 1.6);
61 return PostStage.build(context).scene..add(lantern);
62 }
64 @override
65 void update(DemoContext context, double dt) {
66 if (!stepping) return;
67 _shown += dt;
68 if (_shown > 2.0) {
69 _shown = 0.0;
70 channel = (channel + 1) % _channels.length;
71 }
72 }
74 @override
75 RenderSettings settings(DemoContext context) {
76 _debug = DebugViewSettings(view: _channels[channel], split: split);
77 return RenderSettings(debugView: _debug);
78 }
80 @override
81 List<DemoControl> controls(DemoContext context) => <DemoControl>[
82 ChoiceControl(
83 'Channel',
84 options: <String>[for (final DebugView v in _channels) v.name],
85 index: () => channel,
86 onChanged: (int i) => channel = i,
87 ),
88 SliderControl(
89 'Split (share left lit)',
90 min: 0,
91 max: 1,
92 value: () => split,
93 onChanged: (double v) => split = v,
94 ),
95 ToggleControl(
96 'Step through the channels',
97 value: () => stepping,
98 onChanged: (bool v) => stepping = v,
99 ),
100 ];
102 @override
103 void verify(Scene scene, FrameResult frame) {
104 if (!_debug.active) {
105 throw StateError('no channel would be drawn with $_debug');
106 }
107 final FramePass scenePass = frame.passes.firstWhere(
108 (FramePass p) => p.name == 'scene',
109 );
110 final FramePass composite = frame.passes.firstWhere(
111 (FramePass p) => p.name == 'composite',
112 );
113 final int passDraws = frame.passes.fold<int>(
114 0,
115 (int total, FramePass p) => total + p.drawCalls,
116 );
117 // The scene pass drew the five meshes' triangles, and the composite,
118 // which passes the debug side through untoned, is one triangle's draw.
119 if (scenePass.drawCalls < 5 ||
120 scenePass.triangles <= 0 ||
121 composite.drawCalls != 1 ||
122 passDraws != frame.drawCalls ||
123 frame.triangles < scenePass.triangles) {
124 throw StateError('the pass costs do not add up to the frame');
125 }
126 // The scene's own colour target alone is at least as large as the
127 // eight-bit picture handed back, so the targets cannot hold less.
128 if (frame.targetBytes < textureBytes(frame.frame)) {
129 throw StateError(
130 'the targets hold ${frame.targetBytes} bytes, less than the picture',
131 );
132 }
133 }