mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Preserve FBX material slot assignments
This commit is contained in:
1 parent
3f4de9ca11
commit
a4977030c0
2 files changed
+36
-8
No files matched your search
+26
-8
@@ -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 => {
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
Reference in new issue
Block a user