flutter3d
Showcase Changelog 62 packages API reference

A scene written as widgets: the source

since 1.0.0-rc.1 Widgets and the rest

Regions the guide quotes: widgets (line 33), mount (line 90), check (line 150).

1/// A scene written as widgets: a light, a material the boxes below it share,
2/// a row of boxes and a turning pivot with a ball on it, built into the
3/// page's scene by `SceneWidgets.mount` and rebuilt every frame.
4///
5/// Quoted by `scene_widgets.md` and shown whole in the Source tab.
6library;
7
8import 'package:flutter/widgets.dart';
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_app/flutter3d_app.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `scene-widgets` page: a scene written as widgets and mounted into the
14/// page's scene. Its check is a mesh per widget, boxes that share one
15/// material, a rebuild that keeps and changes its nodes, and a removed box
16/// gone from the scene.
17final class SceneWidgetsDemo extends ShowcaseDemo {
18 late final SceneWidgetsMount _mount;
20 int _boxes = 3;
21 bool _warm = true;
22 double _angle = 0.0;
24 @override
25 void configureView(DemoContext context) {
26 context.orbit
27 ..distance = 6.0
28 ..pitch = 0.4
29 ..yaw = 0.3;
30 context.orbit.target.setValues(0.0, 0.4, 0.0);
31 }
33 /// The whole scene, as a function of the page's three fields. Nothing here
34 /// makes a node: each widget owns one, and a rebuild changes it.
35 List<Widget> _children() => <Widget>[
36 // Lux: about 14 500, a bright overcast-to-hazy sun.
37 Light3D.directional(
38 direction: Vector3(-0.4, -0.8, -0.5),
39 intensity: 14500.0,
40 ),
41 Mesh3D(
42 shape: CuboidShape(size: Vector3(6.0, 0.2, 3.0)),
43 material: _floor,
44 position: Vector3(0.0, -0.1, 0.0),
45 name: 'floor',
46 ),
47 Material3D(
48 name: 'paint',
49 baseColor: _warm
50 ? LinearColor.fromSrgb(0.9, 0.45, 0.2, 1.0)
51 : LinearColor.fromSrgb(0.2, 0.5, 0.9, 1.0),
52 roughness: 0.6,
53 children: <Widget>[
54 for (var i = 0; i < _boxes; i++)
55 Mesh3D(
56 key: ValueKey<String>('box $i'),
57 shape: CuboidShape(size: Vector3.all(0.6)),
58 position: Vector3(-2.2 + i * 0.9, 0.3, -0.6),
59 name: 'box $i',
60 ),
61 ],
62 ),
63 Node3D(
64 name: 'pivot',
65 position: Vector3(0.0, 0.0, 0.8),
66 rotation: Quaternion.axisAngle(Vector3(0.0, 1.0, 0.0), _angle),
67 children: <Widget>[
68 Mesh3D(
69 shape: SphereShape(radius: 0.3, segments: 32, rings: 16),
70 material: _ball,
71 position: Vector3(1.0, 0.3, 0.0),
72 name: 'ball',
73 ),
74 ],
75 ),
76 ];
78 final RenderMaterial _floor = RenderMaterial(
79 name: 'floor',
80 baseColor: LinearColor.fromSrgb(0.4, 0.42, 0.45, 1.0),
81 roughness: 0.9,
82 );
83 final RenderMaterial _ball = RenderMaterial(
84 name: 'ball',
85 baseColor: LinearColor.fromSrgb(0.85, 0.85, 0.9, 1.0),
86 roughness: 0.25,
87 metallic: 1.0,
88 );
90 @override
91 Scene build(DemoContext context) {
92 final Scene scene = Scene()
93 ..ambientColor = LinearColor(0.5, 0.55, 0.65)
94 ..ambientIntensity = 0.3 * LightNode.legacyUnit;
95 _mount = SceneWidgets.mount(
96 scene: scene,
97 renderer: context.renderer,
98 device: context.device,
99 children: _children(),
100 );
101 return scene;
102 }
104 @override
105 void update(DemoContext context, double dt) {
106 _angle += dt * 0.8;
107 // A rebuild, as `setState` would cause under a `Scene3D`: the same nodes,
108 // given the widgets' new properties.
109 _mount.update(_children());
110 }
112 @override
113 void dispose() => _mount.dispose();
115 @override
116 List<DemoControl> controls(DemoContext context) => <DemoControl>[
117 SliderControl(
118 'Boxes',
119 min: 1,
120 max: 6,
121 divisions: 5,
122 value: () => _boxes.toDouble(),
123 onChanged: (double v) => _boxes = v.round(),
124 format: (double v) => '${v.round()}',
125 ),
126 ToggleControl(
127 'Warm paint',
128 value: () => _warm,
129 onChanged: (bool v) => _warm = v,
130 ),
131 ];
133 List<MeshNode> _meshes(Scene scene) {
134 final List<MeshNode> found = <MeshNode>[];
135 scene.root.traverse((SceneNode node) {
136 if (node is MeshNode) found.add(node);
137 });
138 return found;
139 }
141 MeshNode? _named(Scene scene, String name) {
142 for (final MeshNode mesh in _meshes(scene)) {
143 if (mesh.name == name) return mesh;
144 }
145 return null;
146 }
148 @override
149 void verify(Scene scene, FrameResult frame) {
150 final List<MeshNode> before = _meshes(scene);
151 if (before.length != 2 + _boxes) {
152 throw StateError('${before.length} meshes for ${2 + _boxes} widgets');
153 }
154 final MeshNode first = _named(scene, 'box 0')!;
155 final RenderMaterial shared = first.material;
156 if (!identical(_named(scene, 'box 1')!.material, shared)) {
157 throw StateError('the boxes under one Material3D do not share it');
158 }
159 // A rebuild with one more box and the other paint: box 0 keeps its node
160 // and its material, which changes in place.
161 final (int boxes, bool warm) = (_boxes, _warm);
162 _boxes = boxes + 1;
163 _warm = !warm;
164 _mount.update(_children());
165 if (!identical(_named(scene, 'box 0'), first) ||
166 !identical(first.material, shared) ||
167 _named(scene, 'box $boxes') == null ||
168 (shared.baseColor.toSrgb().b - (warm ? 0.9 : 0.2)).abs() > 1e-6) {
169 throw StateError('a rebuild did not keep and change the nodes it owns');
170 }
171 // And one box fewer takes that box's node out of the scene.
172 _boxes = boxes;
173 _warm = warm;
174 _mount.update(_children());
175 if (_named(scene, 'box $boxes') != null) {
176 throw StateError('a box whose widget went is still in the scene');
177 }
178 if (frame.drawCalls < 1) throw StateError('nothing reached the frame');
179 }