use serde_json::Value;
use super::accessor::Acc;
use super::{IDENTITY, M4, Reader, Use, affine_inverse, at, det3, ids, index, items, list_of, mul, normal_matrix};
use crate::Result;
use crate::document::Bounds;
#[cfg(doc)]
use crate::document::Document;
use crate::mesh::{Mesh, Prim};
use crate::num::{f32_of, u32_of};
use crate::vec3;
#[cfg(feature = "draco")]
use crate::{Context as _, ErrorKind};
type Frames<'a> = (&'a mut [[f32; 3]], &'a mut [[f32; 3]], &'a mut [[f32; 4]]);
fn transform(
m: &M4,
nm: &[[f64; 3]; 3],
morphed: bool,
(positions, normals, tangents): Frames<'_>,
range: std::ops::Range<usize>,
) {
let still = m[..12] == IDENTITY[..12] && m[12..15] == [0.0; 3];
if !still {
for p in &mut positions[range.clone()] {
*p = apply_point(m, *p);
}
}
let end = range.end.min(tangents.len());
if (morphed || !still) && range.start < end {
let flip = det3(m) < 0.0;
for t in &mut tangents[range.start..end] {
*t = apply_tangent(m, *t, flip);
}
}
let end = range.end.min(normals.len());
if (morphed || !still) && range.start < end {
let n = nm.map(|r| r.map(f32_of));
let turn = |[x, y, z]: [f32; 3]| {
[
n[0][0] * x + n[0][1] * y + n[0][2] * z,
n[1][0] * x + n[1][1] * y + n[1][2] * z,
n[2][0] * x + n[2][1] * y + n[2][2] * z,
]
};
let fits = |len2: f32| len2 > 0.0 && len2.is_finite();
let normals = &mut normals[range.start..end];
if normals.len() < 8 {
for nv in normals {
let [x, y, z] = turn(*nv);
let len2 = x * x + y * y + z * z;
*nv = if fits(len2) {
let inv = 1.0 / len2.sqrt();
[x * inv, y * inv, z * inv]
} else {
apply_normal(nm, *nv)
};
}
return;
}
for block in normals.chunks_mut(64) {
let mut kept = [[0.0; 3]; 64];
kept[..block.len()].copy_from_slice(block);
let mut all = true;
for nv in block.iter_mut() {
let [x, y, z] = turn(*nv);
let len2 = x * x + y * y + z * z;
all &= fits(len2);
let inv = 1.0 / len2.sqrt();
*nv = [x * inv, y * inv, z * inv];
}
if !all {
for (nv, &k) in block.iter_mut().zip(&kept) {
let o = turn(k);
if !fits(o[0] * o[0] + o[1] * o[1] + o[2] * o[2]) {
*nv = apply_normal(nm, k);
}
}
}
}
}
}
fn apply_point(m: &M4, p: [f32; 3]) -> [f32; 3] {
let [x, y, z] = p.map(f64::from);
[
f32_of(m[0] * x + m[4] * y + m[8] * z + m[12]),
f32_of(m[1] * x + m[5] * y + m[9] * z + m[13]),
f32_of(m[2] * x + m[6] * y + m[10] * z + m[14]),
]
}
fn apply_normal(n: &[[f64; 3]; 3], v: [f32; 3]) -> [f32; 3] {
let [x, y, z] = v.map(f64::from);
let (a, b, c) = (
n[0][0] * x + n[0][1] * y + n[0][2] * z,
n[1][0] * x + n[1][1] * y + n[1][2] * z,
n[2][0] * x + n[2][1] * y + n[2][2] * z,
);
let len = (a * a + b * b + c * c).sqrt();
if len > 0.0 {
let inv = 1.0 / len;
[f32_of(a * inv), f32_of(b * inv), f32_of(c * inv)]
} else {
[0.0, 0.0, 1.0]
}
}
fn apply_tangent(m: &M4, t: [f32; 4], flip: bool) -> [f32; 4] {
let [x, y, z] = [t[0], t[1], t[2]].map(f64::from);
let (a, b, c) = (
m[0] * x + m[4] * y + m[8] * z,
m[1] * x + m[5] * y + m[9] * z,
m[2] * x + m[6] * y + m[10] * z,
);
let w = if (t[3] < 0.0) == flip { 1.0 } else { -1.0 };
let len = (a * a + b * b + c * c).sqrt();
if len > 0.0 && len.is_finite() {
let inv = 1.0 / len;
[f32_of(a * inv), f32_of(b * inv), f32_of(c * inv), w]
} else {
[1.0, 0.0, 0.0, w]
}
}
fn normalize(v: &mut [f32; 3]) {
*v = vec3::unit_or(*v, [0.0, 0.0, 1.0]);
}
struct Geometry {
positions: Vec<[f32; 3]>,
normals: Vec<[f32; 3]>,
tangents: Vec<[f32; 4]>,
uvs: Vec<[f32; 2]>,
uv_sets: Vec<Vec<[f32; 2]>>,
colors: Vec<[f32; 4]>,
color_sets: Vec<Vec<[f32; 4]>>,
draws: Draws,
joints: Vec<u32>,
skin_joints: Vec<[u32; 4]>,
skin_weights: Vec<[f32; 4]>,
}
#[derive(Default)]
struct Draws {
triangles: Vec<u32>,
lines: Vec<u32>,
points: Vec<u32>,
}
impl Draws {
fn is_empty(&self) -> bool {
self.triangles.is_empty() && self.lines.is_empty() && self.points.is_empty()
}
}
fn same_vertices(p: &Value, q: &Value) -> bool {
let draco = |v: &Value| at(v, &["extensions", "KHR_draco_mesh_compression"]).is_some();
["attributes", "material", "targets", "extensions"]
.iter()
.all(|k| p.get(k) == q.get(k))
&& !draco(p)
&& !draco(q)
}
pub(super) fn segments(mode: u64, idx: Vec<u32>) -> Vec<u32> {
match mode {
2 | 3 => {
let n = idx.len();
let mut out = Vec::with_capacity(n * 2);
for w in idx.windows(2) {
out.extend_from_slice(w);
}
if mode == 2 && n >= 2 {
out.extend_from_slice(&[idx[n - 1], idx[0]]);
}
out
}
_ => {
let mut idx = idx;
idx.truncate(idx.len() / 2 * 2);
idx
}
}
}
pub(super) fn retain_groups<const N: usize>(idx: &mut Vec<u32>, keep: impl Fn(&[u32; N]) -> bool) {
let mut w = 0;
for g in 0..idx.len() / N {
let group: [u32; N] = std::array::from_fn(|k| idx[g * N + k]);
if keep(&group) {
idx[w..w + N].copy_from_slice(&group);
w += N;
}
}
idx.truncate(w);
}
#[derive(Default)]
struct Skinned {
strongest: Vec<u32>,
joints: Vec<[u32; 4]>,
weights: Vec<[f32; 4]>,
thickness: Option<f32>,
}
pub(super) fn triangles(mode: u64, idx: Vec<u32>) -> Vec<u32> {
match mode {
5 => {
let n = idx.len().saturating_sub(2);
let mut out = Vec::with_capacity(n * 3);
for i in 0..n {
if i % 2 == 0 {
out.extend_from_slice(&[idx[i], idx[i + 1], idx[i + 2]]);
} else {
out.extend_from_slice(&[idx[i], idx[i + 2], idx[i + 1]]);
}
}
out
}
6 => {
let n = idx.len().saturating_sub(2);
let mut out = Vec::with_capacity(n * 3);
for i in 0..n {
out.extend_from_slice(&[idx[i + 1], idx[i + 2], idx[0]]);
}
out
}
_ => {
let mut idx = idx;
idx.truncate(idx.len() / 3 * 3);
idx
}
}
}
impl Reader<'_> {
pub(super) fn mesh(&mut self) -> Result<Option<Mesh>> {
let uses = self.uses()?;
let mut mesh = Mesh::default();
let prims: usize = uses
.iter()
.map(|u| self.meshes.get(u.mesh).map_or(0, |m| items(m, "primitives").len()))
.sum();
mesh.prims.reserve(prims);
mesh.materials.reserve(self.materials.len().min(prims) + 1);
for u in &uses {
let mut last: Option<(&Value, usize)> = None;
for p in items(self.get("meshes", u.mesh)?, "primitives") {
if let Some((q, at)) = last
&& mode(p) < 4
&& same_vertices(p, q)
{
self.join(p, u, &mut mesh.prims[at])?;
continue;
}
let before = mesh.prims.len();
self.primitive(p, u, &mut mesh)?;
last = (mesh.prims.len() > before).then_some((p, before));
}
}
self.skeleton(&mut mesh);
Ok(mesh.prims.iter().any(|p| !p.is_empty()).then_some(mesh))
}
pub(super) fn bounds(&mut self) -> Result<Option<Bounds>> {
let uses = self.uses()?;
let mut lo = [f32::INFINITY; 3];
let mut hi = [f32::NEG_INFINITY; 3];
for u in &uses {
for p in items(self.get("meshes", u.mesh)?, "primitives") {
let mode = mode(p);
let attrs = p.get("attributes").unwrap_or(&Value::Null);
let Some(pos) = index(attrs, "POSITION") else {
continue;
};
if !self.drawn(mode) {
continue;
}
self.draco.clear();
if let Some(ext) = p.get("extensions").and_then(|e| e.get("KHR_draco_mesh_compression")) {
if mode < 4 {
continue;
}
#[cfg(feature = "draco")]
self.decode_draco(ext, attrs, index(p, "indices"))?;
#[cfg(not(feature = "draco"))]
{
let _ = ext;
ensure!(
self.acc(pos)?.view.is_some(),
FeatureDisabled,
"glTF uses Draco compression, which needs Polysmelt's `draco` feature"
);
}
}
let mut positions = self.read(pos, [0.0; 3])?;
for (k, target) in items(p, "targets").iter().enumerate() {
let w = u.weights.get(k).copied().unwrap_or(0.0);
if w == 0.0 {
continue;
}
if let Some(a) = index(target, "POSITION") {
for (v, d) in positions.iter_mut().zip(self.read(a, [0.0; 3])?) {
for c in 0..3 {
v[c] += w * d[c];
}
}
}
}
if let Some(skin) = u.skin {
let morphed = !u.weights.is_empty();
self.skin(skin, attrs, &mut positions, &mut [], &mut [], morphed)?;
}
for m in &u.transforms {
let still = m[..12] == IDENTITY[..12] && m[12..15] == [0.0; 3];
for &v in &positions {
let v = if still { v } else { apply_point(m, v) };
if v.iter().all(|x| x.is_finite()) {
for c in 0..3 {
lo[c] = lo[c].min(v[c]);
hi[c] = hi[c].max(v[c]);
}
}
}
}
}
}
Ok((lo[0] <= hi[0]).then_some(Bounds { min: lo, max: hi }))
}
fn drawn(&self, mode: u64) -> bool {
matches!(mode, 4..=6) || (mode < 4 && self.doc.lines_and_points)
}
fn afford(&self, indices: Option<usize>, pos: usize, mode: u64, k: usize) -> Result<Acc> {
let counted = self.acc(indices.unwrap_or(pos))?;
let corners = counted.count;
let n = match mode {
0 | 2 => corners,
1 => corners / 2,
3 => corners.saturating_sub(1),
4 => corners / 3,
_ => corners.saturating_sub(2),
};
let total = n.saturating_mul(k);
if mode < 4 {
ensure!(
total <= self.point_budget,
LimitExceeded,
"too many points and lines to convert (more than {})",
self.doc.max_points
);
} else {
ensure!(
total <= self.budget,
LimitExceeded,
"too many triangles to convert (more than {})",
self.doc.max_triangles
);
}
Ok(counted)
}
fn draws(&mut self, indices: Option<(usize, &Acc)>, mode: u64, n: usize) -> Result<Draws> {
let idx = match indices {
Some((i, a)) => self.read_index_acc(i, a)?,
None => (0..u32_of(n)).collect(),
};
let fits = |i: u32| (i as usize) < n;
let mut d = Draws::default();
match mode {
0 => {
d.points = idx;
d.points.retain(|&i| fits(i));
}
1..=3 => {
d.lines = segments(mode, idx);
retain_groups(&mut d.lines, |&[a, b]: &[u32; 2]| a != b && fits(a) && fits(b));
}
_ => {
d.triangles = triangles(mode, idx);
if d.triangles.iter().copied().max().is_some_and(|m| !fits(m)) {
retain_groups(&mut d.triangles, |t: &[u32; 3]| t.iter().all(|&i| fits(i)));
}
}
}
Ok(d)
}
fn charge(&mut self, d: &Draws, k: usize) {
self.budget -= d.triangles.len() / 3 * k;
self.point_budget -= (d.lines.len() / 2 + d.points.len()) * k;
}
fn join(&mut self, p: &Value, u: &Use, prim: &mut Prim) -> Result<()> {
let mode = mode(p);
let Some(pos) = p.get("attributes").and_then(|a| index(a, "POSITION")) else {
return Ok(());
};
if !self.drawn(mode) {
return Ok(());
}
let k = u.transforms.len();
let indices = index(p, "indices");
let counted = self.afford(indices, pos, mode, k)?;
let n = prim.positions.len() / k.max(1);
let d = self.draws(indices.map(|i| (i, &counted)), mode, n)?;
self.charge(&d, k);
extend(prim, &d, n, &u.transforms);
Ok(())
}
fn skeleton(&self, mesh: &mut Mesh) {
let nodes = self.nodes;
let mut used = vec![false; nodes.len()];
for p in mesh.prims.iter().filter(|p| p.is_skinned()) {
for &j in p.skin_joints.as_flattened() {
if let Some(u) = used.get_mut(j as usize) {
*u = true;
}
}
}
let mut parent = vec![None; nodes.len()];
for (i, n) in nodes.iter().enumerate() {
for c in ids(n, "children") {
if c < nodes.len() && c != i && parent[c].is_none() {
parent[c] = Some(i);
}
}
}
for i in 0..nodes.len() {
if !used[i] {
continue;
}
let mut at = parent[i];
for _ in 0..nodes.len() {
let Some(p) = at else { break };
if used[p] {
break;
}
used[p] = true;
at = parent[p];
}
}
let mut kids = vec![Vec::new(); nodes.len()];
for i in (0..nodes.len()).filter(|&i| used[i]) {
if let Some(p) = parent[i].filter(|&p| used[p]) {
kids[p].push(i);
}
}
let mut bone_of = vec![None::<usize>; nodes.len()];
let mut order = Vec::new();
let roots: Vec<usize> = (0..nodes.len())
.filter(|&i| used[i] && parent[i].is_none_or(|p| !used[p]))
.collect();
let cyclic: Vec<usize> = (0..nodes.len()).filter(|&i| used[i]).collect();
for root in roots.into_iter().chain(cyclic) {
if bone_of[root].is_some() {
continue;
}
let mut stack = vec![root];
while let Some(i) = stack.pop() {
if bone_of[i].is_some() {
continue;
}
bone_of[i] = Some(order.len());
order.push(i);
stack.extend(kids[i].iter().rev().copied());
}
}
mesh.skeleton = order
.iter()
.map(|&i| crate::mesh::Bone {
name: nodes[i].get("name").and_then(Value::as_str).map(str::to_owned),
parent: parent[i]
.and_then(|p| bone_of[p])
.filter(|&b| bone_of[i].is_some_and(|me| b < me)),
node: i,
matrix: self
.bound
.get(i)
.copied()
.flatten()
.or_else(|| self.world.get(i).copied())
.unwrap_or(IDENTITY)
.map(f32_of),
})
.collect();
let bone: Vec<u32> = bone_of.iter().map(|b| b.map_or(0, u32_of)).collect();
for p in &mut mesh.prims {
if mesh.skeleton.is_empty() || !p.is_skinned() {
p.skin_joints = Vec::new();
p.skin_weights = Vec::new();
continue;
}
for (j, w) in p.skin_joints.iter_mut().zip(&p.skin_weights) {
*j = std::array::from_fn(|k| {
let b = bone.get(j[k] as usize).copied().unwrap_or(0);
if w[k] > 0.0 { b } else { 0 }
});
}
}
}
fn splat_colors(&mut self, p: &Value, attrs: &Value) -> Result<Vec<[f32; 4]>> {
let Some(ext) = at(p, &["extensions", "KHR_gaussian_splatting"]) else {
return Ok(Vec::new());
};
let (Some(dc), Some(opacity)) = (
index(attrs, crate::gaussian::SH0),
index(attrs, crate::gaussian::OPACITY),
) else {
return Ok(Vec::new());
};
let srgb = ext.get("colorSpace").and_then(Value::as_str) != Some("lin_rec709_display");
let dc = self.read(dc, [0.0; 3])?;
let opacity = self.read(opacity, [1.0])?;
if dc.len() != opacity.len() {
return Ok(Vec::new());
}
let fallback = |(&d, &[o]): (&[f32; 3], &[f32; 1])| crate::gaussian::fallback(d, o, srgb);
Ok(dc.iter().zip(&opacity).map(fallback).collect())
}
fn primitive(&mut self, p: &Value, u: &Use, mesh: &mut Mesh) -> Result<()> {
let mode = mode(p);
let attrs = p.get("attributes").unwrap_or(&Value::Null);
let Some(pos) = index(attrs, "POSITION") else {
return Ok(());
};
if !self.drawn(mode) {
return Ok(());
}
self.draco.clear();
if let Some(ext) = p.get("extensions").and_then(|e| e.get("KHR_draco_mesh_compression")) {
if mode < 4 {
return Ok(());
}
#[cfg(feature = "draco")]
self.decode_draco(ext, attrs, index(p, "indices"))?;
#[cfg(not(feature = "draco"))]
{
let _ = ext;
ensure!(
self.acc(pos)?.view.is_some(),
FeatureDisabled,
"glTF uses Draco compression, which needs Polysmelt's `draco` feature"
);
}
}
let indices = index(p, "indices");
let counted = self.afford(indices, pos, mode, u.transforms.len())?;
let mut positions = match indices {
Some(_) => self.read(pos, [0.0; 3])?,
None => self.read_acc(&counted, [0.0; 3])?,
};
let n = positions.len();
let draws = self.draws(indices.map(|i| (i, &counted)), mode, n)?;
if draws.is_empty() {
return Ok(());
}
let material = index(p, "material");
let (set, uv_matrix) = self.uv_set(material);
let attr = |name: &str| index(attrs, name);
let mut normals = match attr("NORMAL") {
Some(a) => self.read(a, [0.0; 3])?,
None => Vec::new(),
};
let mut uvs = match attr(&texcoord(set)) {
Some(a) => self.read(a, [0.0; 2])?,
None => Vec::new(),
};
let colors = match attr("COLOR_0") {
Some(a) => self.read(a, [0.0, 0.0, 0.0, 1.0])?,
None => self.splat_colors(p, attrs)?,
};
if normals.len() != n {
normals = Vec::new();
}
if uvs.len() != n {
uvs = Vec::new();
}
let colors = if colors.len() == n { colors } else { Vec::new() };
let mut tangents = match attr("TANGENT") {
Some(a) if !normals.is_empty() => self.read(a, [1.0, 0.0, 0.0, 1.0])?,
_ => Vec::new(),
};
if tangents.len() != n {
tangents = Vec::new();
}
let uv_sets = numbered(attrs, "TEXCOORD_", |k| k != set, |a| self.read(a, [0.0; 2]).ok(), n);
let color_sets = numbered(
attrs,
"COLOR_",
|k| k != 0,
|a| self.read(a, [0.0, 0.0, 0.0, 1.0]).ok(),
n,
);
if let Some(t) = uv_matrix {
for uv in &mut uvs {
let [u0, v0] = *uv;
*uv = [t[0] * u0 + t[1] * v0 + t[2], t[3] * u0 + t[4] * v0 + t[5]];
}
}
for (k, target) in items(p, "targets").iter().enumerate() {
let w = u.weights.get(k).copied().unwrap_or(0.0);
if w == 0.0 {
continue;
}
if let Some(a) = index(target, "POSITION") {
for (v, d) in positions.iter_mut().zip(self.read(a, [0.0; 3])?) {
for c in 0..3 {
v[c] += w * d[c];
}
}
}
if !normals.is_empty()
&& let Some(a) = index(target, "NORMAL")
{
for (v, d) in normals.iter_mut().zip(self.read(a, [0.0; 3])?) {
for c in 0..3 {
v[c] += w * d[c];
}
}
}
if !tangents.is_empty()
&& let Some(a) = index(target, "TANGENT")
{
for (v, d) in tangents.iter_mut().zip(self.read(a, [0.0; 3])?) {
for c in 0..3 {
v[c] += w * d[c];
}
}
}
}
let mut skinned = Skinned::default();
let morphed = !u.weights.is_empty();
if let Some(skin) = u.skin {
skinned = self.skin(skin, attrs, &mut positions, &mut normals, &mut tangents, morphed)?;
} else if morphed {
normals.iter_mut().for_each(normalize);
for t in &mut tangents {
*t = apply_tangent(&IDENTITY, *t, false);
}
}
let g = Geometry {
positions,
normals,
tangents,
uvs,
uv_sets,
colors,
color_sets,
draws,
joints: skinned.strongest,
skin_joints: skinned.joints,
skin_weights: skinned.weights,
};
let material = self.material(material, skinned.thickness.unwrap_or(u.thickness), mesh);
self.charge(&g.draws, u.transforms.len());
mesh.prims.push(place(g, &u.transforms, material));
Ok(())
}
#[cfg(feature = "draco")]
pub(super) fn decode_draco(&mut self, ext: &Value, attrs: &Value, indices: Option<usize>) -> Result<()> {
use draco_core::decode_limits::DecodeLimits;
use draco_core::decoder_buffer::DecoderBuffer;
use draco_core::draco_types::DataType;
use draco_core::mesh_decoder::MeshDecoder;
let view = index(ext, "bufferView").context(ErrorKind::Corrupt, "glTF Draco primitive has no bufferView")?;
let bytes = self.view_shared(view)?;
let limits = DecodeLimits::default()
.with_max_faces(self.budget as u64)
.with_max_points(self.max_vertices as u64);
let mut buffer = DecoderBuffer::new(&bytes).with_limits(limits);
let mut dm = draco_core::mesh::Mesh::new();
MeshDecoder::new()
.decode(&mut buffer, &mut dm)
.context(ErrorKind::Corrupt, "glTF Draco data doesn't decode")?;
drop(bytes);
if let Some(i) = indices {
let mut out = Vec::with_capacity(dm.num_faces() * 12);
for face in dm.faces() {
for p in face {
out.extend_from_slice(&p.0.to_le_bytes());
}
}
self.draco.push((i, out.into(), 5125, 1));
}
let points = dm.num_points();
let mut decoded = Vec::new();
for (name, id) in ext.get("attributes").and_then(Value::as_object).into_iter().flatten() {
let (Some(acc), Some(id)) = (index(attrs, name), id.as_u64().and_then(|v| u32::try_from(v).ok())) else {
continue;
};
let attribute = dm.attribute_by_unique_id(id).with_context(ErrorKind::Corrupt, || {
format!("glTF Draco stream has no attribute {id} for {name}")
})?;
let a = self.acc(acc)?;
let mut values = attribute.read_f32s(points, a.n);
if a.normalized || attribute.normalized() {
let max = match attribute.data_type() {
DataType::Uint8 => Some((255.0, 0.0)),
DataType::Int8 => Some((127.0, -1.0)),
DataType::Uint16 => Some((65535.0, 0.0)),
DataType::Int16 => Some((32767.0, -1.0)),
_ => None,
};
if let Some((max, floor)) = max {
for v in &mut values {
*v = (*v / max).max(floor);
}
}
}
let mut out = Vec::with_capacity(values.len() * 4);
for v in values {
out.extend_from_slice(&v.to_le_bytes());
}
decoded.push((acc, out.into(), 5126, a.n));
}
self.draco.extend(decoded);
Ok(())
}
fn skin(
&mut self,
skin: usize,
attrs: &Value,
positions: &mut [[f32; 3]],
normals: &mut [[f32; 3]],
tangents: &mut [[f32; 4]],
morphed: bool,
) -> Result<Skinned> {
let s = self.get("skins", skin)?;
let joints = list_of(s, "joints");
let ibm = match index(s, "inverseBindMatrices") {
Some(a) => self.read(a, IDENTITY.map(f32_of))?,
None => Vec::new(),
};
if self.bind {
if self.bound.is_empty() {
self.bound = vec![None; self.posed.len()];
}
for (k, &j) in joints.iter().enumerate() {
let b = ibm.get(k).map_or(IDENTITY, |m| m.map(f64::from));
if let Some(slot @ None) = self.bound.get_mut(j) {
*slot = Some(affine_inverse(&b).unwrap_or(IDENTITY));
}
}
}
let mats: Vec<M4> = joints
.iter()
.enumerate()
.map(|(k, &j)| {
let w = self.world.get(j).copied().unwrap_or(IDENTITY);
let b = ibm.get(k).map_or(IDENTITY, |m| m.map(f64::from));
mul(&w, &b)
})
.collect();
let mut sets = Vec::new();
for n in 0.. {
let (Some(ja), Some(wa)) = (
index(attrs, &format!("JOINTS_{n}")),
index(attrs, &format!("WEIGHTS_{n}")),
) else {
break;
};
let ji = self.read(ja, [0.0; 4])?;
let wi = self.read(wa, [0.0; 4])?;
if ji.len() != positions.len() || wi.len() != positions.len() {
break;
}
sets.push((ji, wi));
}
if sets.is_empty() {
return Ok(Skinned::default());
}
let node = |j: f32| {
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "joint indices are small whole numbers; `as` saturates bad ones and `get` refuses them"
)]
joints.get(j as usize).copied()
};
let n = positions.len();
let scales = mats.iter().map(|m| det3(m).abs().cbrt().ln()).filter(|s| s.is_finite());
let (sum, count) = scales.fold((0.0, 0.0), |(sum, count), s| (sum + s, count + 1.0));
let mean = if count > 0.0 { (sum / count).exp() } else { 1.0 };
let mut out = Skinned {
strongest: Vec::with_capacity(n),
joints: Vec::with_capacity(n),
weights: Vec::with_capacity(n),
thickness: Some(self.scale_met(mean)),
};
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "joint indices are small whole numbers; `as` saturates bad ones and `get` refuses them"
)]
let joint = |j: f32| j as usize;
let mut rigid: Vec<Option<[[f64; 3]; 3]>> = vec![None; mats.len()];
let unmoved = normal_matrix(&IDENTITY);
let mut last: Vec<(f32, f32)> = Vec::with_capacity(4 * sets.len());
let (mut m, mut nm) = (IDENTITY, unmoved);
let (mut start, mut moving) = (0, false);
let bind = self.bind;
let bits = |a: [f32; 4]| a.map(f32::to_bits);
let mut inf: Vec<(f32, f32)> = Vec::with_capacity(4 * sets.len());
let mut prev = (u32::MAX, [u32::MAX; 4], [0.0f32; 4]);
for v in 0..n {
if v > 0
&& sets
.iter()
.all(|(ji, wi)| bits(ji[v]) == bits(ji[v - 1]) && bits(wi[v]) == bits(wi[v - 1]))
{
out.strongest.push(prev.0);
out.joints.push(prev.1);
out.weights.push(prev.2);
continue;
}
inf.clear();
for (ji, wi) in &sets {
for k in 0..4 {
let (j, w) = (ji[v][k], wi[v][k]);
if w > 0.0 {
match inf.iter_mut().find(|(i, _)| i.to_bits() == j.to_bits()) {
Some((_, x)) => *x += w,
None => inf.push((j, w)),
}
}
}
}
for i in 1..inf.len() {
let mut k = i;
while k > 0 && inf[k - 1].1 < inf[k].1 {
inf.swap(k - 1, k);
k -= 1;
}
}
prev.0 = inf.first().and_then(|&(j, _)| node(j)).map_or(u32::MAX, u32_of);
out.strongest.push(prev.0);
let mut top = [(u32::MAX, 0.0f32); 4];
let mut slots = top.iter_mut();
for &(j, w) in &inf {
if let Some(nd) = node(j) {
let Some(slot) = slots.next() else { break };
*slot = (u32_of(nd), w);
}
}
let kept: f32 = top.iter().map(|t| t.1).sum();
prev.1 = top.map(|t| t.0);
prev.2 = if kept > 0.0 { top.map(|t| t.1 / kept) } else { [0.0; 4] };
out.joints.push(prev.1);
out.weights.push(prev.2);
let sum: f32 = inf.iter().map(|t| t.1).sum();
if bind || !(sum > 0.0 && sum.is_finite()) {
if moving {
transform(&m, &nm, morphed, (positions, normals, tangents), start..v);
moving = false;
}
continue;
}
let same = inf.len() == last.len()
&& inf
.iter()
.zip(&last)
.all(|(a, b)| a.0.to_bits() == b.0.to_bits() && a.1.to_bits() == b.1.to_bits());
if !(same && moving) {
if moving {
transform(&m, &nm, morphed, (positions, normals, tangents), start..v);
}
(start, moving) = (v, true);
}
if !same {
if let [(j, _)] = inf[..] {
let j = joint(j);
m = mats.get(j).copied().unwrap_or(IDENTITY);
nm = match rigid.get_mut(j) {
Some(slot) => *slot.get_or_insert_with(|| normal_matrix(&m)),
None => unmoved,
};
} else if let Some(first) = inf
.iter()
.map(|&(j, _)| mats.get(joint(j)).unwrap_or(&IDENTITY))
.try_fold(None::<&M4>, |seen, jm| match seen {
#[allow(clippy::float_cmp, reason = "the very same matrix, bit for bit, is what's sought")]
Some(s) if s != jm => Err(()),
_ => Ok(Some(jm)),
})
.ok()
.flatten()
{
m = *first;
nm = normal_matrix(&m);
} else {
m = [0.0; 16];
for &(j, w) in &inf {
let jm = mats.get(joint(j)).unwrap_or(&IDENTITY);
let f = f64::from(w / sum);
for c in 0..4 {
for r in 0..3 {
m[c * 4 + r] += f * jm[c * 4 + r];
}
}
}
nm = normal_matrix(&m);
}
last.clone_from(&inf);
}
}
if moving {
transform(&m, &nm, morphed, (positions, normals, tangents), start..n);
}
if bind && morphed {
normals.iter_mut().for_each(normalize);
for t in tangents.iter_mut() {
*t = apply_tangent(&IDENTITY, *t, false);
}
}
Ok(out)
}
}
const MAX_SETS: usize = 64;
fn numbered<T>(
attrs: &Value,
prefix: &str,
want: impl Fn(u64) -> bool,
mut read: impl FnMut(usize) -> Option<Vec<T>>,
n: usize,
) -> Vec<Vec<T>> {
let mut out: Vec<Vec<T>> = Vec::new();
let Some(attrs) = attrs.as_object() else { return out };
for (name, a) in attrs {
let Some(k) = name.strip_prefix(prefix).and_then(|k| k.parse::<u64>().ok()) else {
continue;
};
let (Some(a), Ok(slot)) = (a.as_u64().and_then(|a| usize::try_from(a).ok()), usize::try_from(k)) else {
continue;
};
if slot >= MAX_SETS || !want(k) {
continue;
}
if let Some(v) = read(a).filter(|v| v.len() == n) {
if out.len() <= slot {
out.resize_with(slot + 1, Vec::new);
}
out[slot] = v;
}
}
out
}
#[allow(clippy::float_cmp_const, reason = "only the exact identity may skip the transform")]
fn place(mut g: Geometry, transforms: &[M4], material: usize) -> Prim {
if let [t] = transforms {
if *t != IDENTITY {
for v in &mut g.positions {
*v = apply_point(t, *v);
}
let nm = normal_matrix(t);
for v in &mut g.normals {
*v = apply_normal(&nm, *v);
}
let flip = det3(t) < 0.0;
for v in &mut g.tangents {
*v = apply_tangent(t, *v, flip);
}
let whole = g.draws.triangles.len() / 3 * 3;
g.draws.triangles.truncate(whole);
if flip {
for tri in g.draws.triangles.chunks_exact_mut(3) {
tri.swap(1, 2);
}
}
}
return Prim {
positions: g.positions,
normals: g.normals,
tangents: g.tangents,
uvs: g.uvs,
uv_sets: g.uv_sets,
colors: g.colors,
color_sets: g.color_sets,
indices: g.draws.triangles,
lines: g.draws.lines,
points: g.draws.points,
material,
joints: g.joints,
skin_joints: g.skin_joints,
skin_weights: g.skin_weights,
};
}
let n = g.positions.len();
let k = transforms.len();
let mut p = Prim {
positions: Vec::with_capacity(n * k),
normals: Vec::with_capacity(g.normals.len() * k),
tangents: Vec::with_capacity(g.tangents.len() * k),
uvs: Vec::with_capacity(g.uvs.len() * k),
uv_sets: g.uv_sets.iter().map(|s| Vec::with_capacity(s.len() * k)).collect(),
colors: Vec::with_capacity(g.colors.len() * k),
color_sets: g.color_sets.iter().map(|s| Vec::with_capacity(s.len() * k)).collect(),
indices: Vec::with_capacity(g.draws.triangles.len() * k),
lines: Vec::with_capacity(g.draws.lines.len() * k),
points: Vec::with_capacity(g.draws.points.len() * k),
material,
joints: Vec::with_capacity(g.joints.len() * k),
skin_joints: Vec::with_capacity(g.skin_joints.len() * k),
skin_weights: Vec::with_capacity(g.skin_weights.len() * k),
};
for t in transforms {
p.positions.extend(g.positions.iter().map(|&v| apply_point(t, v)));
let nm = normal_matrix(t);
p.normals.extend(g.normals.iter().map(|&v| apply_normal(&nm, v)));
let flip = det3(t) < 0.0;
p.tangents.extend(g.tangents.iter().map(|&v| apply_tangent(t, v, flip)));
p.uvs.extend_from_slice(&g.uvs);
for (to, from) in p.uv_sets.iter_mut().zip(&g.uv_sets) {
to.extend_from_slice(from);
}
p.colors.extend_from_slice(&g.colors);
for (to, from) in p.color_sets.iter_mut().zip(&g.color_sets) {
to.extend_from_slice(from);
}
p.joints.extend_from_slice(&g.joints);
p.skin_joints.extend_from_slice(&g.skin_joints);
p.skin_weights.extend_from_slice(&g.skin_weights);
}
extend(&mut p, &g.draws, n, transforms);
p
}
fn extend(p: &mut Prim, d: &Draws, n: usize, transforms: &[M4]) {
for (k, t) in transforms.iter().enumerate() {
let base = u32_of(k * n);
let flip = det3(t) < 0.0;
for tri in d.triangles.chunks_exact(3) {
let [a, b, c] = [tri[0] + base, tri[1] + base, tri[2] + base];
p.indices.extend_from_slice(&if flip { [a, c, b] } else { [a, b, c] });
}
p.lines.extend(d.lines.iter().map(|&i| i + base));
p.points.extend(d.points.iter().map(|&i| i + base));
}
}
fn texcoord(set: u64) -> std::borrow::Cow<'static, str> {
const NAMES: [&str; 4] = ["TEXCOORD_0", "TEXCOORD_1", "TEXCOORD_2", "TEXCOORD_3"];
match usize::try_from(set).ok().and_then(|s| NAMES.get(s)) {
Some(name) => (*name).into(),
None => format!("TEXCOORD_{set}").into(),
}
}
pub(super) fn mode(p: &Value) -> u64 {
p.get("mode").and_then(Value::as_u64).unwrap_or(4)
}