From a6c457788c28e8c7f206e975c54176dfc9ccacad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thea=20Sch=C3=B6bl?= Date: Mon, 29 May 2023 21:15:10 +0200 Subject: [PATCH] =?UTF-8?q?(=E3=83=8E=E0=B2=A0=E7=9B=8A=E0=B2=A0)=E3=83=8E?= =?UTF-8?q?=E5=BD=A1=E2=94=BB=E2=94=81=E2=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Materials were wrong because I used materials[polygon_materials[surface_material_id]] Don't ask me why, but it took me half a day and many refactorings to find. --- rust/mhgd/src/lwo/intermediate_layer.rs | 38 ++++++++++--------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/rust/mhgd/src/lwo/intermediate_layer.rs b/rust/mhgd/src/lwo/intermediate_layer.rs index 07ddba7..97ab6b8 100644 --- a/rust/mhgd/src/lwo/intermediate_layer.rs +++ b/rust/mhgd/src/lwo/intermediate_layer.rs @@ -28,21 +28,20 @@ impl IntermediateLayer { pub fn commit(self, materials: &HashMap) -> Gd { let mut mesh = ArrayMesh::new(); mesh.set_name(self.name.clone().into()); - let mut surface_material_ids = Vec::::new(); + let surface_material_ids = self.material_mappings.values().unique().collect_vec(); - for material_id in self.material_mappings.values().unique() { + for material_id in surface_material_ids.iter() { let surface_info = SurfaceInfo::collect_from_layer(&self, &materials[material_id]); - if !surface_info.is_empty() { - mesh.add_surface_from_arrays( - PrimitiveType::PRIMITIVE_TRIANGLES, - surface_info.commit_to_arrays(), - Array::new(), - Dictionary::new(), - ArrayFormat::ARRAY_FORMAT_NORMAL, - ); - surface_material_ids.push(*material_id); - } + debug_assert!(!surface_info.is_empty()); + + mesh.add_surface_from_arrays( + PrimitiveType::PRIMITIVE_TRIANGLES, + surface_info.commit_to_arrays(), + Array::new(), + Dictionary::new(), + ArrayFormat::ARRAY_FORMAT_NORMAL, + ); } godot_print!( @@ -55,12 +54,7 @@ impl IntermediateLayer { .collect_vec() ); - let mut final_mesh = post_process_mesh( - mesh, - materials, - surface_material_ids, - &self.material_mappings, - ); + let mut final_mesh = post_process_mesh(mesh, materials, surface_material_ids); final_mesh.set_name(self.name.into()); final_mesh } @@ -69,8 +63,7 @@ impl IntermediateLayer { fn post_process_mesh( mesh: Gd, materials: &HashMap, - material_ids: Vec, - material_mappings: &HashMap, + material_ids: Vec<&u16>, ) -> Gd { let mut out_mesh = ArrayMesh::new(); @@ -91,9 +84,8 @@ fn post_process_mesh( ArrayFormat::ARRAY_FORMAT_NORMAL, ); - if let Some(mat) = materials.get(&material_mappings[&(surface_id as i32)]) { - out_mesh.surface_set_material(surface_idx as i64, mat.material.share().upcast()) - } + let mat = &materials[surface_id]; + out_mesh.surface_set_material(surface_idx as i64, mat.material.share().upcast()) } out_mesh