flutter3d
Showcase Changelog 62 packages API reference

Step, linear and cubic tracks: the source

since 0.7.0 Animation

Regions the guide quotes: tracks (line 59), player (line 82), live (line 138).

1/// Three cubes bounce on the same keyframes through three different
2/// `AnimationInterpolation`s: step, linear and cubic spline.
3///
4/// Quoted by `interpolation.md` and shown whole in the Source tab.
5library;
6
7import 'dart:typed_data';
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `interpolation` page: three cubes on the same bounce keys, through step,
13/// linear and cubic spline. Its check reads all three just after the first key,
14/// where they must already disagree.
15final class InterpolationDemo extends ShowcaseDemo {
16 late final AnimationPlayer _player;
17 late final MeshNode _stepCube;
18 late final MeshNode _linearCube;
19 late final MeshNode _cubicCube;
21 @override
22 void configureView(DemoContext context) {
23 context.orbit
24 ..distance = 5.5
25 ..pitch = 0.2;
26 }
28 @override
29 Scene build(DemoContext context) {
30 final DeviceMesh box = DeviceMesh.upload(
31 context.device,
32 CuboidShape(size: Vector3.all(0.6)).build(),
33 );
34 _stepCube = MeshNode(
35 box,
36 RenderMaterial(
37 name: 'step',
38 baseColor: LinearColor.fromSrgb(0.85, 0.25, 0.2, 1.0),
39 ),
40 name: 'step',
41 );
42 _linearCube = MeshNode(
43 box,
44 RenderMaterial(
45 name: 'linear',
46 baseColor: LinearColor.fromSrgb(0.25, 0.75, 0.3, 1.0),
47 ),
48 name: 'linear',
49 );
50 _cubicCube = MeshNode(
51 box,
52 RenderMaterial(
53 name: 'cubic',
54 baseColor: LinearColor.fromSrgb(0.25, 0.4, 0.9, 1.0),
55 ),
56 name: 'cubic',
57 );
59 // The player writes a translation track's sampled (x, y, z) straight
60 // through `setPosition`, replacing whatever was there — so the column
61 // each cube stands in has to be baked into the track itself. A
62 // `setPosition` on the node instead would have held for exactly one
63 // frame, until `AnimationPlayer.play` below sampled time zero and
64 // snapped every cube back to x = 0, stacking all three on top of each
65 // other.
66 final AnimationTrack stepTrack = _bounceTrack(
67 nodeIndex: 0,
68 x: -1.4,
69 interpolation: AnimationInterpolation.step,
70 );
71 final AnimationTrack linearTrack = _bounceTrack(
72 nodeIndex: 1,
73 x: 0.0,
74 interpolation: AnimationInterpolation.linear,
75 );
76 final AnimationTrack cubicTrack = _bounceTrack(
77 nodeIndex: 2,
78 x: 1.4,
79 interpolation: AnimationInterpolation.cubicSpline,
80 );
82 final AnimationClip bounce = AnimationClip(
83 name: 'bounce',
84 tracks: <AnimationTrack>[stepTrack, linearTrack, cubicTrack],
85 );
86 _player = AnimationPlayer(
87 clips: <AnimationClip>[bounce],
88 targets: <AnimationTarget?>[_stepCube, _linearCube, _cubicCube],
89 )..play(index: 0);
91 return Scene()
92 ..add(_stepCube)
93 ..add(_linearCube)
94 ..add(_cubicCube)
95 ..add(
96 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
97 ..setLocalForward(Vector3(-0.3, -0.8, -0.4)),
98 );
99 }
101 /// The same three keys, up and back down, on every track: only how the
102 /// player reads *between* them changes.
103 ///
104 /// A cubic key carries an in tangent, the value and an out tangent; flat
105 /// tangents (zero) are enough to show the ease the other two do not have.
106 static AnimationTrack _bounceTrack({
107 required int nodeIndex,
108 required double x,
109 required AnimationInterpolation interpolation,
110 }) {
111 final Float32List times = Float32List.fromList(<double>[0.0, 0.5, 1.0]);
112 final List<double> heights = <double>[0.0, 1.4, 0.0];
113 final int perKey = interpolation.valuesPerKey;
114 final Float32List values = Float32List(heights.length * 3 * perKey);
116 for (var i = 0; i < heights.length; i++) {
117 final int base = i * 3 * perKey;
118 // For a cubic key the value sits in the middle third; the in and out
119 // tangent thirds stay zero, which is a flat tangent — including x's,
120 // since it never moves and a flat tangent is what "never moves" means.
121 final int valueAt = base + (perKey == 3 ? 3 : 0);
122 values[valueAt] = x;
123 values[valueAt + 1] = heights[i];
124 }
126 return AnimationTrack(
127 nodeIndex: nodeIndex,
128 path: AnimationPath.translation,
129 interpolation: interpolation,
130 times: times,
131 values: values,
132 componentCount: 3,
133 );
134 }
136 @override
137 void update(DemoContext context, double dt) {
138 _player.advance(dt);
139 }
141 @override
142 List<DemoControl> controls(DemoContext context) => <DemoControl>[
143 SliderControl(
144 'Speed',
145 min: 0.1,
146 max: 2,
147 value: () => _player.speed,
148 onChanged: (double v) => _player.speed = v,
149 ),
150 ];
152 @override
153 void verify(Scene scene, FrameResult frame) {
154 final Vector3 step = _stepCube.sceneMatrix.getTranslation();
155 final Vector3 linear = _linearCube.sceneMatrix.getTranslation();
156 final Vector3 cubic = _cubicCube.sceneMatrix.getTranslation();
158 // Each track's baked x must survive the player writing translation
159 // through — a track that forgot it would snap every cube to x = 0 and
160 // stack the three on top of each other.
161 if ((step.x - -1.4).abs() > 1e-6 ||
162 linear.x.abs() > 1e-6 ||
163 (cubic.x - 1.4).abs() > 1e-6) {
164 throw StateError(
165 'the three cubes should stand apart at x = -1.4, 0, 1.4; got '
166 '${step.x}, ${linear.x}, ${cubic.x}',
167 );
168 }
169 if (step.y.abs() > 1e-6) {
170 throw StateError('step should hold the first key until the next one');
171 }
172 if (linear.y <= 1e-4) {
173 throw StateError('linear should already have left the first key');
174 }
175 if (cubic.y <= 0.0 || cubic.y >= linear.y) {
176 throw StateError(
177 'cubic should ease away from the first key slower than linear does',
178 );
179 }
180 }