From fcaf4839bb9e2bbca8c339ec554d9e386f6c6cd1 Mon Sep 17 00:00:00 2001 From: Iain Date: Sun, 31 Mar 2019 13:56:04 +0100 Subject: [PATCH 1/2] Fix material changes within an object being ignored Previously, a single object would always create a single model with the last specified material. A usemtl statement in the middle of an object will now emit a model at that point (with the previous material). --- src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7f47bd1..dec232b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -692,6 +692,17 @@ pub fn load_obj_buf(reader: &mut B, material_loader: ML) -> LoadResult } Some("usemtl") => { if let Some(mat_name) = words.next() { + // As materials are returned per-model, a new material within an object + // has to emit a new model with the same name but different material + if !tmp_faces.is_empty() { + models.push(Model::new(export_faces(&tmp_pos, + &tmp_texcoord, + &tmp_normal, + &tmp_faces, + mat_id), + name.clone())); + tmp_faces.clear(); + } match mat_map.get(mat_name) { Some(m) => mat_id = Some(*m), None => { From ac912d398611510663c4fba140fd502f31d0e6b4 Mon Sep 17 00:00:00 2001 From: Iain Date: Tue, 2 Apr 2019 09:36:35 +0100 Subject: [PATCH 2/2] Fix emitting multiple models for repeat usemtl statements --- src/lib.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dec232b..8f5fa81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -692,9 +692,10 @@ pub fn load_obj_buf(reader: &mut B, material_loader: ML) -> LoadResult } Some("usemtl") => { if let Some(mat_name) = words.next() { + let new_mat = mat_map.get(mat_name).cloned(); // As materials are returned per-model, a new material within an object // has to emit a new model with the same name but different material - if !tmp_faces.is_empty() { + if mat_id != new_mat && !tmp_faces.is_empty() { models.push(Model::new(export_faces(&tmp_pos, &tmp_texcoord, &tmp_normal, @@ -703,15 +704,12 @@ pub fn load_obj_buf(reader: &mut B, material_loader: ML) -> LoadResult name.clone())); tmp_faces.clear(); } - match mat_map.get(mat_name) { - Some(m) => mat_id = Some(*m), - None => { - mat_id = None; - println!("Warning: Object {} refers to unfound material: {}", - name, - mat_name); - } + if new_mat.is_none() { + println!("Warning: Object {} refers to unfound material: {}", + name, + mat_name); } + mat_id = new_mat; } else { return Err(LoadError::MaterialParseError); }