forked from StrafesNET/strafe-client
use .entry().or_insert_with() pattern everywhere
This commit is contained in:
parent
b1d860edf1
commit
4a53040011
@ -218,11 +218,8 @@ impl GraphicsState{
|
||||
color:GraphicsModelColor4::new(model.color),
|
||||
};
|
||||
//get or create owned mesh map
|
||||
let owned_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
|
||||
owned_mesh_map
|
||||
}else{
|
||||
let owned_mesh_map=owned_mesh_id_from_mesh_id_render_config_id
|
||||
.entry(model.mesh).or_insert_with(||{
|
||||
let mut owned_mesh_map=HashMap::new();
|
||||
//add mesh if renderid never before seen for this model
|
||||
//add instance
|
||||
@ -231,12 +228,10 @@ impl GraphicsState{
|
||||
if let Some(mesh)=map.meshes.get(model.mesh.get() as usize){
|
||||
for graphics_group in mesh.graphics_groups.iter(){
|
||||
//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{
|
||||
let owned_mesh_id=owned_mesh_map
|
||||
.entry(graphics_group.render).or_insert_with(||{
|
||||
//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::<glam::Vec3>::into(v).as_ref()).collect(),
|
||||
unique_tex:mesh.unique_tex.iter().map(|v|*v.as_ref()).collect(),
|
||||
@ -248,7 +243,7 @@ impl GraphicsState{
|
||||
instances:Vec::new(),
|
||||
});
|
||||
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(
|
||||
@ -262,9 +257,8 @@ impl GraphicsState{
|
||||
}
|
||||
}
|
||||
}
|
||||
owned_mesh_id_from_mesh_id_render_config_id.insert(model.mesh,owned_mesh_map);
|
||||
owned_mesh_id_from_mesh_id_render_config_id.get(&model.mesh).unwrap()
|
||||
};
|
||||
owned_mesh_map
|
||||
});
|
||||
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();
|
||||
let render_config=&map.render_configs[owned_mesh.render_config.get() as usize];
|
||||
@ -291,24 +285,14 @@ impl GraphicsState{
|
||||
continue;
|
||||
}
|
||||
//populate hashmap
|
||||
let unique_color=if let Some(unique_color)=unique_texture_color.get_mut(&model.render_config){
|
||||
unique_color
|
||||
}else{
|
||||
//make new hashmap
|
||||
let unique_color=HashMap::new();
|
||||
unique_texture_color.insert(model.render_config,unique_color);
|
||||
unique_texture_color.get_mut(&model.render_config).unwrap()
|
||||
};
|
||||
let unique_color=unique_texture_color
|
||||
.entry(model.render_config)
|
||||
.or_insert_with(||HashMap::new());
|
||||
//separate instances by color
|
||||
for (instance_id,instance) in model.instances.iter().enumerate(){
|
||||
let model_instance_list=if let Some(model_instance_list)=unique_color.get_mut(&instance.color){
|
||||
model_instance_list
|
||||
}else{
|
||||
//make new hashmap
|
||||
let model_instance_list=Vec::new();
|
||||
unique_color.insert(instance.color.clone(),model_instance_list);
|
||||
unique_color.get_mut(&instance.color).unwrap()
|
||||
};
|
||||
let model_instance_list=unique_color
|
||||
.entry(instance.color)
|
||||
.or_insert_with(||Vec::new());
|
||||
//add model instance to list
|
||||
model_instance_list.push((model_id,instance_id));
|
||||
}
|
||||
@ -344,46 +328,34 @@ impl GraphicsState{
|
||||
let map_pos_id:Vec<PositionId>=model.unique_pos.iter().map(|untransformed_pos|{
|
||||
let pos=instance.transform.transform_point3(glam::Vec3::from_array(untransformed_pos.clone())).to_array();
|
||||
let h=bytemuck::cast::<[f32;3],[u32;3]>(pos);
|
||||
PositionId::new((if let Some(&pos_id)=pos_id_from.get(&h){
|
||||
pos_id
|
||||
}else{
|
||||
PositionId::new(*pos_id_from.entry(h).or_insert_with(||{
|
||||
let pos_id=unique_pos.len();
|
||||
unique_pos.push(pos);
|
||||
pos_id_from.insert(h,pos_id);
|
||||
pos_id
|
||||
}) as u32)
|
||||
}).collect();
|
||||
let map_tex_id:Vec<TextureCoordinateId>=model.unique_tex.iter().map(|&tex|{
|
||||
let h=bytemuck::cast::<[f32;2],[u32;2]>(tex);
|
||||
TextureCoordinateId::new((if let Some(&tex_id)=tex_id_from.get(&h){
|
||||
tex_id
|
||||
}else{
|
||||
TextureCoordinateId::new(*tex_id_from.entry(h).or_insert_with(||{
|
||||
let tex_id=unique_tex.len();
|
||||
unique_tex.push(tex);
|
||||
tex_id_from.insert(h,tex_id);
|
||||
tex_id
|
||||
}) as u32)
|
||||
}).collect();
|
||||
let map_normal_id:Vec<NormalId>=model.unique_normal.iter().map(|untransformed_normal|{
|
||||
let normal=(instance.normal_transform*glam::Vec3::from_array(untransformed_normal.clone())).to_array();
|
||||
let h=bytemuck::cast::<[f32;3],[u32;3]>(normal);
|
||||
NormalId::new((if let Some(&normal_id)=normal_id_from.get(&h){
|
||||
normal_id
|
||||
}else{
|
||||
NormalId::new(*normal_id_from.entry(h).or_insert_with(||{
|
||||
let normal_id=unique_normal.len();
|
||||
unique_normal.push(normal);
|
||||
normal_id_from.insert(h,normal_id);
|
||||
normal_id
|
||||
}) as u32)
|
||||
}).collect();
|
||||
let map_color_id:Vec<ColorId>=model.unique_color.iter().map(|&color|{
|
||||
let h=bytemuck::cast::<[f32;4],[u32;4]>(color);
|
||||
ColorId::new((if let Some(&color_id)=color_id_from.get(&h){
|
||||
color_id
|
||||
}else{
|
||||
ColorId::new(*color_id_from.entry(h).or_insert_with(||{
|
||||
let color_id=unique_color.len();
|
||||
unique_color.push(color);
|
||||
color_id_from.insert(h,color_id);
|
||||
color_id
|
||||
}) as u32)
|
||||
}).collect();
|
||||
@ -396,12 +368,9 @@ impl GraphicsState{
|
||||
normal:map_normal_id[unmapped_vertex.normal.get() as usize],
|
||||
color:map_color_id[unmapped_vertex.color.get() as usize],
|
||||
};
|
||||
VertexId::new((if let Some(&vertex_id)=vertex_id_from.get(&vertex){
|
||||
vertex_id
|
||||
}else{
|
||||
VertexId::new(*vertex_id_from.entry(vertex.clone()).or_insert_with(||{
|
||||
let vertex_id=unique_vertices.len();
|
||||
unique_vertices.push(vertex.clone());
|
||||
vertex_id_from.insert(vertex,vertex_id);
|
||||
unique_vertices.push(vertex);
|
||||
vertex_id
|
||||
}) as u32)
|
||||
}).collect();
|
||||
@ -447,9 +416,7 @@ impl GraphicsState{
|
||||
for end_index in 2..poly.len(){
|
||||
for index in [0,end_index-1,end_index]{
|
||||
let vertex_index=poly[index];
|
||||
if let Some(&i)=index_from_vertex.get(&vertex_index){
|
||||
indices.push(i);
|
||||
}else{
|
||||
indices.push(*index_from_vertex.entry(vertex_index).or_insert_with(||{
|
||||
let i=vertices.len();
|
||||
let vertex=&model.unique_vertices[vertex_index.get() as usize];
|
||||
vertices.push(GraphicsVertex{
|
||||
@ -458,9 +425,8 @@ impl GraphicsState{
|
||||
normal:model.unique_normal[vertex.normal.get() as usize],
|
||||
color:model.unique_color[vertex.color.get() as usize],
|
||||
});
|
||||
index_from_vertex.insert(vertex_index,i);
|
||||
indices.push(i);
|
||||
}
|
||||
i
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -992,6 +992,7 @@ impl PhysicsContext{
|
||||
let mut used_meshes=Vec::new();
|
||||
let mut physics_mesh_id_from_model_mesh_id=HashMap::<MeshId,PhysicsMeshId>::new();
|
||||
self.data.models.models=map.models.iter().enumerate().filter_map(|(model_id,model)|{
|
||||
//TODO: use .entry().or_insert_with(||{
|
||||
let attr_id=if let Some(&attr_id)=physics_attr_id_from_model_attr_id.get(&model.attributes){
|
||||
attr_id
|
||||
}else{
|
||||
|
Loading…
Reference in New Issue
Block a user