diff --git a/src/graphics.rs b/src/graphics.rs index 12bdd52..44d3d7b 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -205,7 +205,7 @@ impl GraphicsState{ let indexed_models_len=map.models.len(); //models split into graphics_group.RenderConfigId let mut owned_mesh_id_from_mesh_id_render_config_id:HashMap>=HashMap::new(); - let mut unique_render_config_models=Vec::with_capacity(indexed_models_len); + let mut unique_render_config_models:Vec=Vec::with_capacity(indexed_models_len); for model in &map.models{ //wow let instance=GraphicsModelOwned{ @@ -214,51 +214,59 @@ impl GraphicsState{ color:GraphicsModelColor4::new(model.color), }; //get or create owned mesh map - let owned_mesh_map=if let Some(map)=owned_mesh_id_from_mesh_id_render_config_id.get_mut(&model.mesh){ - map + if let Some(owned_mesh_map)=owned_mesh_id_from_mesh_id_render_config_id.get(&model.mesh){ + //the mesh has already been split into a set of unique renderconfig meshes + //simply add one instance to each of them + for owned_mesh_id in owned_mesh_map.values(){ + let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap(); + owned_mesh.instances.push(instance.clone()); + } }else{ - let map=HashMap::new(); - owned_mesh_id_from_mesh_id_render_config_id.insert(model.mesh,map); - owned_mesh_id_from_mesh_id_render_config_id.get_mut(&model.mesh).unwrap() - }; - //convert Model into GraphicsModelOwned - //check each group, if it's using a new render config then make a new clone of the model - if let Some(mesh)=map.meshes.get(model.mesh.get() as usize){ - for graphics_group in mesh.graphics_groups.iter(){ - let render_config=&map.render_configs[graphics_group.render.get() as usize]; - if model.color.w==0.0&&render_config.texture.is_none(){ - continue; - } - //get or create owned mesh - let owned_mesh_id=if let Some(&owned_mesh_id)=owned_mesh_map.get(&graphics_group.render){ - owned_mesh_id - }else{ - //create - let owned_mesh_id=IndexedGraphicsMeshOwnedRenderConfigId::new(unique_render_config_models.len() as u32); - owned_mesh_map.insert(graphics_group.render,owned_mesh_id); - unique_render_config_models.push(IndexedGraphicsMeshOwnedRenderConfig{ - unique_pos:mesh.unique_pos.iter().map(|&v|*Into::::into(v).as_ref()).collect(), - unique_tex:mesh.unique_tex.iter().map(|v|*v.as_ref()).collect(), - unique_normal:mesh.unique_normal.iter().map(|&v|*Into::::into(v).as_ref()).collect(), - unique_color:mesh.unique_color.iter().map(|v|*v.as_ref()).collect(), - unique_vertices:mesh.unique_vertices.clone(), - render_config:graphics_group.render, - polys:model::PolygonGroup::PolygonList(model::PolygonList::new( + let mut owned_mesh_map=HashMap::new(); + //add mesh if renderid never before seen for this model + //add instance + //convert Model into GraphicsModelOwned + //check each group, if it's using a new render config then make a new clone of the model + if let Some(mesh)=map.meshes.get(model.mesh.get() as usize){ + for graphics_group in mesh.graphics_groups.iter(){ + let render_config=&map.render_configs[graphics_group.render.get() as usize]; + if model.color.w==0.0&&render_config.texture.is_none(){ + continue; + } + //get or create owned mesh + let owned_mesh_id=if let Some(&owned_mesh_id)=owned_mesh_map.get(&graphics_group.render){ + owned_mesh_id + }else{ + //create + let owned_mesh_id=IndexedGraphicsMeshOwnedRenderConfigId::new(unique_render_config_models.len() as u32); + owned_mesh_map.insert(graphics_group.render,owned_mesh_id); + unique_render_config_models.push(IndexedGraphicsMeshOwnedRenderConfig{ + unique_pos:mesh.unique_pos.iter().map(|&v|*Into::::into(v).as_ref()).collect(), + unique_tex:mesh.unique_tex.iter().map(|v|*v.as_ref()).collect(), + unique_normal:mesh.unique_normal.iter().map(|&v|*Into::::into(v).as_ref()).collect(), + unique_color:mesh.unique_color.iter().map(|v|*v.as_ref()).collect(), + unique_vertices:mesh.unique_vertices.clone(), + render_config:graphics_group.render, + polys:model::PolygonGroup::PolygonList(model::PolygonList::new(Vec::new())), + instances:vec![instance.clone()], + }); + owned_mesh_id + }; + let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap(); + match &mut owned_mesh.polys{ + model::PolygonGroup::PolygonList(polygon_list)=>polygon_list.extend( graphics_group.groups.iter().flat_map(|polygon_group_id|{ mesh.polygon_groups[polygon_group_id.get() as usize].polys() }) .map(|vertex_id_slice| - vertex_id_slice.iter().copied().collect() - ).collect() - )), - instances:Vec::new(), - }); - owned_mesh_id - }; - let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap(); - owned_mesh.instances.push(instance.clone()); + vertex_id_slice.to_vec() + ) + ), + } + } } - } + owned_mesh_id_from_mesh_id_render_config_id.insert(model.mesh,owned_mesh_map); + }; } //check every model to see if it's using the same (texture,color) but has few instances,if it is combine it into one model //1. collect unique instances of texture and color,note model id