From 35f4fc735877286360a3629c3660efdcb4f47875 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:16:54 +0000 Subject: [PATCH 01/22] Bound FBX ASCII parsing while scanning --- crates/mesh/src/fbx.rs | 182 +++++++++++++++++++---------------- crates/mesh/src/tests/fbx.rs | 14 +++ 2 files changed, 113 insertions(+), 83 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index acb654513..49191c0c0 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -89,7 +89,8 @@ impl Budget { } /// Bytes the values under `records` keep. Used for ASCII files, which hold array elements as one -/// [`Prop`] each and are parsed before they can be counted (the text itself bounds them). +/// [`Prop`] each. +#[cfg(test)] pub(crate) fn value_bytes(records: &[Record]) -> Option { records.iter().try_fold(0usize, |acc, n| { let own = n.props.iter().try_fold(0usize, |a, p| { @@ -233,9 +234,16 @@ pub fn read_document(bytes: &[u8], limits: &Limits) -> Result { let (version, records) = if is_binary(bytes) { read_binary(bytes, &budget)? } else { - let (version, records) = read_ascii(&String::from_utf8_lossy(bytes))?; - budget.take(value_bytes(&records), "its values")?; - (version, records) + let text = match std::str::from_utf8(bytes) { + Ok(text) => std::borrow::Cow::Borrowed(text), + Err(_) => { + if bytes.len() > limits.max_data_bytes { + return Err(too_large(KIND, "invalid UTF-8 text exceeds the memory limit")); + } + String::from_utf8_lossy(bytes) + } + }; + read_ascii(&text, &budget)? }; Ok(Document { version, records, left: budget.left.get() }) } @@ -418,110 +426,118 @@ fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec)> { // --------------------------------------------------------------------------------------------- // ASCII -#[derive(Clone, Debug, PartialEq)] -enum Tok { - Key(String), - Val(Prop), - /// `*N` array length (the values follow in an `a:` child). +#[derive(Clone, Copy, Debug, PartialEq)] +enum Tok<'a> { + Key(&'a str), + Value(&'a str, bool), Count, Comma, Open, Close, } -fn tokenize(text: &str) -> Vec { - let mut out = vec![]; - let mut it = text.char_indices().peekable(); - while let Some(&(i, c)) = it.peek() { - match c { - c if c.is_whitespace() => { - it.next(); +/// Scan on demand so punctuation and ignored input cannot build an unbounded token vector. +struct Ascii<'a> { + text: &'a str, + pos: usize, + lookahead: Option>, +} + +impl<'a> Ascii<'a> { + fn next_token(&mut self) -> Option> { + loop { + let rest = self.text.get(self.pos..)?; + let c = rest.chars().next()?; + if c.is_whitespace() { + self.pos += c.len_utf8(); + continue; } - ';' => { - for (_, c) in it.by_ref() { - if c == '\n' { - break; - } + if c == ';' { + self.pos += rest.find('\n').map_or(rest.len(), |i| i + 1); + continue; + } + match c { + ',' | '{' | '}' => { + self.pos += 1; + return Some(match c { + ',' => Tok::Comma, + '{' => Tok::Open, + _ => Tok::Close, + }); } - } - ',' => { - it.next(); - out.push(Tok::Comma); - } - '{' => { - it.next(); - out.push(Tok::Open); - } - '}' => { - it.next(); - out.push(Tok::Close); - } - '"' => { - it.next(); - let start = i + 1; - let mut end = text.len(); - for (j, c) in it.by_ref() { - if c == '"' { - end = j; - break; - } + '"' => { + let tail = rest.get(1..).unwrap_or(""); + let end = tail.find('"').unwrap_or(tail.len()); + self.pos += 1 + end + usize::from(end < tail.len()); + return Some(Tok::Value(tail.get(..end).unwrap_or(""), true)); } - out.push(Tok::Val(Prop::Str(text.get(start..end).unwrap_or("").replace(""", "\"")))); - } - _ => { - let start = i; - let mut end = text.len(); - while let Some(&(j, c)) = it.peek() { - if c.is_whitespace() || matches!(c, ',' | '{' | '}' | '"' | ';' | ':') { - end = j; - break; + _ => { + let end = rest.find(|c: char| c.is_whitespace() || matches!(c, ',' | '{' | '}' | '"' | ';' | ':')).unwrap_or(rest.len()); + let word = rest.get(..end).unwrap_or(""); + self.pos += end; + if rest.as_bytes().get(end) == Some(&b':') { + self.pos += 1; + return Some(Tok::Key(word)); + } + if word.starts_with('*') { + return Some(Tok::Count); + } + if !word.is_empty() { + return Some(Tok::Value(word, false)); } - it.next(); - } - let word = text.get(start..end).unwrap_or(""); - if it.peek().is_some_and(|(_, c)| *c == ':') { - it.next(); - out.push(Tok::Key(word.to_string())); - } else if word.starts_with('*') { - out.push(Tok::Count); - } else if let Ok(i) = word.parse::() { - out.push(Tok::Val(Prop::Int(i))); - } else if let Ok(f) = word.parse::() { - out.push(Tok::Val(Prop::Float(f))); - } else if !word.is_empty() { - out.push(Tok::Val(Prop::Str(word.to_string()))); - } else { // A stray `:` with no key. - it.next(); + self.pos += 1; } } } } - out + + fn peek(&mut self) -> Option> { + if self.lookahead.is_none() { + self.lookahead = self.next_token(); + } + self.lookahead + } + + fn take(&mut self) -> Option> { + self.lookahead.take().or_else(|| self.next_token()) + } } -fn ascii_list(toks: &[Tok], i: &mut usize, depth: usize) -> Result> { +fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, depth: usize) -> Result> { if depth > MAX_DEPTH { return Err(too_large(KIND, "records nested too deeply")); } let mut out = vec![]; - while let Some(t) = toks.get(*i) { - *i += 1; + while let Some(t) = scan.take() { match t { Tok::Close if depth > 0 => return Ok(out), Tok::Key(name) => { - let mut record = Record { name: name.clone(), ..Record::default() }; + records_budget.take(std::mem::size_of::().checked_add(name.len()), "its records")?; + let mut record = Record { name: name.to_string(), ..Record::default() }; loop { - match toks.get(*i) { - Some(Tok::Val(v)) => record.props.push(v.clone()), + match scan.peek() { + Some(Tok::Value(value, quoted)) => { + let number = if quoted { + None + } else { + value.parse::().ok().map(Prop::Int).or_else(|| value.parse::().ok().map(Prop::Float)) + }; + let size = std::mem::size_of::().checked_add(if number.is_some() { 0 } else { value.len() }); + // Charge before copying a string or growing the property vector. + budget.take(size, "its values")?; + record + .props + .push(number.unwrap_or_else(|| Prop::Str(if quoted { value.replace(""", "\"") } else { value.to_string() }))); + } Some(Tok::Count | Tok::Comma) => {} _ => break, } - *i += 1; + scan.take(); } - if toks.get(*i) == Some(&Tok::Open) { - *i += 1; - record.children = ascii_list(toks, i, depth + 1)?; + if scan.peek() == Some(Tok::Open) { + scan.take(); + record.children = ascii_list(scan, budget, records_budget, depth + 1)?; } out.push(record); } @@ -531,10 +547,10 @@ fn ascii_list(toks: &[Tok], i: &mut usize, depth: usize) -> Result> Ok(out) } -fn read_ascii(text: &str) -> Result<(u32, Vec)> { - let toks = tokenize(text); - let mut i = 0; - let records = ascii_list(&toks, &mut i, 0)?; +fn read_ascii(text: &str, budget: &Budget) -> Result<(u32, Vec)> { + let mut scan = Ascii { text, pos: 0, lookahead: None }; + let records_budget = Budget::new(budget.limit); + let records = ascii_list(&mut scan, budget, &records_budget, 0)?; let version = records .iter() .find(|n| n.name == "FBXHeaderExtension") diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index a58ddeb19..d5978883e 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -627,3 +627,17 @@ fn fbx6_model_holds_its_mesh() { assert_eq!(d.unit_scale(), Some(100.0)); } } + +#[test] +fn ascii_budget_applies_while_reading_values_and_records() { + let limits = Limits { max_data_bytes: 512, ..Limits::default() }; + let many_values = format!("Objects: {{\n A: {}\n}}", "1,".repeat(1000)); + assert!(matches!(read_document(many_values.as_bytes(), &limits), Err(Error::TooLarge(_)))); + let many_records = format!("Objects: {{\n{}\n}}", "A: {}\n".repeat(1000)); + assert!(matches!(read_document(many_records.as_bytes(), &limits), Err(Error::TooLarge(_)))); + let giant_string = format!("Objects: {{ A: \"{}\" }}", "x".repeat(1024)); + assert!(matches!(read_document(giant_string.as_bytes(), &limits), Err(Error::TooLarge(_)))); + // Empty punctuation and comments do not consume memory in proportion to their length. + let ignored = format!(";{}\nObjects: {{}}", ",".repeat(10_000)); + assert!(read_document(ignored.as_bytes(), &limits).is_ok()); +} From ef91c57ad0efb1223793ec658f5cddaab18254ba Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:19:07 +0000 Subject: [PATCH 02/22] Read FBX camera lenses and placements --- crates/mesh/src/fbx.rs | 45 ++++++++++++++++++++++++++++++++---- crates/mesh/src/tests/fbx.rs | 35 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index acb654513..133a13cda 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -39,8 +39,7 @@ //! name is listed in [`Scene::external`]. The names come from the file: a resolver must not trust //! them as paths. //! -//! Not read: FBX 5 and older, animation stacks, skin deformers, blend shapes, cameras and -//! lights, NURBS and patches. +//! Not read: FBX 5 and older, animation stacks, skin deformers, blend shapes, lights, NURBS and patches. //! //! [`Limits::max_data_bytes`] bounds what one file may keep in memory: its parsed values (an //! array element counts 8 bytes whatever its type in the file), the meshes built from them and @@ -50,8 +49,8 @@ use std::cell::Cell; use std::collections::HashMap; use crate::{ - AlphaMode, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Resolver, Result, Scene, TextureRef, base64_decode, - count_warning, malformed, mat_mul, srgb_to_linear, too_large, + AlphaMode, Camera, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Projection, Resolver, Result, Scene, TextureRef, + base64_decode, count_warning, malformed, mat_mul, srgb_to_linear, too_large, }; const KIND: &str = "FBX"; @@ -782,6 +781,44 @@ impl Document { } } + // FBX cameras look along +X; a child turns the neutral camera's -Z onto +X. + // Keep this separate from the Model so its children retain their original transforms. + for (id, model) in &models { + let class_index = if matches!(model.props.first(), Some(Prop::Int(_))) { 2 } else { 1 }; + if model.str_at(class_index) != Some("Camera") { + continue; + } + let name = short_name(model.first_str().unwrap_or("")); + if name.starts_with("Producer ") { + continue; + } + let Some(ni) = node_of.get(id).copied() else { continue }; + let lens = b.children_of(*id, "NodeAttribute").last().and_then(|(key, _)| b.by_key.get(key)).copied().unwrap_or(model); + let props = Props::of(lens); + let positive = |key| props.f(key).filter(|v| *v > 0.0); + let projection = if props.f("CameraProjectionType") == Some(1.0) { + let mag = positive("OrthoZoom").unwrap_or(1.0); + Projection::Orthographic { xmag: mag, ymag: mag, znear: 0.01, zfar: 1000.0 } + } else { + let yfov = match positive("FocalLength") { + Some(focal) => 2.0 * (positive("FilmHeight").unwrap_or(0.612) * 25.4 / (2.0 * focal)).atan(), + None => positive("FieldOfViewY").or_else(|| positive("FieldOfView")).unwrap_or(40.0).to_radians(), + }; + Projection::Perspective { yfov, aspect: None, znear: 0.01, zfar: None } + }; + if b.scene.cameras.len() >= limits.max_extras { + return Err(too_large(KIND, "too many cameras")); + } + b.node_room()?; + let ci = b.scene.cameras.len(); + b.scene.cameras.push(Camera { name: name.to_string(), projection }); + let child = b.scene.nodes.len(); + b.scene.nodes.push(Node { name: format!("{name} camera"), transform: rotate(1, -90.0), camera: Some(ci), ..Node::default() }); + if let Some(node) = b.scene.nodes.get_mut(ni) { + node.children.push(child); + } + } + // One mesh per Model with Mesh geometry (material slots are per Model). Models instancing // the same geometry with the same materials share one mesh rather than each building a // copy. diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index a58ddeb19..61f300989 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -627,3 +627,38 @@ fn fbx6_model_holds_its_mesh() { assert_eq!(d.unit_scale(), Some(100.0)); } } + +#[test] +fn cameras_keep_lens_and_model_hierarchy() { + let text = r#"FBXHeaderExtension: { FBXVersion: 7400 } +Objects: { + Model: 1, "Model::Rig", "Null" { Properties70: { P: "Lcl Translation", "", "", "", 1,2,3 } } + Model: 2, "Model::View", "Camera" { } + NodeAttribute: 3, "NodeAttribute::Lens", "Camera" { Properties70: { + P: "FocalLength", "", "", "", 50 + P: "FilmHeight", "", "", "", 1 + } } + Model: 4, "Model::Ortho", "Camera" { Properties70: { + P: "CameraProjectionType", "", "", "", 1 + P: "OrthoZoom", "", "", "", 3 + } } + Model: 5, "Model::Producer Perspective", "Camera" { } +} +Connections: { + C: "OO",2,1 + C: "OO",3,2 +} +"#; + let scene = parse(text.as_bytes()).unwrap(); + assert_eq!(scene.cameras.len(), 2); + let crate::Projection::Perspective { yfov, .. } = scene.cameras[0].projection else { panic!("perspective") }; + assert!((yfov - 2.0 * (25.4_f64 / 100.0).atan()).abs() < 1e-12); + assert!(matches!(scene.cameras[1].projection, crate::Projection::Orthographic { ymag: 3.0, .. })); + let camera_node = scene.nodes.iter().find(|n| n.camera == Some(0)).unwrap(); + let forward = crate::transform_point(&camera_node.transform, [0.0, 0.0, -1.0]); + assert!((forward[0] - 1.0).abs() < 1e-12); + let rig = scene.nodes.iter().find(|n| n.name == "Rig").unwrap(); + assert!(rig.children.iter().any(|i| scene.nodes[*i].name == "View")); + let limits = Limits { max_extras: 1, ..Limits::default() }; + assert!(matches!(read(text.as_bytes(), &limits, &mut no_external), Err(Error::TooLarge(_)))); +} From 2bacea41de86e8784d7cdad7ac94a129064c3b55 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:21:03 +0000 Subject: [PATCH 03/22] Read all FBX geometries and unplaced meshes --- crates/mesh/src/fbx.rs | 103 +++++++++++++++++++++++------------ crates/mesh/src/tests/fbx.rs | 15 +++++ 2 files changed, 82 insertions(+), 36 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index acb654513..8a3cd524a 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -47,7 +47,7 @@ //! the image bytes. use std::cell::Cell; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use crate::{ AlphaMode, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Resolver, Result, Scene, TextureRef, base64_decode, @@ -782,47 +782,74 @@ impl Document { } } - // One mesh per Model with Mesh geometry (material slots are per Model). Models instancing - // the same geometry with the same materials share one mesh rather than each building a - // copy. + // One mesh per geometry and material-slot combination, shared by instances. let mut mesh_of: HashMap<(Key, Vec), usize> = HashMap::new(); - for (id, o) in &models { + let mut placed = HashSet::new(); + for (id, model) in &models { let Some(ni) = node_of.get(id).copied() else { continue }; - let Some((gid, geom)) = b.geometry_of(*id) else { continue }; - let mut mats: Vec = vec![]; + let geometries = b.geometries_of(*id); + if geometries.is_empty() { + continue; + } + let mut mats = Vec::new(); for (mid, _) in b.children_of(*id, "Material") { mats.extend(b.material(mid)?); } let name = b.scene.nodes.get(ni).map(|n| n.name.clone()).unwrap_or_default(); - let mesh_index = match mesh_of.get(&(gid, mats.clone())) { - Some(i) => *i, - None => { - let Some(mesh) = b.mesh(&name, geom, &mats)? else { continue }; - b.budget.take(mesh_bytes(&mesh), &format!("mesh `{name}`"))?; - if b.scene.meshes.len() >= limits.max_meshes { - return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes))); - } - b.scene.meshes.push(mesh); - mesh_of.insert((gid, mats), b.scene.meshes.len() - 1); - b.scene.meshes.len() - 1 + for (gid, geom) in geometries { + if !placed.insert((gid, Some(*id))) { + continue; } - }; - match geometric(&Props::of(o)) { - Some(transform) => { + let mesh_index = match mesh_of.get(&(gid, mats.clone())) { + Some(i) => *i, + None => { + let Some(mesh) = b.mesh(&name, geom, &mats)? else { continue }; + b.budget.take(mesh_bytes(&mesh), &format!("mesh `{name}`"))?; + if b.scene.meshes.len() >= limits.max_meshes { + return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes))); + } + b.scene.meshes.push(mesh); + mesh_of.insert((gid, mats.clone()), b.scene.meshes.len() - 1); + b.scene.meshes.len() - 1 + } + }; + let transform = geometric(&Props::of(model)); + let occupied = b.scene.nodes.get(ni).is_some_and(|n| n.mesh.is_some()); + if transform.is_some() || occupied { b.node_room()?; - let gi = b.scene.nodes.len(); - b.scene.nodes.push(Node { name: format!("{name} geometry"), transform, mesh: Some(mesh_index), ..Node::default() }); - if let Some(n) = b.scene.nodes.get_mut(ni) { - n.children.push(gi); - } - } - None => { - if let Some(n) = b.scene.nodes.get_mut(ni) { - n.mesh = Some(mesh_index); + let child = b.scene.nodes.len(); + b.scene.nodes.push(Node { + name: format!("{name} geometry"), + transform: transform.unwrap_or(IDENTITY), + mesh: Some(mesh_index), + ..Node::default() + }); + if let Some(node) = b.scene.nodes.get_mut(ni) { + node.children.push(child); } + } else if let Some(node) = b.scene.nodes.get_mut(ni) { + node.mesh = Some(mesh_index); } } } + // Geometry with no Model connection still contributes a mesh at the origin. + let placed_ids: HashSet> = placed.iter().map(|(gid, _)| *gid).collect(); + for geom in objects.children.iter().filter(|o| o.name == "Geometry" && o.child("Vertices").is_some()) { + let Some(gid) = key(geom.props.first()) else { continue }; + if placed_ids.contains(&gid) { + continue; + } + let Some(mesh) = b.mesh("", geom, &[])? else { continue }; + b.budget.take(mesh_bytes(&mesh), "unplaced mesh")?; + if b.scene.meshes.len() >= limits.max_meshes { + return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes))); + } + b.node_room()?; + let mesh_index = b.scene.meshes.len(); + b.scene.meshes.push(mesh); + b.scene.roots.push(b.scene.nodes.len()); + b.scene.nodes.push(Node { mesh: Some(mesh_index), ..Node::default() }); + } let mut scene = b.scene; count_warning(&mut scene.warnings, b.skipped, "polygons with fewer than three corners or a vertex that is not there were left out"); Ok(scene) @@ -971,14 +998,18 @@ impl<'a> Build<'a, '_> { Ok(()) } - /// The mesh geometry of Model `id`, and what it is shared by. - fn geometry_of(&self, id: Key<'a>) -> Option<(Key<'a>, &'a Record)> { - // 6.x: the Model holds its own mesh. + /// The geometries connected to a Model, including its own FBX 6 mesh. + fn geometries_of(&self, id: Key<'a>) -> Vec<(Key<'a>, &'a Record)> { + let mut geometries = Vec::new(); if let Some(model) = self.by_key.get(&id).copied().filter(|o| o.child("Vertices").is_some()) { - return Some((id, model)); + geometries.push((id, model)); } - let (gid, _) = self.children_of(id, "Geometry").into_iter().next()?; - Some((gid, self.by_key.get(&gid).copied()?)) + for (gid, _) in self.children_of(id, "Geometry") { + if let Some(geometry) = self.by_key.get(&gid).copied().filter(|o| o.child("Vertices").is_some()) { + geometries.push((gid, geometry)); + } + } + geometries } /// The image of Texture `tid`. diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index a58ddeb19..424e587e9 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -627,3 +627,18 @@ fn fbx6_model_holds_its_mesh() { assert_eq!(d.unit_scale(), Some(100.0)); } } + +#[test] +fn all_connected_and_unplaced_geometries_are_instances() { + let geometry = |id| format!("Geometry: {id}, \"Geometry::Part{id}\", \"Mesh\" {{ Vertices: 0,0,0,1,0,0,0,1,0 PolygonVertexIndex: 0,1,-3 }}\n"); + let text = format!( + "FBXHeaderExtension: {{ FBXVersion: 7400 }}\nObjects: {{\n{}{}{} Model: 10, \"Model::M\", \"Mesh\" {{}} }}\nConnections: {{ C: \"OO\",1,10 C: \"OO\",2,10 C: \"OO\",2,10 }}", + geometry(1), + geometry(2), + geometry(3) + ); + let scene = parse(text.as_bytes()).unwrap(); + assert_eq!(scene.meshes.len(), 3); + assert_eq!(scene.instances(&Limits::default()).unwrap().len(), 3); + assert_eq!(scene.roots.len(), 2); +} From aa4d4c5f13e6a182a57eaacd5dc5fc23b8ccac6d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:23:12 +0000 Subject: [PATCH 04/22] Preserve FBX per-corner colors --- crates/mesh/src/fbx.rs | 79 ++++++++++++++++++++++++++++++++---- crates/mesh/src/tests/fbx.rs | 21 ++++++++++ 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index acb654513..62e11b0eb 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -109,7 +109,13 @@ pub(crate) fn value_bytes(records: &[Record]) -> Option { /// Bytes a mesh keeps. pub(crate) fn mesh_bytes(m: &Mesh) -> Option { let uvs = m.uv_sets.iter().try_fold(0usize, |a, s| a.checked_add(s.len().checked_mul(8)?))?; - let pools = m.positions.len().checked_mul(24)?.checked_add(m.normals.len().checked_mul(24)?)?.checked_add(uvs)?; + let pools = m + .positions + .len() + .checked_mul(24)? + .checked_add(m.normals.len().checked_mul(24)?)? + .checked_add(uvs)? + .checked_add(m.colors.len().checked_mul(16)?)?; m.parts.iter().try_fold(pools, |a, p| a.checked_add(p.corners.len().checked_mul(12)?)?.checked_add(p.face_sizes.len().checked_mul(4)?)) } @@ -1077,10 +1083,19 @@ impl<'a> Build<'a, '_> { let pool = |what: &str, n: usize| { if n > limits.max_vertices { Err(too_large(KIND, format!("more than {} {what}", limits.max_vertices))) } else { Ok(()) } }; - let positions: Vec<[f64; 3]> = vertices.floats().as_chunks::<3>().0.iter().map(|c| c.map(finite)).collect(); + let mut positions: Vec<[f64; 3]> = vertices.floats().as_chunks::<3>().0.iter().map(|c| c.map(finite)).collect(); pool("vertices", positions.len())?; + let original_positions = positions.len(); let normals = Layer::of(geom, "LayerElementNormal", "Normals", &["NormalsIndex", "NormalIndex"], 3); let uvs = Layer::of(geom, "LayerElementUV", "UV", &["UVIndex"], 2); + let colors = Layer::of(geom, "LayerElementColor", "Colors", &["ColorIndex", "ColorsIndex"], 4); + if colors.is_some() && positions.len().checked_mul(16).is_none_or(|n| n > self.budget.left.get()) { + return Err(too_large(KIND, "vertex colors exceed the memory limit")); + } + let mut vertex_colors = colors.as_ref().map(|_| vec![[1.0; 4]; positions.len()]).unwrap_or_default(); + let mut color_indices = HashMap::<(u32, u32), u32>::new(); + let mut first_color = HashMap::::new(); + let mut colors_complete = colors.is_some(); let slots = geom.child("LayerElementMaterial").and_then(|e| { let mapping = e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("AllSame"); Some((mapping == "ByPolygon", e.child("Materials")?.ints())) @@ -1094,12 +1109,44 @@ impl<'a> Build<'a, '_> { for (pv, raw) in pvi.ints().into_iter().enumerate() { // The last corner of a polygon is stored as `-index - 1`. let (v, last) = if raw < 0 { (!raw, true) } else { (raw, false) }; - match u32::try_from(v).ok().filter(|v| (*v as usize) < positions.len()) { - Some(p) => face.push(Corner { - position: p, - normal: Layer::at(normal_layer, pv, p as usize, poly), - uv: Layer::at(uv_layer, pv, p as usize, poly), - }), + match u32::try_from(v).ok().filter(|v| (*v as usize) < original_positions) { + Some(p) => { + let normal = Layer::at(normal_layer, pv, p as usize, poly); + let uv = Layer::at(uv_layer, pv, p as usize, poly); + let mut position = p; + if let Some((layer, values)) = &colors { + let color = Layer::at(Some(layer), pv, p as usize, poly); + let rgba = usize::try_from(color) + .ok() + .and_then(|i| i.checked_mul(4)) + .and_then(|i| i.checked_add(4).and_then(|end| values.get(i..end))); + if let Some(rgba) = rgba { + if let Some(existing) = color_indices.get(&(p, color)) { + position = *existing; + } else { + if first_color.insert(p, color).is_some() { + pool("vertices", positions.len().saturating_add(1))?; + if positions.len().saturating_add(1).checked_mul(40).is_none_or(|n| n > self.budget.left.get()) { + return Err(too_large(KIND, "colored vertices exceed the memory limit")); + } + position = u32::try_from(positions.len()).map_err(|_| too_large(KIND, "too many vertices"))?; + let Some(original) = positions.get(p as usize).copied() else { continue }; + positions.push(original); + vertex_colors.push([1.0; 4]); + } + if let Some(dst) = vertex_colors.get_mut(position as usize) { + if let [r, g, b, a] = rgba { + *dst = [finite(*r) as f32, finite(*g) as f32, finite(*b) as f32, finite(*a) as f32]; + } + } + color_indices.insert((p, color), position); + } + } else { + colors_complete = false; + } + } + face.push(Corner { position, normal, uv }); + } None => broken = true, } if face.len() > limits.max_face_corners { @@ -1152,6 +1199,20 @@ impl<'a> Build<'a, '_> { let uvs: Option> = uvs.map(|(_, d)| d.as_chunks::<2>().0.iter().map(|c| [finite(c[0]) as f32, 1.0 - finite(c[1]) as f32]).collect()); pool("UVs", uvs.as_ref().map_or(0, Vec::len))?; - Ok(Some(Mesh { name: name.to_string(), positions, normals, uv_sets: uvs.into_iter().collect(), parts, ..Mesh::default() })) + if !colors_complete { + vertex_colors.clear(); + if colors.is_some() { + self.scene.warnings.push("FBX: incomplete vertex color layer was omitted".into()); + } + } + Ok(Some(Mesh { + name: name.to_string(), + positions, + normals, + uv_sets: uvs.into_iter().collect(), + colors: vertex_colors, + parts, + ..Mesh::default() + })) } } diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index a58ddeb19..8de0eb776 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -627,3 +627,24 @@ fn fbx6_model_holds_its_mesh() { assert_eq!(d.unit_scale(), Some(100.0)); } } + +#[test] +fn fbx_corner_colors_keep_seams_and_incomplete_layers_are_omitted() { + let layer = r#"LayerElementColor: 0 { + MappingInformationType: "ByPolygonVertex" + ReferenceInformationType: "Direct" + Colors: 1,0,0,1, 0,1,0,1, 0,0,1,1, 1,1,0,1, 0,1,0,1, 0,0,1,1 +}"#; + let text = ASCII.replace("LayerElementNormal: 0 {", &format!("{layer}\n LayerElementNormal: 0 {{")); + // Add a color for every corner of the original quad and triangle. + let text = text.replace("0,0,1,1\n}", "0,0,1,1, 1,0,1,1\n}"); + let scene = parse(text.as_bytes()).unwrap(); + let mesh = &scene.meshes[0]; + let colors: Vec<_> = mesh.parts.iter().flat_map(|part| part.corners.iter()).map(|corner| mesh.colors[corner.position as usize]).collect(); + assert_eq!(colors.len(), 7); + assert!(colors.contains(&[1.0, 0.0, 0.0, 1.0])); + assert!(colors.contains(&[0.0, 1.0, 0.0, 1.0])); + assert!(colors.contains(&[1.0, 0.0, 1.0, 1.0])); + let incomplete = text.replace(", 1,0,1,1\n}", "\n}"); + assert!(parse(incomplete.as_bytes()).unwrap().meshes[0].colors.is_empty()); +} From cc92663deb302c0fc11d692d69d521ea6435b908 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:25:39 +0000 Subject: [PATCH 05/22] Expose FBX diffuse values and texture candidates --- crates/mesh/src/fbx.rs | 126 +++++++++++++++++++++++++++++++++++ crates/mesh/src/tests/fbx.rs | 28 ++++++++ 2 files changed, 154 insertions(+) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index acb654513..e8fd78096 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -829,6 +829,132 @@ impl Document { } } +/// Diffuse material values as stored in FBX, before color-space conversion. +#[derive(Clone, Debug, PartialEq)] +pub struct DiffuseMaterial { + pub name: String, + pub factor: [f32; 4], + /// Encoded embedded or resolved image bytes, when available. + pub image: Option>, +} + +impl Document { + /// Read all FBX diffuse materials, including unconnected materials. The resolver receives + /// the relative path followed by the absolute path's leaf, in preference order. + pub fn diffuse_materials(&self, limits: &Limits, resolve: &mut dyn FnMut(&[&str]) -> Option>) -> Result> { + let objects = self.top("Objects").ok_or_else(|| malformed(KIND, "no Objects section"))?; + let mut materials = HashMap::new(); + let mut textures = HashMap::new(); + let mut videos = HashMap::new(); + for record in &objects.children { + let Some(id) = key(record.props.first()) else { continue }; + match record.name.as_str() { + "Material" => { + materials.insert(id, record); + } + "Texture" => { + textures.insert(id, record); + } + "Video" => { + videos.insert(id, record); + } + _ => {} + } + } + if materials.len() > limits.max_materials { + return Err(too_large(KIND, "too many materials")); + } + let mut video_of = HashMap::new(); + let mut diffuse = HashMap::new(); + for link in self.top("Connections").into_iter().flat_map(|c| c.children.iter()).filter(|c| c.name == "C" || c.name == "Connect") { + let (Some(child), Some(parent)) = (key(link.props.get(1)), key(link.props.get(2))) else { continue }; + if videos.contains_key(&child) && textures.contains_key(&parent) { + video_of.insert(parent, child); + } else if textures.contains_key(&child) && materials.contains_key(&parent) { + match (link.str_at(0), link.str_at(3)) { + (Some("OP"), Some("DiffuseColor" | "Maya|baseColor")) => { + diffuse.insert(parent, child); + } + (Some("OO"), _) => { + diffuse.entry(parent).or_insert(child); + } + _ => {} + } + } + } + let budget = Budget { left: Cell::new(self.left.min(limits.max_data_bytes)), limit: limits.max_data_bytes }; + let mut out = Vec::new(); + for (id, material) in materials { + let tex = diffuse.get(&id).and_then(|t| textures.get(t)).copied(); + let video = diffuse.get(&id).and_then(|t| video_of.get(t)).and_then(|v| videos.get(v)).copied(); + let mut image = None; + if let Some(content) = video.or(tex).and_then(|n| n.child("Content")) { + match content.props.first() { + Some(Prop::Bytes(bytes)) if !bytes.is_empty() => { + budget.take(Some(bytes.len()), "an embedded image")?; + image = Some(bytes.clone()); + } + Some(Prop::Str(_)) => { + let size = content.props.iter().filter_map(Prop::as_str).try_fold(0usize, |n, s| n.checked_add(s.len())); + // Reserve a conservative bound before concatenating and decoding. + budget.take(size, "an embedded image")?; + let text: String = content.props.iter().filter_map(Prop::as_str).collect(); + image = base64_decode(text.trim()).filter(|b| !b.is_empty()); + } + _ => {} + } + } + if image.is_none() { + for record in [tex, video].into_iter().flatten() { + let relative = record.child("RelativeFilename").and_then(|n| n.str_at(0)).unwrap_or(""); + let absolute = ["FileName", "Filename"] + .into_iter() + .find_map(|n| record.child(n).and_then(|v| v.str_at(0)).filter(|s| !s.is_empty())) + .unwrap_or(""); + let leaf = absolute.rsplit(['/', '\\']).next().unwrap_or(""); + let mut paths = Vec::new(); + for path in [relative, leaf] { + if !path.is_empty() && !paths.contains(&path) { + paths.push(path); + } + } + if !paths.is_empty() { + if let Some(bytes) = resolve(&paths) { + budget.take(Some(bytes.len()), "a resolved image")?; + image = Some(bytes); + break; + } + } + } + } + let props = Props::of(material); + let values = props.values("DiffuseColor"); + let factor = match values.as_slice() { + [r, g, b, ..] => { + let f = props.f("DiffuseFactor").unwrap_or(1.0); + [(*r * f) as f32, (*g * f) as f32, (*b * f) as f32, 1.0] + } + _ => [1.0; 4], + }; + if factor.iter().any(|v| !v.is_finite()) || (image.is_none() && values.len() < 3) { + continue; + } + let raw_name = short_name(material.first_str().unwrap_or("")); + let name = if raw_name.is_empty() { + match id { + Key::Id(i) => format!("Material_{i}"), + Key::Name(s) => format!("Material_{s}"), + } + } else { + raw_name.to_string() + }; + out.push(DiffuseMaterial { name, factor, image }); + } + out.sort_by(|a, b| a.name.cmp(&b.name)); + Ok(out) + } +} + /// What connections name an object by: its id, or its `Class::Name`. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] enum Key<'a> { diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index a58ddeb19..561d0882b 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -627,3 +627,31 @@ fn fbx6_model_holds_its_mesh() { assert_eq!(d.unit_scale(), Some(100.0)); } } + +#[test] +fn diffuse_materials_preserve_factors_and_texture_candidates() { + let text = with( + "Texture: 600, \"Texture::Base\", \"\" { RelativeFilename: \"maps/base.png\" FileName: \"C:/a/base.png\" }\n", + "C: \"OP\",600,400,\"Maya|baseColor\"\n", + ); + let doc = document(text.as_bytes()); + let mut asked = Vec::new(); + let materials = doc + .diffuse_materials(&Limits::default(), &mut |paths| { + asked.push(paths.iter().map(|s| s.to_string()).collect::>()); + Some(b"image".to_vec()) + }) + .unwrap(); + assert_eq!(asked, vec![vec!["maps/base.png", "base.png"]]); + let red = materials.iter().find(|m| m.name == "Red").unwrap(); + assert_eq!(red.factor, [1.0, 0.0, 0.0, 1.0]); + assert_eq!(red.image.as_deref(), Some(&b"image"[..])); + let small = Limits { max_data_bytes: 4, ..Limits::default() }; + assert!(matches!(doc.diffuse_materials(&small, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_)))); + let embedded_text = with( + &format!("Texture: 600, \"Texture::Base\", \"\" {{}}\nVideo: 700, \"Video::Base\", \"Clip\" {{ Content: \"{}\" }}\n", b64(b"embedded")), + "C: \"OP\",600,400,\"DiffuseColor\"\nC: \"OO\",700,600\n", + ); + let embedded = document(embedded_text.as_bytes()).diffuse_materials(&Limits::default(), &mut |_| None).unwrap(); + assert_eq!(embedded.iter().find(|m| m.name == "Red").unwrap().image.as_deref(), Some(&b"embedded"[..])); +} From d8e0746d6bb62bb188253d9172fa57b2b2abac20 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:33:08 +0000 Subject: [PATCH 06/22] Share bounded FBX texture packages --- Cargo.lock | 1 + crates/mesh/Cargo.toml | 3 +- crates/mesh/src/fbx.rs | 3 + crates/mesh/src/fbx/package.rs | 151 +++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 crates/mesh/src/fbx/package.rs diff --git a/Cargo.lock b/Cargo.lock index b3325f790..0e981a8c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2126,6 +2126,7 @@ version = "0.1.0" dependencies = [ "craft-opc", "craft-xml", + "craft-zip", "miniz_oxide 0.9.1", "serde_json", ] diff --git a/crates/mesh/Cargo.toml b/crates/mesh/Cargo.toml index 8c95d3734..861c19239 100644 --- a/crates/mesh/Cargo.toml +++ b/crates/mesh/Cargo.toml @@ -16,13 +16,14 @@ stl = [] obj = [] ply = [] gltf = ["dep:serde_json"] -fbx = ["dep:miniz_oxide"] +fbx = ["dep:miniz_oxide", "dep:craft-zip"] threemf = ["dep:craft-opc", "dep:craft-xml"] [dependencies] serde_json = { workspace = true, optional = true } # zlib-compressed arrays in binary FBX. miniz_oxide = { version = "0.9", optional = true } +craft-zip = { workspace = true, optional = true } # 3MF is an OPC package of XML parts. craft-opc = { workspace = true, optional = true } craft-xml = { workspace = true, optional = true } diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index e8fd78096..cc9a0b003 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -54,6 +54,9 @@ use crate::{ count_warning, malformed, mat_mul, srgb_to_linear, too_large, }; +/// Bounded ZIP packages containing an FBX and its texture files. +pub mod package; + const KIND: &str = "FBX"; /// The binary file's magic (`Kaydara FBX Binary`, two spaces, NUL, 0x1A, 0x00). diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs new file mode 100644 index 000000000..4f49f5ec5 --- /dev/null +++ b/crates/mesh/src/fbx/package.rs @@ -0,0 +1,151 @@ +//! FBX and referenced texture files in a ZIP container. +use super::{Document, KIND}; +use crate::{Error, Limits, Result, malformed, too_large}; + +const ZIP_MAGIC: &[u8] = b"PK\x03\x04"; + +/// Limits for an FBX package and each referenced texture. +#[derive(Clone, Copy, Debug)] +pub struct PackageLimits { + pub max_entries: u64, + pub max_model_bytes: u64, + pub max_texture_bytes: u64, + pub max_total_bytes: u64, +} + +/// An open FBX package. Reads are charged to its cumulative decompression budget. +pub struct Package<'a> { + zip: craft_zip::Archive<'a>, + limits: PackageLimits, + name: String, +} + +impl<'a> Package<'a> { + pub fn open(bytes: &'a [u8], limits: PackageLimits) -> Result { + let zip = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries, max_total_bytes: limits.max_total_bytes }) + .map_err(|e| malformed(KIND, format!("package: {e}")))?; + let name = zip + .names() + .find(|n| n.rsplit('.').next().is_some_and(|ext| ext.eq_ignore_ascii_case("fbx"))) + .map(|n| n.into_owned()) + .ok_or_else(|| malformed(KIND, "the package contains no FBX file"))?; + Ok(Self { zip, limits, name }) + } + + pub fn model(&mut self) -> Result> { + let bytes = self + .zip + .read(&self.name, self.limits.max_model_bytes) + .map_err(|e| malformed(KIND, format!("package: {e}")))? + .ok_or_else(|| malformed(KIND, "package FBX is missing"))?; + if bytes.starts_with(ZIP_MAGIC) { + return Err(malformed(KIND, "the package holds another package")); + } + Ok(bytes) + } + + /// Resolve candidate names within the package; outside paths are never read. + pub fn texture(&mut self, paths: &[&str]) -> Option> { + paths.iter().filter_map(|p| below(p)).find_map(|name| self.zip.read(&name, self.limits.max_texture_bytes).ok().flatten()) + } +} + +/// Resolve a safe relative path inside a package. +fn below(path: &str) -> Option { + let path = path.replace('\\', "/"); + if path.starts_with('/') || path.contains(':') { + return None; + } + let mut parts = Vec::new(); + for part in path.split('/') { + match part { + "" | "." => {} + ".." => { + parts.pop()?; + } + part => parts.push(part), + } + } + (!parts.is_empty()).then(|| parts.join("/")) +} + +/// Package the FBX and its externally resolved diffuse textures, if any. Returns unresolved paths. +pub fn write( + doc: &Document, + file_name: &str, + bytes: &[u8], + limits: &Limits, + package_limits: PackageLimits, + found: &mut dyn FnMut(&str) -> Option>, +) -> Result<(Option>, Vec)> { + if bytes.starts_with(ZIP_MAGIC) { + return Ok((None, Vec::new())); + } + let mut files: Vec<(String, Vec)> = Vec::new(); + let mut missing = Vec::new(); + let mut total = bytes.len() as u64; + let mut exceeded = total > package_limits.max_total_bytes || total > package_limits.max_model_bytes; + doc.diffuse_materials(limits, &mut |paths| { + if let Some((_, data)) = files.iter().find(|(n, _)| paths.iter().any(|p| below(p).as_ref() == Some(n))) { + return Some(data.clone()); + } + for path in paths { + if let Some(name) = below(path) { + if let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) { + total = total.saturating_add(data.len() as u64); + if total > package_limits.max_total_bytes { + exceeded = true; + return None; + } + files.push((name, data.clone())); + return Some(data); + } + } + } + if let Some(first) = paths.first().filter(|p| !missing.iter().any(|m| m == **p)) { + missing.push((*first).to_string()); + } + None + })?; + if exceeded { + return Err(too_large(KIND, "the FBX and its textures exceed the package limit")); + } + if files.is_empty() { + return Ok((None, missing)); + } + if files.len() as u64 >= package_limits.max_entries { + return Err(too_large(KIND, "too many package entries")); + } + let leaf = file_name.rsplit(['/', '\\']).next().filter(|n| !n.is_empty()).unwrap_or("mesh.fbx"); + let mut writer = craft_zip::Writer::new(); + for (name, data) in std::iter::once((leaf, bytes)).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))) { + writer.add(name, data, craft_zip::Compression::Stored).map_err(|e| Error::Malformed(format!("FBX package: {e}")))?; + } + Ok((Some(writer.finish().map_err(|e| Error::Malformed(format!("FBX package: {e}")))?), missing)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::fbx::read_document; + + const MODEL: &[u8] = b"FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Material: 1, \"Material::M\", \"\" { } Texture: 2, \"Texture::T\", \"\" { RelativeFilename: \"tex/a.png\" } } Connections: { C: \"OP\",2,1,\"DiffuseColor\" }"; + fn bounds() -> PackageLimits { + PackageLimits { max_entries: 4, max_model_bytes: 4096, max_texture_bytes: 32, max_total_bytes: 8192 } + } + #[test] + fn package_roundtrip_and_limits() { + let limits = Limits::default(); + let doc = read_document(MODEL, &limits).unwrap(); + let (bytes, missing) = write(&doc, "asset.fbx", MODEL, &limits, bounds(), &mut |p| (p == "tex/a.png").then(|| b"image".to_vec())).unwrap(); + assert!(missing.is_empty()); + let bytes = bytes.unwrap(); + let mut package = Package::open(&bytes, bounds()).unwrap(); + assert_eq!(package.model().unwrap(), MODEL); + assert_eq!(package.texture(&["../tex/a.png", "tex/a.png"]), Some(b"image".to_vec())); + assert!(package.texture(&["../tex/a.png"]).is_none()); + let small = PackageLimits { max_total_bytes: MODEL.len() as u64 + 4, ..bounds() }; + assert!(matches!(write(&doc, "a.fbx", MODEL, &limits, small, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_)))); + assert!(Package::open(&bytes, PackageLimits { max_entries: 1, ..bounds() }).is_err()); + } +} From ac4a6b79a71167cbc23d2e244ca643512b8fd6eb Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:34:21 +0000 Subject: [PATCH 07/22] Expose FBX package detection --- crates/mesh/src/fbx/package.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs index 4f49f5ec5..6605194fc 100644 --- a/crates/mesh/src/fbx/package.rs +++ b/crates/mesh/src/fbx/package.rs @@ -4,6 +4,11 @@ use crate::{Error, Limits, Result, malformed, too_large}; const ZIP_MAGIC: &[u8] = b"PK\x03\x04"; +/// Whether bytes begin with a ZIP local-file header. +pub fn is_package(bytes: &[u8]) -> bool { + bytes.starts_with(ZIP_MAGIC) +} + /// Limits for an FBX package and each referenced texture. #[derive(Clone, Copy, Debug)] pub struct PackageLimits { @@ -38,7 +43,7 @@ impl<'a> Package<'a> { .read(&self.name, self.limits.max_model_bytes) .map_err(|e| malformed(KIND, format!("package: {e}")))? .ok_or_else(|| malformed(KIND, "package FBX is missing"))?; - if bytes.starts_with(ZIP_MAGIC) { + if is_package(bytes) { return Err(malformed(KIND, "the package holds another package")); } Ok(bytes) @@ -78,7 +83,7 @@ pub fn write( package_limits: PackageLimits, found: &mut dyn FnMut(&str) -> Option>, ) -> Result<(Option>, Vec)> { - if bytes.starts_with(ZIP_MAGIC) { + if is_package(bytes) { return Ok((None, Vec::new())); } let mut files: Vec<(String, Vec)> = Vec::new(); From 138f4e273b90afc6d739e3c975c71a10bf9b225e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:37:59 +0000 Subject: [PATCH 08/22] Borrow package bytes for detection --- crates/mesh/src/fbx/package.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs index 6605194fc..4d78c95f1 100644 --- a/crates/mesh/src/fbx/package.rs +++ b/crates/mesh/src/fbx/package.rs @@ -43,7 +43,7 @@ impl<'a> Package<'a> { .read(&self.name, self.limits.max_model_bytes) .map_err(|e| malformed(KIND, format!("package: {e}")))? .ok_or_else(|| malformed(KIND, "package FBX is missing"))?; - if is_package(bytes) { + if is_package(&bytes) { return Err(malformed(KIND, "the package holds another package")); } Ok(bytes) From f658926eb18daf583437cd7a179725b097c5cae7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:40:50 +0000 Subject: [PATCH 09/22] Offer strict FBX ASCII record parsing --- crates/mesh/src/fbx.rs | 36 ++++++++++++++++++++++++++++++++---- crates/mesh/src/tests/fbx.rs | 10 ++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 49191c0c0..6c2d92e32 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -230,6 +230,15 @@ pub fn is_binary(bytes: &[u8]) -> bool { /// Read an FBX file (binary or ASCII) into its records. pub fn read_document(bytes: &[u8], limits: &Limits) -> Result { + read_document_impl(bytes, limits, false) +} + +/// Read records with strict ASCII syntax checking for malformed strings and record delimiters. +pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result { + read_document_impl(bytes, limits, true) +} + +fn read_document_impl(bytes: &[u8], limits: &Limits, strict: bool) -> Result { let budget = Budget::new(limits.max_data_bytes); let (version, records) = if is_binary(bytes) { read_binary(bytes, &budget)? @@ -243,7 +252,7 @@ pub fn read_document(bytes: &[u8], limits: &Limits) -> Result { String::from_utf8_lossy(bytes) } }; - read_ascii(&text, &budget)? + read_ascii(&text, &budget, strict)? }; Ok(Document { version, records, left: budget.left.get() }) } @@ -441,6 +450,8 @@ struct Ascii<'a> { text: &'a str, pos: usize, lookahead: Option>, + strict: bool, + unterminated: bool, } impl<'a> Ascii<'a> { @@ -468,6 +479,7 @@ impl<'a> Ascii<'a> { '"' => { let tail = rest.get(1..).unwrap_or(""); let end = tail.find('"').unwrap_or(tail.len()); + self.unterminated |= end == tail.len(); self.pos += 1 + end + usize::from(end < tail.len()); return Some(Tok::Value(tail.get(..end).unwrap_or(""), true)); } @@ -512,11 +524,14 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de while let Some(t) = scan.take() { match t { Tok::Close if depth > 0 => return Ok(out), + Tok::Close if scan.strict => return Err(malformed(KIND, "unbalanced closing brace")), Tok::Key(name) => { records_budget.take(std::mem::size_of::().checked_add(name.len()), "its records")?; let mut record = Record { name: name.to_string(), ..Record::default() }; + let mut needs_comma = false; loop { match scan.peek() { + Some(Tok::Value(..) | Tok::Count) if scan.strict && needs_comma => break, Some(Tok::Value(value, quoted)) => { let number = if quoted { None @@ -530,9 +545,15 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de .props .push(number.unwrap_or_else(|| Prop::Str(if quoted { value.replace(""", "\"") } else { value.to_string() }))); } - Some(Tok::Count | Tok::Comma) => {} + Some(Tok::Count) => {} + Some(Tok::Comma) => { + needs_comma = false; + scan.take(); + continue; + } _ => break, } + needs_comma = true; scan.take(); } if scan.peek() == Some(Tok::Open) { @@ -541,16 +562,23 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de } out.push(record); } + _ if scan.strict => return Err(malformed(KIND, "expected a property name")), _ => {} } } + if scan.strict && depth > 0 { + return Err(malformed(KIND, "missing closing brace")); + } Ok(out) } -fn read_ascii(text: &str, budget: &Budget) -> Result<(u32, Vec)> { - let mut scan = Ascii { text, pos: 0, lookahead: None }; +fn read_ascii(text: &str, budget: &Budget, strict: bool) -> Result<(u32, Vec)> { + let mut scan = Ascii { text, pos: 0, lookahead: None, strict, unterminated: false }; let records_budget = Budget::new(budget.limit); let records = ascii_list(&mut scan, budget, &records_budget, 0)?; + if strict && scan.unterminated { + return Err(malformed(KIND, "unterminated string")); + } let version = records .iter() .find(|n| n.name == "FBXHeaderExtension") diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index d5978883e..9541d67cb 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -641,3 +641,13 @@ fn ascii_budget_applies_while_reading_values_and_records() { let ignored = format!(";{}\nObjects: {{}}", ",".repeat(10_000)); assert!(read_document(ignored.as_bytes(), &limits).is_ok()); } + +#[test] +fn strict_ascii_reports_unbalanced_and_unterminated_records() { + let strict = |s: &str| crate::fbx::read_document_strict(s.as_bytes(), &Limits::default()); + assert!(strict(ASCII).is_ok()); + for s in ["Objects: { A: 1", "Objects: { } }", "Objects: { A: \"unterminated", "Objects: { A: 1 2 }"] { + assert!(strict(s).is_err(), "{s}"); + } + assert!(read_document(b"Objects: { A: 1", &Limits::default()).is_ok()); +} From a4977030c06a0330d71c84daece008733ce37c4d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:45:14 +0000 Subject: [PATCH 10/22] Preserve FBX material slot assignments --- crates/mesh/src/fbx.rs | 34 ++++++++++++++++++++++++++-------- crates/mesh/src/tests/fbx.rs | 10 ++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index acb654513..f0b52ff3b 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -1046,7 +1046,18 @@ impl<'a> Build<'a, '_> { return Ok(Some(*m)); } let Some(record) = self.by_key.get(&mid).copied() else { return Ok(None) }; - let mut m = material(short_name(record.first_str().unwrap_or("")), record); + let name = short_name(record.first_str().unwrap_or("")); + let fallback; + let name = if name.is_empty() { + fallback = match mid { + Key::Id(i) => format!("Material_{i}"), + Key::Name(s) => format!("Material_{s}"), + }; + &fallback + } else { + name + }; + let mut m = material(name, record); for (tid, prop) in self.children_of(mid, "Texture") { let slot = match prop { Some("DiffuseColor") => &mut m.base_color_texture, @@ -1081,9 +1092,14 @@ impl<'a> Build<'a, '_> { pool("vertices", positions.len())?; let normals = Layer::of(geom, "LayerElementNormal", "Normals", &["NormalsIndex", "NormalIndex"], 3); let uvs = Layer::of(geom, "LayerElementUV", "UV", &["UVIndex"], 2); - let slots = geom.child("LayerElementMaterial").and_then(|e| { + let slots = geom.children_named("LayerElementMaterial").min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)).and_then(|e| { let mapping = e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("AllSame"); - Some((mapping == "ByPolygon", e.child("Materials")?.ints())) + let by_poly = match mapping { + "ByPolygon" | "ByPolygonVertex" | "ByVertex" | "ByVertice" | "ByControlPoint" => true, + "AllSame" => false, + _ => return None, + }; + Some((by_poly, e.child("Materials")?.ints())) }); let (normal_layer, uv_layer) = (normals.as_ref().map(|l| &l.0), uvs.as_ref().map(|l| &l.0)); @@ -1118,11 +1134,13 @@ impl<'a> Build<'a, '_> { if self.triangles > limits.max_triangles { return Err(too_large(KIND, format!("more than {} triangles", limits.max_triangles))); } - let slot = slots.as_ref().and_then(|(by_poly, ids)| { - let k = if *by_poly { *ids.get(p_index)? } else { *ids.first()? }; - materials.get(usize::try_from(k).ok()?).copied() - }); - let slot = slot.or_else(|| materials.first().copied()); + let slot = match &slots { + Some((by_poly, ids)) => { + let k = if *by_poly { ids.get(p_index) } else { ids.first() }; + k.and_then(|k| usize::try_from(*k).ok()).and_then(|k| materials.get(k)).copied() + } + None => materials.first().copied(), + }; let at = match part_of.get(&slot) { Some(at) => *at, None => { diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index a58ddeb19..afa410dca 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -627,3 +627,13 @@ fn fbx6_model_holds_its_mesh() { assert_eq!(d.unit_scale(), Some(100.0)); } } + +#[test] +fn fbx_invalid_material_slot_is_unassigned_and_unnamed_material_has_id() { + let text = ASCII.replace("a: 0,1\n", "a: 0,99\n").replace("\"Material::Red\"", "\"\""); + let scene = parse(text.as_bytes()).unwrap(); + assert!(scene.materials.iter().any(|m| m.name == "Material_400")); + let parts = &scene.meshes[0].parts; + assert!(parts.iter().any(|p| p.material.is_none())); + assert!(parts.iter().any(|p| p.material.is_some())); +} From ad0205e903780046a587f4842df65fc0bed43785 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:47:54 +0000 Subject: [PATCH 11/22] Bound binary FBX record and property allocations --- crates/mesh/src/fbx.rs | 21 +++++++++++++++++---- crates/mesh/src/tests/fbx.rs | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index acb654513..d01cbc4ba 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -254,6 +254,7 @@ struct Bin<'a> { /// 64-bit record offsets (version 7500 and later). wide: bool, budget: &'a Budget, + records_budget: &'a Budget, } impl Bin<'_> { @@ -313,6 +314,8 @@ impl Bin<'_> { if end <= start || end > limit.min(self.d.len()) { return Err(malformed(KIND, format!("bad record end {end} at byte {start}"))); } + // Bound record and property-vector allocations before copying names or reserving storage. + self.records_budget.take(std::mem::size_of::().checked_add(nlen.saturating_mul(3)), "its records")?; let name = String::from_utf8_lossy(self.take(pos, nlen)?).into_owned(); let pstart = *pos; let pend = @@ -321,7 +324,9 @@ impl Bin<'_> { if nprops > plen as u64 { return Err(malformed(KIND, format!("`{name}`: {nprops} properties in {plen} bytes"))); } - let mut props = Vec::with_capacity(nprops as usize); + let count = usize::try_from(nprops).map_err(|_| too_large(KIND, "too many properties"))?; + self.records_budget.take(count.checked_mul(std::mem::size_of::()), "its properties")?; + let mut props = Vec::with_capacity(count); for _ in 0..nprops { props.push(self.prop(pos, pend)?); } @@ -332,7 +337,12 @@ impl Bin<'_> { } fn prop(&self, pos: &mut usize, pend: usize) -> Result { - let sub = Bin { d: self.d.get(..pend).ok_or_else(|| malformed(KIND, "truncated property"))?, wide: self.wide, budget: self.budget }; + let sub = Bin { + d: self.d.get(..pend).ok_or_else(|| malformed(KIND, "truncated property"))?, + wide: self.wide, + budget: self.budget, + records_budget: self.records_budget, + }; let t = sub.arr::<1>(pos)?[0]; Ok(match t { b'Y' => Prop::Int(i64::from(i16::from_le_bytes(sub.arr(pos)?))), @@ -343,10 +353,12 @@ impl Bin<'_> { b'D' => Prop::Float(f64::from_le_bytes(sub.arr(pos)?)), b'S' => { let n = sub.u32(pos)? as usize; + sub.budget.take(n.checked_mul(3), "a string")?; Prop::Str(object_name(sub.take(pos, n)?)) } b'R' => { let n = sub.u32(pos)? as usize; + sub.budget.take(Some(n), "binary data")?; Prop::Bytes(sub.take(pos, n)?.to_vec()) } b'f' | b'd' | b'l' | b'i' | b'b' => { @@ -400,8 +412,9 @@ fn object_name(b: &[u8]) -> String { fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec)> { let mut pos = BINARY_MAGIC.len(); - let version = Bin { d: bytes, wide: false, budget }.u32(&mut pos)?; - let bin = Bin { d: bytes, wide: version >= 7500, budget }; + let records_budget = Budget::new(budget.limit); + let version = Bin { d: bytes, wide: false, budget, records_budget: &records_budget }.u32(&mut pos)?; + let bin = Bin { d: bytes, wide: version >= 7500, budget, records_budget: &records_budget }; let mut records = vec![]; while pos < bytes.len() { match bin.record(&mut pos, bytes.len(), 0) { diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index a58ddeb19..b5464b829 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -627,3 +627,22 @@ fn fbx6_model_holds_its_mesh() { assert_eq!(d.unit_scale(), Some(100.0)); } } + +#[test] +fn binary_scalar_properties_are_bounded_before_reserving() { + let mut bytes = BINARY_MAGIC.to_vec(); + bytes.extend(7400u32.to_le_bytes()); + let count = 1000u32; + let end = bytes.len() + 13 + 1 + count as usize * 2; + bytes.extend((end as u32).to_le_bytes()); + bytes.extend(count.to_le_bytes()); + bytes.extend((count * 2).to_le_bytes()); + bytes.push(1); + bytes.push(b'A'); + for _ in 0..count { + bytes.extend([b'C', 1]); + } + let limits = Limits { max_data_bytes: 1024, ..Limits::default() }; + assert!(matches!(read_document(&bytes, &limits), Err(Error::TooLarge(_)))); + assert!(read_document(&bytes, &Limits::default()).is_ok()); +} From 22bbfe934a275081742851197dfb54c3e5102daf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:52:22 +0000 Subject: [PATCH 12/22] Require a binary FBX closing record in strict mode --- crates/mesh/src/fbx.rs | 17 ++++++++++++----- crates/mesh/src/tests/fbx.rs | 10 ++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 6c2d92e32..fcec50728 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -233,7 +233,7 @@ pub fn read_document(bytes: &[u8], limits: &Limits) -> Result { read_document_impl(bytes, limits, false) } -/// Read records with strict ASCII syntax checking for malformed strings and record delimiters. +/// Read records strictly, checking ASCII delimiters and the binary closing record. pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result { read_document_impl(bytes, limits, true) } @@ -241,7 +241,7 @@ pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result { fn read_document_impl(bytes: &[u8], limits: &Limits, strict: bool) -> Result { let budget = Budget::new(limits.max_data_bytes); let (version, records) = if is_binary(bytes) { - read_binary(bytes, &budget)? + read_binary(bytes, &budget, strict)? } else { let text = match std::str::from_utf8(bytes) { Ok(text) => std::borrow::Cow::Borrowed(text), @@ -415,20 +415,27 @@ fn object_name(b: &[u8]) -> String { } } -fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec)> { +fn read_binary(bytes: &[u8], budget: &Budget, strict: bool) -> Result<(u32, Vec)> { let mut pos = BINARY_MAGIC.len(); let version = Bin { d: bytes, wide: false, budget }.u32(&mut pos)?; let bin = Bin { d: bytes, wide: version >= 7500, budget }; let mut records = vec![]; + let mut closed = false; while pos < bytes.len() { match bin.record(&mut pos, bytes.len(), 0) { Ok(Some(n)) => records.push(n), - Ok(None) => break, + Ok(None) => { + closed = true; + break; + } // A damaged tail (footer, padding) after complete sections is ignored. - Err(crate::Error::Malformed(_)) if records.iter().any(|n| n.name == "Objects") => break, + Err(crate::Error::Malformed(_)) if !strict && records.iter().any(|n| n.name == "Objects") => break, Err(e) => return Err(e), } } + if strict && !closed { + return Err(malformed(KIND, "missing binary closing record")); + } Ok((version, records)) } diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index 9541d67cb..b65643021 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -651,3 +651,13 @@ fn strict_ascii_reports_unbalanced_and_unterminated_records() { } assert!(read_document(b"Objects: { A: 1", &Limits::default()).is_ok()); } + +#[test] +fn strict_binary_requires_a_closing_record() { + let doc = document(ASCII.as_bytes()); + let bytes = write_binary(&doc.records, 7400, false); + assert!(crate::fbx::read_document_strict(&bytes, &Limits::default()).is_ok()); + let truncated = &bytes[..bytes.len() - 29]; + assert!(read_document(truncated, &Limits::default()).is_ok()); + assert!(crate::fbx::read_document_strict(truncated, &Limits::default()).is_err()); +} From 827eddfdbd6e3ce639cfb5f48dcfff37c2e9c5bd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:54:08 +0000 Subject: [PATCH 13/22] Fix FBX package clippy warning --- crates/mesh/src/fbx/package.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs index 4d78c95f1..fce07d037 100644 --- a/crates/mesh/src/fbx/package.rs +++ b/crates/mesh/src/fbx/package.rs @@ -95,16 +95,16 @@ pub fn write( return Some(data.clone()); } for path in paths { - if let Some(name) = below(path) { - if let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) { - total = total.saturating_add(data.len() as u64); - if total > package_limits.max_total_bytes { - exceeded = true; - return None; - } - files.push((name, data.clone())); - return Some(data); + if let Some(name) = below(path) + && let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) + { + total = total.saturating_add(data.len() as u64); + if total > package_limits.max_total_bytes { + exceeded = true; + return None; } + files.push((name, data.clone())); + return Some(data); } } if let Some(first) = paths.first().filter(|p| !missing.iter().any(|m| m == **p)) { From d4c2eb7b74407acf1de301173f5e2ded3e542c87 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:54:08 +0000 Subject: [PATCH 14/22] Fix FBX color clippy warning --- crates/mesh/src/fbx.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 62e11b0eb..64e018bb5 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -1134,10 +1134,10 @@ impl<'a> Build<'a, '_> { positions.push(original); vertex_colors.push([1.0; 4]); } - if let Some(dst) = vertex_colors.get_mut(position as usize) { - if let [r, g, b, a] = rgba { - *dst = [finite(*r) as f32, finite(*g) as f32, finite(*b) as f32, finite(*a) as f32]; - } + if let Some(dst) = vertex_colors.get_mut(position as usize) + && let [r, g, b, a] = rgba + { + *dst = [finite(*r) as f32, finite(*g) as f32, finite(*b) as f32, finite(*a) as f32]; } color_indices.insert((p, color), position); } From 0984808dd7361061c94054c6d4871782040ce348 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:54:08 +0000 Subject: [PATCH 15/22] Fix FBX diffuse clippy warning --- crates/mesh/src/fbx.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index e8fd78096..f022d6052 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -918,12 +918,12 @@ impl Document { paths.push(path); } } - if !paths.is_empty() { - if let Some(bytes) = resolve(&paths) { - budget.take(Some(bytes.len()), "a resolved image")?; - image = Some(bytes); - break; - } + if !paths.is_empty() + && let Some(bytes) = resolve(&paths) + { + budget.take(Some(bytes.len()), "a resolved image")?; + image = Some(bytes); + break; } } } From 9b43d91e1d0e00c9aaf69522dd6d9394118f2017 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:57:25 +0000 Subject: [PATCH 16/22] Bound FBX color seam staging memory --- crates/mesh/src/fbx.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 64e018bb5..6c6092440 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -1124,6 +1124,16 @@ impl<'a> Build<'a, '_> { if let Some(existing) = color_indices.get(&(p, color)) { position = *existing; } else { + // Color seams need two maps as well as the output pools; do not + // grow them past the remaining file budget on hostile corner lists. + let staging = color_indices + .len() + .checked_add(1) + .and_then(|n| n.checked_mul(64)) + .and_then(|n| positions.len().checked_mul(40).and_then(|pool| n.checked_add(pool))); + if staging.is_none_or(|n| n > self.budget.left.get()) { + return Err(too_large(KIND, "color seams exceed the memory limit")); + } if first_color.insert(p, color).is_some() { pool("vertices", positions.len().saturating_add(1))?; if positions.len().saturating_add(1).checked_mul(40).is_none_or(|n| n > self.budget.left.get()) { From 1448b89a2ae604f0412faad45ae97c078943a343 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:20:35 +0100 Subject: [PATCH 17/22] Fix merged FBX test boundaries --- crates/mesh/src/tests/fbx.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index 33567c746..15ecfdac6 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -771,6 +771,9 @@ fn fbx_corner_colors_keep_seams_and_incomplete_layers_are_omitted() { assert!(colors.contains(&[1.0, 0.0, 1.0, 1.0])); let incomplete = text.replace(", 1,0,1,1\n}", "\n}"); assert!(parse(incomplete.as_bytes()).unwrap().meshes[0].colors.is_empty()); +} + +#[test] fn ascii_budget_applies_while_reading_values_and_records() { let limits = Limits { max_data_bytes: 512, ..Limits::default() }; let many_values = format!("Objects: {{\n A: {}\n}}", "1,".repeat(1000)); From 6ec4c3135501e228b6d9cfb8b1788827b51a4255 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:16:29 +0100 Subject: [PATCH 18/22] mesh: FBX material IDs per corner/control point, texture candidate order Addresses CodeRabbit on #1777: ByPolygonVertex/ByControlPoint material IDs use the polygon's first corner instead of the polygon number; an earlier texture candidate is tried before a later cached one; regression test for strict colours reading the validated layer. --- crates/mesh/src/fbx.rs | 28 +++++++++++++++++++++------- crates/mesh/src/fbx/package.rs | 30 ++++++++++++++++++++++++------ crates/mesh/src/tests/fbx.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 6902f54ed..9dc163a47 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -1436,12 +1436,15 @@ impl<'a> Build<'a, '_> { let mut colors_complete = colors.is_some(); let slots = geom.children_named("LayerElementMaterial").min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)).and_then(|e| { let mapping = e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("AllSame"); - let by_poly = match mapping { - "ByPolygon" | "ByPolygonVertex" | "ByVertex" | "ByVertice" | "ByControlPoint" => true, - "AllSame" => false, + // a polygon goes to one part, so per-corner and per-vertex IDs use its first corner + let by = match mapping { + "ByPolygon" => Mapping::Polygon, + "ByPolygonVertex" => Mapping::PolygonVertex, + "ByVertex" | "ByVertice" | "ByControlPoint" => Mapping::Vertex, + "AllSame" => Mapping::AllSame, _ => return None, }; - Some((by_poly, e.child("Materials")?.ints())) + Some((by, e.child("Materials")?.ints())) }); let (normal_layer, uv_layer) = (normals.as_ref().map(|l| &l.0), uvs.as_ref().map(|l| &l.0)); @@ -1451,7 +1454,8 @@ impl<'a> Build<'a, '_> { let (mut broken, mut poly) = (false, 0usize); let polygon_indices = pvi.ints(); let index_count = polygon_indices.len(); - for (pv, raw) in polygon_indices.into_iter().enumerate() { + let mut poly_start = 0usize; + for (pv, raw) in polygon_indices.iter().copied().enumerate() { // The last corner of a polygon is stored as `-index - 1`. let (v, last) = if raw < 0 { (!raw, true) } else { (raw, self.strict_geometry && pv + 1 == index_count) }; match u32::try_from(v).ok().filter(|v| (*v as usize) < original_positions) { @@ -1512,6 +1516,7 @@ impl<'a> Build<'a, '_> { continue; } let (corners, p_index, was_broken) = (std::mem::take(&mut face), poly, std::mem::take(&mut broken)); + let first_pv = std::mem::replace(&mut poly_start, pv + 1); poly += 1; if was_broken || corners.len() < 3 { self.skipped += 1; @@ -1522,8 +1527,17 @@ impl<'a> Build<'a, '_> { return Err(too_large(KIND, format!("more than {} triangles", limits.max_triangles))); } let slot = match &slots { - Some((by_poly, ids)) => { - let k = if *by_poly { ids.get(p_index) } else { ids.first() }; + Some((by, ids)) => { + let k = match by { + Mapping::Polygon => ids.get(p_index), + Mapping::PolygonVertex => ids.get(first_pv), + Mapping::Vertex => polygon_indices + .get(first_pv) + .map(|&r| if r < 0 { !r } else { r }) + .and_then(|v| usize::try_from(v).ok()) + .and_then(|v| ids.get(v)), + Mapping::AllSame => ids.first(), + }; k.and_then(|k| usize::try_from(*k).ok()).and_then(|k| materials.get(k)).copied() } None => materials.first().copied(), diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs index fce07d037..748f6e7f4 100644 --- a/crates/mesh/src/fbx/package.rs +++ b/crates/mesh/src/fbx/package.rs @@ -91,13 +91,13 @@ pub fn write( let mut total = bytes.len() as u64; let mut exceeded = total > package_limits.max_total_bytes || total > package_limits.max_model_bytes; doc.diffuse_materials(limits, &mut |paths| { - if let Some((_, data)) = files.iter().find(|(n, _)| paths.iter().any(|p| below(p).as_ref() == Some(n))) { - return Some(data.clone()); - } + // candidates in order of preference: an earlier one that exists wins over a later cached one for path in paths { - if let Some(name) = below(path) - && let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) - { + let Some(name) = below(path) else { continue }; + if let Some((_, data)) = files.iter().find(|(n, _)| *n == name) { + return Some(data.clone()); + } + if let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) { total = total.saturating_add(data.len() as u64); if total > package_limits.max_total_bytes { exceeded = true; @@ -153,4 +153,22 @@ mod tests { assert!(matches!(write(&doc, "a.fbx", MODEL, &limits, small, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_)))); assert!(Package::open(&bytes, PackageLimits { max_entries: 1, ..bounds() }).is_err()); } + + #[test] + fn an_earlier_candidate_wins_over_a_cached_later_one() { + let model = b"FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Material: 1, \"Material::A\", \"\" { } Texture: 2, \"Texture::A\", \"\" { RelativeFilename: \"base.png\" } Material: 3, \"Material::B\", \"\" { } Texture: 4, \"Texture::B\", \"\" { RelativeFilename: \"maps/base.png\" FileName: \"C:/art/base.png\" } } Connections: { C: \"OP\",2,1,\"DiffuseColor\" C: \"OP\",4,3,\"DiffuseColor\" }"; + let limits = Limits::default(); + let doc = read_document(model, &limits).unwrap(); + let mut found = |p: &str| match p { + "base.png" => Some(b"flat".to_vec()), + "maps/base.png" => Some(b"maps".to_vec()), + _ => None, + }; + let (bytes, missing) = write(&doc, "asset.fbx", model, &limits, bounds(), &mut found).unwrap(); + assert!(missing.is_empty()); + let bytes = bytes.unwrap(); + let mut package = Package::open(&bytes, bounds()).unwrap(); + assert_eq!(package.texture(&["maps/base.png"]), Some(b"maps".to_vec())); + assert_eq!(package.texture(&["base.png"]), Some(b"flat".to_vec())); + } } diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index 3cedd7478..25b58935f 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -844,3 +844,32 @@ fn diffuse_materials_preserve_factors_and_texture_candidates() { let embedded = document(embedded_text.as_bytes()).diffuse_materials(&Limits::default(), &mut |_| None).unwrap(); assert_eq!(embedded.iter().find(|m| m.name == "Red").unwrap().image.as_deref(), Some(&b"embedded"[..])); } + +#[test] +fn per_corner_and_per_vertex_material_ids_use_each_polygons_first_corner() { + let by_corner = ASCII.replace("MappingInformationType: \"ByPolygon\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *2 {\n\t\t\t\ta: 0,1", "MappingInformationType: \"ByPolygonVertex\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *7 {\n\t\t\t\ta: 0,0,0,0,1,1,1"); + assert_ne!(by_corner, ASCII); + let parts = &parse(by_corner.as_bytes()).unwrap().meshes[0].parts; + assert_eq!(parts.len(), 2, "the triangle takes material 1 from its first corner"); + // The triangle starts at control point 4, whose material is 1. + let by_vertex = ASCII.replace("a: 0,1,2,-4,0,1,-5", "a: 0,1,2,-4,4,0,-2").replace( + "MappingInformationType: \"ByPolygon\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *2 {\n\t\t\t\ta: 0,1", + "MappingInformationType: \"ByControlPoint\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *5 {\n\t\t\t\ta: 0,0,0,0,1", + ); + let scene = parse(by_vertex.as_bytes()).unwrap(); + assert_eq!(scene.meshes[0].parts.len(), 2); + assert!(scene.meshes[0].parts.iter().all(|p| p.material.is_some())); +} + +#[test] +fn strict_colors_validate_and_read_the_same_layer() { + let good = "LayerElementColor: 0 { MappingInformationType: \"AllSame\" ReferenceInformationType: \"Direct\" Colors: 0,1,0,1 }\n"; + let bad = "LayerElementColor: 1 { MappingInformationType: \"AllSame\" ReferenceInformationType: \"Direct\" Colors: \"bad\",1,0,0,1 }\n"; + let text = ASCII.replace("LayerElementNormal: 0 {", &format!("{bad} {good} LayerElementNormal: 0 {{")); + let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() }; + let scene = document(text.as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).unwrap(); + assert!(scene.meshes[0].colors.iter().all(|c| *c == [0.0, 1.0, 0.0, 1.0])); + assert!(!scene.meshes[0].colors.is_empty()); + let only_bad = ASCII.replace("LayerElementNormal: 0 {", &format!("{} LayerElementNormal: 0 {{", bad.replace(": 1 {", ": 0 {"))); + assert!(document(only_bad.as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err()); +} From 82597f403dcf4cc7a3b663f7f0d7005a52e6f095 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:13:21 +0100 Subject: [PATCH 19/22] fbx: strict check for ColorsIndex/NormalIndex, stop texture resolution at the entry limit, model entry keeps .fbx --- crates/mesh/src/fbx.rs | 4 +-- crates/mesh/src/fbx/package.rs | 51 ++++++++++++++++++++++++++++++++-- crates/mesh/src/tests/fbx.rs | 13 +++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 9dc163a47..22ac39428 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -1406,10 +1406,10 @@ impl<'a> Build<'a, '_> { return Err(malformed(KIND, "mesh array contains a non-numeric, non-finite or fractional index value")); } for (element, names) in [ - ("LayerElementNormal", &["Normals", "NormalsIndex"][..]), + ("LayerElementNormal", &["Normals", "NormalsIndex", "NormalIndex"][..]), ("LayerElementUV", &["UV", "UVIndex"][..]), ("LayerElementMaterial", &["Materials"][..]), - ("LayerElementColor", &["Colors", "ColorIndex"][..]), + ("LayerElementColor", &["Colors", "ColorIndex", "ColorsIndex"][..]), ] { if let Some(layer) = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)) && names.iter().filter_map(|n| layer.child(n)).any(|r| !valid(r, false)) diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs index 748f6e7f4..01a0e542d 100644 --- a/crates/mesh/src/fbx/package.rs +++ b/crates/mesh/src/fbx/package.rs @@ -87,6 +87,8 @@ pub fn write( return Ok((None, Vec::new())); } let mut files: Vec<(String, Vec)> = Vec::new(); + let mut by_name = std::collections::HashMap::::new(); + let mut too_many = false; let mut missing = Vec::new(); let mut total = bytes.len() as u64; let mut exceeded = total > package_limits.max_total_bytes || total > package_limits.max_model_bytes; @@ -94,8 +96,13 @@ pub fn write( // candidates in order of preference: an earlier one that exists wins over a later cached one for path in paths { let Some(name) = below(path) else { continue }; - if let Some((_, data)) = files.iter().find(|(n, _)| *n == name) { - return Some(data.clone()); + if let Some(data) = by_name.get(&name).and_then(|&i| files.get(i)).map(|(_, d)| d.clone()) { + return Some(data); + } + // the model is an entry too; stop resolving once another texture could not fit + if too_many || files.len() as u64 + 1 >= package_limits.max_entries { + too_many = true; + return None; } if let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) { total = total.saturating_add(data.len() as u64); @@ -103,6 +110,7 @@ pub fn write( exceeded = true; return None; } + by_name.insert(name.clone(), files.len()); files.push((name, data.clone())); return Some(data); } @@ -118,10 +126,13 @@ pub fn write( if files.is_empty() { return Ok((None, missing)); } - if files.len() as u64 >= package_limits.max_entries { + if too_many { return Err(too_large(KIND, "too many package entries")); } let leaf = file_name.rsplit(['/', '\\']).next().filter(|n| !n.is_empty()).unwrap_or("mesh.fbx"); + // the package is found by its `.fbx` entry, so the model always carries that extension + let leaf = if leaf.rsplit_once('.').is_some_and(|(_, ext)| ext.eq_ignore_ascii_case("fbx")) { leaf.to_string() } else { format!("{leaf}.fbx") }; + let leaf = leaf.as_str(); let mut writer = craft_zip::Writer::new(); for (name, data) in std::iter::once((leaf, bytes)).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))) { writer.add(name, data, craft_zip::Compression::Stored).map_err(|e| Error::Malformed(format!("FBX package: {e}")))?; @@ -171,4 +182,38 @@ mod tests { assert_eq!(package.texture(&["maps/base.png"]), Some(b"maps".to_vec())); assert_eq!(package.texture(&["base.png"]), Some(b"flat".to_vec())); } + + #[test] + fn the_model_entry_always_has_an_fbx_extension() { + let limits = Limits::default(); + let doc = read_document(MODEL, &limits).unwrap(); + let (bytes, _) = write(&doc, "dir/asset", MODEL, &limits, bounds(), &mut |_| Some(b"image".to_vec())).unwrap(); + let bytes = bytes.unwrap(); + let mut package = Package::open(&bytes, bounds()).unwrap(); + assert_eq!(package.model().unwrap(), MODEL); + } + + #[test] + fn texture_resolution_stops_at_the_entry_limit() { + let mut model = String::from("FBXHeaderExtension: { FBXVersion: 7400 } Objects: {"); + let mut links = String::new(); + for i in 0..20 { + model.push_str(&format!( + " Material: {m}, \"Material::M{i}\", \"\" {{ }} Texture: {t}, \"Texture::T{i}\", \"\" {{ RelativeFilename: \"t{i}.png\" }}", + m = 2 * i + 1, + t = 2 * i + 2 + )); + links.push_str(&format!(" C: \"OP\",{t},{m},\"DiffuseColor\"", m = 2 * i + 1, t = 2 * i + 2)); + } + model.push_str(&format!(" }} Connections: {{{links} }}")); + let limits = Limits::default(); + let doc = read_document(model.as_bytes(), &limits).unwrap(); + let mut calls = 0; + let result = write(&doc, "a.fbx", model.as_bytes(), &limits, bounds(), &mut |_| { + calls += 1; + Some(b"image".to_vec()) + }); + assert!(matches!(result, Err(Error::TooLarge(_)))); + assert!(calls < bounds().max_entries as usize, "{calls} resolver calls past the entry limit"); + } } diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index 25b58935f..d0761a8df 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -873,3 +873,16 @@ fn strict_colors_validate_and_read_the_same_layer() { let only_bad = ASCII.replace("LayerElementNormal: 0 {", &format!("{} LayerElementNormal: 0 {{", bad.replace(": 1 {", ": 0 {"))); assert!(document(only_bad.as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err()); } + +#[test] +fn strict_geometry_validates_the_colors_index_alias() { + let layer = |index: &str| { + format!( + "LayerElementColor: 0 {{ MappingInformationType: \"AllSame\" ReferenceInformationType: \"IndexToDirect\" Colors: 0,1,0,1 ColorsIndex: {index} }}\n" + ) + }; + let with = |index: &str| ASCII.replace("LayerElementNormal: 0 {", &format!("{} LayerElementNormal: 0 {{", layer(index))); + let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() }; + assert!(document(with("0").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_ok()); + assert!(document(with("\"bad\",0").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err()); +} From 0479988f652d2e00cddd4740987557a0d5826fd1 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:39:34 +0100 Subject: [PATCH 20/22] fbx package: entry limit counts only resolved textures; report overflow before the no-texture return --- crates/mesh/src/fbx/package.rs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs index 01a0e542d..9e5abee01 100644 --- a/crates/mesh/src/fbx/package.rs +++ b/crates/mesh/src/fbx/package.rs @@ -99,12 +99,16 @@ pub fn write( if let Some(data) = by_name.get(&name).and_then(|&i| files.get(i)).map(|(_, d)| d.clone()) { return Some(data); } - // the model is an entry too; stop resolving once another texture could not fit - if too_many || files.len() as u64 + 1 >= package_limits.max_entries { - too_many = true; + // once a resolved texture has not fit, the package fails: stop calling the resolver + if too_many { return None; } if let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) { + // the model is an entry too; only a texture that resolves needs a new entry + if files.len() as u64 + 1 >= package_limits.max_entries { + too_many = true; + return None; + } total = total.saturating_add(data.len() as u64); if total > package_limits.max_total_bytes { exceeded = true; @@ -123,12 +127,12 @@ pub fn write( if exceeded { return Err(too_large(KIND, "the FBX and its textures exceed the package limit")); } - if files.is_empty() { - return Ok((None, missing)); - } if too_many { return Err(too_large(KIND, "too many package entries")); } + if files.is_empty() { + return Ok((None, missing)); + } let leaf = file_name.rsplit(['/', '\\']).next().filter(|n| !n.is_empty()).unwrap_or("mesh.fbx"); // the package is found by its `.fbx` entry, so the model always carries that extension let leaf = if leaf.rsplit_once('.').is_some_and(|(_, ext)| ext.eq_ignore_ascii_case("fbx")) { leaf.to_string() } else { format!("{leaf}.fbx") }; @@ -214,6 +218,19 @@ mod tests { Some(b"image".to_vec()) }); assert!(matches!(result, Err(Error::TooLarge(_)))); - assert!(calls < bounds().max_entries as usize, "{calls} resolver calls past the entry limit"); + assert!(calls <= bounds().max_entries as usize, "{calls} resolver calls past the entry limit"); + } + + #[test] + fn the_entry_limit_counts_only_textures_that_resolve() { + let limits = Limits::default(); + let doc = read_document(MODEL, &limits).unwrap(); + // the model alone fills a one-entry package, so a resolved texture cannot fit + let one = PackageLimits { max_entries: 1, ..bounds() }; + assert!(matches!(write(&doc, "a.fbx", MODEL, &limits, one, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_)))); + // an unresolved texture adds no entry, so it is reported missing rather than overflowing + let (bytes, missing) = write(&doc, "a.fbx", MODEL, &limits, one, &mut |_| None).unwrap(); + assert!(bytes.is_none()); + assert_eq!(missing, vec!["tex/a.png".to_string()]); } } From 2ecbd1e9cd665b7c5fe08325d0d275f40b6c96be Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:04:55 +0100 Subject: [PATCH 21/22] fbx: integer index arrays in strict mode, cache package textures, set-based missing dedup --- crates/mesh/src/fbx.rs | 3 ++- crates/mesh/src/fbx/package.rs | 32 +++++++++++++++++++++++++++++--- crates/mesh/src/tests/fbx.rs | 1 + 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 22ac39428..c9a7b869c 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -1412,7 +1412,8 @@ impl<'a> Build<'a, '_> { ("LayerElementColor", &["Colors", "ColorIndex", "ColorsIndex"][..]), ] { if let Some(layer) = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)) - && names.iter().filter_map(|n| layer.child(n)).any(|r| !valid(r, false)) + // index arrays and material ids are integers; the data arrays are floats + && names.iter().filter_map(|n| layer.child(n).map(|r| (r, n.ends_with("Index") || *n == "Materials"))).any(|(r, integers)| !valid(r, integers)) { return Err(malformed(KIND, "mesh array contains a non-numeric or non-finite value")); } diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs index 9e5abee01..4a159edea 100644 --- a/crates/mesh/src/fbx/package.rs +++ b/crates/mesh/src/fbx/package.rs @@ -23,6 +23,8 @@ pub struct Package<'a> { zip: craft_zip::Archive<'a>, limits: PackageLimits, name: String, + /// Textures read so far by name: a texture several materials share is read, and charged, once. + textures: std::collections::HashMap>, } impl<'a> Package<'a> { @@ -34,7 +36,7 @@ impl<'a> Package<'a> { .find(|n| n.rsplit('.').next().is_some_and(|ext| ext.eq_ignore_ascii_case("fbx"))) .map(|n| n.into_owned()) .ok_or_else(|| malformed(KIND, "the package contains no FBX file"))?; - Ok(Self { zip, limits, name }) + Ok(Self { zip, limits, name, textures: std::collections::HashMap::new() }) } pub fn model(&mut self) -> Result> { @@ -51,7 +53,16 @@ impl<'a> Package<'a> { /// Resolve candidate names within the package; outside paths are never read. pub fn texture(&mut self, paths: &[&str]) -> Option> { - paths.iter().filter_map(|p| below(p)).find_map(|name| self.zip.read(&name, self.limits.max_texture_bytes).ok().flatten()) + for name in paths.iter().filter_map(|p| below(p)) { + if let Some(data) = self.textures.get(&name) { + return Some(data.clone()); + } + if let Some(data) = self.zip.read(&name, self.limits.max_texture_bytes).ok().flatten() { + self.textures.insert(name, data.clone()); + return Some(data); + } + } + None } } @@ -90,6 +101,7 @@ pub fn write( let mut by_name = std::collections::HashMap::::new(); let mut too_many = false; let mut missing = Vec::new(); + let mut missing_seen = std::collections::HashSet::::new(); let mut total = bytes.len() as u64; let mut exceeded = total > package_limits.max_total_bytes || total > package_limits.max_model_bytes; doc.diffuse_materials(limits, &mut |paths| { @@ -119,7 +131,7 @@ pub fn write( return Some(data); } } - if let Some(first) = paths.first().filter(|p| !missing.iter().any(|m| m == **p)) { + if let Some(first) = paths.first().filter(|p| missing_seen.insert((**p).to_string())) { missing.push((*first).to_string()); } None @@ -233,4 +245,18 @@ mod tests { assert!(bytes.is_none()); assert_eq!(missing, vec!["tex/a.png".to_string()]); } + + #[test] + fn a_shared_texture_is_read_and_charged_once() { + let limits = Limits::default(); + let doc = read_document(MODEL, &limits).unwrap(); + let (bytes, _) = write(&doc, "asset.fbx", MODEL, &limits, bounds(), &mut |_| Some(b"image".to_vec())).unwrap(); + let bytes = bytes.unwrap(); + // room for the model and one read of the texture + let tight = PackageLimits { max_total_bytes: MODEL.len() as u64 + 5, ..bounds() }; + let mut package = Package::open(&bytes, tight).unwrap(); + package.model().unwrap(); + assert_eq!(package.texture(&["tex/a.png"]), Some(b"image".to_vec())); + assert_eq!(package.texture(&["tex/a.png"]), Some(b"image".to_vec()), "served from the cache"); + } } diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index d0761a8df..ef9543e81 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -885,4 +885,5 @@ fn strict_geometry_validates_the_colors_index_alias() { let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() }; assert!(document(with("0").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_ok()); assert!(document(with("\"bad\",0").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err()); + assert!(document(with("0.5").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err(), "a fractional index"); } From 5e780f24e1be4725ac247ed4d18e8e2c3f0ba2df Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:26:28 +0100 Subject: [PATCH 22/22] fbx package: refuse an oversized model up front and stop resolving once the byte limit is passed --- crates/mesh/src/fbx/package.rs | 43 ++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs index 4a159edea..10f7037c2 100644 --- a/crates/mesh/src/fbx/package.rs +++ b/crates/mesh/src/fbx/package.rs @@ -103,7 +103,10 @@ pub fn write( let mut missing = Vec::new(); let mut missing_seen = std::collections::HashSet::::new(); let mut total = bytes.len() as u64; - let mut exceeded = total > package_limits.max_total_bytes || total > package_limits.max_model_bytes; + if total > package_limits.max_total_bytes || total > package_limits.max_model_bytes { + return Err(too_large(KIND, "the FBX and its textures exceed the package limit")); + } + let mut exceeded = false; doc.diffuse_materials(limits, &mut |paths| { // candidates in order of preference: an earlier one that exists wins over a later cached one for path in paths { @@ -112,7 +115,7 @@ pub fn write( return Some(data); } // once a resolved texture has not fit, the package fails: stop calling the resolver - if too_many { + if too_many || exceeded { return None; } if let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) { @@ -233,6 +236,42 @@ mod tests { assert!(calls <= bounds().max_entries as usize, "{calls} resolver calls past the entry limit"); } + #[test] + fn resolution_stops_once_the_byte_limit_is_passed() { + let mut model = String::from("FBXHeaderExtension: { FBXVersion: 7400 } Objects: {"); + let mut links = String::new(); + for i in 0..10 { + model.push_str(&format!( + " Material: {m}, \"Material::M{i}\", \"\" {{ }} Texture: {t}, \"Texture::T{i}\", \"\" {{ RelativeFilename: \"big.png\" }}", + m = 2 * i + 1, + t = 2 * i + 2 + )); + links.push_str(&format!(" C: \"OP\",{t},{m},\"DiffuseColor\"", m = 2 * i + 1, t = 2 * i + 2)); + } + model.push_str(&format!(" }} Connections: {{{links} }}")); + let limits = Limits::default(); + let doc = read_document(model.as_bytes(), &limits).unwrap(); + let tight = PackageLimits { max_total_bytes: model.len() as u64 + 4, max_model_bytes: 1 << 20, ..bounds() }; + let mut calls = 0; + let result = write(&doc, "a.fbx", model.as_bytes(), &limits, tight, &mut |_| { + calls += 1; + Some(b"image".to_vec()) + }); + assert!(matches!(result, Err(Error::TooLarge(_)))); + assert_eq!(calls, 1, "one texture past the limit ends resolution"); + // a model past its own limit is refused before any texture is resolved + let small = PackageLimits { max_model_bytes: 8, ..bounds() }; + let mut calls = 0; + assert!(matches!( + write(&doc, "a.fbx", model.as_bytes(), &limits, small, &mut |_| { + calls += 1; + None + }), + Err(Error::TooLarge(_)) + )); + assert_eq!(calls, 0); + } + #[test] fn the_entry_limit_counts_only_textures_that_resolve() { let limits = Limits::default();