Compare commits

..

1 Commits

Author SHA1 Message Date
5c835961c4 rustfmt 2024-02-15 19:01:23 -08:00
6 changed files with 82 additions and 35 deletions

2
Cargo.lock generated

@ -1632,7 +1632,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strafe-client"
version = "0.9.5"
version = "0.9.4"
dependencies = [
"bytemuck",
"configparser",

@ -1,6 +1,6 @@
[package]
name = "strafe-client"
version = "0.9.5"
version = "0.9.4"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

15
rustfmt.toml Normal file

@ -0,0 +1,15 @@
hard_tabs=true
imports_layout="HorizontalVertical"
match_arm_blocks=false
match_block_trailing_comma=true
newline_style="Unix"
#overflow_delimited_expr=true
reorder_impl_items=true
reorder_imports=false
group_imports="StdExternalCrate"
reorder_modules=false
space_after_colon=false
type_punctuation_density="Compressed"
use_field_init_shorthand=true
use_try_shorthand=true
#wrap_comments=true

@ -218,8 +218,14 @@ impl GraphicsState{
color:GraphicsModelColor4::new(model.color),
};
//get or create owned mesh map
let owned_mesh_map=owned_mesh_id_from_mesh_id_render_config_id
.entry(model.mesh).or_insert_with(||{
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 mut owned_mesh_map=HashMap::new();
//add mesh if renderid never before seen for this model
//add instance
@ -227,11 +233,17 @@ impl GraphicsState{
//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=owned_mesh_map
.entry(graphics_group.render).or_insert_with(||{
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::<glam::Vec3>::into(v).as_ref()).collect(),
unique_tex:mesh.unique_tex.iter().map(|v|*v.as_ref()).collect(),
@ -240,10 +252,10 @@ impl GraphicsState{
unique_vertices:mesh.unique_vertices.clone(),
render_config:graphics_group.render,
polys:model::PolygonGroup::PolygonList(model::PolygonList::new(Vec::new())),
instances: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(
@ -257,16 +269,8 @@ impl GraphicsState{
}
}
}
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];
if model.color.w==0.0&&render_config.texture.is_none(){
continue;
}
owned_mesh.instances.push(instance.clone());
}
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
@ -285,14 +289,24 @@ impl GraphicsState{
continue;
}
//populate hashmap
let unique_color=unique_texture_color
.entry(model.render_config)
.or_insert_with(||HashMap::new());
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()
};
//separate instances by color
for (instance_id,instance) in model.instances.iter().enumerate(){
let model_instance_list=unique_color
.entry(instance.color)
.or_insert_with(||Vec::new());
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()
};
//add model instance to list
model_instance_list.push((model_id,instance_id));
}
@ -328,34 +342,46 @@ 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(*pos_id_from.entry(h).or_insert_with(||{
PositionId::new((if let Some(&pos_id)=pos_id_from.get(&h){
pos_id
}else{
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(*tex_id_from.entry(h).or_insert_with(||{
TextureCoordinateId::new((if let Some(&tex_id)=tex_id_from.get(&h){
tex_id
}else{
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(*normal_id_from.entry(h).or_insert_with(||{
NormalId::new((if let Some(&normal_id)=normal_id_from.get(&h){
normal_id
}else{
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(*color_id_from.entry(h).or_insert_with(||{
ColorId::new((if let Some(&color_id)=color_id_from.get(&h){
color_id
}else{
let color_id=unique_color.len();
unique_color.push(color);
color_id_from.insert(h,color_id);
color_id
}) as u32)
}).collect();
@ -368,9 +394,12 @@ impl GraphicsState{
normal:map_normal_id[unmapped_vertex.normal.get() as usize],
color:map_color_id[unmapped_vertex.color.get() as usize],
};
VertexId::new(*vertex_id_from.entry(vertex.clone()).or_insert_with(||{
VertexId::new((if let Some(&vertex_id)=vertex_id_from.get(&vertex){
vertex_id
}else{
let vertex_id=unique_vertices.len();
unique_vertices.push(vertex);
unique_vertices.push(vertex.clone());
vertex_id_from.insert(vertex,vertex_id);
vertex_id
}) as u32)
}).collect();
@ -416,7 +445,9 @@ impl GraphicsState{
for end_index in 2..poly.len(){
for index in [0,end_index-1,end_index]{
let vertex_index=poly[index];
indices.push(*index_from_vertex.entry(vertex_index).or_insert_with(||{
if let Some(&i)=index_from_vertex.get(&vertex_index){
indices.push(i);
}else{
let i=vertices.len();
let vertex=&model.unique_vertices[vertex_index.get() as usize];
vertices.push(GraphicsVertex{
@ -425,8 +456,9 @@ impl GraphicsState{
normal:model.unique_normal[vertex.normal.get() as usize],
color:model.unique_color[vertex.color.get() as usize],
});
i
}));
index_from_vertex.insert(vertex_index,i);
indices.push(i);
}
}
}
}

@ -1,4 +1,3 @@
mod file;
mod setup;
mod window;
mod worker;
@ -12,6 +11,8 @@ mod model_graphics;
mod physics_worker;
mod graphics_worker;
mod file;
fn main(){
setup::setup_and_start(format!("Strafe Client v{}",env!("CARGO_PKG_VERSION")));
}

@ -992,7 +992,6 @@ 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{