Skip to main content

metaverse_avatar/
skeleton.rs

1use std::io::Error;
2
3use crate::avatar::Avatar;
4use benthic_default_asset_converter::generated::DEFAULT_SKELETON;
5use benthic_protocol::skeleton::{Joint, JointName, Skeleton, Transform};
6use glam::{Mat4, Vec4};
7use indexmap::IndexMap;
8use metaverse_messages::http::mesh::Skin;
9use uuid::Uuid;
10
11/// This function takes an object's skeleton, and applies it to the agent's combined skeleton. The
12/// combined skeleton contains all of the transforms for all of the joints, ranked by how many
13/// elements of the outfit contain the same transform.
14/// Highest ranking joint values will always be last.
15pub fn update_global_avatar_skeleton(avatar: &mut Avatar, skeleton: &Skeleton) {
16    for joint in skeleton.joints.values() {
17        // add the mentioned joints to the used joints set.
18        avatar.used_joints.insert(joint.name);
19        if let Some(global_joint) = avatar.skeleton.joints.get_mut(&joint.name) {
20            for transform in &joint.global_transforms {
21                set_rank(global_joint, transform, |j| &mut j.global_transforms);
22            }
23            for local_transform in &joint.local_transforms {
24                set_rank(global_joint, local_transform, |j| &mut j.local_transforms);
25            }
26        }
27    }
28}
29
30/// Determine the local and global joint transforms for a skinned SceneObject.
31pub fn create_skeleton(object_name: String, id: Uuid, skin: &Skin) -> Result<Skeleton, Error> {
32    // if the object has a mesh, handle the skeleton
33    // fetch the default skeleton which is generated at compile time
34    // this will be used for calculating transforms, and getting joint local transforms for the
35    // root object.
36    let default_skeleton = DEFAULT_SKELETON.clone();
37
38    let mut joints = IndexMap::new();
39    // for every joint in the skin
40    for (i, name) in skin.joint_names.iter().enumerate() {
41        // apply the rotations from the default skeleton to the object
42        // the default skeleton's transforms are stored in [0]
43        // these rotations need to be applied, because the IBMs from the server are mostly
44        // the identity matrix, and only contain translation information. The default
45        // skeleton contains the rotations.
46        let default_joints = default_skeleton.joints.get(name).unwrap().clone();
47        let mut default_transform = default_joints.global_transforms[0].transform;
48
49        default_transform.w_axis = Vec4::new(0.0, 0.0, 0.0, 1.0);
50
51        // this one generates correct global coordinates. Currently working out the bugs with this.
52        let transform_matrix = Mat4::from_rotation_translation(
53            default_transform.to_scale_rotation_translation().1,
54            skin.inverse_bind_matrices[i].inverse().w_axis.truncate(),
55        );
56        let transform = Transform {
57            name: object_name.clone(),
58            id,
59            transform: transform_matrix,
60            // Default transforms are rank 0, and will stay at position 0 in the array.
61            // Custom transforms start at rank 1.
62            rank: 1,
63        };
64
65        // create the joint object that contains the calculted transforms
66        let joint = Joint {
67            name: *name,
68            parent: default_joints.parent,
69            children: default_joints.children.into_iter().collect(),
70            global_transforms: vec![transform.clone()],
71            // leave the local tranforms empty for now. They will be added after the loop.
72            local_transforms: vec![],
73        };
74        joints.insert(*name, joint.clone());
75    }
76
77    // determine the root joints of the skeleton
78    let root_joints: Vec<JointName> = joints
79        .values()
80        .filter(|joint| joint.parent.is_none())
81        .map(|joint| joint.name)
82        .collect();
83
84    let last_transforms: IndexMap<JointName, Mat4> = joints
85        .iter()
86        .map(|(name, joint)| (*name, joint.global_transforms.last().unwrap().transform))
87        .collect();
88
89    for (_, joint) in joints.iter_mut() {
90        if let Some(parent_name) = &joint.parent {
91            // if last_transforms doesn't contain the parent, it means that is the root, and
92            // the parent should be calculated via the default skeleton.
93            let parent = last_transforms
94                .get(parent_name)
95                .or_else(|| {
96                    Some(
97                        &default_skeleton
98                            .joints
99                            .get(parent_name)
100                            .unwrap()
101                            .global_transforms[0]
102                            .transform,
103                    )
104                })
105                .unwrap();
106
107            let child = last_transforms.get(&joint.name).unwrap();
108            // IBM-based local matrix
109            let local_matrix = parent.inverse() * child;
110            let local_transform = Transform {
111                name: object_name.clone(),
112                id,
113                transform: local_matrix,
114                rank: 1,
115            };
116            joint.local_transforms.push(local_transform);
117        } else {
118            let child = last_transforms.get(&joint.name).unwrap();
119
120            let local_transform = Transform {
121                name: object_name.clone(),
122                id,
123                transform: *child,
124                rank: 1,
125            };
126
127            joint.local_transforms.push(local_transform);
128        }
129    }
130
131    Ok(Skeleton {
132        root: root_joints,
133        joints,
134    })
135}
136
137// Modify the rank for local and global transforms.
138fn set_rank<F>(joint: &mut Joint, transform: &Transform, mut get_vec: F)
139where
140    F: FnMut(&mut Joint) -> &mut Vec<Transform>,
141{
142    let transforms = get_vec(joint);
143
144    // Track whether we matched only rank 0
145    let mut matched_rank0 = false;
146
147    for t in transforms.iter_mut().skip(1) {
148        if t.transform.abs_diff_eq(transform.transform, 1e-4) {
149            t.rank += 1;
150            transforms.sort_by_key(|a| a.rank);
151            return;
152        }
153    }
154
155    // If we didn’t find a non-zero match, check rank 0.
156    if let Some(first) = transforms.first()
157        && first.transform.abs_diff_eq(transform.transform, 1e-4)
158    {
159        matched_rank0 = true;
160    }
161
162    // If it matched only rank 0 or none at all, create a new transform
163    if matched_rank0
164        || !transforms
165            .iter()
166            .any(|t| t.transform.abs_diff_eq(transform.transform, 1e-4))
167    {
168        let mut new_t = transform.clone();
169        // If it matched only rank 0, start at rank 1
170        if matched_rank0 {
171            new_t.rank = 1;
172        }
173        transforms.push(new_t);
174        transforms.sort_by_key(|a| a.rank);
175    }
176}