flutter3d
Showcase Changelog 62 packages API reference

Clips and crossfades: the source

since 0.1.0 Animation

Regions the guide quotes: clips (line 47), player (line 56), live (line 132).

1/// Two clips on one cube: `AnimationPlayer` plays one, fades to the other and
2/// answers to a speed and a wrap mode while it does.
3///
4/// Quoted by `clip_playback.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8import 'dart:typed_data';
9
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `clip-playback` page: a cube that spins on one clip and crossfades into
14/// a nod on another. Its check catches the crossfade partway through.
15final class ClipPlaybackDemo extends ShowcaseDemo {
16 /// Which wrap mode the player uses, as an index into loop, once and
17 /// ping-pong. The "Wrap" choice writes it and hands the player the mode at
18 /// once.
19 int wrapIndex = 0;
21 late final AnimationPlayer _player;
23 static const List<AnimationWrap> _wraps = <AnimationWrap>[
24 AnimationWrap.loop,
25 AnimationWrap.once,
26 AnimationWrap.pingPong,
27 ];
29 @override
30 void configureView(DemoContext context) {
31 context.orbit
32 ..distance = 3.5
33 ..pitch = 0.25;
34 }
36 @override
37 Scene build(DemoContext context) {
38 final MeshNode cube = MeshNode(
39 DeviceMesh.upload(context.device, CuboidShape().build()),
40 RenderMaterial(
41 name: 'cube',
42 baseColor: LinearColor.fromSrgb(0.3, 0.55, 0.9, 1.0),
43 ),
44 name: 'cube',
45 );
47 final AnimationClip spin = AnimationClip(
48 name: 'spin',
49 tracks: <AnimationTrack>[_yawTrack(turns: 1, duration: 2.0)],
50 );
51 final AnimationClip nod = AnimationClip(
52 name: 'nod',
53 tracks: <AnimationTrack>[_nodTrack(duration: 1.2)],
54 );
56 _player = AnimationPlayer(
57 clips: <AnimationClip>[spin, nod],
58 targets: <AnimationTarget?>[cube],
59 );
60 _player.play(index: 0);
61 _player.crossFadeTo(1, fade: const Duration(milliseconds: 400));
63 return Scene()
64 ..add(cube)
65 ..add(
66 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
67 ..setLocalForward(Vector3(-0.4, -0.7, -0.5)),
68 );
69 }
71 /// A full turn about Y, five keys so the loop closes exactly on itself.
72 static AnimationTrack _yawTrack({
73 required int turns,
74 required double duration,
75 }) {
76 const int keys = 5;
77 final Float32List times = Float32List(keys);
78 final Float32List values = Float32List(keys * 4);
79 for (var i = 0; i < keys; i++) {
80 final double t = i / (keys - 1);
81 times[i] = t * duration;
82 final Quaternion q = Quaternion.axisAngle(
83 Vector3(0.0, 1.0, 0.0),
84 t * turns * 2 * math.pi,
85 );
86 values[i * 4] = q.x;
87 values[i * 4 + 1] = q.y;
88 values[i * 4 + 2] = q.z;
89 values[i * 4 + 3] = q.w;
90 }
91 return AnimationTrack(
92 nodeIndex: 0,
93 path: AnimationPath.rotation,
94 interpolation: AnimationInterpolation.linear,
95 times: times,
96 values: values,
97 componentCount: 4,
98 );
99 }
101 /// A short dip on X: level, down, level.
102 static AnimationTrack _nodTrack({required double duration}) {
103 final List<double> angles = <double>[0.0, -0.5, 0.0];
104 final Float32List times = Float32List.fromList(<double>[
105 0.0,
106 duration / 2,
107 duration,
108 ]);
109 final Float32List values = Float32List(angles.length * 4);
110 for (var i = 0; i < angles.length; i++) {
111 final Quaternion q = Quaternion.axisAngle(
112 Vector3(1.0, 0.0, 0.0),
113 angles[i],
114 );
115 values[i * 4] = q.x;
116 values[i * 4 + 1] = q.y;
117 values[i * 4 + 2] = q.z;
118 values[i * 4 + 3] = q.w;
119 }
120 return AnimationTrack(
121 nodeIndex: 0,
122 path: AnimationPath.rotation,
123 interpolation: AnimationInterpolation.linear,
124 times: times,
125 values: values,
126 componentCount: 4,
127 );
128 }
130 @override
131 void update(DemoContext context, double dt) {
132 _player.advance(dt);
133 }
135 @override
136 List<DemoControl> controls(DemoContext context) => <DemoControl>[
137 SliderControl(
138 'Speed',
139 min: 0.1,
140 max: 3,
141 value: () => _player.speed,
142 onChanged: (double v) => _player.speed = v,
143 ),
144 ChoiceControl(
145 'Wrap',
146 options: const <String>['Loop', 'Once', 'Ping-pong'],
147 index: () => wrapIndex,
148 onChanged: (int i) {
149 wrapIndex = i;
150 _player.wrap = _wraps[i];
151 },
152 ),
153 ];
155 @override
156 void verify(Scene scene, FrameResult frame) {
157 if (!_player.isPlaying) {
158 throw StateError('the player is not playing a clip');
159 }
160 if (!_player.isCrossFading) {
161 throw StateError('the crossfade to the second clip did not start');
162 }
163 final double weight = _player.fadeWeight;
164 if (weight <= 0.0 || weight >= 1.0) {
165 throw StateError('the crossfade is not partway through, got $weight');
166 }
167 }