flutter3d
Showcase Changelog 62 packages API reference

A pose with no scene: the source

since 0.7.0 Animation

Regions the guide quotes: pose (line 34), clip (line 57), sample (line 116).

1/// A two-joint pendulum sampled straight off a clip through `Pose`, with no
2/// `AnimationPlayer` and no scene node anywhere near the arithmetic. The
3/// clip turns the root; the tip only moves because it hangs off it. Markers
4/// are placed afterwards, only to give this page something to draw.
5///
6/// Quoted by `pose_sampling.md` and shown whole in the Source tab.
7library;
8
9import 'dart:typed_data';
11import 'package:flutter3d/flutter3d.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `pose-sampling` page: a two-joint pendulum whose swing clip is sampled
15/// into an `AnimationPose` with no player or scene node involved. Its check
16/// fails if the tip never leaves its rest position.
17final class PoseSamplingDemo extends ShowcaseDemo {
18 double _time = 0.3;
20 late final AnimationPose _pose;
21 late final AnimationClip _swing;
22 late final MeshNode _rootMarker;
23 late final MeshNode _tipMarker;
25 @override
26 void configureView(DemoContext context) {
27 context.orbit
28 ..distance = 3.5
29 ..pitch = 0.15;
30 }
32 @override
33 Scene build(DemoContext context) {
34 _pose = AnimationPose(
35 parents: <int>[-1, 0],
36 restTranslations: Float32List.fromList(<double>[
37 0.0,
38 0.0,
39 0.0,
40 0.0,
41 -1.2,
42 0.0,
43 ]),
44 restRotations: Float32List.fromList(<double>[
45 0.0,
46 0.0,
47 0.0,
48 1.0,
49 0.0,
50 0.0,
51 0.0,
52 1.0,
53 ]),
54 restScales: Float32List.fromList(<double>[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]),
55 );
57 final Float32List times = Float32List.fromList(<double>[0.0, 0.75, 1.5]);
58 final List<double> angles = <double>[-0.6, 0.6, -0.6];
59 final Float32List values = Float32List(angles.length * 4);
60 for (var i = 0; i < angles.length; i++) {
61 final Quaternion q = Quaternion.axisAngle(
62 Vector3(1.0, 0.0, 0.0),
63 angles[i],
64 );
65 values[i * 4] = q.x;
66 values[i * 4 + 1] = q.y;
67 values[i * 4 + 2] = q.z;
68 values[i * 4 + 3] = q.w;
69 }
70 _swing = AnimationClip(
71 name: 'swing',
72 tracks: <AnimationTrack>[
73 AnimationTrack(
74 nodeIndex: 0,
75 path: AnimationPath.rotation,
76 interpolation: AnimationInterpolation.linear,
77 times: times,
78 values: values,
79 componentCount: 4,
80 ),
81 ],
82 );
84 final DeviceMesh bead = DeviceMesh.upload(
85 context.device,
86 SphereShape(radius: 0.15, segments: 16, rings: 8).build(),
87 );
88 _rootMarker = MeshNode(
89 bead,
90 RenderMaterial(
91 name: 'root',
92 baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.65, 1.0),
93 ),
94 name: 'root marker',
95 );
96 _tipMarker = MeshNode(
97 bead,
98 RenderMaterial(
99 name: 'tip',
100 baseColor: LinearColor.fromSrgb(0.9, 0.5, 0.2, 1.0),
101 ),
102 name: 'tip marker',
103 );
105 return Scene()
106 ..add(_rootMarker)
107 ..add(_tipMarker)
108 ..add(
109 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
110 ..setLocalForward(Vector3(-0.3, -0.7, -0.5)),
111 );
112 }
114 @override
115 void update(DemoContext context, double dt) {
116 _time = (_time + dt) % _swing.durationSeconds;
117 _pose.sampleClip(_swing, _time);
118 final List<Matrix4> world = _pose.modelMatrices();
119 _rootMarker.setPositionFrom(world[0].getTranslation());
120 _tipMarker.setPositionFrom(world[1].getTranslation());
121 }
123 @override
124 void verify(Scene scene, FrameResult frame) {
125 final Matrix4 restTip = _pose.restOf(1);
126 final Vector3 sampledTip = _pose.modelMatrices()[1].getTranslation();
127 final double moved = (sampledTip - restTip.getTranslation()).length;
128 if (moved < 1e-3) {
129 throw StateError(
130 'sampling the clip did not move the tip off its rest pose',
131 );
132 }
133 }