From 214e5495fe1622595ed7b60b8f9d510096ef53ab Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Wed, 10 Aug 2022 14:52:11 -0400 Subject: [PATCH] Properly set bone parenting Skeleton::from_skel, export bone transforms --- Cargo.lock | 7 +++++++ Cargo.toml | 5 ++++- src/skeleton.rs | 34 +++++++++++++++------------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0420c84..7f47118 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -248,6 +248,12 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be" +[[package]] +name = "glam" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "518faa5064866338b013ff9b2350dc318e14cc4fcd6cb8206d7e7c9886c98815" + [[package]] name = "half" version = "1.8.2" @@ -437,6 +443,7 @@ dependencies = [ "bitfield-struct", "crc", "criterion", + "glam", "half", "hard-xml", "libz-sys", diff --git a/Cargo.toml b/Cargo.toml index cb6ebd5..d6dfa3e 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,4 +46,7 @@ serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } # needed for file info (fiin) -sha1_smol = "1.0.0" \ No newline at end of file +sha1_smol = "1.0.0" + +# needed for deconstructing skeleton pose matrices +glam = "0.21.3" \ No newline at end of file diff --git a/src/skeleton.rs b/src/skeleton.rs index d7158c9..1180383 100644 --- a/src/skeleton.rs +++ b/src/skeleton.rs @@ -1,19 +1,20 @@ use crate::gamedata::MemoryBuffer; use hard_xml::XmlRead; +use glam::Mat4; #[derive(Debug)] pub struct Bone { - name: String, - parent_index: usize, + pub name: String, + pub parent_index: i32, - position: [f32; 3], - rotation: [f32; 4], - scale: [f32; 3] + pub position: [f32; 3], + pub rotation: [f32; 4], + pub scale: [f32; 3] } #[derive(Debug)] pub struct Skeleton { - bones : Vec + pub bones : Vec } impl Skeleton { @@ -116,24 +117,19 @@ impl Skeleton { }; for bone in &json_bones { + let pose_matrix = Mat4::from_cols_array(&bone.pose_matrix); + + let (scale, rotation, translation) = pose_matrix.to_scale_rotation_translation(); + skeleton.bones.push(Bone { name: bone.bone_name.clone(), - parent_index: 0, - position: [0.0; 3], - rotation: [0.0; 4], - scale: [0.0; 3] + parent_index: bone.bone_parent, + position: translation.to_array(), + rotation: rotation.to_array(), + scale: scale.to_array() }); } - // assign parenting - for bone in &json_bones { - if bone.bone_parent != -1 { - let mut new_bone = &mut skeleton.bones[bone.bone_number as usize]; - - new_bone.parent_index = bone.bone_parent as usize; - } - } - Some(skeleton) } } \ No newline at end of file