The skeleton drawn: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: rig (line 41), settings (line 101).
1/// The same bending banner `skinning` builds, with its skeleton drawn over
2/// it: `DebugDrawSettings.skeletons` puts one octahedron a bone and one cross
3/// a leaf joint on every skinned mesh in the scene.
4///
5/// Quoted by `skeleton_debug.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 `skeleton-debug` page: the two-joint banner from `skinning` with its
14/// bones drawn by the debug overlay. Its check fails if the overlay draws no
15/// lines.
16final class SkeletonDebugDemo extends ShowcaseDemo {
17 /// Whether the debug overlay draws the skeleton, through
18 /// `DebugDrawSettings.skeletons`. The "Show skeleton" switch writes it.
19 bool showSkeleton = true;
21 /// How far the tip joint bends, from 0 to 1, where 1 is 1.2 radians about the
22 /// x axis. The "Bend" slider writes it.
23 double bend = 0.6;
25 static const double _depth = 2.4;
27 late final SceneNode _base;
28 late final SceneNode _tip;
29 late final Skeleton _skeleton;
32 void configureView(DemoContext context) {
33 context.orbit
34 ..distance = 4.0
35 ..pitch = 0.15
36 ..yaw = 0.5;
37 }
40 Scene build(DemoContext context) {
41 _base = SceneNode(name: 'base')..setPosition(0.0, 0.0, -_depth / 2);
42 _tip = SceneNode(name: 'tip')..setPosition(0.0, 0.0, _depth);
43 _base.add(_tip);
45 final Matrix4 inverseBindBase = Matrix4.copy(_base.sceneMatrix)..invert();
46 final Matrix4 inverseBindTip = Matrix4.copy(_tip.sceneMatrix)..invert();
47 _skeleton = Skeleton(
48 joints: <SceneNode>[_base, _tip],
49 inverseBindMatrices: <Matrix4>[inverseBindBase, inverseBindTip],
50 name: 'arm',
51 );
53 final MeshData mesh = const PlaneShape(
54 width: 0.7,
55 depth: _depth,
56 depthSegments: 8,
57 ).build().convertedTo(VertexLayout.skinned);
58 _paintWeights(mesh);
60 final MeshNode banner = MeshNode(
61 DeviceMesh.upload(context.device, mesh),
62 RenderMaterial(
63 name: 'banner',
64 baseColor: LinearColor.fromSrgb(0.5, 0.55, 0.6, 1.0),
65 doubleSided: true,
66 ),
67 name: 'banner',
68 )..skeleton = _skeleton;
70 return Scene()
71 ..add(banner)
72 ..add(
73 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
74 ..setLocalForward(Vector3(-0.3, -0.6, -0.7)),
75 );
76 }
78 void _paintWeights(MeshData mesh) {
79 final VertexLayout layout = mesh.layout;
80 final int stride = layout.floatsPerVertex;
81 final int positionAt = layout.floatOffsetOf(VertexLayout.position.name);
82 final int jointsAt = layout.floatOffsetOf(VertexLayout.joints.name);
83 final int weightsAt = layout.floatOffsetOf(VertexLayout.weights.name);
84 final Float32List vertices = mesh.vertices;
85 for (var v = 0; v < mesh.vertexCount; v++) {
86 final int at = v * stride;
87 final double z = vertices[at + positionAt + 2];
88 final double t = ((z + _depth / 2) / _depth).clamp(0.0, 1.0);
89 vertices[at + jointsAt] = 0.0;
90 vertices[at + jointsAt + 1] = 1.0;
91 vertices[at + weightsAt] = 1.0 - t;
92 vertices[at + weightsAt + 1] = t;
93 }
94 }
97 void update(DemoContext context, double dt) {
98 _tip.setRotation(Quaternion.axisAngle(Vector3(1.0, 0.0, 0.0), bend * 1.2));
99 }
102 RenderSettings settings(DemoContext context) =>
103 RenderSettings(debug: DebugDrawSettings(skeletons: showSkeleton));
106 List<DemoControl> controls(DemoContext context) => <DemoControl>[
107 ToggleControl(
108 'Show skeleton',
109 value: () => showSkeleton,
110 onChanged: (bool v) => showSkeleton = v,
111 ),
112 SliderControl(
113 'Bend',
114 min: 0,
115 max: 1,
116 value: () => bend,
117 onChanged: (double v) => bend = v,
118 ),
119 ];
122 void verify(Scene scene, FrameResult frame) {
123 if (frame.debugLines < 1) {
124 throw StateError('the skeleton overlay drew no lines');
125 }
126 }
127}