1use heapless::Vec;
27use nalgebra::{Matrix4, Point3, UnitQuaternion, Vector3};
28
29#[allow(unused_imports)]
30use nalgebra::ComplexField;
31
32pub const MAX_BONE_INFLUENCES: usize = 4;
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub struct BoneId(pub usize);
38
39#[derive(Debug, Clone)]
44pub struct Bone {
45 pub name: heapless::String<32>,
47
48 pub position: Vector3<f32>,
50
51 pub rotation: UnitQuaternion<f32>,
53
54 pub scale: Vector3<f32>,
56
57 pub parent: Option<BoneId>,
59
60 pub local_transform: Matrix4<f32>,
62
63 pub world_transform: Matrix4<f32>,
65
66 pub inverse_bind_pose: Matrix4<f32>,
68}
69
70impl Bone {
71 pub fn new(name: &str) -> Self {
73 let mut name_str = heapless::String::new();
74 let _ = name_str.push_str(name);
75
76 Self {
77 name: name_str,
78 position: Vector3::zeros(),
79 rotation: UnitQuaternion::identity(),
80 scale: Vector3::new(1.0, 1.0, 1.0),
81 parent: None,
82 local_transform: Matrix4::identity(),
83 world_transform: Matrix4::identity(),
84 inverse_bind_pose: Matrix4::identity(),
85 }
86 }
87
88 pub fn with_position(mut self, position: Vector3<f32>) -> Self {
90 self.position = position;
91 self.update_local_transform();
92 self
93 }
94
95 pub fn with_rotation(mut self, rotation: UnitQuaternion<f32>) -> Self {
97 self.rotation = rotation;
98 self.update_local_transform();
99 self
100 }
101
102 pub fn with_scale(mut self, scale: Vector3<f32>) -> Self {
104 self.scale = scale;
105 self.update_local_transform();
106 self
107 }
108
109 pub fn update_local_transform(&mut self) {
111 let translation = Matrix4::new_translation(&self.position);
113 let rotation = self.rotation.to_homogeneous();
114 let scale = Matrix4::new_nonuniform_scaling(&self.scale);
115
116 self.local_transform = translation * rotation * scale;
117 }
118
119 pub fn set_position(&mut self, position: Vector3<f32>) {
121 self.position = position;
122 self.update_local_transform();
123 }
124
125 pub fn set_rotation(&mut self, rotation: UnitQuaternion<f32>) {
127 self.rotation = rotation;
128 self.update_local_transform();
129 }
130}
131
132#[derive(Debug, Clone)]
136pub struct Skeleton<const N: usize> {
137 pub bones: Vec<Bone, N>,
138}
139
140impl<const N: usize> Skeleton<N> {
141 pub fn new() -> Self {
143 Self { bones: Vec::new() }
144 }
145
146 pub fn add_bone(&mut self, mut bone: Bone, parent: Option<BoneId>) -> Result<BoneId, ()> {
150 bone.parent = parent;
151 bone.update_local_transform();
152
153 let id = BoneId(self.bones.len());
154 self.bones.push(bone).map_err(|_| ())?;
155
156 Ok(id)
157 }
158
159 pub fn get_bone(&self, id: BoneId) -> Option<&Bone> {
161 self.bones.get(id.0)
162 }
163
164 pub fn get_bone_mut(&mut self, id: BoneId) -> Option<&mut Bone> {
166 self.bones.get_mut(id.0)
167 }
168
169 pub fn update_transforms(&mut self) {
173 for bone in self.bones.iter_mut() {
175 bone.update_local_transform();
176 }
177
178 for i in 0..self.bones.len() {
180 let parent_transform = if let Some(parent_id) = self.bones[i].parent {
181 self.bones[parent_id.0].world_transform
182 } else {
183 Matrix4::identity()
184 };
185
186 self.bones[i].world_transform = parent_transform * self.bones[i].local_transform;
187 }
188 }
189
190 pub fn compute_inverse_bind_poses(&mut self) {
194 self.update_transforms();
195
196 for bone in self.bones.iter_mut() {
197 bone.inverse_bind_pose = bone
198 .world_transform
199 .try_inverse()
200 .unwrap_or(Matrix4::identity());
201 }
202 }
203
204 pub fn get_skinning_matrix(&self, bone_id: BoneId) -> Matrix4<f32> {
206 if let Some(bone) = self.get_bone(bone_id) {
207 bone.world_transform * bone.inverse_bind_pose
208 } else {
209 Matrix4::identity()
210 }
211 }
212}
213
214impl<const N: usize> Default for Skeleton<N> {
215 fn default() -> Self {
216 Self::new()
217 }
218}
219
220#[derive(Debug, Clone, Copy)]
224pub struct VertexSkinning {
225 pub bone_indices: [usize; MAX_BONE_INFLUENCES],
227
228 pub bone_weights: [f32; MAX_BONE_INFLUENCES],
230
231 pub num_influences: usize,
233}
234
235impl VertexSkinning {
236 pub fn single_bone(bone_index: usize) -> Self {
238 Self {
239 bone_indices: [bone_index, 0, 0, 0],
240 bone_weights: [1.0, 0.0, 0.0, 0.0],
241 num_influences: 1,
242 }
243 }
244
245 pub fn two_bones(bone0: usize, weight0: f32, bone1: usize, weight1: f32) -> Self {
247 Self {
248 bone_indices: [bone0, bone1, 0, 0],
249 bone_weights: [weight0, weight1, 0.0, 0.0],
250 num_influences: 2,
251 }
252 }
253
254 pub fn new(
258 bone_indices: [usize; MAX_BONE_INFLUENCES],
259 bone_weights: [f32; MAX_BONE_INFLUENCES],
260 num_influences: usize,
261 ) -> Self {
262 Self {
263 bone_indices,
264 bone_weights,
265 num_influences: num_influences.min(MAX_BONE_INFLUENCES),
266 }
267 }
268}
269
270impl Default for VertexSkinning {
271 fn default() -> Self {
272 Self::single_bone(0)
273 }
274}
275
276#[derive(Debug, Clone)]
280pub struct SkinningData {
281 pub vertex_skinning: heapless::Vec<VertexSkinning, 512>,
283}
284
285impl SkinningData {
286 pub fn new() -> Self {
288 Self {
289 vertex_skinning: Vec::new(),
290 }
291 }
292
293 pub fn add_vertex(&mut self, skinning: VertexSkinning) -> Result<(), ()> {
295 self.vertex_skinning.push(skinning).map_err(|_| ())
296 }
297}
298
299impl Default for SkinningData {
300 fn default() -> Self {
301 Self::new()
302 }
303}
304
305pub fn apply_skinning<const N: usize>(
318 skeleton: &Skeleton<N>,
319 skinning_data: &SkinningData,
320 source_vertices: &[[f32; 3]],
321 output_vertices: &mut [[f32; 3]],
322) -> usize {
323 let count = source_vertices
324 .len()
325 .min(output_vertices.len())
326 .min(skinning_data.vertex_skinning.len());
327
328 for i in 0..count {
329 let vertex = Point3::new(
330 source_vertices[i][0],
331 source_vertices[i][1],
332 source_vertices[i][2],
333 );
334
335 let skinning = &skinning_data.vertex_skinning[i];
336 let mut deformed = Point3::new(0.0, 0.0, 0.0);
337
338 for j in 0..skinning.num_influences {
340 let bone_id = BoneId(skinning.bone_indices[j]);
341 let weight = skinning.bone_weights[j];
342
343 if weight > 0.0 {
344 let skinning_matrix = skeleton.get_skinning_matrix(bone_id);
345 let transformed = skinning_matrix.transform_point(&vertex);
346 deformed += transformed.coords * weight;
347 }
348 }
349
350 output_vertices[i] = [deformed.x, deformed.y, deformed.z];
351 }
352
353 count
354}
355
356pub fn apply_skinning_to_normals<const N: usize>(
370 skeleton: &Skeleton<N>,
371 skinning_data: &SkinningData,
372 source_normals: &[[f32; 3]],
373 output_normals: &mut [[f32; 3]],
374) -> usize {
375 let count = source_normals
376 .len()
377 .min(output_normals.len())
378 .min(skinning_data.vertex_skinning.len());
379
380 for i in 0..count {
381 let normal = Vector3::new(
382 source_normals[i][0],
383 source_normals[i][1],
384 source_normals[i][2],
385 );
386
387 let skinning = &skinning_data.vertex_skinning[i];
388 let mut deformed = Vector3::zeros();
389
390 for j in 0..skinning.num_influences {
391 let bone_id = BoneId(skinning.bone_indices[j]);
392 let weight = skinning.bone_weights[j];
393
394 if weight > 0.0 {
395 let skinning_matrix = skeleton.get_skinning_matrix(bone_id);
396
397 let rotation_part = skinning_matrix.fixed_view::<3, 3>(0, 0);
399 let transformed = rotation_part * normal;
400 deformed += transformed * weight;
401 }
402 }
403
404 let normalized = deformed.normalize();
406 output_normals[i] = [normalized.x, normalized.y, normalized.z];
407 }
408
409 count
410}
411
412#[cfg(test)]
413mod tests {
414 use super::*;
415
416 #[test]
417 fn test_bone_creation() {
418 let bone = Bone::new("test_bone");
419 assert_eq!(bone.name.as_str(), "test_bone");
420 assert_eq!(bone.position, Vector3::zeros());
421 assert_eq!(bone.parent, None);
422 }
423
424 #[test]
425 fn test_skeleton_add_bone() {
426 let mut skeleton = Skeleton::<4>::new();
427
428 let root = skeleton.add_bone(Bone::new("root"), None);
429 assert!(root.is_ok());
430
431 let root_id = root.unwrap();
432 let child = skeleton.add_bone(Bone::new("child"), Some(root_id));
433 assert!(child.is_ok());
434
435 assert_eq!(skeleton.bones.len(), 2);
436 }
437
438 #[test]
439 fn test_hierarchy_transforms() {
440 let mut skeleton = Skeleton::<4>::new();
441
442 let root = skeleton.add_bone(Bone::new("root"), None).unwrap();
444
445 let child = skeleton
447 .add_bone(
448 Bone::new("child").with_position(Vector3::new(1.0, 0.0, 0.0)),
449 Some(root),
450 )
451 .unwrap();
452
453 skeleton.update_transforms();
454
455 let child_bone = skeleton.get_bone(child).unwrap();
457 let world_pos = child_bone.world_transform.column(3);
458 assert!((world_pos.x - 1.0).abs() < 0.001);
459 assert!(world_pos.y.abs() < 0.001);
460 assert!(world_pos.z.abs() < 0.001);
461 }
462
463 #[test]
464 fn test_vertex_skinning_single_bone() {
465 let skinning = VertexSkinning::single_bone(0);
466 assert_eq!(skinning.num_influences, 1);
467 assert_eq!(skinning.bone_weights[0], 1.0);
468 }
469
470 #[test]
471 fn test_vertex_skinning_two_bones() {
472 let skinning = VertexSkinning::two_bones(0, 0.7, 1, 0.3);
473 assert_eq!(skinning.num_influences, 2);
474 assert_eq!(skinning.bone_weights[0], 0.7);
475 assert_eq!(skinning.bone_weights[1], 0.3);
476 }
477}