flutter3d
Showcase Changelog 62 packages API reference

Layers and masks: the source

since 0.5.2 Animation

Regions the guide quotes: clips (line 60), layer (line 77), live (line 124).

1/// A walk on the base clip, a wave laid over just the arm: `AnimationLayer`
2/// and `AnimationMask` say which joints a second clip may touch while the
3/// base keeps playing everywhere else.
4///
5/// Quoted by `animation_layers.md` and shown whole in the Source tab.
6library;
7
8import 'dart:typed_data';
9
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `animation-layers` page: a body walking on its base clip, with a wave on
14/// a layer whose mask names only the arm. Its check fails if the mask reaches
15/// past the arm or the base clip stops moving the body.
16final class AnimationLayersDemo extends ShowcaseDemo {
17 late final AnimationPlayer _player;
18 late final AnimationLayer _wave;
19 late final AnimationTrack _waveTrack;
20 late final SceneNode _body;
21 late final SceneNode _arm;
23 @override
24 void configureView(DemoContext context) {
25 context.orbit
26 ..distance = 4.5
27 ..pitch = 0.2;
28 }
30 @override
31 Scene build(DemoContext context) {
32 final DeviceMesh torso = DeviceMesh.upload(
33 context.device,
34 CuboidShape(size: Vector3(0.8, 1.4, 0.5)).build(),
35 );
36 final DeviceMesh limb = DeviceMesh.upload(
37 context.device,
38 CuboidShape(size: Vector3(0.2, 0.9, 0.2)).build(),
39 );
41 final MeshNode bodyMesh = MeshNode(
42 torso,
43 RenderMaterial(
44 name: 'body',
45 baseColor: LinearColor.fromSrgb(0.5, 0.55, 0.65, 1.0),
46 ),
47 name: 'body',
48 );
49 final MeshNode armMesh = MeshNode(
50 limb,
51 RenderMaterial(
52 name: 'arm',
53 baseColor: LinearColor.fromSrgb(0.85, 0.5, 0.2, 1.0),
54 ),
55 name: 'arm',
56 )..setPosition(0.6, 0.3, 0.0);
57 _body = bodyMesh;
58 _arm = armMesh;
60 final AnimationClip walk = AnimationClip(
61 name: 'walk',
62 tracks: <AnimationTrack>[
63 _rotationTrack(nodeIndex: 0, axis: Vector3(0, 0, 1), peak: 0.12),
64 _rotationTrack(nodeIndex: 1, axis: Vector3(1, 0, 0), peak: 0.5),
65 ],
66 );
67 _waveTrack = _rotationTrack(
68 nodeIndex: 1,
69 axis: Vector3(0, 0, 1),
70 peak: 1.1,
71 );
72 final AnimationClip wave = AnimationClip(
73 name: 'wave',
74 tracks: <AnimationTrack>[_waveTrack],
75 );
77 _player = AnimationPlayer(
78 clips: <AnimationClip>[walk, wave],
79 targets: <AnimationTarget?>[bodyMesh, armMesh],
80 )..play(index: 0);
81 _wave = _player.playLayer(
82 1,
83 mask: AnimationMask(<int>[1]),
84 wrap: AnimationWrap.loop,
85 fade: Duration.zero,
86 );
88 return Scene()
89 ..add(bodyMesh)
90 ..add(armMesh)
91 ..add(
92 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
93 ..setLocalForward(Vector3(-0.4, -0.7, -0.5)),
94 );
95 }
97 static AnimationTrack _rotationTrack({
98 required int nodeIndex,
99 required Vector3 axis,
100 required double peak,
101 }) {
102 final Float32List times = Float32List.fromList(<double>[0.0, 0.5, 1.0]);
103 final List<double> angles = <double>[0.0, peak, 0.0];
104 final Float32List values = Float32List(angles.length * 4);
105 for (var i = 0; i < angles.length; i++) {
106 final Quaternion q = Quaternion.axisAngle(axis, angles[i]);
107 values[i * 4] = q.x;
108 values[i * 4 + 1] = q.y;
109 values[i * 4 + 2] = q.z;
110 values[i * 4 + 3] = q.w;
111 }
112 return AnimationTrack(
113 nodeIndex: nodeIndex,
114 path: AnimationPath.rotation,
115 interpolation: AnimationInterpolation.linear,
116 times: times,
117 values: values,
118 componentCount: 4,
119 );
120 }
122 @override
123 void update(DemoContext context, double dt) {
124 _player.advance(dt);
125 }
127 @override
128 void verify(Scene scene, FrameResult frame) {
129 if (!_wave.mask.covers(1) || _wave.mask.covers(0)) {
130 throw StateError('the mask should cover the arm and nothing else');
131 }
132 final Quaternion bodyRotation = Quaternion.fromRotation(
133 _body.sceneMatrix.getRotation(),
134 );
135 if ((bodyRotation.w - 1.0).abs() < 1e-6) {
136 throw StateError('the base clip did not move the body');
137 }
139 final Float32List sample = Float32List(4);
140 _waveTrack.sample(_wave.timeSeconds, sample);
141 final Quaternion armRotation = Quaternion.fromRotation(
142 _arm.sceneMatrix.getRotation(),
143 );
144 final double dot =
145 (armRotation.x * sample[0] +
146 armRotation.y * sample[1] +
147 armRotation.z * sample[2] +
148 armRotation.w * sample[3])
149 .abs();
150 if (dot < 0.999) {
151 throw StateError("the arm's pose came from the base clip, not the layer");
152 }
153 }