use std::slice;
use {accessor, extensions, json, scene};
use Gltf;
#[derive(Clone, Debug)]
pub struct Skin<'a> {
gltf: &'a Gltf,
index: usize,
json: &'a json::skin::Skin,
}
#[derive(Debug)]
pub struct InverseBindMatrices(accessor::Iter<[[f32; 4]; 4]>);
#[derive(Clone, Debug)]
pub struct Joints<'a> {
gltf: &'a Gltf,
iter: slice::Iter<'a, json::Index<json::scene::Node>>,
}
impl<'a> Skin<'a> {
pub fn new(gltf: &'a Gltf, index: usize, json: &'a json::skin::Skin) -> Self {
Self {
gltf: gltf,
index: index,
json: json,
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn as_json(&self) -> &json::skin::Skin {
self.json
}
pub fn extensions(&self) -> extensions::skin::Skin {
extensions::skin::Skin::new(
self.gltf,
&self.json.extensions,
)
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
pub fn inverse_bind_matrices(&self) -> Option<InverseBindMatrices> {
self.json.inverse_bind_matrices.as_ref().map(|index| {
let accessor = self.gltf.accessors().nth(index.value()).unwrap();
unsafe {
InverseBindMatrices(accessor.iter())
}
})
}
pub fn joints(&self) -> Joints<'a> {
Joints {
gltf: self.gltf,
iter: self.json.joints.iter(),
}
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_ref().map(String::as_str)
}
pub fn skeleton(&self) -> Option<scene::Node<'a>> {
self.json.skeleton.as_ref().map(|index| {
self.gltf.nodes().nth(index.value()).unwrap()
})
}
}
impl Iterator for InverseBindMatrices {
type Item = [[f32; 4]; 4];
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl<'a> Iterator for Joints<'a> {
type Item = scene::Node<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|index| self.gltf.nodes().nth(index.value()).unwrap())
}
}