Merge remote-tracking branch 'origin/codex/fbx-material-slots' into fredapps/i778-fbx-batch

# Conflicts:
#	crates/mesh/src/tests/fbx.rs
This commit is contained in:
FredApps committed 2026-11-16 00:19:24 +01:00
commit 0c33cf0bb5
2 files changed
+36 -8

No files matched your search

+26 -8
View File
@@ -1099,7 +1099,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,
@@ -1160,9 +1171,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));
@@ -1200,11 +1216,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 => {
+10
View File
@@ -691,3 +691,13 @@ fn binary_scalar_properties_are_bounded_before_reserving() {
assert!(matches!(read_document(&bytes, &limits), Err(Error::TooLarge(_))));
assert!(read_document(&bytes, &Limits::default()).is_ok());
}
#[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()));
}