From cdbb04b49e04716387eda47e72eadc6ef24c40ff Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 7 Feb 2025 17:46:11 +0100 Subject: vrm bone names --- world/src/main.rs | 36 +++++++++++++++++++++++++++++++++--- world/src/vrm.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 world/src/vrm.rs (limited to 'world/src') diff --git a/world/src/main.rs b/world/src/main.rs index 081aa09..c3fb959 100644 --- a/world/src/main.rs +++ b/world/src/main.rs @@ -18,6 +18,7 @@ #![allow(clippy::too_many_arguments, clippy::type_complexity)] pub mod mesh; pub mod physics; +pub mod vrm; use anyhow::{Result, anyhow}; use clap::Parser; @@ -41,6 +42,7 @@ use std::{ thread::{self, sleep}, time::Duration, }; +use vrm::{Vrm, Vrmc}; use weareshared::{ Affine3A, Vec3A, helper::ReadWrite, @@ -110,6 +112,8 @@ fn main() -> Result<()> { root_affine.matrix3 *= args.scale.unwrap_or(1.); root_affine.translation *= args.scale.unwrap_or(1.); + let mut debug_bone = (0, Affine3A::IDENTITY); + for scenepath in &args.scene { let path_base = scenepath.parent().unwrap(); let mut gltf = Gltf::from_reader_without_validation(File::open(scenepath)?)?; @@ -169,6 +173,27 @@ fn main() -> Result<()> { }) .collect::>(); + if let Some(vrm) = gltf.extension_value("VRM") { + let vrm: Vrm = serde_json::from_value(vrm.clone())?; + for bone in vrm.humanoid.human_bones { + let ind = joint_index_to_arm_index[&bone.node]; + name[ind] = bone.bone; + } + } + if let Some(vrm) = gltf.extension_value("VRMC_vrm") { + let vrm: Vrmc = serde_json::from_value(vrm.clone())?; + for (bname, bone) in vrm.humanoid.human_bones { + let ind = joint_index_to_arm_index[&bone.node]; + name[ind] = bname; + } + } + + for (i, (name, tr)) in name.iter().zip(transform.iter()).enumerate() { + if name == "head" { + debug_bone = (i, *tr); + } + } + if !parent.is_empty() { Some(store.set(&ArmaturePart { name: Some(name), @@ -317,10 +342,15 @@ fn main() -> Result<()> { thread::spawn(move || { let mut x = 0f32; loop { - let a = Affine3A::from_rotation_y(x.sin()); - Packet::Pose(ob, vec![(6, a)]).write(&mut sock2).unwrap(); + let a = Affine3A::IDENTITY + * Affine3A::from_rotation_x(x.cos() * 0.3) + * Affine3A::from_rotation_y(x.sin() * 0.5) + * debug_bone.1; + Packet::Pose(ob, vec![(debug_bone.0 as u16, a)]) + .write(&mut sock2) + .unwrap(); sock2.flush().unwrap(); - x += 0.1; + x += 0.2; sleep(Duration::from_millis(50)); } }); diff --git a/world/src/vrm.rs b/world/src/vrm.rs new file mode 100644 index 0000000..a9ea6f4 --- /dev/null +++ b/world/src/vrm.rs @@ -0,0 +1,38 @@ +use std::collections::BTreeMap; + +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct Vrm { + pub humanoid: VrmHumanoid, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct VrmHumanoid { + pub human_bones: Vec, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct VrmHumanBone { + pub bone: String, + pub node: usize, +} + +#[derive(Debug, Deserialize)] +pub struct Vrmc { + pub humanoid: VrmcHumanoid, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct VrmcHumanoid { + pub human_bones: BTreeMap, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct VrmcHumanBone { + pub node: usize, +} -- cgit v1.2.3-70-g09d2