From cc92663deb302c0fc11d692d69d521ea6435b908 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:25:39 +0000 Subject: [PATCH 1/2] 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 0984808dd7361061c94054c6d4871782040ce348 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:54:08 +0000 Subject: [PATCH 2/2] 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; } } }