Skip to main content

re_core/
skeleton.rs

1use crate::transform::Transform;
2use serde::{Deserialize, Serialize};
3
4/// A single bone. `parent` is the index into `Skeleton::joints`.
5#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
6pub struct Joint {
7    pub name: String,
8    #[serde(default, skip_serializing_if = "Option::is_none")]
9    pub parent: Option<usize>,
10    /// Bind-pose transform relative to the parent joint.
11    pub local: Transform,
12}
13
14/// A joint hierarchy in bind pose. Skeletons are reusable assets; scene nodes
15/// instantiate them via `NodeKind::SkeletonInstance`.
16#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
17pub struct Skeleton {
18    pub name: String,
19    pub joints: Vec<Joint>,
20}
21
22impl Skeleton {
23    pub fn new(name: impl Into<String>) -> Self {
24        Self {
25            name: name.into(),
26            joints: Vec::new(),
27        }
28    }
29
30    /// Append a joint; returns its index.
31    pub fn add_joint(
32        &mut self,
33        name: impl Into<String>,
34        parent: Option<usize>,
35        local: Transform,
36    ) -> usize {
37        let idx = self.joints.len();
38        self.joints.push(Joint {
39            name: name.into(),
40            parent,
41            local,
42        });
43        idx
44    }
45
46    pub fn joint_by_name(&self, name: &str) -> Option<usize> {
47        self.joints.iter().position(|j| j.name == name)
48    }
49
50    /// Global (bind-pose) transform of a joint relative to the skeleton root space.
51    pub fn global_transform(&self, joint: usize) -> Transform {
52        let j = &self.joints[joint];
53        match j.parent {
54            Some(p) => self.global_transform(p).then(&j.local),
55            None => j.local,
56        }
57    }
58
59    /// Direct children of a joint (`None` = root level).
60    pub fn children_of(&self, parent: Option<usize>) -> Vec<usize> {
61        self.joints
62            .iter()
63            .enumerate()
64            .filter(|(_, j)| j.parent == parent)
65            .map(|(i, _)| i)
66            .collect()
67    }
68
69    /// End of a humanoid rig roughly following common (Mixamo-style) naming,
70    /// so imported animations can retarget by name. Bones point +Y in
71    /// rest pose, meters.
72    pub fn humanoid(name: impl Into<String>) -> Skeleton {
73        use glam::Vec3;
74        let mut sk = Skeleton::new(name);
75        let t = |x, y, z| Transform::from_parts(Vec3::new(x, y, z), Default::default(), Vec3::ONE);
76
77        let hips = sk.add_joint("Hips", None, t(0.0, 1.0, 0.0));
78        let spine = sk.add_joint("Spine", Some(hips), t(0.0, 0.15, 0.0));
79        let chest = sk.add_joint("Chest", Some(spine), t(0.0, 0.2, 0.0));
80        let neck = sk.add_joint("Neck", Some(chest), t(0.0, 0.25, 0.0));
81        sk.add_joint("Head", Some(neck), t(0.0, 0.1, 0.0));
82
83        let l_shoulder = sk.add_joint("LeftShoulder", Some(chest), t(0.06, 0.2, 0.0));
84        let l_arm = sk.add_joint("LeftArm", Some(l_shoulder), t(0.14, -0.02, 0.0));
85        let l_fore = sk.add_joint("LeftForeArm", Some(l_arm), t(0.0, -0.28, 0.0));
86        let l_hand = sk.add_joint("LeftHand", Some(l_fore), t(0.0, -0.26, 0.0));
87        sk.add_joint("LeftHandThumb1", Some(l_hand), t(0.03, -0.05, 0.02));
88        sk.add_joint("LeftHandIndex1", Some(l_hand), t(0.0, -0.09, 0.03));
89
90        let r_shoulder = sk.add_joint("RightShoulder", Some(chest), t(-0.06, 0.2, 0.0));
91        let r_arm = sk.add_joint("RightArm", Some(r_shoulder), t(-0.14, -0.02, 0.0));
92        let r_fore = sk.add_joint("RightForeArm", Some(r_arm), t(0.0, -0.28, 0.0));
93        let r_hand = sk.add_joint("RightHand", Some(r_fore), t(0.0, -0.26, 0.0));
94        sk.add_joint("RightHandThumb1", Some(r_hand), t(-0.03, -0.05, 0.02));
95        sk.add_joint("RightHandIndex1", Some(r_hand), t(0.0, -0.09, 0.03));
96
97        let l_up = sk.add_joint("LeftUpLeg", Some(hips), t(0.09, -0.05, 0.0));
98        let l_leg = sk.add_joint("LeftLeg", Some(l_up), t(0.0, -0.44, 0.0));
99        let l_foot = sk.add_joint("LeftFoot", Some(l_leg), t(0.0, -0.42, 0.0));
100        sk.add_joint("LeftToeBase", Some(l_foot), t(0.0, -0.06, 0.1));
101
102        let r_up = sk.add_joint("RightUpLeg", Some(hips), t(-0.09, -0.05, 0.0));
103        let r_leg = sk.add_joint("RightLeg", Some(r_up), t(0.0, -0.44, 0.0));
104        let r_foot = sk.add_joint("RightFoot", Some(r_leg), t(0.0, -0.42, 0.0));
105        sk.add_joint("RightToeBase", Some(r_foot), t(0.0, -0.06, 0.1));
106
107        sk
108    }
109}