From c5fae1f2fbaed3d76664a0bb27639b1b9398e097 Mon Sep 17 00:00:00 2001 From: Morion Date: Fri, 13 Nov 2026 16:52:56 -0500 Subject: [PATCH] Preserve separate OBJ parts with matching names for Submesh ID baking --- crates/engine/tests/it/bake.rs | 15 +++++++++++++++ crates/formats/src/mesh_import/obj.rs | 25 ++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/crates/engine/tests/it/bake.rs b/crates/engine/tests/it/bake.rs index 85b0142c..3ebc85b0 100644 --- a/crates/engine/tests/it/bake.rs +++ b/crates/engine/tests/it/bake.rs @@ -612,3 +612,18 @@ fn a_failed_multi_set_bake_restores_settings_pixels_and_history() { assert_eq!(session.doc.baked, original.baked); assert_eq!(session.history_labels(), history); } + +#[test] +fn obj_submesh_id_separates_objects_with_the_same_name_and_material() { + use texturecraft_engine::{core::IdColorSource, formats::import_mesh_bytes}; + let file = b"o Part\nusemtl Shared\nv 0 0 0\nv 1 0 0\nv 0 1 0\nvt 0 0\nvt 0.45 0\nvt 0 1\nf 1/1 2/2 3/3\no Part\nusemtl Shared\nv 2 0 0\nv 3 0 0\nv 2 1 0\nvt 0.5 0\nvt 0.95 0\nvt 0.5 1\nf 4/4 5/5 6/6"; + let mesh = import_mesh_bytes("parts.obj", file).unwrap().mesh; + assert_eq!(mesh.materials.len(), 1); + assert_eq!(mesh.submeshes.len(), 2); + let mut session = Session::new_project("parts", mesh, "parts.obj", None, 128).unwrap(); + session.doc.project.texture_sets[0].bake.maps.id.parameters.source = IdColorSource::Submesh; + execute(&mut session, "bake.run", &json!({"resolution":128,"maps":["id"],"dilation":0})).unwrap(); + let map = &session.doc.baked[&0].maps[&BakedMap::Id]; + let pixel = |x: usize| &map[(100 * 128 + x) * 3..(100 * 128 + x) * 3 + 3]; + assert_ne!(pixel(10), pixel(74)); +} diff --git a/crates/formats/src/mesh_import/obj.rs b/crates/formats/src/mesh_import/obj.rs index 2f284704..f9c37642 100644 --- a/crates/formats/src/mesh_import/obj.rs +++ b/crates/formats/src/mesh_import/obj.rs @@ -27,7 +27,23 @@ fn read_inner(bytes: &[u8]) -> Result { } let library: String = names.iter().map(|name| format!("newmtl {name}\n")).collect(); - let source = format!("mtllib texturecraft-usemtl.mtl\n{text}"); + // tobj also splits models at material changes. Give each explicit object/group + // declaration its own key so repeated materials regroup within that declaration only. + let mut source = String::from("mtllib texturecraft-usemtl.mtl\n"); + let mut part_names = Vec::new(); + for line in text.lines() { + let trimmed = line.trim(); + match trimmed.split_whitespace().next() { + Some("o" | "g") => { + source.push_str(&format!("o {}\n", part_names.len())); + part_names.push(trimmed[1..].trim()); + } + _ => { + source.push_str(line); + source.push('\n'); + } + } + } // Polygons stay whole so the wireframe can show them; `RawMesh::polygon` triangulates let options = tobj::LoadOptions { single_index: true, triangulate: false, ignore_points: true, ignore_lines: true }; @@ -98,6 +114,13 @@ fn read_inner(bytes: &[u8]) -> Result { } } + // Restore user-facing names after all faces have been grouped by declaration. + for (_, part, _) in &mut raw.groups { + if let Some(name) = part.parse::().ok().and_then(|id| part_names.get(id)) { + *part = (*name).to_owned(); + } + } + if partial_uvs { raw.warnings.push("some objects have no UVs; default UVs were assigned".into()); }