The shared camera rig: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: tuning (line 15), rig (line 43), place (line 66), settle (line 103).
1/// The part of a chasing view every chasing view has: easing towards where
2/// it should be, carrying a knock or a shake that fades, and staying out of
3/// the walls.
4///
5/// Quoted by `camera_shake.md` and shown whole in the Source tab.
6library;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_camera/flutter3d_camera.dart'
10 show CameraRig, RigSettings;
11import 'package:flutter3d_physics/flutter3d_physics.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
13import 'package:flutter3d_showcase/src/demo/run_physics.dart';
15/// The six numbers `CameraRig` needs to place itself, gathered under a name
16/// this page's own view uses.
17final class _ChaseTuning extends RigSettings {
18 const _ChaseTuning()
19 : super(
20 distance: 4.0,
21 height: 1.5,
22 aimHeight: 1.0,
23 lag: 8.0,
24 nearClearance: 0.3,
25 minDistance: 1.0,
26 );
27}
29/// The `camera-shake` page: a chasing rig following a runner, with a kick
30/// that shakes it. Its check is that an undisturbed rig settles onto its
31/// free eye.
32final class CameraShakeDemo extends ShowcaseDemo {
33 /// Set by the "Kick the rig" switch; the next update kicks the rig once and
34 /// turns it back off.
35 bool shaken = false;
37 late final CameraRig _rig;
38 late final MeshNode _ball;
39 final Vector3 _target = Vector3.zero();
42 Scene build(DemoContext context) {
43 _rig = CameraRig(world: onRunPhysics(CollisionWorld()));
45 final material = RenderMaterial(
46 name: 'runner',
47 baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.9, 1.0),
48 );
49 _ball = MeshNode(
50 DeviceMesh.upload(context.device, SphereShape(segments: 16).build()),
51 material,
52 );
53 return Scene()
54 ..add(_ball)
55 ..add(
56 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
57 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
58 );
59 }
62 void configureView(DemoContext context) => context.orbit.frameBounds(
63 Aabb3.minMax(Vector3(-2, -2, -2), Vector3(2, 2, 2)),
64 );
66 static const _tuning = _ChaseTuning();
68 void _place(double dt) {
69 if (shaken) {
70 _rig.kick(Vector3(0, -0.4, 0));
71 _rig.shake(0.05, seconds: 0.3);
72 shaken = false;
73 }
74 final desiredEye = Vector3(0, _tuning.height, _tuning.distance);
75 _rig.place(
76 desiredEye: desiredEye,
77 desiredTarget: _target,
78 lag: _tuning.lag,
79 dt: dt,
80 );
81 }
84 void update(DemoContext context, double dt) {
85 _place(dt);
86 _ball.setPositionFrom(_rig.eye);
87 }
90 List<DemoControl> controls(DemoContext context) => <DemoControl>[
91 ToggleControl(
92 'Kick the rig',
93 value: () => shaken,
94 onChanged: (bool v) => shaken = v,
95 ),
96 ];
99 void verify(Scene scene, FrameResult frame) {
100 if (frame.drawCalls < 1) {
101 throw StateError('the marker was not drawn');
102 }
103 // Placed enough times with nothing disturbing it, the rig settles to
104 // within a few millimetres of the free eye it is chasing.
105 for (var i = 0; i < 240; i++) {
106 _place(1 / 60);
107 }
108 final settledGap = (_rig.eye - _rig.freeEye).length;
109 if (settledGap > 0.01) {
110 throw StateError(
111 'an undisturbed rig should settle onto its free eye, '
112 'gap was $settledGap',
113 );
114 }
115 }
116}