mod decode;
mod repair;
mod rig;
pub(crate) use repair::repaired;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use serde_json::{Map, Value};
#[cfg(doc)]
use crate::document::Keep;
use crate::document::{Bounds, Counts, Document, Pose};
use crate::gltf as container;
use crate::mesh::{
Anisotropy, Clearcoat, Extensions, Factor, Image, Iridescence, Material, Mesh, Prim, Sampling, Sheen, Specular,
Texture, UvTransform, Volume,
};
use crate::num::{f32_of, u32_of, usize_of};
use crate::{Context as _, ErrorKind, Result, Within as _};
const BLOCK: usize = 256 * 1024;
impl Document {
pub fn flatten(&self, pose: Pose) -> Result<Option<Mesh>> {
let mut r = Reader::new(self)?;
r.bind = pose == Pose::Bind;
r.pose(pose)?;
r.mesh()
}
pub fn flatten_nodes(&self, nodes: &[crate::rig::Trs]) -> Result<Option<Mesh>> {
let mut r = Reader::new(self)?;
for (p, n) in r.posed.iter_mut().zip(nodes) {
p.translation = Some(n.translation.map(f64::from));
p.rotation = Some(n.rotation.map(f64::from));
p.scale = Some(n.scale.map(f64::from));
p.given = true;
}
r.mesh()
}
pub fn read_floats(&self, accessor: usize) -> Result<(usize, Vec<f32>)> {
let mut r = Reader::bare(self);
r.draco_holding(accessor)?;
let a = r.acc(accessor)?;
let values = match a.n {
1 => r.read_acc(&a, [0.0])?.into_flattened(),
2 => r.read_acc(&a, [0.0; 2])?.into_flattened(),
3 => r.read_acc(&a, [0.0; 3])?.into_flattened(),
4 => r.read_acc(&a, [0.0; 4])?.into_flattened(),
9 => r.read_acc(&a, [0.0; 9])?.into_flattened(),
_ => r.read_acc(&a, [0.0; 16])?.into_flattened(),
};
Ok((a.n, values))
}
pub fn read_indices(&self, accessor: usize) -> Result<Vec<u32>> {
let mut r = Reader::bare(self);
r.draco_holding(accessor)?;
r.read_indices(accessor)
}
#[must_use]
pub fn counts(&self) -> Counts {
counts(&self.json)
}
pub fn bounds(&self, pose: Pose) -> Result<Option<Bounds>> {
let mut r = Reader::new(self)?;
r.bind = pose == Pose::Bind;
r.pose(pose)?;
r.bounds()
}
#[must_use]
pub fn triangles(&self) -> usize {
self.counts().triangles
}
#[allow(clippy::cast_precision_loss, reason = "sample numbers are small")]
pub fn animation_bounds(&self, index: usize, samples: usize) -> Result<Option<Bounds>> {
let (first, last) = Reader::bare(self).span(index)?;
let n = samples.max(1);
let mut out: Option<Bounds> = None;
for k in 0..n {
let time = if k == 0 {
first
} else {
first + (last - first) * (k as f32 / (n - 1) as f32)
};
let Some(b) = self.bounds(Pose::Animation { index, time })? else {
continue;
};
out = Some(out.map_or(b, |o| Bounds {
min: std::array::from_fn(|c| o.min[c].min(b.min[c])),
max: std::array::from_fn(|c| o.max[c].max(b.max[c])),
}));
}
Ok(out)
}
}
fn counts(json: &Map<String, Value>) -> Counts {
let mut c = Counts::default();
let Ok(order) = scene_order(json) else {
return c;
};
let meshes = list(json, "meshes");
let accessors = list(json, "accessors");
let count = |a: usize| accessors.get(a).and_then(|a| index(a, "count")).unwrap_or(0);
let mut add = |mesh: &Value, k: usize| {
for p in list(mesh.as_object().unwrap_or(&Map::new()), "primitives") {
let Some(pos) = p.get("attributes").and_then(|a| index(a, "POSITION")) else {
continue;
};
c.vertices += count(pos) * k;
let n = index(p, "indices").map_or_else(|| count(pos), count);
match p.get("mode").and_then(Value::as_u64).unwrap_or(4) {
0 => c.points += n * k,
1 => c.lines += n / 2 * k,
2 => c.lines += n * k,
3 => c.lines += n.saturating_sub(1) * k,
4 => c.triangles += n / 3 * k,
5 | 6 => c.triangles += n.saturating_sub(2) * k,
_ => {}
}
}
};
let Some(order) = order else {
for m in meshes {
add(m, 1);
}
return c;
};
let nodes = list(json, "nodes");
let mut skinned: HashSet<(usize, usize, Vec<u32>)> = HashSet::new();
for i in order {
let n = &nodes[i];
let Some(m) = index(n, "mesh") else { continue };
let Some(mesh) = meshes.get(m) else { continue };
let k = if let Some(skin) = index(n, "skin") {
let w: Vec<u32> = default_weights(n, mesh).iter().map(|x| x.to_bits()).collect();
if !skinned.insert((m, skin, w)) {
continue;
}
1
} else {
match instancing(n) {
None => 1,
Some(attrs) => ["TRANSLATION", "ROTATION", "SCALE"]
.iter()
.filter_map(|k| index(attrs, k))
.map(count)
.min()
.unwrap_or(0),
}
};
add(mesh, k);
}
c
}
fn instancing(node: &Value) -> Option<&Value> {
node.get("extensions")?
.get("EXT_mesh_gpu_instancing")?
.get("attributes")
}
fn default_weights(node: &Value, mesh: &Value) -> Vec<f32> {
weights_of(node.get("weights").or_else(|| mesh.get("weights")))
}
fn weights_of(w: Option<&Value>) -> Vec<f32> {
w.and_then(Value::as_array)
.map(|w| w.iter().map(|x| f32_of(x.as_f64().unwrap_or(0.0))).collect())
.unwrap_or_default()
}
#[derive(Default, Clone, Copy)]
struct NodeView<'v> {
mesh: Option<&'v Value>,
skin: Option<&'v Value>,
children: Option<&'v Value>,
matrix: Option<&'v Value>,
translation: Option<&'v Value>,
rotation: Option<&'v Value>,
scale: Option<&'v Value>,
weights: Option<&'v Value>,
extensions: Option<&'v Value>,
}
impl<'v> NodeView<'v> {
fn of(node: &'v Value) -> Self {
let mut v = Self::default();
for (k, x) in node.as_object().into_iter().flatten() {
let slot = match k.as_str() {
"mesh" => &mut v.mesh,
"skin" => &mut v.skin,
"children" => &mut v.children,
"matrix" => &mut v.matrix,
"translation" => &mut v.translation,
"rotation" => &mut v.rotation,
"scale" => &mut v.scale,
"weights" => &mut v.weights,
"extensions" => &mut v.extensions,
_ => continue,
};
*slot = Some(x);
}
v
}
fn children(&self) -> impl Iterator<Item = usize> + 'v {
self.children
.and_then(Value::as_array)
.map_or(&[][..], Vec::as_slice)
.iter()
.filter_map(Value::as_u64)
.map(usize_of)
}
}
fn children(nodes: &[Value]) -> Vec<bool> {
let mut child = vec![false; nodes.len()];
for n in nodes {
for c in ids(n, "children") {
if let Some(f) = child.get_mut(c) {
*f = true;
}
}
}
child
}
fn scene_order(json: &Map<String, Value>) -> Result<Option<Vec<usize>>> {
let nodes = list(json, "nodes");
let scenes = list(json, "scenes");
let roots: Vec<usize> = if scenes.is_empty() {
if nodes.is_empty() {
return Ok(None);
}
let child = children(nodes);
(0..nodes.len()).filter(|&i| !child[i]).collect()
} else {
let s = json.get("scene").and_then(Value::as_u64).map_or(0, usize_of);
list_of(
scenes
.get(s)
.context(ErrorKind::Corrupt, "glTF default scene doesn't exist")?,
"nodes",
)
};
let mut seen = vec![false; nodes.len()];
let mut order = Vec::new();
let mut stack: Vec<usize> = roots.into_iter().rev().collect();
while let Some(i) = stack.pop() {
if i >= nodes.len() || seen[i] {
continue;
}
seen[i] = true;
let visible = nodes[i]
.get("extensions")
.and_then(|e| e.get("KHR_node_visibility"))
.and_then(|v| v.get("visible"));
if visible == Some(&Value::Bool(false)) {
continue;
}
order.push(i);
if let Some(kids) = nodes[i].get("children").and_then(Value::as_array) {
stack.extend(kids.iter().rev().filter_map(Value::as_u64).map(usize_of));
}
}
Ok(Some(order))
}
#[derive(Clone)]
struct Acc {
view: Option<usize>,
offset: u64,
count: usize,
ctype: u64,
n: usize,
rows: usize,
normalized: bool,
sparse: Option<Value>,
decoded: Option<Arc<[u8]>>,
}
impl Acc {
fn csize(&self) -> usize {
match self.ctype {
5120 | 5121 => 1,
5122 | 5123 => 2,
_ => 4,
}
}
fn column(&self) -> usize {
(self.rows * self.csize()).next_multiple_of(if self.rows < self.n { 4 } else { 1 })
}
fn esize(&self) -> usize {
self.column() * (self.n / self.rows.max(1))
}
fn padded(&self) -> bool {
self.esize() != self.csize() * self.n
}
fn at(&self, c: usize) -> usize {
(c / self.rows) * self.column() + (c % self.rows) * self.csize()
}
}
#[allow(
clippy::cast_precision_loss,
reason = "past 2^24 an f32 attribute rounds, as in any viewer"
)]
fn component(ctype: u64, normalized: bool, b: &[u8]) -> f32 {
match (ctype, normalized) {
(5126, _) => f32::from_le_bytes([b[0], b[1], b[2], b[3]]),
(5121, true) => f32::from(b[0]) / 255.0,
(5121, false) => f32::from(b[0]),
(5120, true) => (f32::from(b[0].cast_signed()) / 127.0).max(-1.0),
(5120, false) => f32::from(b[0].cast_signed()),
(5123, true) => f32::from(u16::from_le_bytes([b[0], b[1]])) / 65535.0,
(5123, false) => f32::from(u16::from_le_bytes([b[0], b[1]])),
(5122, true) => (f32::from(i16::from_le_bytes([b[0], b[1]])) / 32767.0).max(-1.0),
(5122, false) => f32::from(i16::from_le_bytes([b[0], b[1]])),
_ => u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f32,
}
}
pub(crate) type M4 = [f64; 16];
pub(crate) const IDENTITY: M4 = [
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
];
struct Use {
mesh: usize,
weights: Vec<f32>,
skin: Option<usize>,
transforms: Vec<M4>,
}
struct Reader<'d> {
doc: &'d Document,
json: &'d Map<String, Value>,
accessors: &'d [Value],
views: &'d [Value],
meshes: &'d [Value],
nodes: &'d [Value],
materials_json: &'d [Value],
textures: &'d [Value],
samplers: &'d [Value],
images_json: &'d [Value],
scratch: Vec<u8>,
meshopt: HashMap<usize, Vec<u8>>,
materials: Vec<Option<usize>>,
default_material: Option<usize>,
#[allow(clippy::option_option, reason = "untried and tried-but-failed are both cached")]
images: Vec<Option<Option<usize>>>,
world: Vec<M4>,
posed: Vec<Posed>,
bind: bool,
bound: Vec<Option<M4>>,
budget: usize,
point_budget: usize,
max_vertices: usize,
draco: Vec<(usize, Arc<[u8]>, u64, usize)>,
}
#[derive(Default, Clone)]
struct Posed {
translation: Option<[f64; 3]>,
rotation: Option<[f64; 4]>,
scale: Option<[f64; 3]>,
weights: Option<Vec<f32>>,
given: bool,
}
fn list<'v>(obj: &'v Map<String, Value>, key: &str) -> &'v [Value] {
obj.get(key).and_then(Value::as_array).map_or(&[], Vec::as_slice)
}
fn at<'v>(v: &'v Value, path: &[&str]) -> Option<&'v Value> {
path.iter().try_fold(v, |v, k| v.get(k))
}
fn items<'v>(v: &'v Value, key: &str) -> &'v [Value] {
v.get(key).and_then(Value::as_array).map_or(&[], Vec::as_slice)
}
fn index(v: &Value, key: &str) -> Option<usize> {
v.get(key)?.as_u64().map(usize_of)
}
fn floats<const N: usize>(v: Option<&Value>, default: [f64; N]) -> [f64; N] {
let mut out = default;
if let Some(a) = v.and_then(Value::as_array).filter(|a| a.len() == N) {
for (o, x) in out.iter_mut().zip(a) {
*o = x.as_f64().unwrap_or(*o);
}
}
out
}
fn drawable(extension: &str) -> bool {
const DECODED: &[&str] = &[
"KHR_draco_mesh_compression",
"EXT_meshopt_compression",
"KHR_meshopt_compression",
"KHR_mesh_quantization",
"EXT_mesh_gpu_instancing",
"KHR_animation_pointer",
"KHR_node_visibility",
"KHR_gaussian_splatting",
];
const LOOKS: &[&str] = &[
"VRMC_",
"KHR_materials_",
"KHR_texture_",
"EXT_texture_",
"KHR_lights_",
"KHR_xmp",
"EXT_materials_",
"EXT_lights_",
"MSFT_texture_",
"MSFT_packing_",
"ADOBE_materials_",
"KHR_technique",
];
const ALSO: &[&str] = &["MSFT_lod", "VRM"];
DECODED.contains(&extension) || ALSO.contains(&extension) || LOOKS.iter().any(|p| extension.starts_with(p))
}
fn f32s<const N: usize>(v: Option<&Value>, default: [f32; N]) -> [f32; N] {
floats(v, default.map(f64::from)).map(f32_of)
}
impl<'d> Reader<'d> {
#[allow(clippy::unnecessary_wraps, reason = "it fails without the draco feature")]
fn new(doc: &'d Document) -> Result<Self> {
let json = &doc.json;
#[cfg(not(feature = "draco"))]
ensure!(
!list(json, "extensionsRequired")
.iter()
.any(|e| e.as_str() == Some("KHR_draco_mesh_compression")),
FeatureDisabled,
"glTF uses Draco compression, which needs Polysmelt's `draco` feature"
);
if let Some(e) = list(json, "extensionsRequired")
.iter()
.filter_map(Value::as_str)
.find(|e| !drawable(e))
{
bail!(
Unsupported,
"glTF requires the `{e}` extension, which polysmelt can't draw"
);
}
Ok(Self::bare(doc))
}
fn bare(doc: &'d Document) -> Self {
let json = &doc.json;
let n = |key| list(json, key).len();
Self {
doc,
json,
accessors: list(json, "accessors"),
views: list(json, "bufferViews"),
meshes: list(json, "meshes"),
nodes: list(json, "nodes"),
materials_json: list(json, "materials"),
textures: list(json, "textures"),
samplers: list(json, "samplers"),
images_json: list(json, "images"),
scratch: Vec::new(),
meshopt: HashMap::new(),
materials: vec![None; n("materials")],
default_material: None,
images: vec![None; n("images")],
world: Vec::new(),
posed: vec![Posed::default(); n("nodes")],
bind: false,
bound: Vec::new(),
budget: doc.max_triangles,
point_budget: doc.max_points,
max_vertices: doc.max_triangles.saturating_mul(3).max(if doc.lines_and_points {
doc.max_points
} else {
0
}),
draco: Vec::new(),
}
}
fn get(&self, key: &str, i: usize) -> Result<&'d Value> {
let all = match key {
"accessors" => self.accessors,
"bufferViews" => self.views,
"meshes" => self.meshes,
_ => list(self.json, key),
};
all.get(i)
.with_context(ErrorKind::Corrupt, || format!("glTF {key}[{i}] doesn't exist"))
}
fn view(&self, i: usize) -> Result<(usize, u64, u64, Option<usize>, bool)> {
let v = self.get("bufferViews", i)?;
let (mut buffer, mut offset, mut len, mut stride, mut ext) = (None, None, None, None, false);
for (k, x) in v.as_object().into_iter().flatten() {
match k.as_str() {
"buffer" => buffer = Some(x),
"byteOffset" => offset = Some(x),
"byteLength" => len = Some(x),
"byteStride" => stride = Some(x),
"extensions" => ext = true,
_ => {}
}
}
let buffer = buffer
.and_then(Value::as_u64)
.map(usize_of)
.context(ErrorKind::Corrupt, "glTF buffer view has no buffer")?;
let offset = offset.and_then(Value::as_u64).unwrap_or(0);
let len = len
.and_then(Value::as_u64)
.context(ErrorKind::Corrupt, "glTF buffer view has no byteLength")?;
Ok((buffer, offset, len, stride.and_then(Value::as_u64).map(usize_of), ext))
}
fn meshopt(&mut self, i: usize) -> Result<bool> {
if self.meshopt.contains_key(&i) {
return Ok(true);
}
let v = self.get("bufferViews", i)?;
let Some((name, ext)) = container::meshopt_of(v) else {
return Ok(false);
};
let len = usize_of(v.get("byteLength").and_then(Value::as_u64).unwrap_or(0));
let b = ext
.get("buffer")
.and_then(Value::as_u64)
.map(usize_of)
.with_context(ErrorKind::Corrupt, || {
format!("glTF buffer view {i}: {name} names no buffer")
})?;
let off = ext.get("byteOffset").and_then(Value::as_u64).unwrap_or(0);
let src_len = ext
.get("byteLength")
.and_then(Value::as_u64)
.with_context(ErrorKind::Corrupt, || {
format!("glTF buffer view {i}: {name} has no byteLength")
})?;
let blob = self.doc.buffers.get(b).with_context(ErrorKind::Corrupt, || {
format!("glTF buffer view {i}: {name} names missing buffer {b}")
})?;
let held;
let src = if let Some(s) = blob.slice(off, src_len) {
s
} else {
ensure!(
off.saturating_add(src_len) <= blob.len(),
Truncated,
"glTF buffer view {i}: {name} data runs past the end of its buffer"
);
let mut v = vec![0; usize_of(src_len)];
blob.read_at(off, &mut v)?;
held = v;
&held
};
ensure!(
len / 1024 <= src.len(),
Truncated,
"glTF buffer view {i}: {name} data is cut short"
);
let mut out = vec![0; len];
container::decode_view(ext, src, &mut out).with_within(|| format!("glTF buffer view {i}"))?;
self.meshopt.insert(i, out);
Ok(true)
}
fn view_shared(&mut self, i: usize) -> Result<crate::Bytes> {
if self.meshopt(i)? {
return Ok(crate::Bytes::copy_from_slice(&self.meshopt[&i]));
}
let (buffer, offset, len, _, _) = self.view(i)?;
let b = self
.doc
.buffers
.get(buffer)
.context(ErrorKind::Corrupt, "glTF buffer view names a missing buffer")?;
ensure!(
offset.saturating_add(len) <= b.len(),
Truncated,
"glTF buffer view runs past the end of its buffer"
);
usize::try_from(len).map_err(|_| err!(LimitExceeded, "glTF buffer view is too big to hold"))?;
b.shared(offset, len)
}
fn acc(&self, i: usize) -> Result<Acc> {
if let Some((_, bytes, ctype, n)) = self.draco.iter().find(|d| d.0 == i) {
let count = bytes.len() / (n * 4);
return Ok(Acc {
view: None,
offset: 0,
count,
ctype: *ctype,
n: *n,
rows: *n,
normalized: false,
sparse: None,
decoded: Some(bytes.clone()),
});
}
let a = self.get("accessors", i)?;
let (mut ctype, mut kind, mut view, mut offset, mut count, mut normalized, mut sparse) =
(None, None, None, None, None, None, None);
for (k, x) in a.as_object().into_iter().flatten() {
match k.as_str() {
"componentType" => ctype = Some(x),
"type" => kind = Some(x),
"bufferView" => view = Some(x),
"byteOffset" => offset = Some(x),
"count" => count = Some(x),
"normalized" => normalized = Some(x),
"sparse" => sparse = Some(x),
_ => {}
}
}
let ctype = ctype.and_then(Value::as_u64).unwrap_or(0);
ensure!(
matches!(ctype, 5120..=5123 | 5125 | 5126),
Corrupt,
"glTF accessor {i} has component type {ctype}"
);
let (n, rows) = match kind.and_then(Value::as_str) {
Some("SCALAR") => (1, 1),
Some("VEC2") => (2, 2),
Some("VEC3") => (3, 3),
Some("VEC4") => (4, 4),
Some("MAT2") => (4, 2),
Some("MAT3") => (9, 3),
Some("MAT4") => (16, 4),
t => bail!(Corrupt, "glTF accessor {i} has type {t:?}, not one read here"),
};
let u = |x: Option<&Value>| x.and_then(Value::as_u64).map(usize_of);
Ok(Acc {
view: u(view),
offset: offset.and_then(Value::as_u64).unwrap_or(0),
count: u(count).context(ErrorKind::Corrupt, "glTF accessor has no count")?,
ctype,
n,
rows,
normalized: normalized.and_then(Value::as_bool).unwrap_or(false),
sparse: sparse.cloned(),
decoded: None,
})
}
fn blocks(&mut self, a: &Acc, mut f: impl FnMut(usize, &[u8], usize)) -> Result<()> {
if let Some(bytes) = &a.decoded {
f(0, bytes, a.esize());
return Ok(());
}
let Some(view) = a.view else {
ensure!(
a.count <= self.max_vertices,
LimitExceeded,
"glTF accessor counts {} elements",
a.count
);
return Ok(());
};
if a.count == 0 {
return Ok(());
}
let (buffer, voff, vlen, stride, ext) = self.view(view)?;
let esize = a.esize();
let stride = stride.filter(|&s| s > 0).unwrap_or(esize);
ensure!(
stride >= esize,
Corrupt,
"glTF buffer view stride {stride} is under an element's {esize} bytes"
);
let span = (a.count as u64 - 1)
.checked_mul(stride as u64)
.and_then(|s| s.checked_add(esize as u64))
.filter(|&s| a.offset.checked_add(s).is_some_and(|end| end <= vlen))
.with_context(ErrorKind::Corrupt, || {
format!(
"glTF accessor's {} elements run past the end of their buffer view",
a.count
)
})?;
if ext && self.meshopt(view)? {
let bytes = &self.meshopt[&view];
let s = bytes.get(usize_of(a.offset)..usize_of(a.offset + span)).context(
ErrorKind::Corrupt,
"glTF accessor runs past the end of its decoded buffer view",
)?;
f(0, s, stride);
return Ok(());
}
let start = voff + a.offset;
let b = self
.doc
.buffers
.get(buffer)
.context(ErrorKind::Corrupt, "glTF buffer view names a missing buffer")?;
ensure!(
start + span <= b.len(),
Truncated,
"glTF accessor runs past the end of its buffer: bytes {start}..{} of buffer {buffer}, which holds {}",
start + span,
b.len()
);
if let Some(s) = b.slice(start, span) {
f(0, s, stride);
return Ok(());
}
let mut scratch = std::mem::take(&mut self.scratch);
let per = (BLOCK / stride).max(1);
let mut i = 0;
while i < a.count {
let k = per.min(a.count - i);
let len = (k - 1) * stride + esize;
if scratch.len() < len {
scratch.resize(len, 0);
}
b.read_at(start + (i * stride) as u64, &mut scratch[..len])?;
f(i, &scratch[..len], stride);
i += k;
}
self.scratch = scratch;
Ok(())
}
fn read<const N: usize>(&mut self, i: usize, fill: [f32; N]) -> Result<Vec<[f32; N]>> {
let a = self.acc(i)?;
self.read_acc(&a, fill)
}
fn read_acc<const N: usize>(&mut self, a: &Acc, fill: [f32; N]) -> Result<Vec<[f32; N]>> {
let mut zero = fill;
for z in zero.iter_mut().take(a.n) {
*z = 0.0;
}
if a.view.is_none() {
ensure!(
a.count <= self.max_vertices,
LimitExceeded,
"glTF accessor counts {} elements",
a.count
);
}
let count = a.count;
let mut out = Vec::new();
out.try_reserve_exact(count)
.map_err(|_| err!(LimitExceeded, "glTF accessor counts {count} elements"))?;
let m = a.n.min(N);
let cs = a.csize();
let (ctype, normalized) = (a.ctype, a.normalized);
let offsets: Option<Vec<usize>> = a.padded().then(|| (0..m).map(|c| a.at(c)).collect());
self.blocks(a, |first, bytes, stride| {
if out.len() < first {
out.resize(first.min(count), zero);
}
let room = count - out.len();
let bytes = &bytes[..bytes.len().min(room.saturating_mul(stride))];
if let Some(offsets) = &offsets {
out.extend(bytes.chunks(stride).map(|e| {
let mut o = zero;
for (x, &at) in o.iter_mut().zip(offsets) {
*x = component(ctype, normalized, &e[at..]);
}
o
}));
return;
}
if ctype == 5126 && m == N && stride == 4 * N {
out.extend(bytes.chunks_exact(4 * N).map(|e| {
std::array::from_fn(|c| f32::from_le_bytes([e[4 * c], e[4 * c + 1], e[4 * c + 2], e[4 * c + 3]]))
}));
return;
}
let o = &mut out;
match (ctype, normalized) {
(5126, _) => put(o, bytes, stride, cs, m, zero, |b| {
f32::from_le_bytes([b[0], b[1], b[2], b[3]])
}),
(5121, true) => put(o, bytes, stride, cs, m, zero, |b| f32::from(b[0]) / 255.0),
(5121, false) => put(o, bytes, stride, cs, m, zero, |b| f32::from(b[0])),
(5120, true) => put(o, bytes, stride, cs, m, zero, |b| {
(f32::from(b[0].cast_signed()) / 127.0).max(-1.0)
}),
(5120, false) => put(o, bytes, stride, cs, m, zero, |b| f32::from(b[0].cast_signed())),
(5123, true) => put(o, bytes, stride, cs, m, zero, |b| {
f32::from(u16::from_le_bytes([b[0], b[1]])) / 65535.0
}),
(5123, false) => put(o, bytes, stride, cs, m, zero, |b| {
f32::from(u16::from_le_bytes([b[0], b[1]]))
}),
(5122, true) => put(o, bytes, stride, cs, m, zero, |b| {
(f32::from(i16::from_le_bytes([b[0], b[1]])) / 32767.0).max(-1.0)
}),
(5122, false) => put(o, bytes, stride, cs, m, zero, |b| {
f32::from(i16::from_le_bytes([b[0], b[1]]))
}),
#[allow(
clippy::cast_precision_loss,
reason = "past 2^24 an f32 attribute rounds, as in any viewer"
)]
_ => put(o, bytes, stride, cs, m, zero, |b| {
u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f32
}),
}
})?;
out.resize(count, zero);
if let Some(s) = a.sparse.clone() {
let (at, values) = self.sparse(a, &s)?;
let values = self.read_acc(&values, fill)?;
for (k, v) in at.into_iter().zip(values) {
if let Some(o) = out.get_mut(k as usize) {
*o = v;
}
}
}
Ok(out)
}
fn draco_holding(&mut self, i: usize) -> Result<()> {
if self.draco.iter().any(|d| d.0 == i) || self.get("accessors", i)?.get("bufferView").is_some() {
return Ok(());
}
let meshes = self.meshes;
let found = meshes.iter().flat_map(|m| items(m, "primitives")).find_map(|p| {
let ext = p.get("extensions")?.get("KHR_draco_mesh_compression")?;
let attrs = p.get("attributes")?;
let names = ext.get("attributes").and_then(Value::as_object);
let holds = index(p, "indices") == Some(i)
|| names
.into_iter()
.flat_map(|n| n.keys())
.any(|n| index(attrs, n) == Some(i));
holds.then(|| (ext, attrs, index(p, "indices")))
});
let Some((ext, attrs, indices)) = found else {
return Ok(());
};
#[cfg(feature = "draco")]
{
self.decode_draco(ext, attrs, indices)
}
#[cfg(not(feature = "draco"))]
{
let _ = (ext, attrs, indices);
bail!(
FeatureDisabled,
"glTF accessor {i} is Draco-compressed, which needs Polysmelt's `draco` feature"
)
}
}
fn read_indices(&mut self, i: usize) -> Result<Vec<u32>> {
let a = self.acc(i)?;
self.read_index_acc(i, &a)
}
fn read_index_acc(&mut self, i: usize, a: &Acc) -> Result<Vec<u32>> {
ensure!(
a.n == 1 && matches!(a.ctype, 5121 | 5123 | 5125),
Corrupt,
"glTF index accessor {i} isn't unsigned integer scalars"
);
self.read_u32s(a)
}
fn read_u32s(&mut self, a: &Acc) -> Result<Vec<u32>> {
let count = a.count;
let mut out = Vec::new();
out.try_reserve_exact(count)
.map_err(|_| err!(LimitExceeded, "glTF accessor counts {count} elements"))?;
let ctype = a.ctype;
self.blocks(a, |first, bytes, stride| {
if out.len() < first {
out.resize(first.min(count), 0);
}
let room = count - out.len();
let bytes = &bytes[..bytes.len().min(room.saturating_mul(stride))];
let need = match ctype {
5121 => 1,
5123 => 2,
_ => 4,
};
let whole = |e: &&[u8]| e.len() >= need;
match ctype {
5121 => out.extend(bytes.chunks(stride).map(|e| u32::from(e[0]))),
5123 if stride == 2 => out.extend(
bytes
.chunks_exact(2)
.map(|e| u32::from(u16::from_le_bytes([e[0], e[1]]))),
),
5125 if stride == 4 => out.extend(
bytes
.chunks_exact(4)
.map(|e| u32::from_le_bytes([e[0], e[1], e[2], e[3]])),
),
5123 => out.extend(
bytes
.chunks(stride)
.filter(whole)
.map(|e| u32::from(u16::from_le_bytes([e[0], e[1]]))),
),
_ => out.extend(
bytes
.chunks(stride)
.filter(whole)
.map(|e| u32::from_le_bytes([e[0], e[1], e[2], e[3]])),
),
}
})?;
out.resize(count, 0);
if let Some(s) = a.sparse.clone() {
let (at, values) = self.sparse(a, &s)?;
let values = self.read_u32s(&values)?;
for (k, v) in at.into_iter().zip(values) {
if let Some(o) = out.get_mut(k as usize) {
*o = v;
}
}
}
Ok(out)
}
fn sparse(&mut self, a: &Acc, s: &Value) -> Result<(Vec<u32>, Acc)> {
let count = index(s, "count").context(ErrorKind::Corrupt, "glTF sparse accessor has no count")?;
ensure!(
count <= a.count,
Corrupt,
"glTF sparse accessor replaces more elements than it has"
);
let part = |key: &str| -> Result<(usize, u64)> {
let p = s
.get(key)
.with_context(ErrorKind::Corrupt, || format!("glTF sparse accessor has no {key}"))?;
Ok((
index(p, "bufferView")
.with_context(ErrorKind::Corrupt, || format!("glTF sparse {key} has no bufferView"))?,
p.get("byteOffset").and_then(Value::as_u64).unwrap_or(0),
))
};
let (iv, ioff) = part("indices")?;
let ictype = s["indices"].get("componentType").and_then(Value::as_u64).unwrap_or(0);
ensure!(
matches!(ictype, 5121 | 5123 | 5125),
Corrupt,
"glTF sparse indices' component type {ictype}"
);
let idx = Acc {
view: Some(iv),
offset: ioff,
count,
ctype: ictype,
n: 1,
rows: 1,
normalized: false,
sparse: None,
decoded: None,
};
let at = self.read_u32s(&idx)?;
let (vv, voff) = part("values")?;
let values = Acc {
view: Some(vv),
offset: voff,
sparse: None,
decoded: None,
count,
..a.clone()
};
Ok((at, values))
}
fn uses(&mut self) -> Result<Vec<Use>> {
let nodes = self.nodes;
let views: Vec<NodeView<'_>> = nodes.iter().map(NodeView::of).collect();
let mut world = vec![None::<M4>; nodes.len()];
let mut child = vec![false; nodes.len()];
for v in &views {
for c in v.children() {
if let Some(f) = child.get_mut(c) {
*f = true;
}
}
}
let mut stack: Vec<(usize, M4)> = (0..nodes.len()).filter(|&i| !child[i]).map(|i| (i, IDENTITY)).collect();
while let Some((i, parent)) = stack.pop() {
if world[i].is_some() {
continue;
}
let m = mul(&parent, &self.local(i, &views[i]));
world[i] = Some(m);
for c in views[i].children() {
if c < nodes.len() && world[c].is_none() {
stack.push((c, m));
}
}
}
let Some(order) = scene_order(self.json)? else {
return Ok((0..self.meshes.len())
.map(|mesh| Use {
mesh,
weights: Vec::new(),
skin: None,
transforms: vec![IDENTITY],
})
.collect());
};
let mut uses: Vec<Use> = Vec::new();
let mut seen: HashMap<(usize, Option<usize>, Vec<u32>), usize> = HashMap::new();
for i in order {
let n = &views[i];
let Some(mesh) = n.mesh.and_then(Value::as_u64).map(usize_of) else {
continue;
};
let skin = n.skin.and_then(Value::as_u64).map(usize_of);
let weights = match &self.posed[i].weights {
Some(w) => w.clone(),
None => weights_of(
n.weights
.or_else(|| self.meshes.get(mesh).and_then(|m| m.get("weights"))),
),
};
let w = world[i].unwrap_or(IDENTITY);
let transforms = if skin.is_some() {
vec![IDENTITY]
} else {
self.instances(n.extensions, &w)?
};
let key = (mesh, skin, weights.iter().map(|x| x.to_bits()).collect());
match seen.get(&key) {
Some(_) if skin.is_some() => {}
Some(&u) => uses[u].transforms.extend(transforms),
None => {
seen.insert(key, uses.len());
uses.push(Use {
mesh,
weights,
skin,
transforms,
});
}
}
}
self.world = world.into_iter().map(|w| w.unwrap_or(IDENTITY)).collect();
Ok(uses)
}
fn instances(&mut self, extensions: Option<&Value>, world: &M4) -> Result<Vec<M4>> {
let Some(attrs) = extensions
.and_then(|e| e.get("EXT_mesh_gpu_instancing"))
.and_then(|e| e.get("attributes"))
else {
return Ok(vec![*world]);
};
let get = |k: &str| index(attrs, k);
let t = get("TRANSLATION").map(|a| self.read(a, [0.0; 3])).transpose()?;
let r = get("ROTATION")
.map(|a| self.read(a, [0.0, 0.0, 0.0, 1.0]))
.transpose()?;
let s = get("SCALE").map(|a| self.read(a, [1.0; 3])).transpose()?;
let count = [
t.as_ref().map(Vec::len),
r.as_ref().map(Vec::len),
s.as_ref().map(Vec::len),
]
.into_iter()
.flatten()
.min()
.unwrap_or(0);
ensure!(
count <= self.doc.max_triangles,
LimitExceeded,
"glTF node has {count} instances"
);
Ok((0..count)
.map(|k| {
let t = t.as_ref().map_or([0.0; 3], |v| v[k].map(f64::from));
let r = r.as_ref().map_or([0.0, 0.0, 0.0, 1.0], |v| v[k].map(f64::from));
let s = s.as_ref().map_or([1.0; 3], |v| v[k].map(f64::from));
mul(world, &trs(t, r, s))
})
.collect())
}
}
fn ids<'v>(v: &'v Value, key: &str) -> impl Iterator<Item = usize> + 'v {
items(v, key).iter().filter_map(Value::as_u64).map(usize_of)
}
fn list_of(v: &Value, key: &str) -> Vec<usize> {
v.get(key)
.and_then(Value::as_array)
.map(|a| a.iter().filter_map(Value::as_u64).map(usize_of).collect())
.unwrap_or_default()
}
#[inline]
fn put<const N: usize>(
out: &mut Vec<[f32; N]>,
bytes: &[u8],
stride: usize,
csize: usize,
m: usize,
zero: [f32; N],
conv: impl Fn(&[u8]) -> f32,
) {
if m == N && stride == N * csize {
let elements = bytes.chunks_exact(stride);
let short = !elements.remainder().is_empty();
out.extend(elements.map(|e| std::array::from_fn(|c| conv(&e[c * csize..]))));
if short {
out.push(zero);
}
return;
}
if m == N {
out.extend(bytes.chunks(stride).map(|e| {
if e.len() >= N * csize {
std::array::from_fn(|c| conv(&e[c * csize..]))
} else {
zero
}
}));
return;
}
out.extend(bytes.chunks(stride).map(|e| {
let mut o = zero;
if e.len() >= m * csize {
for (c, x) in o.iter_mut().enumerate().take(m) {
*x = conv(&e[c * csize..]);
}
}
o
}));
}
pub(crate) fn mul(a: &M4, b: &M4) -> M4 {
let mut r = [0.0; 16];
for c in 0..4 {
for row in 0..4 {
r[c * 4 + row] = (0..4).map(|k| a[k * 4 + row] * b[c * 4 + k]).sum();
}
}
r
}
pub(crate) fn trs(t: [f64; 3], q: [f64; 4], s: [f64; 3]) -> M4 {
let len = (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]).sqrt();
let [x, y, z, w] = if len > 0.0 {
q.map(|c| c / len)
} else {
[0.0, 0.0, 0.0, 1.0]
};
let r = [
[
1.0 - 2.0 * (y * y + z * z),
2.0 * (x * y - z * w),
2.0 * (x * z + y * w),
],
[
2.0 * (x * y + z * w),
1.0 - 2.0 * (x * x + z * z),
2.0 * (y * z - x * w),
],
[
2.0 * (x * z - y * w),
2.0 * (y * z + x * w),
1.0 - 2.0 * (x * x + y * y),
],
];
let mut m = IDENTITY;
for col in 0..3 {
for row in 0..3 {
m[col * 4 + row] = r[row][col] * s[col];
}
}
m[12..15].copy_from_slice(&t);
m
}
pub(crate) fn affine_inverse(m: &M4) -> Option<M4> {
let det = det3(m);
if !(det.abs() > 1e-30 && det.is_finite()) {
return None;
}
let a = |r: usize, c: usize| m[c * 4 + r];
let mut out = IDENTITY;
for r in 0..3 {
for c in 0..3 {
let (r1, r2) = ((r + 1) % 3, (r + 2) % 3);
let (c1, c2) = ((c + 1) % 3, (c + 2) % 3);
out[r * 4 + c] = (a(r1, c1) * a(r2, c2) - a(r1, c2) * a(r2, c1)) / det;
}
}
for r in 0..3 {
out[12 + r] = -(0..3).map(|k| out[k * 4 + r] * m[12 + k]).sum::<f64>();
}
Some(out)
}
pub(crate) fn decompose(m: &M4) -> ([f64; 3], [f64; 4], [f64; 3]) {
let t = [m[12], m[13], m[14]];
let col = |c: usize| [m[c * 4], m[c * 4 + 1], m[c * 4 + 2]];
let len = |v: [f64; 3]| (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt();
let mut s = [len(col(0)), len(col(1)), len(col(2))];
if det3(m) < 0.0 {
s[0] = -s[0];
}
if s.iter().any(|&x| !(x.abs() > 1e-30 && x.is_finite())) {
return (t, [0.0, 0.0, 0.0, 1.0], s.map(|x| if x.is_finite() { x } else { 0.0 }));
}
let r = |row: usize, c: usize| m[c * 4 + row] / s[c];
let trace = r(0, 0) + r(1, 1) + r(2, 2);
let q = if trace > 0.0 {
let k = 0.5 / (trace + 1.0).sqrt();
[
(r(2, 1) - r(1, 2)) * k,
(r(0, 2) - r(2, 0)) * k,
(r(1, 0) - r(0, 1)) * k,
0.25 / k,
]
} else if r(0, 0) > r(1, 1) && r(0, 0) > r(2, 2) {
let k = 2.0 * (1.0 + r(0, 0) - r(1, 1) - r(2, 2)).max(0.0).sqrt();
[
0.25 * k,
(r(0, 1) + r(1, 0)) / k,
(r(0, 2) + r(2, 0)) / k,
(r(2, 1) - r(1, 2)) / k,
]
} else if r(1, 1) > r(2, 2) {
let k = 2.0 * (1.0 + r(1, 1) - r(0, 0) - r(2, 2)).max(0.0).sqrt();
[
(r(0, 1) + r(1, 0)) / k,
0.25 * k,
(r(1, 2) + r(2, 1)) / k,
(r(0, 2) - r(2, 0)) / k,
]
} else {
let k = 2.0 * (1.0 + r(2, 2) - r(0, 0) - r(1, 1)).max(0.0).sqrt();
[
(r(0, 2) + r(2, 0)) / k,
(r(1, 2) + r(2, 1)) / k,
0.25 * k,
(r(1, 0) - r(0, 1)) / k,
]
};
let n = q.iter().map(|c| c * c).sum::<f64>().sqrt();
let q = if n > 0.0 && n.is_finite() {
q.map(|c| c / n)
} else {
[0.0, 0.0, 0.0, 1.0]
};
(t, q, s)
}
fn det3(m: &M4) -> f64 {
m[0] * (m[5] * m[10] - m[9] * m[6]) - m[4] * (m[1] * m[10] - m[9] * m[2]) + m[8] * (m[1] * m[6] - m[5] * m[2])
}
fn normal_matrix(m: &M4) -> [[f64; 3]; 3] {
let a = |r: usize, c: usize| m[c * 4 + r];
let sign = if det3(m) < 0.0 { -1.0 } else { 1.0 };
let mut n = [[0.0; 3]; 3];
for (r, row) in n.iter_mut().enumerate() {
for (c, x) in row.iter_mut().enumerate() {
let (r1, r2) = ((r + 1) % 3, (r + 2) % 3);
let (c1, c2) = ((c + 1) % 3, (c + 2) % 3);
*x = sign * (a(r1, c1) * a(r2, c2) - a(r1, c2) * a(r2, c1));
}
}
n
}
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]) {
let len = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt();
if len > 0.0 {
for c in v.iter_mut() {
*c /= len;
}
} else {
*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)
}
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
}
}
}
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]>,
}
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<'_> {
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))
}
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, mesh);
self.charge(&g.draws, u.transforms.len());
mesh.prims.push(place(g, &u.transforms, material));
Ok(())
}
#[cfg(feature = "draco")]
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 mut out = Skinned {
strongest: Vec::with_capacity(n),
joints: Vec::with_capacity(n),
weights: Vec::with_capacity(n),
};
#[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)
}
fn uv_set(&self, material: Option<usize>) -> (u64, Option<[f32; 6]>) {
material
.and_then(|m| self.materials_json.get(m))
.and_then(color_texture)
.map_or((0, None), mapping)
}
fn material(&mut self, i: Option<usize>, mesh: &mut Mesh) -> usize {
let slot = match i {
Some(i) if i < self.materials.len() => self.materials[i],
_ => self.default_material,
};
if let Some(s) = slot {
return s;
}
let mut m = Material {
color: [1.0; 4],
metallic: 1.0,
roughness: 1.0,
..Material::plain("", [1.0; 4])
};
if let Some(d) = i.and_then(|i| self.materials_json.get(i)) {
d.get("name")
.and_then(Value::as_str)
.unwrap_or("")
.clone_into(&mut m.name);
let ext = d.get("extensions");
let sg = ext.and_then(|e| e.get("KHR_materials_pbrSpecularGlossiness"));
match (d.get("pbrMetallicRoughness"), sg) {
(None, Some(sg)) => {
m.color = f32s(sg.get("diffuseFactor"), [1.0; 4]);
m.metallic = 0.0;
m.roughness = 1.0 - f32_of(sg.get("glossinessFactor").and_then(Value::as_f64).unwrap_or(1.0));
}
(pbr, _) => {
let pbr = pbr.unwrap_or(&Value::Null);
m.color = f32s(pbr.get("baseColorFactor"), [1.0; 4]);
m.metallic = f32_of(pbr.get("metallicFactor").and_then(Value::as_f64).unwrap_or(1.0));
m.roughness = f32_of(pbr.get("roughnessFactor").and_then(Value::as_f64).unwrap_or(1.0));
}
}
let strength = ext
.and_then(|e| at(e, &["KHR_materials_emissive_strength", "emissiveStrength"]))
.and_then(Value::as_f64)
.map_or(1.0, f32_of);
m.emissive = f32s(d.get("emissiveFactor"), [0.0; 3]).map(|e| e * strength);
m.double_sided = d.get("doubleSided").and_then(Value::as_bool).unwrap_or(false);
match d.get("alphaMode").and_then(Value::as_str) {
Some("BLEND") => {}
Some("MASK") => {
let cutoff = d.get("alphaCutoff").and_then(Value::as_f64).map_or(0.5, f32_of);
m.alpha_cutoff = Some(if cutoff.is_finite() { cutoff.max(0.0) } else { 0.5 });
}
_ => m.color[3] = 1.0,
}
m.unlit = ext.and_then(|e| e.get("KHR_materials_unlit")).is_some();
let color = color_texture(d);
let uv = color.map_or((0, None), mapping);
let mut take = |info: Option<&Value>| self.texture_info(info?, uv, mesh);
let mut slot = |info: Option<&Value>, image: &mut Option<usize>, sampling: &mut Sampling| {
if let Some(t) = take(info) {
*image = Some(t.image);
*sampling = t.sampling;
}
};
let s = &mut m.sampling;
slot(color, &mut m.color_image, &mut s.color);
slot(d.get("normalTexture"), &mut m.normal_image, &mut s.normal);
slot(d.get("occlusionTexture"), &mut m.occlusion_image, &mut s.occlusion);
slot(d.get("emissiveTexture"), &mut m.emissive_image, &mut s.emissive);
slot(
at(d, &["pbrMetallicRoughness", "metallicRoughnessTexture"]),
&mut m.metallic_roughness_image,
&mut s.metallic_roughness,
);
if m.normal_image.is_some() {
m.normal_scale = at(d, &["normalTexture", "scale"])
.and_then(Value::as_f64)
.map_or(1.0, f32_of);
}
if m.occlusion_image.is_some() {
m.occlusion_strength = at(d, &["occlusionTexture", "strength"])
.and_then(Value::as_f64)
.map_or(1.0, f32_of);
}
if let Some(ext) = ext {
m.extensions = self.extensions(ext, uv, mesh).map(Box::new);
}
}
mesh.materials.push(m);
let s = mesh.materials.len() - 1;
match i {
Some(i) if i < self.materials.len() => self.materials[i] = Some(s),
_ => self.default_material = Some(s),
}
s
}
fn texture_info(&mut self, info: &Value, color: (u64, Option<[f32; 6]>), mesh: &mut Mesh) -> Option<Texture> {
let t = index(info, "index")?;
let (set, own) = mapping(info);
let (uv_set, transform) = if set != color.0 {
(Some(usize::try_from(set).ok()?), uv_transform(info))
} else if color.1.is_none() {
(None, uv_transform(info))
} else if own == color.1 {
(None, None)
} else {
let relative = relative(own.unwrap_or([1.0, 0.0, 0.0, 0.0, 1.0, 0.0]), color.1?)?;
(None, Some(uv_decompose(relative)?))
};
let image = self.texture(t, mesh)?;
let sampler = self
.textures
.get(t)
.and_then(|tex| index(tex, "sampler"))
.and_then(|s| self.samplers.get(s));
let num = |k: &str| {
sampler
.and_then(|s| s.get(k))
.and_then(Value::as_u64)
.and_then(|v| u32::try_from(v).ok())
};
Some(Texture {
image,
sampling: Sampling {
uv_set,
transform: transform.filter(|x| *x != UvTransform::IDENTITY),
wrap: [num("wrapS").unwrap_or(10497), num("wrapT").unwrap_or(10497)],
filter: [num("magFilter"), num("minFilter")],
},
})
}
fn extensions(&mut self, ext: &Value, uv: (u64, Option<[f32; 6]>), mesh: &mut Mesh) -> Option<Extensions> {
let num = |e: &Value, k: &str, default: f32| e.get(k).and_then(Value::as_f64).map_or(default, f32_of);
let mut tex = |e: &Value, k: &str| e.get(k).and_then(|info| self.texture_info(info, uv, mesh));
let get = |name: &str| ext.get(name).filter(|e| e.is_object());
let mut out = Extensions {
ior: get("KHR_materials_ior").map(|e| num(e, "ior", 1.5)),
dispersion: get("KHR_materials_dispersion").map(|e| num(e, "dispersion", 0.0)),
..Extensions::default()
};
if let Some(e) = get("KHR_materials_specular") {
out.specular = Some(Specular {
factor: Factor {
factor: num(e, "specularFactor", 1.0),
tex: tex(e, "specularTexture"),
},
color: f32s(e.get("specularColorFactor"), [1.0; 3]),
color_tex: tex(e, "specularColorTexture"),
});
}
if let Some(e) = get("KHR_materials_transmission") {
out.transmission = Some(Factor {
factor: num(e, "transmissionFactor", 0.0),
tex: tex(e, "transmissionTexture"),
});
}
if let Some(e) = get("KHR_materials_clearcoat") {
out.clearcoat = Some(Clearcoat {
factor: Factor {
factor: num(e, "clearcoatFactor", 0.0),
tex: tex(e, "clearcoatTexture"),
},
roughness: Factor {
factor: num(e, "clearcoatRoughnessFactor", 0.0),
tex: tex(e, "clearcoatRoughnessTexture"),
},
normal: tex(e, "clearcoatNormalTexture"),
});
}
if let Some(e) = get("KHR_materials_sheen") {
out.sheen = Some(Sheen {
color: f32s(e.get("sheenColorFactor"), [0.0; 3]),
color_tex: tex(e, "sheenColorTexture"),
roughness: Factor {
factor: num(e, "sheenRoughnessFactor", 0.0),
tex: tex(e, "sheenRoughnessTexture"),
},
});
}
if let Some(e) = get("KHR_materials_volume") {
out.volume = Some(Volume {
thickness: Factor {
factor: num(e, "thicknessFactor", 0.0),
tex: tex(e, "thicknessTexture"),
},
attenuation_distance: e
.get("attenuationDistance")
.and_then(Value::as_f64)
.map(f32_of)
.filter(|d| d.is_finite() && *d > 0.0),
attenuation_color: f32s(e.get("attenuationColor"), [1.0; 3]),
});
}
if let Some(e) = get("KHR_materials_anisotropy") {
out.anisotropy = Some(Anisotropy {
strength: Factor {
factor: num(e, "anisotropyStrength", 0.0),
tex: tex(e, "anisotropyTexture"),
},
rotation: num(e, "anisotropyRotation", 0.0),
});
}
if let Some(e) = get("KHR_materials_iridescence") {
out.iridescence = Some(Iridescence {
factor: Factor {
factor: num(e, "iridescenceFactor", 0.0),
tex: tex(e, "iridescenceTexture"),
},
ior: num(e, "iridescenceIor", 1.3),
thickness: [
num(e, "iridescenceThicknessMinimum", 100.0),
num(e, "iridescenceThicknessMaximum", 400.0),
],
thickness_tex: tex(e, "iridescenceThicknessTexture"),
});
}
(out != Extensions::default()).then_some(out)
}
fn texture(&mut self, t: usize, mesh: &mut Mesh) -> Option<usize> {
let tex = self.textures.get(t)?;
let mut sources: Vec<usize> = index(tex, "source").into_iter().collect();
if let Some(ext) = tex.get("extensions").and_then(Value::as_object) {
sources.extend(ext.values().filter_map(|e| index(e, "source")));
}
sources.into_iter().find_map(|s| self.image(s, mesh))
}
fn image(&mut self, s: usize, mesh: &mut Mesh) -> Option<usize> {
if let Some(done) = *self.images.get(s)? {
return done;
}
let img = self.images_json.get(s)?;
let name = match img.get("mimeType").and_then(Value::as_str) {
Some("image/x-tga" | "image/tga" | "image/x-targa" | "image/targa") => "image.tga",
_ => img
.get("uri")
.and_then(Value::as_str)
.filter(|u| !u.starts_with("data:"))
.unwrap_or(""),
};
let name = std::path::PathBuf::from(name);
let bytes = if let Some(v) = index(img, "bufferView") {
self.view_shared(v).ok()
} else {
img.get("uri")
.and_then(Value::as_str)
.filter(|u| u.starts_with("data:"))
.and_then(|u| container::data_uri(u).ok())
.map(|(_, b)| b.into())
};
let out = bytes.and_then(|b: crate::Bytes| Image::kept_named(b, &name)).map(|i| {
mesh.images.push(i);
mesh.images.len() - 1
});
self.images[s] = Some(out);
out
}
}
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"]
.into_iter()
.find(|&p| p == path)?;
Some((node.parse().ok()?, path))
}
fn mapping(info: &Value) -> (u64, Option<[f32; 6]>) {
let mut set = info.get("texCoord").and_then(Value::as_u64).unwrap_or(0);
let Some(t) = info.get("extensions").and_then(|e| e.get("KHR_texture_transform")) else {
return (set, None);
};
if let Some(s) = t.get("texCoord").and_then(Value::as_u64) {
set = s;
}
let [ox, oy] = f32s(t.get("offset"), [0.0; 2]);
let [sx, sy] = f32s(t.get("scale"), [1.0; 2]);
let r = f32_of(t.get("rotation").and_then(Value::as_f64).unwrap_or(0.0));
let (sin, cos) = r.sin_cos();
(set, Some([cos * sx, sin * sy, ox, -sin * sx, cos * sy, oy]))
}
fn uv_transform(info: &Value) -> Option<UvTransform> {
let t = info.get("extensions")?.get("KHR_texture_transform")?;
Some(UvTransform {
offset: f32s(t.get("offset"), [0.0; 2]),
rotation: f32_of(t.get("rotation").and_then(Value::as_f64).unwrap_or(0.0)),
scale: f32s(t.get("scale"), [1.0; 2]),
})
}
fn relative(own: [f32; 6], color: [f32; 6]) -> Option<[f32; 6]> {
let [a, b, e, c, d, f] = color.map(f64::from);
let det = a * d - b * c;
if det == 0.0 || !det.is_finite() {
return None;
}
let (ia, ib, ic, id) = (d / det, -b / det, -c / det, a / det);
let (ie, if_) = (-(ia * e + ib * f), -(ic * e + id * f));
let [p, q, s, r, t, u] = own.map(f64::from);
let out = [
p * ia + q * ic,
p * ib + q * id,
p * ie + q * if_ + s,
r * ia + t * ic,
r * ib + t * id,
r * ie + t * if_ + u,
];
#[allow(clippy::cast_possible_truncation, reason = "back to the f32s the matrices came in")]
Some(out.map(|v| v as f32))
}
fn uv_decompose(m: [f32; 6]) -> Option<UvTransform> {
let [a, b, e, c, d, f] = m;
let sx = a.hypot(c);
if sx == 0.0 || !sx.is_finite() {
return None;
}
let rotation = (-c).atan2(a);
let (sin, cos) = rotation.sin_cos();
let sy = b * sin + d * cos;
let tolerance = 1e-4 * sy.abs().max(1.0);
if (b - sin * sy).abs() > tolerance || (d - cos * sy).abs() > tolerance {
return None;
}
Some(UvTransform {
offset: [e, f],
rotation,
scale: [sx, sy],
})
}
fn color_texture(m: &Value) -> Option<&Value> {
m.get("pbrMetallicRoughness")
.and_then(|p| p.get("baseColorTexture"))
.or_else(|| {
m.get("extensions")?
.get("KHR_materials_pbrSpecularGlossiness")?
.get("diffuseTexture")
})
}
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(),
}
}
fn mode(p: &Value) -> u64 {
p.get("mode").and_then(Value::as_u64).unwrap_or(4)
}
impl Reader<'_> {
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) })
}
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" => self.read::<1>(output, [0.0])?.into_flattened(),
_ => continue,
};
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()),
_ => {}
}
}
Ok(())
}
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 => slerp(value(k), value(k + 1), s, out),
_ => {
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
}
fn slerp(a: &[f32], b: &[f32], s: f64, out: &mut [f64]) {
let mut d: f64 = a.iter().zip(b).map(|(&x, &y)| f64::from(x) * f64::from(y)).sum();
let sign = if d < 0.0 { -1.0 } else { 1.0 };
d *= sign;
let (wa, wb) = if d > 0.9995 {
(1.0 - s, s)
} else {
let theta = d.acos();
let sin = theta.sin();
(((1.0 - s) * theta).sin() / sin, (s * theta).sin() / sin)
};
for ((o, &x), &y) in out.iter_mut().zip(a).zip(b) {
*o = wa * f64::from(x) + wb * sign * f64::from(y);
}
}
#[cfg(all(test, feature = "fs"))]
#[allow(clippy::cast_precision_loss, reason = "test grids are small")]
mod tests {
use crate::assets::Folder;
use crate::document::{Keep, Pose};
use crate::mesh::{Material, Mesh, Prim};
fn grid(n: u32) -> Mesh {
let mut prims = Vec::new();
for m in 0..2u32 {
let mut p = Prim {
material: m as usize,
..Prim::default()
};
for y in 0..=n {
for x in 0..=n {
let (u, v) = (x as f32 / n as f32, y as f32 / n as f32);
p.positions.push([u + m as f32, (u * 7.0).sin() * 0.1, v]);
p.normals.push([0.0, 1.0, 0.0]);
p.uvs.push([u, v]);
p.colors.push([u, v, 0.5, 1.0]);
}
}
for y in 0..n {
for x in 0..n {
let i = y * (n + 1) + x;
p.indices
.extend_from_slice(&[i, i + n + 1, i + 1, i + 1, i + n + 1, i + n + 2]);
}
}
prims.push(p);
}
let mut red = Material::plain("Red", [1.0, 0.0, 0.0, 1.0]);
red.metallic = 0.5;
red.roughness = 0.25;
red.emissive = [0.1, 0.2, 0.3];
red.double_sided = true;
Mesh {
prims,
materials: vec![red, Material::plain("Blue", [0.0, 0.0, 1.0, 0.5])],
images: Vec::new(),
skeleton: Vec::new(),
}
}
fn triangles(p: &Prim) -> Vec<[[u32; 12]; 3]> {
let key = |i: u32| {
let i = i as usize;
let f = |v: &[f32]| v.iter().map(|c| c.to_bits()).collect::<Vec<_>>();
let mut k = [0u32; 12];
let all = [f(&p.positions[i]), f(&p.normals[i]), f(&p.uvs[i]), f(&p.colors[i])].concat();
k.copy_from_slice(&all);
k
};
let mut out: Vec<_> = p
.indices
.chunks(3)
.map(|t| {
let c = [key(t[0]), key(t[1]), key(t[2])];
let r = (0..3).min_by_key(|&s| c[s]).unwrap();
[c[r], c[(r + 1) % 3], c[(r + 2) % 3]]
})
.collect();
out.sort_unstable();
out
}
fn load(dir: &std::path::Path, name: &str, bytes: &[u8]) -> Mesh {
let path = dir.join(name);
std::fs::write(&path, bytes).unwrap();
let ext = name.rsplit('.').next().unwrap();
let doc = crate::formats::load(&path, ext, &Folder::new(dir), Keep::ALL).unwrap();
let mut memory = crate::assets::Memory::new();
for entry in std::fs::read_dir(dir).unwrap() {
let p = entry.unwrap().path();
memory.insert(&p.file_name().unwrap().to_string_lossy(), std::fs::read(&p).unwrap());
}
let in_memory = crate::formats::load_bytes(bytes, name, ext, &memory, Keep::ALL).unwrap();
assert!(
doc.to_glb().unwrap() == in_memory.to_glb().unwrap(),
"{name} reads differently from memory"
);
let mesh = doc.flatten(Pose::Rest).unwrap().unwrap();
assert!(
mesh.to_glb().unwrap() == in_memory.flatten(Pose::Rest).unwrap().unwrap().to_glb().unwrap(),
"{name} flattens differently from memory"
);
mesh
}
fn same(a: &Mesh, b: &Mesh) {
assert_eq!(a.prims.len(), b.prims.len());
for (pa, pb) in a.prims.iter().zip(&b.prims) {
assert_eq!(triangles(pa), triangles(pb));
assert_eq!(pa.material, pb.material);
}
for (ma, mb) in a.materials.iter().zip(&b.materials) {
assert_eq!(
(
&ma.name,
ma.color,
ma.emissive,
ma.metallic,
ma.roughness,
ma.double_sided
),
(
&mb.name,
mb.color,
mb.emissive,
mb.metallic,
mb.roughness,
mb.double_sided
)
);
}
}
#[test]
fn tangents_and_every_set_are_read_and_follow_the_node() {
let floats: Vec<f32> = [
&[0., 0., 0., 1., 0., 0., 0., 1., 0.][..], &[0., 0., 1., 0., 0., 1., 0., 0., 1.], &[1., 0., 0., 1., 1., 0., 0., 1., 1., 0., 0., 1.], &[0., 0., 1., 0., 0., 1.], &[0.1, 0.2, 0.3, 0.4, 0.5, 0.6], &[7., 8., 7., 8., 7., 8.], &[1., 0., 0., 1., 0., 1., 0., 1., 0., 0., 1., 1.], ]
.concat();
let bin: Vec<u8> = floats.iter().flat_map(|f| f.to_le_bytes()).collect();
let parts = [
("VEC3", 3),
("VEC3", 3),
("VEC4", 4),
("VEC2", 2),
("VEC2", 2),
("VEC2", 2),
("VEC4", 4),
];
let mut offset = 0;
let mut accessors = Vec::new();
let mut views = Vec::new();
for (i, (kind, width)) in parts.iter().enumerate() {
views.push(serde_json::json!({ "buffer": 0, "byteOffset": offset, "byteLength": 12 * width }));
let mut a = serde_json::json!({ "bufferView": i, "componentType": 5126, "count": 3, "type": kind });
if i == 0 {
a["min"] = serde_json::json!([0, 0, 0]);
a["max"] = serde_json::json!([1, 1, 0]);
}
accessors.push(a);
offset += 12 * width;
}
let gltf = serde_json::json!({
"asset": { "version": "2.0" },
"buffers": [{ "uri": "t.bin", "byteLength": bin.len() }],
"bufferViews": views,
"accessors": accessors,
"materials": [{ "pbrMetallicRoughness": { "baseColorTexture": {
"index": 0, "texCoord": 0,
"extensions": { "KHR_texture_transform": { "texCoord": 1, "scale": [2, 2] } }
} } }],
"meshes": [{ "primitives": [{ "material": 0, "attributes": {
"POSITION": 0, "NORMAL": 1, "TANGENT": 2,
"TEXCOORD_0": 3, "TEXCOORD_1": 4, "TEXCOORD_2": 5, "COLOR_1": 6, "TEXCOORD_99999999999": 5
} }] }],
"nodes": [{ "mesh": 0, "scale": [-1, 1, 1] }],
"scenes": [{ "nodes": [0] }],
});
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("t.bin"), &bin).unwrap();
let mesh = load(dir.path(), "t.gltf", gltf.to_string().as_bytes());
let p = &mesh.prims[0];
let near = |a: [f32; 2], b: [f32; 2]| (a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6;
assert!(
near(p.uvs[0], [0.2, 0.4]) && near(p.uvs[2], [1.0, 1.2]),
"set 1, transformed: {:?}",
p.uvs
);
assert_eq!(
p.uv_sets.len(),
3,
"set 0, set 1 (in `uvs`), set 2; a set number past any real one is ignored"
);
assert_eq!(
p.uv_sets[0],
[[0., 0.], [1., 0.], [0., 1.]],
"the other sets as the file has them"
);
assert_eq!(p.uv_sets[1], Vec::<[f32; 2]>::new());
assert_eq!(p.uv_sets[2], [[7., 8.]; 3]);
assert_eq!(p.colors, Vec::<[f32; 4]>::new());
assert_eq!(p.color_sets.len(), 2);
assert_eq!(p.color_sets[1][2], [0., 0., 1., 1.]);
assert_eq!(p.tangents, [[-1., 0., 0., -1.]; 3]);
assert_eq!(p.normals, [[0., 0., 1.]; 3]);
let glb = mesh.to_glb().unwrap();
let back = load(dir.path(), "back.glb", &glb);
let q = &back.prims[0];
assert_eq!(q.uvs, p.uvs);
assert_eq!(q.uv_sets, [vec![], p.uv_sets[0].clone(), p.uv_sets[2].clone()]);
assert_eq!(q.colors, p.color_sets[1]);
assert_eq!(q.tangents, p.tangents);
}
#[test]
fn a_cut_out_unlit_material_keeps_its_textures_through_a_round_trip() {
let floats: Vec<f32> = [
&[0., 0., 0., 1., 0., 0., 0., 1., 0.][..], &[0., 0., 1., 0., 0., 1.], &[0., 0., 1., 0., 0., 1.], ]
.concat();
let bin: Vec<u8> = floats.iter().flat_map(|f| f.to_le_bytes()).collect();
let gltf = serde_json::json!({
"asset": { "version": "2.0" },
"buffers": [{ "uri": "m.bin", "byteLength": bin.len() }],
"bufferViews": [
{ "buffer": 0, "byteOffset": 0, "byteLength": 36 },
{ "buffer": 0, "byteOffset": 36, "byteLength": 24 },
{ "buffer": 0, "byteOffset": 60, "byteLength": 24 },
],
"accessors": [
{ "bufferView": 0, "componentType": 5126, "count": 3, "type": "VEC3",
"min": [0, 0, 0], "max": [1, 1, 0] },
{ "bufferView": 1, "componentType": 5126, "count": 3, "type": "VEC2" },
{ "bufferView": 2, "componentType": 5126, "count": 3, "type": "VEC2" },
],
"images": [{ "uri": "a.png" }, { "uri": "b.png" }, { "uri": "c.png" }],
"textures": [{ "source": 0 }, { "source": 1 }, { "source": 2 }],
"materials": [{
"alphaMode": "MASK", "alphaCutoff": 0.3,
"pbrMetallicRoughness": {
"baseColorFactor": [1, 1, 1, 0.8],
"baseColorTexture": { "index": 0 },
"metallicRoughnessTexture": { "index": 1 },
},
"occlusionTexture": { "index": 1 },
"emissiveTexture": { "index": 2, "texCoord": 1 },
"extensions": { "KHR_materials_unlit": {} },
}],
"meshes": [{ "primitives": [{ "material": 0,
"attributes": { "POSITION": 0, "TEXCOORD_0": 1, "TEXCOORD_1": 2 } }] }],
"nodes": [{ "mesh": 0 }],
"scenes": [{ "nodes": [0] }],
});
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("m.bin"), &bin).unwrap();
for (i, name) in ["a.png", "b.png", "c.png"].into_iter().enumerate() {
let png = [
&b"\x89PNG\r\n\x1a\n"[..],
&[0, 0, 0, 13, b'I', b'H', b'D', b'R', u8::try_from(i).unwrap()],
]
.concat();
std::fs::write(dir.path().join(name), png).unwrap();
}
let mesh = load(dir.path(), "m.gltf", gltf.to_string().as_bytes());
let check = |mesh: &Mesh| {
let m = &mesh.materials[mesh.prims[0].material];
assert_eq!(m.alpha_cutoff, Some(0.3));
assert!((m.color[3] - 0.8).abs() < 1e-6, "a cut-out material keeps its alpha");
assert!(m.unlit);
let (Some(c), Some(o)) = (m.color_image, m.occlusion_image) else {
panic!("{m:?}")
};
assert_ne!(mesh.images[c], mesh.images[o]);
assert_eq!(m.metallic_roughness_image, Some(o), "one image, two textures");
let e = m.emissive_image.expect("the emissive texture on the other set");
assert!(mesh.images[e] != mesh.images[c] && mesh.images[e] != mesh.images[o]);
let set = m.sampling.emissive.uv_set.expect("its own set");
assert_eq!(mesh.prims[0].uv_sets[set], [[0., 0.], [1., 0.], [0., 1.]]);
};
check(&mesh);
let back = load(dir.path(), "back.glb", &mesh.to_glb().unwrap());
check(&back);
}
#[test]
fn an_animation_pointer_to_a_node_moves_it() {
let floats: Vec<f32> = [
&[0., 0., 0., 1., 0., 0., 0., 1., 0.][..], &[0., 1.], &[0., 0., 0., 2., 0., 0.], ]
.concat();
let bin: Vec<u8> = floats.iter().flat_map(|f| f.to_le_bytes()).collect();
let pointer = |p: &str| {
serde_json::json!({ "sampler": 0, "target": { "path": "pointer",
"extensions": { "KHR_animation_pointer": { "pointer": p } } } })
};
let gltf = serde_json::json!({
"asset": { "version": "2.0" },
"extensionsUsed": ["KHR_animation_pointer"],
"extensionsRequired": ["KHR_animation_pointer"],
"buffers": [{ "uri": "a.bin", "byteLength": bin.len() }],
"bufferViews": [
{ "buffer": 0, "byteOffset": 0, "byteLength": 36 },
{ "buffer": 0, "byteOffset": 36, "byteLength": 8 },
{ "buffer": 0, "byteOffset": 44, "byteLength": 24 },
],
"accessors": [
{ "bufferView": 0, "componentType": 5126, "count": 3, "type": "VEC3",
"min": [0, 0, 0], "max": [1, 1, 0] },
{ "bufferView": 1, "componentType": 5126, "count": 2, "type": "SCALAR", "min": [0], "max": [1] },
{ "bufferView": 2, "componentType": 5126, "count": 2, "type": "VEC3" },
],
"meshes": [{ "primitives": [{ "attributes": { "POSITION": 0 } }] }],
"nodes": [{ "mesh": 0 }],
"scenes": [{ "nodes": [0] }],
"animations": [{
"samplers": [{ "input": 1, "output": 2 }],
"channels": [pointer("/nodes/0/translation"), pointer("/materials/0/emissiveFactor"),
pointer("/nodes/00/translation"), pointer("/nodes/0/extras")],
}],
});
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("a.bin"), &bin).unwrap();
std::fs::write(dir.path().join("a.gltf"), gltf.to_string()).unwrap();
let doc =
crate::formats::load(&dir.path().join("a.gltf"), "gltf", &Folder::new(dir.path()), Keep::ALL).unwrap();
let mesh = doc.flatten(Pose::Animation { index: 0, time: 0.5 }).unwrap().unwrap();
assert_eq!(mesh.prims[0].positions[0], [1., 0., 0.]);
let rig = doc.rig().unwrap();
let ch = &rig.animations[0].channels;
assert_eq!(ch.len(), 1);
assert_eq!((ch[0].node, ch[0].property), (0, crate::rig::Property::Translation));
}
#[test]
fn reads_back_what_to_glb_writes() {
let dir = tempfile::tempdir().unwrap();
let mesh = grid(30);
let glb = mesh.to_glb().unwrap();
same(&mesh, &load(dir.path(), "plain.glb", &glb));
}
#[test]
fn streamed_glb_cut_short_is_an_error() {
let dir = tempfile::tempdir().unwrap();
let glb = grid(60).to_glb().unwrap();
assert!(glb.len() > 2 * 64 * 1024);
let path = dir.path().join("cut.glb");
std::fs::write(&path, &glb[..glb.len() - 1000]).unwrap();
let err = crate::formats::load(&path, "glb", &Folder::new(dir.path()), Keep::ALL).unwrap_err();
assert!(err.to_string().contains("declares"), "{err}");
}
#[test]
fn reads_meshopt_compressed_glb() {
let dir = tempfile::tempdir().unwrap();
let mesh = grid(30);
let packed = crate::gltf::compress(&mesh.to_glb().unwrap()).unwrap();
same(&mesh, &load(dir.path(), "packed.glb", &packed));
}
#[test]
fn decode_leaves_plain_gltf() {
let dir = tempfile::tempdir().unwrap();
let mesh = grid(30);
let packed = crate::gltf::compress(&mesh.to_glb().unwrap()).unwrap();
let path = dir.path().join("packed.glb");
std::fs::write(&path, &packed).unwrap();
let mut doc = crate::formats::load(&path, "glb", &Folder::new(dir.path()), Keep::ALL).unwrap();
assert_eq!(doc.json["buffers"].as_array().unwrap().len(), 2, "data and fallback");
let before = doc.flatten(Pose::Rest).unwrap().unwrap();
doc.decode().unwrap();
let text = serde_json::to_string(&doc.json).unwrap();
assert!(!text.contains("meshopt"), "{text}");
assert_eq!(doc.json["buffers"].as_array().unwrap().len(), 1);
let after = doc.flatten(Pose::Rest).unwrap().unwrap();
assert_eq!(before.to_glb().unwrap(), after.to_glb().unwrap());
same(&mesh, &load(dir.path(), "decoded.glb", &doc.to_glb().unwrap()));
let glb = doc.to_glb().unwrap();
doc.decode().unwrap();
assert_eq!(glb, doc.to_glb().unwrap());
}
#[test]
fn reads_accessors_matrices_padded() {
let mut bin = vec![1, 2, 0xAA, 0xAA, 3, 4, 0xAA, 0xAA];
for c in 0..3i16 {
for r in 0..3i16 {
bin.extend_from_slice(&(c * 3 + r - 4).saturating_mul(8191).to_le_bytes());
}
bin.extend_from_slice(&[0xAA, 0xAA]);
}
let floats = bin.len();
for v in [1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 9.0, 8.0, 7.0] {
bin.extend_from_slice(&v.to_le_bytes());
}
let sparse = bin.len();
bin.extend_from_slice(&[1, 0, 0, 0]);
let json = serde_json::json!({
"asset": {"version": "2.0"},
"bufferViews": [
{"buffer": 0, "byteLength": 8},
{"buffer": 0, "byteOffset": 8, "byteLength": 24},
{"buffer": 0, "byteOffset": floats, "byteLength": 24},
{"buffer": 0, "byteOffset": floats + 24, "byteLength": 12},
{"buffer": 0, "byteOffset": sparse, "byteLength": 4},
],
"accessors": [
{"bufferView": 0, "componentType": 5121, "count": 1, "type": "MAT2"},
{"bufferView": 1, "componentType": 5122, "normalized": true, "count": 1, "type": "MAT3"},
{"bufferView": 2, "componentType": 5126, "count": 2, "type": "VEC3",
"sparse": {"count": 1, "indices": {"bufferView": 4, "componentType": 5121},
"values": {"bufferView": 3}}},
{"bufferView": 4, "componentType": 5121, "count": 1, "type": "SCALAR"},
],
});
let mut doc = crate::document::Document::empty(serde_json::Map::new());
doc.json = json.as_object().unwrap().clone();
doc.push_buffer(bin);
assert_eq!(doc.read_floats(0).unwrap(), (4, vec![1.0, 2.0, 3.0, 4.0]));
let (width, m3) = doc.read_floats(1).unwrap();
assert_eq!(width, 9);
let want: Vec<f32> = (0..9).map(|k| ((k - 4) * 8191) as f32 / 32767.0).collect();
assert!(m3.iter().zip(&want).all(|(a, b)| (a - b).abs() < 1e-6), "{m3:?}");
assert_eq!(doc.read_floats(2).unwrap(), (3, vec![1.0, 2.0, 3.0, 9.0, 8.0, 7.0]));
assert_eq!(doc.read_indices(3).unwrap(), vec![1]);
assert!(doc.read_indices(0).is_err(), "a matrix isn't indices");
assert!(doc.read_floats(9).is_err());
}
#[test]
fn decode_keeps_required_extensions_it_cant_draw() {
let dir = tempfile::tempdir().unwrap();
let packed = crate::gltf::compress(&grid(10).to_glb().unwrap()).unwrap();
let path = dir.path().join("packed.glb");
std::fs::write(&path, &packed).unwrap();
let mut doc = crate::formats::load(&path, "glb", &Folder::new(dir.path()), Keep::ALL).unwrap();
for key in ["extensionsUsed", "extensionsRequired"] {
let list = doc.json.entry(key).or_insert_with(|| serde_json::json!([]));
list.as_array_mut().unwrap().push("EXT_made_up".into());
}
assert!(
doc.flatten(Pose::Rest).is_err(),
"drawing needs every required extension"
);
doc.decode().unwrap();
assert_eq!(doc.json["extensionsRequired"], serde_json::json!(["EXT_made_up"]));
assert!(!serde_json::to_string(&doc.json).unwrap().contains("meshopt"));
}
#[test]
fn decode_widens_indices_holding_their_types_largest_value() {
let dir = tempfile::tempdir().unwrap();
let mut bin = Vec::new();
for i in 0..256u16 {
for v in [f32::from(i), f32::from(i % 2), 0.0] {
bin.extend_from_slice(&v.to_le_bytes());
}
}
let index_offset = bin.len();
bin.extend_from_slice(&[0, 1, 2, 253, 254, 255, 0, 0]);
std::fs::write(dir.path().join("line.bin"), &bin).unwrap();
let json = serde_json::json!({
"asset": {"version": "2.0"},
"buffers": [{"uri": "line.bin", "byteLength": bin.len()}],
"bufferViews": [
{"buffer": 0, "byteLength": index_offset},
{"buffer": 0, "byteOffset": index_offset, "byteLength": 6},
],
"accessors": [
{"bufferView": 0, "componentType": 5126, "count": 256, "type": "VEC3",
"min": [0.0, 0.0, 0.0], "max": [255.0, 1.0, 0.0]},
{"bufferView": 1, "componentType": 5121, "count": 6, "type": "SCALAR"},
],
"meshes": [{"primitives": [{"attributes": {"POSITION": 0}, "indices": 1}]}],
"nodes": [{"mesh": 0}],
"scenes": [{"nodes": [0]}],
});
let path = dir.path().join("line.gltf");
std::fs::write(&path, serde_json::to_vec(&json).unwrap()).unwrap();
let mut doc = crate::formats::load(&path, "gltf", &Folder::new(dir.path()), Keep::ALL).unwrap();
let before = doc.flatten(Pose::Rest).unwrap().unwrap();
doc.decode().unwrap();
assert_eq!(doc.json["accessors"][1]["componentType"], 5123);
let after = doc.flatten(Pose::Rest).unwrap().unwrap();
assert_eq!(before.to_glb().unwrap(), after.to_glb().unwrap());
}
#[test]
fn reads_meshopt_compressed_gltf_with_external_buffers() {
let dir = tempfile::tempdir().unwrap();
let mesh = grid(10);
let packed = crate::gltf::compress(&mesh.to_glb().unwrap()).unwrap();
let json_len = u32::from_le_bytes(packed[12..16].try_into().unwrap()) as usize;
let mut json: serde_json::Value = serde_json::from_slice(&packed[20..20 + json_len]).unwrap();
let bin = &packed[20 + json_len + 8..];
json["buffers"][0]["uri"] = "packed.bin".into();
std::fs::write(dir.path().join("packed.bin"), bin).unwrap();
same(
&mesh,
&load(dir.path(), "packed.gltf", &serde_json::to_vec(&json).unwrap()),
);
}
#[test]
fn emissive_brighter_than_one_round_trips_as_emissive_strength() {
let mut mesh = grid(2);
mesh.materials[0].emissive = [4.0, 2.0, 0.0];
let doc = crate::document::Document::from_mesh(mesh, serde_json::Map::new());
let m = &doc.json["materials"][0];
assert_eq!(m["emissiveFactor"], serde_json::json!([1.0, 0.5, 0.0]));
assert_eq!(
m["extensions"]["KHR_materials_emissive_strength"]["emissiveStrength"],
4.0
);
assert_eq!(
doc.json["extensionsUsed"],
serde_json::json!(["KHR_materials_emissive_strength"])
);
let back = doc.flatten(Pose::Rest).unwrap().unwrap();
assert_eq!(back.materials[0].emissive, [4.0, 2.0, 0.0]);
}
#[test]
fn a_required_extension_it_cant_draw_is_an_error() {
let mut doc = crate::document::Document::from_mesh(grid(2), serde_json::Map::new());
doc.json.insert(
"extensionsRequired".into(),
serde_json::json!(["KHR_materials_unlit", "KHR_techniques_webgl"]),
);
assert!(
doc.flatten(Pose::Rest).unwrap().is_some(),
"a look is drawn as best it can be"
);
doc.json.insert(
"extensionsRequired".into(),
serde_json::json!(["EXT_some_future_geometry"]),
);
let err = doc.flatten(Pose::Rest).unwrap_err().to_string();
assert!(err.contains("EXT_some_future_geometry"), "{err}");
}
fn corners(meshes: serde_json::Value, nodes: serde_json::Value) -> crate::document::Document {
let mut bin: Vec<u8> = [[0f32, 0., 0.], [1., 0., 0.], [1., 1., 0.], [0., 1., 0.]]
.iter()
.flatten()
.flat_map(|f| f.to_le_bytes())
.collect();
bin.extend([3u16, 2, 1, 0].iter().flat_map(|i| i.to_le_bytes()));
bin.extend([0u8, 1, 2, 0, 2, 3, 0, 0, 1, 2, 3, 0]);
let n = (0..nodes.as_array().unwrap().len()).collect::<Vec<_>>();
let mut json = serde_json::json!({
"asset": { "version": "2.0" },
"buffers": [{ "byteLength": bin.len() }],
"bufferViews": [
{ "buffer": 0, "byteLength": 48 },
{ "buffer": 0, "byteOffset": 48, "byteLength": 8 },
{ "buffer": 0, "byteOffset": 56, "byteLength": 6 },
{ "buffer": 0, "byteOffset": 62, "byteLength": 5 },
],
"accessors": [
{ "bufferView": 0, "componentType": 5126, "count": 4, "type": "VEC3",
"min": [0, 0, 0], "max": [1, 1, 0] },
{ "bufferView": 1, "componentType": 5123, "count": 4, "type": "SCALAR" },
{ "bufferView": 2, "componentType": 5121, "count": 6, "type": "SCALAR" },
{ "bufferView": 3, "componentType": 5121, "count": 5, "type": "SCALAR" },
],
"scenes": [{ "nodes": n }],
});
json["meshes"] = meshes;
json["nodes"] = nodes;
let mut doc = crate::document::Document::empty(serde_json::Map::new());
let serde_json::Value::Object(json) = json else {
unreachable!()
};
doc.json = json;
doc.push_buffer(bin);
doc
}
#[test]
fn point_only_mesh_flattens_with_points() {
let meshes = serde_json::json!([{ "primitives": [{ "attributes": { "POSITION": 0 }, "mode": 0 }] }]);
let mut doc = corners(meshes, serde_json::json!([{ "mesh": 0 }]));
let mesh = doc.flatten(Pose::Rest).unwrap().expect("points are drawn");
assert_eq!(mesh.prims.len(), 1);
let p = &mesh.prims[0];
assert_eq!(p.points, [0, 1, 2, 3]);
assert!(p.indices.is_empty() && p.lines.is_empty());
assert_eq!(p.positions.len(), 4);
assert!(p.normals.is_empty(), "no normals are made up for points");
assert_eq!((mesh.triangles(), mesh.lines(), mesh.points()), (0, 0, 4));
doc.set_max_points(3);
let err = doc.flatten(Pose::Rest).unwrap_err().to_string();
assert!(err.contains("too many points"), "{err}");
doc.set_lines_and_points(false);
assert!(doc.flatten(Pose::Rest).unwrap().is_none(), "left out, nothing is drawn");
}
#[test]
fn strips_and_loops_expand_to_segments() {
let meshes = serde_json::json!([
{ "primitives": [{ "attributes": { "POSITION": 0 }, "mode": 3, "indices": 1 }] },
{ "primitives": [{ "attributes": { "POSITION": 0 }, "mode": 2 }] },
{ "primitives": [{ "attributes": { "POSITION": 0 }, "mode": 1, "indices": 3 }] },
{ "primitives": [{ "attributes": { "POSITION": 0 }, "mode": 1 }] },
]);
let nodes = serde_json::json!([{ "mesh": 0 }, { "mesh": 1 }, { "mesh": 2 }, { "mesh": 3 }]);
let mesh = corners(meshes, nodes).flatten(Pose::Rest).unwrap().unwrap();
let lines: Vec<&[u32]> = mesh.prims.iter().map(|p| p.lines.as_slice()).collect();
assert_eq!(lines[0], [3, 2, 2, 1, 1, 0], "a strip: each index to the next");
assert_eq!(lines[1], [0, 1, 1, 2, 2, 3, 3, 0], "a loop: closed back to the first");
assert_eq!(lines[2], [1, 2], "a segment to itself and an odd index out are dropped");
assert_eq!(
lines[3],
[0, 1, 2, 3],
"plain lines, without indices, pair the vertices"
);
assert_eq!(mesh.lines(), 3 + 4 + 1 + 2);
assert_eq!(mesh.triangles(), 0);
}
#[test]
fn node_transforms_move_lines_and_points() {
let meshes = serde_json::json!([{ "primitives": [
{ "attributes": { "POSITION": 0 }, "indices": 2 },
{ "attributes": { "POSITION": 0 }, "mode": 3, "indices": 1 },
{ "attributes": { "POSITION": 0 }, "mode": 0 },
] }]);
let nodes = serde_json::json!([
{ "mesh": 0, "translation": [10, 0, 0] },
{ "mesh": 0, "scale": [-1, 1, 1] },
]);
let mesh = corners(meshes, nodes).flatten(Pose::Rest).unwrap().unwrap();
assert_eq!(
mesh.prims.len(),
1,
"lines and points on a primitive's vertices join it"
);
let p = &mesh.prims[0];
assert_eq!(p.positions[2], [11.0, 1.0, 0.0], "moved");
assert_eq!(p.positions[5], [-1.0, 0.0, 0.0], "mirrored");
assert_eq!(
p.indices,
[0, 1, 2, 0, 2, 3, 4, 6, 5, 4, 7, 6],
"a mirror flips triangles back"
);
assert_eq!(p.lines, [3, 2, 2, 1, 1, 0, 7, 6, 6, 5, 5, 4], "but not lines");
assert_eq!(p.points, [0, 1, 2, 3, 4, 5, 6, 7]);
assert_eq!((mesh.triangles(), mesh.lines(), mesh.points()), (4, 6, 8));
}
#[test]
fn instances_repeat_lines_and_points() {
let meshes = serde_json::json!([{ "primitives": [
{ "attributes": { "POSITION": 0 }, "mode": 1, "indices": 1 },
{ "attributes": { "POSITION": 0 }, "mode": 0 },
] }]);
let mut doc = corners(meshes, serde_json::json!([{ "mesh": 0 }]));
let bin: Vec<u8> = [0f32, 0., 0., 5., 0., 0.]
.iter()
.flat_map(|f| f.to_le_bytes())
.collect();
let b = doc.push_buffer(bin);
let json = &mut doc.json;
json["buffers"]
.as_array_mut()
.unwrap()
.push(serde_json::json!({ "byteLength": 24 }));
json["bufferViews"]
.as_array_mut()
.unwrap()
.push(serde_json::json!({ "buffer": b, "byteLength": 24 }));
json["accessors"]
.as_array_mut()
.unwrap()
.push(serde_json::json!({ "bufferView": 4, "componentType": 5126, "count": 2, "type": "VEC3" }));
json["nodes"][0]["extensions"] =
serde_json::json!({ "EXT_mesh_gpu_instancing": { "attributes": { "TRANSLATION": 4 } } });
json.insert("extensionsUsed".into(), serde_json::json!(["EXT_mesh_gpu_instancing"]));
let mesh = doc.flatten(Pose::Rest).unwrap().unwrap();
assert_eq!(mesh.prims.len(), 1);
let p = &mesh.prims[0];
assert_eq!(p.positions.len(), 8);
assert_eq!(p.positions[5], [6.0, 0.0, 0.0]);
assert_eq!(
p.lines,
[3, 2, 1, 0, 7, 6, 5, 4],
"each instance's segments on its own vertices"
);
assert_eq!(p.points, [0, 1, 2, 3, 4, 5, 6, 7]);
}
#[test]
fn glb_round_trip_keeps_lines_and_points() {
let mut mesh = grid(2);
mesh.prims[0].lines = vec![0, 1, 1, 2, 2, 5];
mesh.prims[0].points = vec![4, 8];
mesh.prims[1].indices.clear();
mesh.prims[1].normals.clear();
mesh.prims[1].points = (0..9).collect();
let glb = mesh.to_glb().unwrap();
let len = u32::from_le_bytes(glb[12..16].try_into().unwrap()) as usize;
let json: serde_json::Value = serde_json::from_slice(&glb[20..20 + len]).unwrap();
let prims = json["meshes"]
.as_array()
.unwrap()
.iter()
.flat_map(|m| m["primitives"].as_array().unwrap().clone())
.collect::<Vec<_>>();
let modes: Vec<u64> = prims.iter().map(|p| p["mode"].as_u64().unwrap_or(4)).collect();
assert_eq!(modes, [4, 1, 0, 0]);
assert_eq!(prims[0]["attributes"], prims[1]["attributes"], "one set of vertices");
assert_eq!(prims[0]["attributes"], prims[2]["attributes"]);
assert_eq!(prims[1]["material"], prims[0]["material"]);
let lines = &json["accessors"][usize::try_from(prims[1]["indices"].as_u64().unwrap()).unwrap()];
assert_eq!(
(lines["componentType"].as_u64(), lines["count"].as_u64()),
(Some(5123), Some(6))
);
assert!(prims[3].get("indices").is_none(), "a point cloud needs no indices");
let none = crate::assets::Memory::new();
let back = crate::formats::load_bytes(&glb, "m.glb", "glb", &none, Keep::ALL)
.unwrap()
.flatten(Pose::Rest)
.unwrap()
.unwrap();
assert_eq!(
back.prims.len(),
2,
"lines and points join the triangles they share vertices with"
);
assert_eq!(back.prims[0].indices, mesh.prims[0].indices);
assert_eq!(back.prims[0].lines, mesh.prims[0].lines);
assert_eq!(back.prims[0].points, mesh.prims[0].points);
assert_eq!(back.prims[0].positions, mesh.prims[0].positions);
assert_eq!(back.prims[1].points, mesh.prims[1].points);
assert!(back.prims[1].normals.is_empty(), "no normals are made up for points");
assert!(back.to_glb().unwrap() == glb, "written again, the same bytes");
}
fn box_of(mesh: Option<&Mesh>) -> Option<crate::document::Bounds> {
let mut lo = [f32::INFINITY; 3];
let mut hi = [f32::NEG_INFINITY; 3];
for v in mesh.into_iter().flat_map(|m| &m.prims).flat_map(|p| &p.positions) {
for c in 0..3 {
lo[c] = lo[c].min(v[c]);
hi[c] = hi[c].max(v[c]);
}
}
(lo[0] <= hi[0]).then_some(crate::document::Bounds { min: lo, max: hi })
}
#[cfg(all(feature = "gltf", feature = "fs"))]
#[test]
fn bounds_hold_what_flatten_draws_in_every_pose() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/corpus/gltf");
let cases = [
"skin_animated/skin.glb",
"skin_rest_pose",
"skin_eight_influences",
"blender_shape_key/shape_key.glb",
"morph_default_weights",
"gpu_instancing/inst.glb",
"mirror_x/cube.glb",
"node_hierarchy",
"node_matrix",
"points_and_lines",
"quantized_normalized_i16",
"sparse",
"blender_draco/draco.glb",
];
for case in cases {
let mut path = root.join(case);
if path.is_dir() {
path = std::fs::read_dir(&path)
.unwrap()
.map(|e| e.unwrap().path())
.find(|p| p.extension().is_some_and(|e| e == "glb" || e == "gltf"))
.unwrap();
}
let doc = crate::open(&path).unwrap();
let mut poses = vec![Pose::Rest, Pose::Bind];
let animations = super::list(&doc.json, "animations").len();
poses.extend((0..animations).map(|index| Pose::Animation { index, time: 0.4 }));
for pose in poses {
let Ok(mesh) = doc.flatten(pose) else { continue };
let want = box_of(mesh.as_ref());
assert_eq!(doc.bounds(pose).unwrap(), want, "{case} in {pose:?}");
assert!(want.is_some(), "{case} draws something");
}
}
}
#[cfg(all(feature = "gltf", feature = "fs"))]
#[test]
fn animation_bounds_hold_every_sampled_pose() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/corpus/gltf/skin_animated/skin.glb");
let doc = crate::open(&path).unwrap();
let (first, last) = doc.rig().unwrap().animations[0].span();
assert!(first < last);
let all = doc.animation_bounds(0, 5).unwrap().unwrap();
for k in 0..5u8 {
let time = first + (last - first) * f32::from(k) / 4.0;
let b = doc.bounds(Pose::Animation { index: 0, time }).unwrap().unwrap();
assert!(
(0..3).all(|c| all.min[c] <= b.min[c] && b.max[c] <= all.max[c]),
"{b:?} in {all:?}"
);
}
let start = doc.bounds(Pose::Animation { index: 0, time: first }).unwrap();
assert_eq!(doc.animation_bounds(0, 1).unwrap(), start);
assert_eq!(doc.animation_bounds(0, 0).unwrap(), start);
assert!(doc.animation_bounds(1, 5).is_err());
}
#[test]
fn bounds_and_vertices_count_every_instance() {
let meshes = serde_json::json!([{ "primitives": [{ "attributes": { "POSITION": 0 }, "indices": 2 }] }]);
let doc = corners(
meshes,
serde_json::json!([{ "mesh": 0 }, { "mesh": 0, "translation": [5, 0, -2], "rotation": [0, 0, 0.707_106_77, 0.707_106_77] }]),
);
let b = doc.bounds(Pose::Rest).unwrap().unwrap();
assert_eq!(b, box_of(doc.flatten(Pose::Rest).unwrap().as_ref()).unwrap());
let near = |a: [f32; 3], b: [f32; 3]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-6);
assert!(near(b.min, [0.0, 0.0, -2.0]) && near(b.max, [5.0, 1.0, 0.0]), "{b:?}");
assert!((b.center()[0] - 2.5).abs() < 1e-6 && (b.size()[2] - 2.0).abs() < 1e-6);
let c = doc.counts();
assert_eq!((c.vertices, c.triangles), (8, 4));
let empty = corners(serde_json::json!([]), serde_json::json!([]));
assert_eq!(empty.bounds(Pose::Rest).unwrap(), None);
}
#[cfg(feature = "draco")]
#[test]
fn draco_compressed_accessors_read_as_their_stream_decodes() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/corpus/gltf/blender_draco/draco.glb");
let dir = path.parent().unwrap();
let doc = crate::formats::load(&path, "glb", &Folder::new(dir), Keep::ALL).unwrap();
let mut decoded = crate::formats::load(&path, "glb", &Folder::new(dir), Keep::ALL).unwrap();
decoded.decode().unwrap();
let prim = &doc.json["meshes"][0]["primitives"][0];
let position = usize::try_from(prim["attributes"]["POSITION"].as_u64().unwrap()).unwrap();
let indices = usize::try_from(prim["indices"].as_u64().unwrap()).unwrap();
assert!(
doc.json["accessors"][position].get("bufferView").is_none(),
"the data is in the stream"
);
let (n, floats) = doc.read_floats(position).unwrap();
assert_eq!((n, &floats), (3, &decoded.read_floats(position).unwrap().1));
assert!(floats.iter().any(|&f| f != 0.0), "not zeros");
assert_eq!(
doc.read_indices(indices).unwrap(),
decoded.read_indices(indices).unwrap()
);
}
}