pub mod anim;
pub mod camera;
pub mod format;
pub mod math;
pub mod model;
pub mod morph;
pub mod movement;
pub mod rig;
pub mod wire;
use std::collections::HashMap;
use std::sync::{Arc, LazyLock, RwLock};
pub use anim::{BonePose, Clip, Pose};
pub use camera::{CameraFrame, CameraOut};
pub use math::{Mat4, Vec3};
pub use model::{Model, Quad};
pub use movement::TravelFrame;
pub use rig::Rig;
pub use wire::{Anchor, BoneOp, Capture, Frame, Mesh, Mode, Ops};
#[derive(Default)]
struct Store {
models: HashMap<String, Arc<Model>>,
clips: HashMap<(String, String), Arc<Clip>>,
}
static STORE: LazyLock<RwLock<Store>> = LazyLock::new(Default::default);
fn namespace(id: &str) -> &str {
id.split_once(':').map_or("minecraft", |(ns, _)| ns)
}
pub fn register_model(id: &str, data: &[u8]) -> Result<(), String> {
let file: format::ModelFile = yog_data::read_bytes(data).map_err(|e| format!("model {id}: {e:?}"))?;
let model = Model::new(&file, namespace(id)).map_err(|e| format!("model {id}: {e}"))?;
STORE.write().unwrap_or_else(|e| e.into_inner()).models.insert(id.to_owned(), Arc::new(model));
Ok(())
}
pub fn register_anims(id: &str, data: &[u8]) -> Result<(), String> {
let file: format::AnimFile = yog_data::read_bytes(data).map_err(|e| format!("animations {id}: {e:?}"))?;
let clips = anim::Clips::new(&file).map_err(|e| format!("animations {id}: {e}"))?;
let mut store = STORE.write().unwrap_or_else(|e| e.into_inner());
for c in clips.0 {
store.clips.insert((id.to_owned(), c.name.clone()), Arc::new(c));
}
Ok(())
}
pub fn model(id: &str) -> Option<Arc<Model>> {
STORE.read().unwrap_or_else(|e| e.into_inner()).models.get(id).cloned()
}
pub fn clip(id: &str, name: &str) -> Option<Arc<Clip>> {
STORE.read().unwrap_or_else(|e| e.into_inner()).clips.get(&(id.to_owned(), name.to_owned())).cloned()
}