implement physics::generate_models

This commit is contained in:
Quaternions 2024-02-07 22:29:39 -08:00
parent 4a84d3a419
commit d3531a9aca

View File

@ -935,40 +935,29 @@ impl PhysicsContext{
} }
pub fn generate_models(&mut self,map:&map::CompleteMap){ pub fn generate_models(&mut self,map:&map::CompleteMap){
let mut starts=Vec::new(); self.data.modes=map.modes.clone();
let mut spawns=Vec::new(); self.data.models.attributes=map.attributes
let mut attr_hash=HashMap::new(); .iter().enumerate().filter_map(|(attr_id,m_attr)|
for model in &map.models{ PhysicsCollisionAttributes::try_from(m_attr).ok().map(|p_attr|
let mesh_id=self.data.models.meshes.len(); (PhysicsAttributesId::new(attr_id as u32),p_attr)
let mut make_mesh=false; )
for model_instance in &model.instances{ ).collect();
if let Ok(physics_attributes)=PhysicsCollisionAttributes::try_from(&model_instance.attributes){ self.data.models.meshes=map.meshes.iter().enumerate().map(|(mesh_id,mesh)|
let attr_id=if let Some(&attr_id)=attr_hash.get(&physics_attributes){ (PhysicsMeshId::new(mesh_id as u32),PhysicsMesh::from(mesh))
attr_id ).collect();
}else{ self.data.models.models=map.models.iter().enumerate().map(|(model_id,model)|
let attr_id=self.data.models.push_attr(physics_attributes.clone()); (PhysicsModelId::new(model_id as u32),PhysicsModel::new(model.mesh.into(),model.attributes.into(),PhysicsMeshTransform::new(model.transform)))
attr_hash.insert(physics_attributes,attr_id); ).collect();
attr_id
};
let model_physics=PhysicsModel::new(mesh_id,attr_id,model_instance.transform);
make_mesh=true;
self.data.models.push_model(model_physics);
}
}
if make_mesh{
self.data.models.push_mesh(PhysicsMesh::from(model));
}
}
let convex_mesh_aabb_list=self.data.models.models.iter() let convex_mesh_aabb_list=self.data.models.models.iter()
.enumerate().flat_map(|(model_id,model)|{ .flat_map(|(&model_id,model)|{
self.data.models.meshes[model.mesh_id.get() as usize].submesh_views() self.data.models.meshes[&model.mesh_id].submesh_views()
.enumerate().map(|(submesh_id,view)|{ .enumerate().map(|(submesh_id,view)|{
let mut aabb=aabb::Aabb::default(); let mut aabb=aabb::Aabb::default();
for v in view.verts(){ for v in view.verts(){
aabb.grow(v) aabb.grow(v)
} }
(ConvexMeshId{ (ConvexMeshId{
model_id:PhysicsModelId::new(model_id as u32), model_id,
submesh_id:PhysicsSubmeshId::new(submesh_id as u32), submesh_id:PhysicsSubmeshId::new(submesh_id as u32),
},aabb) },aabb)
}) })