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] 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"); }