forked from StrafesNET/strafe-project
rbx_loader: move mesh size detection into mesh convert
This commit is contained in:
@@ -143,6 +143,12 @@ impl MeshIndex<'_>{
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MeshWithSize{
|
||||
pub(crate) mesh:Mesh,
|
||||
pub(crate) size:strafesnet_common::integer::Planar64Vec3,
|
||||
}
|
||||
|
||||
pub struct MeshLoader;
|
||||
impl MeshLoader{
|
||||
pub fn new()->Self{
|
||||
@@ -152,7 +158,7 @@ impl MeshLoader{
|
||||
impl Loader for MeshLoader{
|
||||
type Error=MeshError;
|
||||
type Index<'a>=MeshIndex<'a>;
|
||||
type Resource=Mesh;
|
||||
type Resource=MeshWithSize;
|
||||
fn load<'a>(&mut self,index:Self::Index<'a>)->Result<Self::Resource,Self::Error>{
|
||||
let mesh=match index.mesh_type{
|
||||
MeshType::FileMesh=>{
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rbx_mesh::mesh::{Vertex2,Vertex2Truncated};
|
||||
use strafesnet_common::{integer::vec3,model::{self,ColorId,IndexedVertex,NormalId,PolygonGroup,PolygonList,PositionId,RenderConfigId,TextureCoordinateId,VertexId}};
|
||||
use strafesnet_common::aabb::Aabb;
|
||||
use strafesnet_common::integer::vec3;
|
||||
use strafesnet_common::model::{self,ColorId,IndexedVertex,NormalId,PolygonGroup,PolygonList,PositionId,RenderConfigId,TextureCoordinateId,VertexId};
|
||||
|
||||
use crate::loader::MeshWithSize;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
@@ -95,7 +99,7 @@ fn ingest_faces2_lods3(
|
||||
))
|
||||
}
|
||||
|
||||
pub fn convert(roblox_mesh_bytes:crate::data::RobloxMeshBytes)->Result<model::Mesh,Error>{
|
||||
pub fn convert(roblox_mesh_bytes:crate::data::RobloxMeshBytes)->Result<MeshWithSize,Error>{
|
||||
//generate that mesh boi
|
||||
let mut unique_pos=Vec::new();
|
||||
let mut pos_id_from=HashMap::new();
|
||||
@@ -108,8 +112,10 @@ pub fn convert(roblox_mesh_bytes:crate::data::RobloxMeshBytes)->Result<model::Me
|
||||
let mut unique_vertices=Vec::new();
|
||||
let mut vertex_id_from=HashMap::new();
|
||||
let mut polygon_groups=Vec::new();
|
||||
let mut aabb=Aabb::default();
|
||||
let mut acquire_pos_id=|pos|{
|
||||
let p=vec3::try_from_f32_array(pos).map_err(Error::Planar64Vec3)?;
|
||||
aabb.grow(p);
|
||||
Ok(PositionId::new(*pos_id_from.entry(p).or_insert_with(||{
|
||||
let pos_id=unique_pos.len();
|
||||
unique_pos.push(p);
|
||||
@@ -197,7 +203,7 @@ pub fn convert(roblox_mesh_bytes:crate::data::RobloxMeshBytes)->Result<model::Me
|
||||
ingest_faces2_lods3(&mut polygon_groups,&vertex_id_map,&mesh.faces,&mesh.lods);
|
||||
},
|
||||
}
|
||||
Ok(model::Mesh{
|
||||
let mesh=model::Mesh{
|
||||
unique_pos,
|
||||
unique_normal,
|
||||
unique_tex,
|
||||
@@ -213,5 +219,6 @@ pub fn convert(roblox_mesh_bytes:crate::data::RobloxMeshBytes)->Result<model::Me
|
||||
}],
|
||||
//disable physics
|
||||
physics_groups:Vec::new(),
|
||||
})
|
||||
};
|
||||
Ok(MeshWithSize{mesh,size:aabb.size()})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
use crate::loader::MeshIndex;
|
||||
use crate::loader::{MeshWithSize,MeshIndex};
|
||||
use crate::primitives::{self,CubeFace,CubeFaceDescription,WedgeFaceDescription,CornerWedgeFaceDescription,FaceDescription,Primitives};
|
||||
use strafesnet_common::aabb::Aabb;
|
||||
use strafesnet_common::map;
|
||||
use strafesnet_common::model;
|
||||
use strafesnet_common::gameplay_modes::{NormalizedModes,Mode,ModeId,ModeUpdate,ModesBuilder,Stage,StageElement,StageElementBehaviour,StageId,Zone};
|
||||
@@ -709,23 +708,23 @@ pub fn convert<'a>(
|
||||
deferred_unions_deferred_attributes,
|
||||
}
|
||||
}
|
||||
struct MeshWithAabb{
|
||||
mesh:model::Mesh,
|
||||
aabb:Aabb,
|
||||
struct MeshIdWithSize{
|
||||
mesh:model::MeshId,
|
||||
size:Planar64Vec3,
|
||||
}
|
||||
fn acquire_mesh_id_from_render_config_id<'a>(
|
||||
primitive_meshes:&mut Vec<model::Mesh>,
|
||||
mesh_id_from_render_config_id:&mut HashMap<model::MeshId,HashMap<RenderConfigId,model::MeshId>>,
|
||||
loaded_meshes:&'a HashMap<model::MeshId,MeshWithAabb>,
|
||||
loaded_meshes:&'a HashMap<model::MeshId,MeshWithSize>,
|
||||
old_mesh_id:model::MeshId,
|
||||
render:RenderConfigId,
|
||||
)->Option<(model::MeshId,&'a Aabb)>{
|
||||
)->Option<MeshIdWithSize>{
|
||||
//ignore meshes that fail to load completely for now
|
||||
loaded_meshes.get(&old_mesh_id).map(|mesh_with_aabb|(
|
||||
*mesh_id_from_render_config_id.entry(old_mesh_id).or_insert_with(||HashMap::new())
|
||||
loaded_meshes.get(&old_mesh_id).map(|&MeshWithSize{ref mesh,size}|MeshIdWithSize{
|
||||
mesh:*mesh_id_from_render_config_id.entry(old_mesh_id).or_insert_with(||HashMap::new())
|
||||
.entry(render).or_insert_with(||{
|
||||
let mesh_id=model::MeshId::new(primitive_meshes.len() as u32);
|
||||
let mut mesh_clone=mesh_with_aabb.mesh.clone();
|
||||
let mut mesh_clone=mesh.clone();
|
||||
//set the render group lool
|
||||
if let Some(graphics_group)=mesh_clone.graphics_groups.first_mut(){
|
||||
graphics_group.render=render;
|
||||
@@ -733,22 +732,22 @@ fn acquire_mesh_id_from_render_config_id<'a>(
|
||||
primitive_meshes.push(mesh_clone);
|
||||
mesh_id
|
||||
}),
|
||||
&mesh_with_aabb.aabb,
|
||||
))
|
||||
size,
|
||||
})
|
||||
}
|
||||
fn acquire_union_id_from_render_config_id<'a>(
|
||||
primitive_meshes:&mut Vec<model::Mesh>,
|
||||
union_id_from_render_config_id:&mut HashMap<model::MeshId,HashMap<RobloxPartDescription,model::MeshId>>,
|
||||
loaded_meshes:&'a HashMap<model::MeshId,MeshWithAabb>,
|
||||
loaded_meshes:&'a HashMap<model::MeshId,MeshWithSize>,
|
||||
old_union_id:model::MeshId,
|
||||
part_texture_description:RobloxPartDescription,
|
||||
)->Option<(model::MeshId,&'a Aabb)>{
|
||||
)->Option<MeshIdWithSize>{
|
||||
//ignore uniones that fail to load completely for now
|
||||
loaded_meshes.get(&old_union_id).map(|union_with_aabb|(
|
||||
*union_id_from_render_config_id.entry(old_union_id).or_insert_with(||HashMap::new())
|
||||
loaded_meshes.get(&old_union_id).map(|&MeshWithSize{ref mesh,size}|MeshIdWithSize{
|
||||
mesh:*union_id_from_render_config_id.entry(old_union_id).or_insert_with(||HashMap::new())
|
||||
.entry(part_texture_description.clone()).or_insert_with(||{
|
||||
let union_id=model::MeshId::new(primitive_meshes.len() as u32);
|
||||
let mut union_clone=union_with_aabb.mesh.clone();
|
||||
let mut union_clone=mesh.clone();
|
||||
//set the render groups
|
||||
for (graphics_group,maybe_face_texture_description) in union_clone.graphics_groups.iter_mut().zip(part_texture_description.0){
|
||||
if let Some(face_texture_description)=maybe_face_texture_description{
|
||||
@@ -758,8 +757,8 @@ fn acquire_union_id_from_render_config_id<'a>(
|
||||
primitive_meshes.push(union_clone);
|
||||
union_id
|
||||
}),
|
||||
&union_with_aabb.aabb,
|
||||
))
|
||||
size,
|
||||
})
|
||||
}
|
||||
pub struct PartialMap1<'a>{
|
||||
primitive_meshes:Vec<model::Mesh>,
|
||||
@@ -770,7 +769,7 @@ pub struct PartialMap1<'a>{
|
||||
impl PartialMap1<'_>{
|
||||
pub fn add_meshpart_meshes_and_calculate_attributes(
|
||||
mut self,
|
||||
meshpart_meshes:Meshes,
|
||||
meshpart_meshes:Meshes<MeshWithSize>,
|
||||
)->PartialMap2{
|
||||
//calculate attributes
|
||||
let mut modes_builder=ModesBuilder::default();
|
||||
@@ -782,17 +781,8 @@ impl PartialMap1<'_>{
|
||||
|
||||
//decode roblox meshes
|
||||
//generate mesh_id_map based on meshes that failed to load
|
||||
let loaded_meshes:HashMap<model::MeshId,MeshWithAabb>=
|
||||
meshpart_meshes.consume().map(|(old_mesh_id,mesh)|{
|
||||
let mut aabb=strafesnet_common::aabb::Aabb::default();
|
||||
for &pos in &mesh.unique_pos{
|
||||
aabb.grow(pos);
|
||||
}
|
||||
(old_mesh_id,MeshWithAabb{
|
||||
mesh,
|
||||
aabb,
|
||||
})
|
||||
}).collect();
|
||||
let loaded_meshes:HashMap<model::MeshId,MeshWithSize>=
|
||||
meshpart_meshes.consume().collect();
|
||||
|
||||
// SAFETY: I have no idea what I'm doing and this is definitely unsound in some subtle way
|
||||
// I just want to chain iterators together man
|
||||
@@ -805,23 +795,22 @@ impl PartialMap1<'_>{
|
||||
self.deferred_models_deferred_attributes.into_iter().flat_map(|deferred_model_deferred_attributes|{
|
||||
//meshes need to be cloned from loaded_meshes with a new id when they are used with a new render_id
|
||||
//insert into primitive_meshes
|
||||
let (mesh,aabb)=acquire_mesh_id_from_render_config_id(
|
||||
let MeshIdWithSize{mesh,size:mesh_size}=acquire_mesh_id_from_render_config_id(
|
||||
unsafe{*aint_no_way.get()},
|
||||
&mut mesh_id_from_render_config_id,
|
||||
&loaded_meshes,
|
||||
deferred_model_deferred_attributes.model.mesh,
|
||||
deferred_model_deferred_attributes.render
|
||||
)?;
|
||||
let size=aabb.size();
|
||||
Some(ModelDeferredAttributes{
|
||||
mesh,
|
||||
deferred_attributes:deferred_model_deferred_attributes.model.deferred_attributes,
|
||||
color:deferred_model_deferred_attributes.model.color,
|
||||
transform:Planar64Affine3::new(
|
||||
Planar64Mat3::from_cols([
|
||||
(deferred_model_deferred_attributes.model.transform.matrix3.x_axis*2/size.x).divide().narrow_1().unwrap(),
|
||||
(deferred_model_deferred_attributes.model.transform.matrix3.y_axis*2/size.y).divide().narrow_1().unwrap(),
|
||||
(deferred_model_deferred_attributes.model.transform.matrix3.z_axis*2/size.z).divide().narrow_1().unwrap(),
|
||||
(deferred_model_deferred_attributes.model.transform.matrix3.x_axis*2/mesh_size.x).divide().narrow_1().unwrap(),
|
||||
(deferred_model_deferred_attributes.model.transform.matrix3.y_axis*2/mesh_size.y).divide().narrow_1().unwrap(),
|
||||
(deferred_model_deferred_attributes.model.transform.matrix3.z_axis*2/mesh_size.z).divide().narrow_1().unwrap(),
|
||||
]),
|
||||
deferred_model_deferred_attributes.model.transform.translation
|
||||
),
|
||||
@@ -829,14 +818,13 @@ impl PartialMap1<'_>{
|
||||
}).chain(self.deferred_unions_deferred_attributes.into_iter().flat_map(|deferred_union_deferred_attributes|{
|
||||
//meshes need to be cloned from loaded_meshes with a new id when they are used with a new render_id
|
||||
//insert into primitive_meshes
|
||||
let (mesh,aabb)=acquire_union_id_from_render_config_id(
|
||||
let MeshIdWithSize{mesh,size}=acquire_union_id_from_render_config_id(
|
||||
unsafe{*aint_no_way.get()},
|
||||
&mut union_id_from_render_config_id,
|
||||
&loaded_meshes,
|
||||
deferred_union_deferred_attributes.model.mesh,
|
||||
deferred_union_deferred_attributes.render
|
||||
)?;
|
||||
let size=aabb.size();
|
||||
Some(ModelDeferredAttributes{
|
||||
mesh,
|
||||
deferred_attributes:deferred_union_deferred_attributes.model.deferred_attributes,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::loader::MeshWithSize;
|
||||
use crate::rbx::RobloxPartDescription;
|
||||
use crate::primitives::{CUBE_DEFAULT_VERTICES,CUBE_DEFAULT_POLYS};
|
||||
|
||||
@@ -61,7 +62,7 @@ pub fn convert(
|
||||
roblox_mesh_data:&[u8],
|
||||
size:glam::Vec3,
|
||||
RobloxPartDescription(part_texture_description):RobloxPartDescription,
|
||||
)->Result<model::Mesh,Error>{
|
||||
)->Result<MeshWithSize,Error>{
|
||||
const NORMAL_FACES:usize=6;
|
||||
let mut polygon_groups_normal_id:[_;NORMAL_FACES]=[vec![],vec![],vec![],vec![],vec![],vec![]];
|
||||
|
||||
@@ -182,9 +183,13 @@ pub fn convert(
|
||||
let physics_groups=(NORMAL_FACES..polygon_groups.len()).map(|id|model::IndexedPhysicsGroup{
|
||||
groups:vec![PolygonGroupId::new(id as u32)]
|
||||
}).collect();
|
||||
Ok(mb.build(
|
||||
let mesh=mb.build(
|
||||
polygon_groups,
|
||||
graphics_groups,
|
||||
physics_groups,
|
||||
))
|
||||
);
|
||||
Ok(MeshWithSize{
|
||||
mesh,
|
||||
size:vec3::ONE,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user