use std::collections::HashMap;
use glam::{Mat4, Quat, Vec3};
use gltf::accessor::{DataType, Dimensions};
use gltf::animation::{Interpolation as GltfInterpolation, Property, util::ReadOutputs};
use super::super::asset::{Result, Vertex};
use super::super::deformation::{
Channel, ChannelValues, Clip, Influence, Interpolation, Joint, MAX_INDEX, MorphDelta,
MorphTarget, MorphWeight, Node, Rig,
};
use super::check_accessor;
pub(super) struct Rigging {
pub rig: Rig,
skins: Vec<u32>,
morphs: HashMap<usize, u32>,
targets: Vec<usize>,
}
impl Rigging {
pub fn new(
document: &gltf::Document,
parents: &[Option<usize>],
(buffers, buffer_bytes): (&[gltf::buffer::Data], usize),
) -> Result<Self> {
let mut rigging = Self {
rig: Rig::default(),
skins: Vec::new(),
morphs: HashMap::new(),
targets: document
.meshes()
.map(|mesh| morph_target_count(&mesh))
.collect(),
};
let morphed = rigging.targets.iter().any(|&targets| targets > 0);
if document.skins().next().is_none() && document.animations().next().is_none() && !morphed {
return Ok(rigging);
}
let mut posed = vec![false; parents.len()];
for joint in document.skins().flat_map(|skin| skin.joints()) {
let mut node = Some(joint.index());
while let Some(index) = node.filter(|&index| !posed[index]) {
posed[index] = true;
node = parents[index];
}
}
rigging.rig.nodes = document
.nodes()
.map(|node| {
let (translation, rotation, scale) = node.transform().decomposed();
if posed[node.index()]
&& !translation
.iter()
.chain(&rotation)
.chain(&scale)
.all(|value| value.is_finite())
{
return Err(format!(
"node {} ({}), a joint or a joint's ancestor, has a transform that does not decompose to a finite translation, rotation and scale; re-export it without a zero-scale axis",
node.index(),
node.name().unwrap_or("unnamed")
)
.into());
}
Ok(Node {
name: node.name().map(str::to_owned),
parent: parents[node.index()],
translation: Vec3::from_array(translation),
rotation: Quat::from_array(rotation),
scale: Vec3::from_array(scale),
})
})
.collect::<Result<_>>()?;
for skin in document.skins() {
rigging.skins.push(rigging.rig.joints.len() as u32);
if let Some(accessor) = skin.inverse_bind_matrices() {
check_accessor(
&accessor,
&format!("skin {}: inverse bind matrices", skin.index()),
&[DataType::F32],
&[Dimensions::Mat4],
Some(skin.joints().count()),
(buffers, buffer_bytes),
)?;
}
let reader = skin.reader(|buffer| Some(&buffers[buffer.index()].0));
let inverse_binds: Vec<Mat4> = match reader.read_inverse_bind_matrices() {
Some(matrices) => matrices.map(|m| Mat4::from_cols_array_2d(&m)).collect(),
None => vec![Mat4::IDENTITY; skin.joints().count()],
};
if inverse_binds.len() != skin.joints().count()
|| inverse_binds.iter().any(|m| !m.is_finite())
{
return Err(format!(
"skin {} has {} finite inverse bind matrices for {} joints",
skin.index(),
inverse_binds.len(),
skin.joints().count()
)
.into());
}
rigging
.rig
.joints
.extend(
skin.joints()
.zip(inverse_binds)
.map(|(node, inverse_bind)| Joint {
node: node.index(),
inverse_bind,
}),
);
}
rigging.rig.clips = document
.animations()
.map(|animation| read_clip(&animation, (buffers, buffer_bytes), &rigging.targets))
.collect::<Result<_>>()?;
Ok(rigging)
}
pub fn mesh_targets(&self, mesh: &gltf::Mesh<'_>) -> usize {
self.targets[mesh.index()]
}
pub fn skin(&self, skin: &gltf::Skin) -> (u32, u32) {
(self.skins[skin.index()], skin.joints().count() as u32)
}
pub fn morph_weights(&mut self, node: &gltf::Node<'_>, count: usize) -> Result<u32> {
if let Some(&first) = self.morphs.get(&node.index()) {
return Ok(first);
}
let rest = node
.weights()
.or_else(|| node.mesh().and_then(|mesh| mesh.weights()))
.map(<[f32]>::to_vec)
.unwrap_or_else(|| vec![0.; count]);
if rest.len() != count || rest.iter().any(|w| !w.is_finite()) {
return Err(format!(
"node {} has {} finite rest morph weights for {count} targets",
node.index(),
rest.len()
)
.into());
}
let first = self.rig.morph_weights.len() as u32;
if (first as usize + count) > MAX_INDEX as usize + 1 {
return Err(format!(
"node {} has morph weights beyond the 65536 an asset may have",
node.index()
)
.into());
}
self.rig
.morph_weights
.extend(rest.into_iter().map(|rest| MorphWeight {
node: node.index(),
rest,
}));
self.morphs.insert(node.index(), first);
Ok(first)
}
}
pub(super) fn parents(document: &gltf::Document) -> Result<Vec<Option<usize>>> {
let nodes: Vec<gltf::Node<'_>> = document.nodes().collect();
let mut parents = vec![None; nodes.len()];
for node in &nodes {
for child in node.children() {
if parents[child.index()].replace(node.index()).is_some() {
return Err(format!(
"node {} has more than one parent; the node hierarchy must be a set of trees",
child.index()
)
.into());
}
}
}
let mut reached = vec![false; nodes.len()];
let mut stack: Vec<usize> = (0..nodes.len())
.filter(|&node| parents[node].is_none())
.collect();
while let Some(node) = stack.pop() {
reached[node] = true;
stack.extend(nodes[node].children().map(|child| child.index()));
}
if let Some(node) = reached.iter().position(|reached| !reached) {
return Err(format!(
"node {node}'s ancestors form a cycle; the node hierarchy must be a set of trees"
)
.into());
}
Ok(parents)
}
fn morph_target_count(mesh: &gltf::Mesh<'_>) -> usize {
mesh.primitives()
.map(|primitive| primitive.morph_targets().count())
.find(|&count| count > 0)
.unwrap_or(0)
}
fn read_clip(
animation: &gltf::Animation<'_>,
(buffers, buffer_bytes): (&[gltf::buffer::Data], usize),
targets: &[usize],
) -> Result<Clip> {
let label = format!("animation {}", animation.index());
let channels = animation
.channels()
.map(|channel| {
use DataType::{F32, I8, I16, U8, U16};
let sampler = channel.sampler();
check_accessor(
&sampler.input(),
&format!("{label}: keyframe times"),
&[F32],
&[Dimensions::Scalar],
None,
(buffers, buffer_bytes),
)?;
let interpolation = match sampler.interpolation() {
GltfInterpolation::Step => Interpolation::Step,
GltfInterpolation::Linear => Interpolation::Linear,
GltfInterpolation::CubicSpline => Interpolation::CubicSpline,
};
let per_key = if interpolation == Interpolation::CubicSpline {
3
} else {
1
};
let (types, shape, per_value): (&[DataType], _, _) = match channel.target().property() {
Property::Translation | Property::Scale => (&[F32], Dimensions::Vec3, 1),
Property::Rotation => (&[F32, I8, U8, I16, U16], Dimensions::Vec4, 1),
Property::MorphTargetWeights => {
let targets = channel
.target()
.node()
.mesh()
.map_or(0, |mesh| targets[mesh.index()]);
if targets == 0 {
return Err(format!(
"{label}: morph weights animate a node without morph targets"
)
.into());
}
(&[F32, I8, U8, I16, U16], Dimensions::Scalar, targets)
}
};
let values = sampler
.input()
.count()
.checked_mul(per_key)
.and_then(|values| values.checked_mul(per_value))
.ok_or_else(|| format!("{label}: a channel has too many keyframe values"))?;
check_accessor(
&sampler.output(),
&format!("{label}: keyframe values"),
types,
&[shape],
Some(values),
(buffers, buffer_bytes),
)?;
let reader = channel.reader(|buffer| Some(&buffers[buffer.index()].0));
let times: Vec<f32> = reader
.read_inputs()
.ok_or_else(|| format!("{label}: a channel has no keyframe times"))?
.collect();
if times.is_empty()
|| times.iter().any(|t| !t.is_finite())
|| times.windows(2).any(|pair| pair[1] <= pair[0])
{
return Err(format!(
"{label}: keyframe times must be finite and strictly increasing; re-export valid glTF"
)
.into());
}
let values = match reader
.read_outputs()
.ok_or_else(|| format!("{label}: a channel has no keyframe values"))?
{
ReadOutputs::Translations(values) => {
ChannelValues::Translation(values.map(Vec3::from_array).collect())
}
ReadOutputs::Rotations(values) => {
ChannelValues::Rotation(values.into_f32().map(Quat::from_array).collect())
}
ReadOutputs::Scales(values) => {
ChannelValues::Scale(values.map(Vec3::from_array).collect())
}
ReadOutputs::MorphTargetWeights(values) => {
ChannelValues::MorphWeights(values.into_f32().collect())
}
};
Ok(Channel {
node: channel.target().node().index(),
interpolation,
times,
values,
})
})
.collect::<Result<_>>()?;
Ok(Clip {
name: animation.name().map(str::to_owned),
channels,
})
}
pub(super) fn read_influences<'a, 's, F>(
reader: &gltf::mesh::Reader<'a, 's, F>,
(first, count): (u32, u32),
vertices: usize,
label: &str,
) -> Result<Vec<Influence>>
where
F: Clone + Fn(gltf::Buffer<'a>) -> Option<&'s [u8]>,
{
let joints: Vec<[u16; 4]> = reader
.read_joints(0)
.ok_or_else(|| format!("{label}: a skinned primitive lacks JOINTS_0"))?
.into_u16()
.collect();
let weights: Vec<[f32; 4]> = reader
.read_weights(0)
.ok_or_else(|| format!("{label}: a skinned primitive lacks WEIGHTS_0"))?
.into_f32()
.collect();
if joints.len() != vertices || weights.len() != vertices {
return Err(format!("{label}: joint and weight counts differ from positions").into());
}
joints
.into_iter()
.zip(weights)
.map(|(joints, weights)| {
if joints.iter().any(|&joint| u32::from(joint) >= count) {
return Err(format!("{label}: a vertex names a joint its skin lacks").into());
}
if joints.iter().any(|&joint| first + u32::from(joint) > MAX_INDEX) {
return Err(
format!("{label}: a vertex names a joint beyond the 65536 an asset may have")
.into(),
);
}
let sum = weights.iter().sum::<f32>();
if weights.iter().any(|w| !w.is_finite() || *w < 0.)
|| !sum.is_finite()
|| sum <= 0.
{
return Err(format!(
"{label}: a vertex's joint weights must be finite and nonnegative with a finite positive sum"
)
.into());
}
Ok(Influence {
joints: joints.map(|joint| first + u32::from(joint)),
weights,
})
})
.collect()
}
pub(super) fn read_morph_targets<'a, 's, F>(
reader: &gltf::mesh::Reader<'a, 's, F>,
first: u32,
(transform, normal_transform): (Mat4, Mat4),
(normals, authored_tangents): (&[[f32; 3]], Option<&[[f32; 4]]>),
baked: &[Vertex],
label: &str,
) -> Result<Vec<MorphTarget>>
where
F: Clone + Fn(gltf::Buffer<'a>) -> Option<&'s [u8]>,
{
let count = normals.len();
let read = |values: Option<Vec<[f32; 3]>>, target: usize| -> Result<Vec<Vec3>> {
let Some(values) = values else {
return Ok(vec![Vec3::ZERO; count]);
};
if values.len() != count || values.iter().flatten().any(|v| !v.is_finite()) {
return Err(
format!("{label}: morph target {target} does not match its positions").into(),
);
}
Ok(values.into_iter().map(Vec3::from_array).collect())
};
reader
.read_morph_targets()
.enumerate()
.map(|(target, (positions, target_normals, tangents))| {
let positions = read(positions.map(Iterator::collect), target)?;
let target_normals = read(target_normals.map(Iterator::collect), target)?;
let tangents = read(tangents.map(Iterator::collect), target)?;
let deltas = (0..count)
.map(|vertex| {
let normal =
normal_transform.transform_vector3(Vec3::from_array(normals[vertex]));
let baked_normal = normal.normalize();
let tangent = authored_tangents
.filter(|_| baked[vertex].tangent != [0.0; 4])
.map_or(Vec3::ZERO, |authored| {
let [x, y, z, _] = authored[vertex];
let transformed = transform.transform_vector3(Vec3::new(x, y, z));
let projected =
transformed - baked_normal * baked_normal.dot(transformed);
transform.transform_vector3(tangents[vertex]) / projected.length()
});
MorphDelta {
position: transform.transform_vector3(positions[vertex]).to_array(),
normal: (normal_transform.transform_vector3(target_normals[vertex])
/ normal.length())
.to_array(),
tangent: tangent.to_array(),
}
})
.collect();
Ok(MorphTarget {
weight: first + target as u32,
deltas,
})
})
.collect()
}