Orbit: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: bounds (line 37), frame (line 42), swing (line 45), advance (line 55).
1/// Framing a model automatically, and swinging the view to a new angle
2/// instead of jumping to it.
3///
4/// Quoted by `orbit_controller.md` and shown whole in the Source tab.
5library;
7import 'dart:math' as math;
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `orbit-controller` page: a box framed by `frameBounds` and a turn
13/// animated with `animateTo`. Its check is the distance framing chose and
14/// that `advance` steps the turn.
15final class OrbitControllerDemo extends ShowcaseDemo {
16 late final Aabb3 _bounds;
17 late final OrbitController _orbit;
18 late final double _startYaw;
21 Scene build(DemoContext context) {
22 _orbit = context.orbit;
23 final RenderMaterial stone = RenderMaterial(
24 name: 'stone',
25 baseColor: LinearColor.fromSrgb(0.6, 0.66, 0.7, 1.0),
26 roughness: 0.7,
27 );
28 final MeshNode box = MeshNode(
29 DeviceMesh.upload(
30 context.device,
31 CuboidShape(size: Vector3(1.0, 2.0, 1.0)).build(),
32 ),
33 stone,
34 name: 'box',
35 );
37 _bounds = Aabb3.centerAndHalfExtents(
38 Vector3.zero(),
39 Vector3(0.5, 1.0, 0.5),
40 );
42 context.orbit.frameBounds(_bounds);
44 _startYaw = context.orbit.yaw;
45 context.orbit.animateTo(yaw: context.orbit.yaw + math.pi, seconds: 1.2);
47 return Scene()
48 ..add(box)
49 ..add(
50 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
51 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
52 );
53 }
56 void update(DemoContext context, double dt) {
57 context.orbit.update(dt);
58 }
61 List<DemoControl> controls(DemoContext context) => <DemoControl>[
62 ToggleControl(
63 'Swing to the other side',
64 value: () => context.orbit.isTurning,
65 onChanged: (bool _) =>
66 context.orbit.animateTo(yaw: context.orbit.yaw + math.pi),
67 ),
68 ];
71 void verify(Scene scene, FrameResult frame) {
72 if (frame.drawCalls < 1) {
73 throw StateError('the box was not drawn');
74 }
75 // frameBounds places the camera at radius / sin(fov / 2) * margin, using
76 // its own default field of view and margin. Recomputing that formula for
77 // this box's own radius has to land on the distance the orbit actually
78 // holds.
79 final Vector3 extent = (_bounds.max - _bounds.min)..scale(0.5);
80 final double radius = math.max(extent.length, 1e-4);
81 final double expected = radius / math.sin(math.pi / 4 * 0.5) * 1.25;
82 if ((_orbit.distance - expected).abs() > 1e-6) {
83 throw StateError(
84 'frameBounds put the camera at ${_orbit.distance}, expected $expected',
85 );
86 }
87 // The camera also spent one frame turning towards the other side, so its
88 // yaw must have moved away from wherever frameBounds first pointed it,
89 // and the turn must still be in flight after a sixtieth of a second of a
90 // 1.2 second swing.
91 if (_orbit.yaw == _startYaw || !_orbit.isTurning) {
92 throw StateError('advance did not step the turn animateTo started');
93 }
94 }
95}