A chain of any length: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: chain (line 40), solve (line 109).
1/// A four-joint chain reaching for a point: `FabrikIk` solves a chain of any
2/// length, the same way `TwoBoneIk` solves an arm, on a `Pose` with no scene
3/// behind it.
4///
5/// Quoted by `fabrik_ik.md` and shown whole in the Source tab.
6library;
8import 'dart:typed_data';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `fabrik-ik` page: a four-joint chain of 0.7 m bones solved by `FabrikIk`
14/// towards a target down and out along one fixed direction. Its check fails if
15/// the last bead ends outside the solver's tolerance of the target.
16final class FabrikIkDemo extends ShowcaseDemo {
17 /// How far out the target sits, as a fraction from 0 to 1 of 85% of the
18 /// chain's full 2.1 m, so even 1 stays reachable. The "Reach" slider writes
19 /// it.
20 double reach = 0.6;
22 late final AnimationPose _pose;
23 late final List<SceneNode> _joints;
24 late final MeshNode _targetMarker;
25 double _lastError = 0.0;
27 static const int _jointCount = 4;
28 static const double _boneLength = 0.7;
31 void configureView(DemoContext context) {
32 context.orbit
33 ..distance = 5.5
34 ..pitch = 0.2
35 ..yaw = 0.3;
36 }
39 Scene build(DemoContext context) {
40 final List<int> parents = <int>[
41 -1,
42 for (var i = 1; i < _jointCount; i++) i - 1,
43 ];
44 final Float32List translations = Float32List(_jointCount * 3);
45 final Float32List rotations = Float32List(_jointCount * 4);
46 final Float32List scales = Float32List(_jointCount * 3);
47 for (var i = 0; i < _jointCount; i++) {
48 translations[i * 3 + 1] = i == 0 ? 0.0 : -_boneLength;
49 rotations[i * 4 + 3] = 1.0;
50 scales[i * 3] = 1.0;
51 scales[i * 3 + 1] = 1.0;
52 scales[i * 3 + 2] = 1.0;
53 }
54 _pose = AnimationPose(
55 parents: parents,
56 restTranslations: translations,
57 restRotations: rotations,
58 restScales: scales,
59 );
61 final DeviceMesh bead = DeviceMesh.upload(
62 context.device,
63 SphereShape(radius: 0.1, segments: 14, rings: 7).build(),
64 );
65 _joints = <SceneNode>[];
66 SceneNode? previous;
67 for (var i = 0; i < _jointCount; i++) {
68 final MeshNode node = MeshNode(
69 bead,
70 RenderMaterial(
71 name: 'bead $i',
72 baseColor: i == _jointCount - 1
73 ? LinearColor.fromSrgb(0.9, 0.5, 0.2, 1.0)
74 : LinearColor.fromSrgb(0.5, 0.65, 0.9, 1.0),
75 ),
76 name: 'bead $i',
77 );
78 if (previous == null) {
79 _joints.add(node);
80 } else {
81 previous.add(node);
82 _joints.add(node);
83 }
84 previous = node;
85 }
86 _targetMarker = MeshNode(
87 DeviceMesh.upload(
88 context.device,
89 SphereShape(radius: 0.08, segments: 12, rings: 6).build(),
90 ),
91 RenderMaterial(
92 name: 'target',
93 baseColor: LinearColor.fromSrgb(0.9, 0.2, 0.2, 1.0),
94 ),
95 name: 'target',
96 );
98 return Scene()
99 ..add(_joints.first)
100 ..add(_targetMarker)
101 ..add(
102 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
103 ..setLocalForward(Vector3(-0.4, -0.7, -0.5)),
104 );
105 }
108 void update(DemoContext context, double dt) {
109 final double maxReach = _boneLength * (_jointCount - 1);
110 final Vector3 direction = Vector3(0.5, -0.8, 0.4)..normalize();
111 final Vector3 target = direction * (reach * maxReach * 0.85);
113 _pose.resetToRest();
114 FabrikIk.solve(
115 _pose,
116 joints: <int>[for (var i = 0; i < _jointCount; i++) i],
117 target: target,
118 );
119 _pose.writeTo(_joints);
120 _targetMarker.setPositionFrom(target);
122 final Vector3 tip = _pose.modelMatrices()[_jointCount - 1].getTranslation();
123 _lastError = (tip - target).length;
124 }
127 List<DemoControl> controls(DemoContext context) => <DemoControl>[
128 SliderControl(
129 'Reach',
130 min: 0,
131 max: 1,
132 value: () => reach,
133 onChanged: (double v) => reach = v,
134 ),
135 ];
138 void verify(Scene scene, FrameResult frame) {
139 if (_lastError > 1e-2) {
140 throw StateError(
141 'the last bead did not reach the target within tolerance',
142 );
143 }
144 }
145}