diff options
Diffstat (limited to 'world/src/vrm.rs')
-rw-r--r-- | world/src/vrm.rs | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/world/src/vrm.rs b/world/src/vrm.rs index 2888ad0..a97323f 100644 --- a/world/src/vrm.rs +++ b/world/src/vrm.rs @@ -18,53 +18,60 @@ use anyhow::Result; use gltf::Gltf; use serde::Deserialize; use std::collections::{BTreeMap, BTreeSet}; +use weareshared::Vec3A; +#[derive(Default)] pub struct VrmInfo { pub bone_node_names: Vec<(usize, String)>, pub hide_first_person: BTreeSet<usize>, + pub camera_mount: Option<usize>, + pub camera_mount_offset: Option<Vec3A>, } pub fn extract_vrm_data(gltf: &Gltf) -> Result<VrmInfo> { - let mut bone_node_names = Vec::new(); - let mut hide_first_person = BTreeSet::new(); + let mut o = VrmInfo::default(); if let Some(vrm) = gltf.extension_value("VRM") { // serde_json::to_writer(std::fs::File::create("/tmp/vrm").unwrap(), vrm).unwrap(); let vrm: Vrm = serde_json::from_value(vrm.clone())?; for bone in vrm.humanoid.human_bones { - bone_node_names.push((bone.node, bone.bone)) + o.bone_node_names.push((bone.node, bone.bone)) + } + if let Some(fp) = vrm.first_person { + o.camera_mount = fp.first_person_bone; + o.camera_mount_offset = fp.first_person_bone_offset.map(convert_vrm_vec); } } if let Some(vrm) = gltf.extension_value("VRMC_vrm") { // serde_json::to_writer(std::fs::File::create("/tmp/vrmc").unwrap(), vrm).unwrap(); let vrm: Vrmc = serde_json::from_value(vrm.clone())?; for (name, bone) in vrm.humanoid.human_bones { - bone_node_names.push((bone.node, name)) + o.bone_node_names.push((bone.node, name)) } if let Some(fp) = vrm.first_person { + o.camera_mount = fp.first_person_bone; + o.camera_mount_offset = fp.first_person_bone_offset.map(convert_vrm_vec); for ann in fp.mesh_annotations { match ann.first_person_flag { FirstPersonFlag::ThirdPersonOnly => { - hide_first_person.insert(ann.node); + o.hide_first_person.insert(ann.node); } _ => (), } } } } - Ok(VrmInfo { - bone_node_names, - hide_first_person, - }) + Ok(o) } #[derive(Debug, Deserialize)] -pub struct Vrm { - pub humanoid: VrmHumanoid, +struct Vrm { + humanoid: VrmHumanoid, + first_person: Option<VrmcFirstPerson>, } #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] -pub struct VrmHumanoid { +struct VrmHumanoid { human_bones: Vec<VrmHumanBone>, } @@ -85,6 +92,8 @@ struct Vrmc { #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] struct VrmcFirstPerson { + first_person_bone: Option<usize>, + first_person_bone_offset: Option<VrmVec3>, mesh_annotations: Vec<VrmcFirstPersonMeshAnnotation>, } @@ -117,3 +126,13 @@ struct VrmcHumanoid { struct VrmcHumanBone { node: usize, } + +#[derive(Debug, Deserialize)] +struct VrmVec3 { + x: f32, + y: f32, + z: f32, +} +fn convert_vrm_vec(VrmVec3 { x, y, z }: VrmVec3) -> Vec3A { + Vec3A::new(x, y, z) +} |