diff --git a/lib/rbx_loader/src/union.rs b/lib/rbx_loader/src/union.rs index 5ba7f13..a68bcc1 100644 --- a/lib/rbx_loader/src/union.rs +++ b/lib/rbx_loader/src/union.rs @@ -52,75 +52,23 @@ pub fn convert(roblox_physics_data:&[u8],roblox_mesh_data:&[u8])->Result<model:: rbx_mesh::physics_data::PhysicsData::CSGPHS(rbx_mesh::physics_data::CSGPHS::PhysicsInfoMesh(pim)) =>vec![pim.mesh], }; - let mut unique_pos=Vec::new(); - let mut pos_id_from=HashMap::new(); - let mut unique_tex=Vec::new(); - let mut tex_id_from=HashMap::new(); - let mut unique_normal=Vec::new(); - let mut normal_id_from=HashMap::new(); - let mut unique_color=Vec::new(); - let mut color_id_from=HashMap::new(); - let mut unique_vertices=Vec::new(); - let mut vertex_id_from=HashMap::new(); - let mut acquire_pos_id=|pos|{ - let p=vec3::try_from_f32_array(pos).map_err(Error::Planar64Vec3)?; - Ok(*pos_id_from.entry(p).or_insert_with(||{ - let pos_id=PositionId::new(unique_pos.len() as u32); - unique_pos.push(p); - pos_id - })) - }; - let mut acquire_tex_id=|tex|{ - let h=bytemuck::cast::<[f32;2],[u32;2]>(tex); - *tex_id_from.entry(h).or_insert_with(||{ - let tex_id=TextureCoordinateId::new(unique_tex.len() as u32); - unique_tex.push(glam::Vec2::from_array(tex)); - tex_id - }) - }; - let mut acquire_normal_id=|normal|{ - let n=vec3::try_from_f32_array(normal).map_err(Error::Planar64Vec3)?; - Ok(*normal_id_from.entry(n).or_insert_with(||{ - let normal_id=NormalId::new(unique_normal.len() as u32); - unique_normal.push(n); - normal_id - })) - }; - let mut acquire_color_id=|color|{ - let h=bytemuck::cast::<[f32;4],[u32;4]>(color); - *color_id_from.entry(h).or_insert_with(||{ - let color_id=ColorId::new(unique_color.len() as u32); - unique_color.push(glam::Vec4::from_array(color)); - color_id - }) - }; - let mut acquire_vertex_id=|vertex:IndexedVertex|{ - *vertex_id_from.entry(vertex.clone()).or_insert_with(||{ - let vertex_id=VertexId::new(unique_vertices.len() as u32); - unique_vertices.push(vertex); - vertex_id - }) - }; - let color=acquire_color_id([1.0f32;4]); - let tex=acquire_tex_id([0.0f32;2]); + let mut mb=strafesnet_common::model::MeshBuilder::new(); + let color=mb.acquire_color_id(glam::Vec4::ONE); + let tex=mb.acquire_tex_id(glam::Vec2::ZERO); let polygon_groups:Vec<PolygonGroup>=physics_convex_meshes.into_iter().map(|mesh|{ Ok(PolygonGroup::PolygonList(PolygonList::new(mesh.faces.into_iter().map(|[vertex_id0,vertex_id1,vertex_id2]|{ - let v0=mesh.vertices.get(vertex_id0.0 as usize).ok_or(Error::MissingVertexId(vertex_id0.0))?; - let v1=mesh.vertices.get(vertex_id1.0 as usize).ok_or(Error::MissingVertexId(vertex_id1.0))?; - let v2=mesh.vertices.get(vertex_id2.0 as usize).ok_or(Error::MissingVertexId(vertex_id2.0))?; - let vertex_norm=(glam::Vec3::from_slice(v1)-glam::Vec3::from_slice(v0)) - .cross(glam::Vec3::from_slice(v2)-glam::Vec3::from_slice(v0)).to_array(); - let mut ingest_vertex_id=|&vertex_pos:&[f32;3]|Ok(acquire_vertex_id(IndexedVertex{ - pos:acquire_pos_id(vertex_pos)?, - tex, - normal:acquire_normal_id(vertex_norm)?, - color, - })); - Ok(vec![ - ingest_vertex_id(v0)?, - ingest_vertex_id(v1)?, - ingest_vertex_id(v2)?, - ]) + let face=[ + mesh.vertices.get(vertex_id0.0 as usize).ok_or(Error::MissingVertexId(vertex_id0.0))?, + mesh.vertices.get(vertex_id1.0 as usize).ok_or(Error::MissingVertexId(vertex_id1.0))?, + mesh.vertices.get(vertex_id2.0 as usize).ok_or(Error::MissingVertexId(vertex_id2.0))?, + ]; + let vertex_norm=(glam::Vec3::from_slice(face[1])-glam::Vec3::from_slice(face[0])) + .cross(glam::Vec3::from_slice(face[2])-glam::Vec3::from_slice(face[0])).to_array(); + let normal=mb.acquire_normal_id(vec3::try_from_f32_array(vertex_norm).map_err(Error::Planar64Vec3)?); + face.into_iter().map(|&vertex_pos|{ + let pos=mb.acquire_pos_id(vec3::try_from_f32_array(vertex_pos).map_err(Error::Planar64Vec3)?); + Ok(mb.acquire_vertex_id(IndexedVertex{pos,tex,normal,color})) + }).collect() }).collect::<Result<_,_>>()?))) }).collect::<Result<_,_>>()?; let graphics_groups=vec![model::IndexedGraphicsGroup{ @@ -130,14 +78,9 @@ pub fn convert(roblox_physics_data:&[u8],roblox_mesh_data:&[u8])->Result<model:: let physics_groups=(0..polygon_groups.len()).map(|id|model::IndexedPhysicsGroup{ groups:vec![PolygonGroupId::new(id as u32)] }).collect(); - Ok(model::Mesh{ - unique_pos, - unique_normal, - unique_tex, - unique_color, - unique_vertices, + Ok(mb.build( polygon_groups, graphics_groups, physics_groups, - }) + )) }