vrm-runtime 0.1.0

Renderer-agnostic VRM 1.0 loader, validator, animation system, and runtime
Documentation
#![allow(dead_code)]

use std::path::PathBuf;
use vrm_runtime::{AvatarAsset, AvatarAssetBuilder, AvatarMeta, AvatarNode, HumanBone, Humanoid};

/// Returns an explicitly configured local VRM fixture without redistributing it.
pub fn optional_local_sample() -> Option<PathBuf> {
    let path = std::env::var_os("VRM_RUNTIME_TEST_SAMPLE").map(PathBuf::from)?;
    path.is_file().then_some(path)
}

pub fn minimal_nodes() -> Vec<AvatarNode> {
    let parent = [
        None,
        Some(0),
        Some(1),
        Some(0),
        Some(3),
        Some(4),
        Some(0),
        Some(6),
        Some(7),
        Some(1),
        Some(9),
        Some(10),
        Some(1),
        Some(12),
        Some(13),
    ];
    let mut children = vec![Vec::new(); parent.len()];
    for (index, parent) in parent.iter().enumerate() {
        if let Some(parent) = parent {
            children[*parent].push(index as u32);
        }
    }
    parent
        .iter()
        .enumerate()
        .map(|(index, parent)| AvatarNode {
            index: index as u32,
            name: None,
            parent: parent.map(|parent| parent as u32),
            children: children[index].clone(),
            initial_translation: glam::Vec3::ZERO,
            initial_rotation: glam::Quat::IDENTITY,
            initial_scale: glam::Vec3::ONE,
        })
        .collect()
}

pub fn minimal_humanoid() -> Humanoid {
    let mut humanoid = Humanoid::default();
    for (bone, node) in [
        (HumanBone::Hips, 0),
        (HumanBone::Spine, 1),
        (HumanBone::Head, 2),
        (HumanBone::LeftUpperLeg, 3),
        (HumanBone::LeftLowerLeg, 4),
        (HumanBone::LeftFoot, 5),
        (HumanBone::RightUpperLeg, 6),
        (HumanBone::RightLowerLeg, 7),
        (HumanBone::RightFoot, 8),
        (HumanBone::LeftUpperArm, 9),
        (HumanBone::LeftLowerArm, 10),
        (HumanBone::LeftHand, 11),
        (HumanBone::RightUpperArm, 12),
        (HumanBone::RightLowerArm, 13),
        (HumanBone::RightHand, 14),
    ] {
        humanoid = humanoid.with_bone(bone, node);
    }

    humanoid
}

pub fn minimal_asset() -> AvatarAsset {
    AvatarAssetBuilder::new()
        .with_meta(AvatarMeta::new(
            "Test Avatar",
            ["vrm-runtime"],
            "https://vrm.dev/licenses/1.0/",
        ))
        .with_humanoid(minimal_humanoid())
        .with_nodes(minimal_nodes())
        .build()
        .expect("the shared test asset must be valid")
}