use serde_json::Value;
use super::{IDENTITY, M4, NodeView, Reader, at, floats, index, items, trs};
use crate::document::Pose;
use crate::num::f32_of;
use crate::quat;
use crate::{Context as _, ErrorKind, Result};
pub(super) const VISIBLE: &str = "extensions/KHR_node_visibility/visible";
pub(crate) fn node_target(target: &Value) -> Option<(usize, &str)> {
if let (Some(node), Some(path)) = (index(target, "node"), target.get("path").and_then(Value::as_str))
&& path != "pointer"
{
return Some((node, path));
}
let pointer = at(target, &["extensions", "KHR_animation_pointer", "pointer"]).and_then(Value::as_str)?;
let (node, path) = pointer.strip_prefix("/nodes/")?.split_once('/')?;
if node.is_empty() || !node.bytes().all(|b| b.is_ascii_digit()) || (node.len() > 1 && node.starts_with('0')) {
return None;
}
let path = ["translation", "rotation", "scale", "weights", VISIBLE]
.into_iter()
.find(|&p| p == path)?;
Some((node.parse().ok()?, path))
}
impl Reader<'_> {
pub(super) fn span(&mut self, a: usize) -> Result<(f32, f32)> {
let anim = self.get("animations", a)?;
let samplers = items(anim, "samplers");
let (mut first, mut last) = (f32::INFINITY, f32::NEG_INFINITY);
for ch in items(anim, "channels") {
if ch.get("target").and_then(node_target).is_none() {
continue;
}
let Some(input) = index(ch, "sampler")
.and_then(|s| samplers.get(s))
.and_then(|s| index(s, "input"))
else {
continue;
};
let times = self.read::<1>(input, [0.0])?;
if let (Some([a]), Some([b])) = (times.first(), times.last()) {
first = first.min(*a);
last = last.max(*b);
}
}
Ok(if first <= last { (first, last) } else { (0.0, 0.0) })
}
pub(super) fn pose(&mut self, pose: Pose) -> Result<()> {
let Pose::Animation { index: a, time } = pose else {
return Ok(());
};
let anim = self.get("animations", a)?;
let samplers = items(anim, "samplers");
for ch in items(anim, "channels") {
let Some((node, path)) = ch.get("target").and_then(node_target) else {
continue;
};
if node >= self.posed.len() {
continue;
}
let s = index(ch, "sampler")
.and_then(|s| samplers.get(s))
.context(ErrorKind::Corrupt, "glTF animation channel names a missing sampler")?;
let (Some(input), Some(output)) = (index(s, "input"), index(s, "output")) else {
bail!(Corrupt, "glTF animation sampler has no input or output");
};
let interpolation = s.get("interpolation").and_then(Value::as_str).unwrap_or("LINEAR");
let times: Vec<f32> = self.read::<1>(input, [0.0])?.into_flattened();
let values: Vec<f32> = match path {
"translation" | "scale" => self.read::<3>(output, [0.0; 3])?.into_flattened(),
"rotation" => self.read::<4>(output, [0.0, 0.0, 0.0, 1.0])?.into_flattened(),
"weights" | VISIBLE => self.read::<1>(output, [0.0])?.into_flattened(),
_ => continue,
};
let interpolation = if path == VISIBLE { "STEP" } else { interpolation };
let Some(v) = sample(×, &values, interpolation, time, path == "rotation") else {
continue;
};
let p = &mut self.posed[node];
match (path, v.as_slice()) {
("translation", &[x, y, z]) => p.translation = Some([x, y, z]),
("scale", &[x, y, z]) => p.scale = Some([x, y, z]),
("rotation", &[x, y, z, w]) => p.rotation = Some([x, y, z, w]),
("weights", _) => p.weights = Some(v.into_iter().map(f32_of).collect()),
(VISIBLE, &[x]) => p.visible = Some(x != 0.0),
_ => {}
}
}
Ok(())
}
pub(super) fn local(&self, i: usize, node: &NodeView<'_>) -> M4 {
let p = &self.posed[i];
if let Some(m) = node
.matrix
.and_then(Value::as_array)
.filter(|m| m.len() == 16 && !p.given)
{
let mut out = IDENTITY;
for (o, x) in out.iter_mut().zip(m) {
*o = x.as_f64().unwrap_or(*o);
}
return out;
}
trs(
p.translation.unwrap_or_else(|| floats(node.translation, [0.0; 3])),
p.rotation
.unwrap_or_else(|| floats(node.rotation, [0.0, 0.0, 0.0, 1.0])),
p.scale.unwrap_or_else(|| floats(node.scale, [1.0; 3])),
)
}
}
pub(crate) fn sample(times: &[f32], values: &[f32], interpolation: &str, t: f32, rotation: bool) -> Option<Vec<f64>> {
let mut out = vec![0.0; width(times, values, interpolation)?];
sample_into(times, values, interpolation, t, rotation, &mut out).then_some(out)
}
pub(crate) fn width(times: &[f32], values: &[f32], interpolation: &str) -> Option<usize> {
let keys = times.len() * if interpolation == "CUBICSPLINE" { 3 } else { 1 };
(keys > 0 && !values.is_empty() && values.len().is_multiple_of(keys)).then(|| values.len() / keys)
}
pub(crate) fn sample_into(
times: &[f32],
values: &[f32],
interpolation: &str,
t: f32,
rotation: bool,
out: &mut [f64],
) -> bool {
let k = times.partition_point(|&x| x <= t);
sample_after(times, values, interpolation, t, rotation, k, out)
}
pub(crate) fn sample_after(
times: &[f32],
values: &[f32],
interpolation: &str,
t: f32,
rotation: bool,
k: usize,
out: &mut [f64],
) -> bool {
let Some(w) = width(times, values, interpolation) else {
return false;
};
if out.len() != w {
return false;
}
let keys = times.len();
let cubic = interpolation == "CUBICSPLINE";
let per = if cubic { 3 } else { 1 };
let at = |k: usize, part: usize| -> &[f32] {
let e = k * per + part;
&values[e * w..(e + 1) * w]
};
let value = |k: usize| at(k, usize::from(cubic));
let copy = |out: &mut [f64], v: &[f32]| {
for (o, &x) in out.iter_mut().zip(v) {
*o = f64::from(x);
}
};
if k == 0 {
copy(out, value(0));
} else if k == keys {
copy(out, value(keys - 1));
} else {
let (k, (t0, t1)) = (k - 1, (f64::from(times[k - 1]), f64::from(times[k])));
let dt = t1 - t0;
let s = if dt > 0.0 { (f64::from(t) - t0) / dt } else { 0.0 };
match interpolation {
_ if s == 0.0 => copy(out, value(k)),
"STEP" => copy(out, value(k)),
"CUBICSPLINE" => {
let (v0, b0, a1, v1) = (at(k, 1), at(k, 2), at(k + 1, 0), at(k + 1, 1));
let (s2, s3) = (s * s, s * s * s);
let h = [
2.0 * s3 - 3.0 * s2 + 1.0,
(s3 - 2.0 * s2 + s) * dt,
-2.0 * s3 + 3.0 * s2,
(s3 - s2) * dt,
];
for (c, o) in out.iter_mut().enumerate() {
let f = |v: &[f32]| f64::from(v[c]);
*o = h[0] * f(v0) + h[1] * f(b0) + h[2] * f(v1) + h[3] * f(a1);
}
}
_ if rotation && w == 4 => {
let q = |v: &[f32]| std::array::from_fn(|c| f64::from(v[c]));
out.copy_from_slice(&quat::slerp(q(value(k)), q(value(k + 1)), s));
}
_ => {
for ((o, &x), &y) in out.iter_mut().zip(value(k)).zip(value(k + 1)) {
let (x, y) = (f64::from(x), f64::from(y));
*o = x + (y - x) * s;
}
}
}
}
if rotation && w == 4 {
let len = out.iter().map(|c| c * c).sum::<f64>().sqrt();
if len > 0.0 {
for c in out.iter_mut() {
*c /= len;
}
}
}
true
}