flutter3d
Showcase Changelog 62 packages API reference

Debug drawing: the source

since 0.7.0 Scene and geometry

Regions the guide quotes: model (line 29), rig (line 45), scene (line 63), options (line 79), controls (line 93), check (line 122).

1/// Built-in overlays for inspecting a scene while it runs.
2library;
3
4import 'package:flutter3d/flutter3d.dart';
5import 'package:flutter3d_showcase/src/demo/demo.dart';
6
7/// The `debug-draw` page: a body, a capsule, a three-joint rig and a light
8/// with the renderer's debug overlays switched on for them. Its check is that
9/// the overlays asked for were drawn.
10final class DebugDrawDemo extends ShowcaseDemo {
11 bool _bounds = true;
12 bool _normals = true;
13 bool _axes = true;
14 bool _lights = true;
15 bool _skeleton = true;
16 late final Skeleton _rig;
18 @override
19 void configureView(DemoContext context) {
20 context.orbit
21 ..distance = 7.2
22 ..pitch = 0.24
23 ..yaw = 0.55;
24 context.orbit.target.setValues(0.0, 0.8, 0.0);
25 }
27 @override
28 Scene build(DemoContext context) {
29 final MeshData bodyData = const CapsuleShape(
30 radius: 0.58,
31 height: 1.4,
32 segments: 20,
33 rings: 6,
34 ).build(layout: VertexLayout.skinned);
35 final MeshNode body = MeshNode(
36 DeviceMesh.upload(context.device, bodyData),
37 RenderMaterial(
38 name: 'blue body',
39 baseColor: LinearColor.fromSrgb(0.12, 0.42, 0.78, 1.0),
40 roughness: 0.48,
41 ),
42 name: 'inspected capsule',
43 )..setPosition(0.0, 1.25, 0.0);
45 final SceneNode hip = SceneNode(name: 'hip')..setPosition(0.0, 0.45, 0.0);
46 final SceneNode chest = SceneNode(name: 'chest')
47 ..setPosition(0.0, 0.8, 0.0);
48 final SceneNode head = SceneNode(name: 'head')
49 ..setPosition(0.25, 0.72, 0.0);
50 hip.add(chest);
51 chest.add(head);
52 _rig = Skeleton(
53 name: 'three-joint rig',
54 joints: <SceneNode>[hip, chest, head],
55 inverseBindMatrices: <Matrix4>[
56 Matrix4.copy(hip.sceneMatrix)..invert(),
57 Matrix4.copy(chest.sceneMatrix)..invert(),
58 Matrix4.copy(head.sceneMatrix)..invert(),
59 ],
60 );
61 body.skeleton = _rig;
63 final Scene scene = Scene()
64 ..ambientColor = LinearColor(0.38, 0.46, 0.62)
65 ..ambientIntensity = 0.14 * LightNode.legacyUnit
66 ..add(hip)
67 ..add(body)
68 ..add(
69 LightNode(
70 type: LightType.point,
71 intensity: 14.0 * LightNode.legacyUnit,
72 range: 8.0,
73 name: 'debugged light',
74 )..setPosition(-2.2, 3.2, 2.0),
75 );
76 return scene;
77 }
79 DebugDrawSettings get _debug => DebugDrawSettings(
80 bounds: _bounds,
81 normals: _normals,
82 axes: _axes,
83 lightGizmos: _lights,
84 skeletons: _skeleton,
85 normalLength: 0.18,
86 );
88 @override
89 RenderSettings settings(DemoContext context) => RenderSettings(debug: _debug);
91 @override
92 List<DemoControl> controls(DemoContext context) => <DemoControl>[
93 ToggleControl(
94 'Bounds',
95 value: () => _bounds,
96 onChanged: (bool value) => _bounds = value,
97 ),
98 ToggleControl(
99 'Normals',
100 value: () => _normals,
101 onChanged: (bool value) => _normals = value,
102 ),
103 ToggleControl(
104 'World axes',
105 value: () => _axes,
106 onChanged: (bool value) => _axes = value,
107 ),
108 ToggleControl(
109 'Light gizmo',
110 value: () => _lights,
111 onChanged: (bool value) => _lights = value,
112 ),
113 ToggleControl(
114 'Skeleton',
115 value: () => _skeleton,
116 onChanged: (bool value) => _skeleton = value,
117 ),
118 ];
120 @override
121 void verify(Scene scene, FrameResult frame) {
122 if (!_debug.anyEnabled ||
123 _rig.jointCount != 3 ||
124 frame.debugLines <= 15 ||
125 frame.drawCalls < 2) {
126 throw StateError('the requested debug overlays were not drawn');
127 }
128 }