diff --git a/src/lib.rs b/src/lib.rs index 7f47bd1..8f5fa81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -692,15 +692,24 @@ pub fn load_obj_buf(reader: &mut B, material_loader: ML) -> LoadResult } Some("usemtl") => { if let Some(mat_name) = words.next() { - 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); - } + 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 mat_id != new_mat && !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(); + } + if new_mat.is_none() { + println!("Warning: Object {} refers to unfound material: {}", + name, + mat_name); } + mat_id = new_mat; } else { return Err(LoadError::MaterialParseError); }