From 5a8d293e8af90d9b4191865a4087fa3792f8b769 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 19 Feb 2025 23:22:58 +0100 Subject: import vrm first person bone --- world/src/vrm.rs | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'world/src/vrm.rs') 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, + pub camera_mount: Option, + pub camera_mount_offset: Option, } pub fn extract_vrm_data(gltf: &Gltf) -> Result { - 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, } #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] -pub struct VrmHumanoid { +struct VrmHumanoid { human_bones: Vec, } @@ -85,6 +92,8 @@ struct Vrmc { #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] struct VrmcFirstPerson { + first_person_bone: Option, + first_person_bone_offset: Option, mesh_annotations: Vec, } @@ -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) +} -- cgit v1.2.3-70-g09d2