load_roblox generates IndexedModelInstances

This commit is contained in:
Quaternions 2023-09-28 15:43:45 -07:00
parent 531473ef83
commit 39861c526b

View File

@ -70,79 +70,41 @@ impl std::hash::Hash for RobloxTextureTransform {
self.scale_v.to_ne_bytes().hash(state); self.scale_v.to_ne_bytes().hash(state);
} }
} }
#[derive(Clone,Copy,PartialEq)] #[derive(PartialEq)]
struct RobloxColorAndTextureTransform{ struct RobloxFaceTextureDescription{
transform:RobloxTextureTransform, texture:u32,
color:glam::Vec4, color:glam::Vec4,
transform:RobloxTextureTransform,
} }
impl std::cmp::Eq for RobloxColorAndTextureTransform{}//???? impl std::cmp::Eq for RobloxFaceTextureDescription{}//????
impl std::default::Default for RobloxColorAndTextureTransform{ impl std::hash::Hash for RobloxFaceTextureDescription {
fn default() -> Self {
Self{
transform:RobloxTextureTransform::default(),
color:glam::Vec4::ONE,
}
}
}
impl std::hash::Hash for RobloxColorAndTextureTransform {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.texture.hash(state);
self.transform.hash(state); self.transform.hash(state);
for &el in self.color.as_ref().iter() { for &el in self.color.as_ref().iter() {
el.to_ne_bytes().hash(state); el.to_ne_bytes().hash(state);
} }
} }
} }
struct PartFaceTextureDescription{ type RobloxPartDescription=[Option<RobloxFaceTextureDescription>;6];
texture:u32, type RobloxWedgeDescription=[Option<RobloxFaceTextureDescription>;5];
color:glam::Vec4, #[derive(Eq, Hash, PartialEq)]
transform:RobloxTextureTransform, enum RobloxBasePartDescription{
Part(RobloxPartDescription),
Wedge(RobloxWedgeDescription),
} }
type PartTextureDescription=[Option<PartFaceTextureDescription>;6]; pub fn generate_indexed_models_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(IndexedModelInstances,glam::Vec3), Box<dyn std::error::Error>>{
#[derive(PartialEq)] //IndexedModelInstances includes textures
struct RobloxUnitCubeGenerationData{
texture:Option<u32>,
faces:[Option<RobloxColorAndTextureTransform>;6],
}
impl std::cmp::Eq for RobloxUnitCubeGenerationData{}//????
impl std::default::Default for RobloxUnitCubeGenerationData{
fn default() -> Self {
Self{
texture:None,
faces:[Some(RobloxColorAndTextureTransform::default());6],
}
}
}
impl std::hash::Hash for RobloxUnitCubeGenerationData {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.texture.hash(state);
for &el in self.color.as_ref().iter() {
el.to_ne_bytes().hash(state);
}
self.faces.hash(state);
}
}
impl RobloxUnitCubeGenerationData{
fn empty() -> Self {
Self{
texture:None,
faces:[None,None,None,None,None,None],
}
}
}
pub fn generate_indexed_model_instances_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(IndexedModelInstances,Vec<String>,glam::Vec3), Box<dyn std::error::Error>>{
//ModelData includes texture dds
let mut spawn_point=glam::Vec3::ZERO; let mut spawn_point=glam::Vec3::ZERO;
//TODO: generate unit Block, Wedge, etc. after based on part shape lists
let mut indexed_models=Vec::new(); let mut indexed_models=Vec::new();
let mut model_instances=Vec::new();
let mut texture_id_from_asset_id=std::collections::HashMap::<u64,u32>::new(); let mut texture_id_from_asset_id=std::collections::HashMap::<u64,u32>::new();
let mut asset_id_from_texture_id=Vec::new(); let mut asset_id_from_texture_id=Vec::new();
let mut object_refs=Vec::new(); let mut object_refs=Vec::new();
let mut temp_objects=Vec::new(); let mut temp_objects=Vec::new();
let mut model_id_from_ucgd=std::collections::HashMap::<RobloxUnitCubeGenerationData,usize>::new(); let mut model_id_from_description=std::collections::HashMap::<RobloxBasePartDescription,usize>::new();
recursive_collect_superclass(&mut object_refs, &dom, dom.root(),"BasePart"); recursive_collect_superclass(&mut object_refs, &dom, dom.root(),"BasePart");
for object_ref in object_refs { for object_ref in object_refs {
if let Some(object)=dom.get_by_ref(object_ref){ if let Some(object)=dom.get_by_ref(object_ref){
@ -183,7 +145,7 @@ pub fn generate_indexed_model_instances_roblox(dom:rbx_dom_weak::WeakDom) -> Res
temp_objects.clear(); temp_objects.clear();
recursive_collect_superclass(&mut temp_objects, &dom, object,"Decal"); recursive_collect_superclass(&mut temp_objects, &dom, object,"Decal");
let mut part_texture_description:PartTextureDescription=[None,None,None,None,None,None]; let mut part_texture_description:RobloxPartDescription=[None,None,None,None,None,None];
for &decal_ref in &temp_objects{ for &decal_ref in &temp_objects{
if let Some(decal)=dom.get_by_ref(decal_ref){ if let Some(decal)=dom.get_by_ref(decal_ref){
if let ( if let (
@ -240,7 +202,7 @@ pub fn generate_indexed_model_instances_roblox(dom:rbx_dom_weak::WeakDom) -> Res
roblox_texture_color=glam::vec4(decal_color3.r as f32/255f32, decal_color3.g as f32/255f32, decal_color3.b as f32/255f32, 1.0-*decal_transparency); roblox_texture_color=glam::vec4(decal_color3.r as f32/255f32, decal_color3.g as f32/255f32, decal_color3.b as f32/255f32, 1.0-*decal_transparency);
} }
} }
part_texture_description[face as usize]=Some(PartFaceTextureDescription{ part_texture_description[face as usize]=Some(RobloxFaceTextureDescription{
texture:texture_id, texture:texture_id,
color:roblox_texture_color, color:roblox_texture_color,
transform:roblox_texture_transform, transform:roblox_texture_transform,
@ -252,65 +214,45 @@ pub fn generate_indexed_model_instances_roblox(dom:rbx_dom_weak::WeakDom) -> Res
} }
} }
} }
let mut unit_cube_generation_data_list=Vec::new(); //TODO: generate unit Block, Wedge, etc. based on part shape lists
let mut unit_cube_from_face_description=std::collections::HashMap::<u32,usize>::new(); let basepart_texture_description=RobloxBasePartDescription::Part(part_texture_description);
//use part_texture_description to extract unique texture faces
let mut add_negative_cube=false;
let mut negative_cube=RobloxUnitCubeGenerationData::empty();
for (i,maybe_part_face) in part_texture_description.iter().enumerate(){
if let Some(part_face)=maybe_part_face{
let unit_cube_id=if let Some(&unit_cube_id)=unit_cube_from_face_description.get(&part_face.texture){
unit_cube_id
}else{
let unit_cube_id=unit_cube_generation_data_list.len();
unit_cube_generation_data_list.push(RobloxUnitCubeGenerationData::empty());
unit_cube_from_face_description.insert(texture_with_color,unit_cube_id);
unit_cube_generation_data_list[unit_cube_id].texture=Some(part_face.texture);
unit_cube_id
};
unit_cube_generation_data_list[unit_cube_id].faces[i]=Some(part_face.transform_color);
}else{
add_negative_cube=true;
negative_cube.faces[i]=Some(RobloxTextureTransform::default());
}
}
//must add the rest of the cube to complete the faces!
if add_negative_cube{
unit_cube_generation_data_list.push(negative_cube);
}
for roblox_unit_cube_generation_data in unit_cube_generation_data_list.drain(..){
//make new model if unit cube has not been crated before //make new model if unit cube has not been crated before
let model_id=if let Some(&model_id)=model_id_from_ucgd.get(&roblox_unit_cube_generation_data){ let model_id=if let Some(&model_id)=model_id_from_description.get(&basepart_texture_description){
//push to existing texture model //push to existing texture model
model_id model_id
}else{ }else{
let unit_cube_generation_data=roblox_unit_cube_generation_data.faces.map(|face|{ let unit_cube_faces=part_texture_description.map(|face|{
match face{ match face{
Some(roblox_texture_transform)=>Some( Some(roblox_texture_transform)=>Some(
glam::Affine2::from_translation( primitives::FaceDescription{
glam::vec2(roblox_texture_transform.offset_u,roblox_texture_transform.offset_v) texture:Some(roblox_texture_transform.texture),
transform:glam::Affine2::from_translation(
glam::vec2(roblox_texture_transform.transform.offset_u,roblox_texture_transform.transform.offset_v)
) )
*glam::Affine2::from_scale( *glam::Affine2::from_scale(
glam::vec2(roblox_texture_transform.scale_u,roblox_texture_transform.scale_v) glam::vec2(roblox_texture_transform.transform.scale_u,roblox_texture_transform.transform.scale_v)
) ),
color:roblox_texture_transform.color,
}
), ),
None=>None, None=>None,
} }
}); });
let mut new_modeldatas=crate::model::generate_modeldatas(primitives::generate_partial_unit_cube(unit_cube_generation_data),*roblox_unit_cube_generation_data.color.as_ref()); let mut indexed_model=primitives::generate_partial_unit_cube(unit_cube_faces);
new_modeldatas[0].texture=roblox_unit_cube_generation_data.texture; let model_id=indexed_models.len();
let model_id=modeldatas.len(); indexed_models.push(indexed_model);
modeldatas.append(&mut new_modeldatas); model_id_from_description.insert(basepart_texture_description,model_id);
model_id_from_ucgd.insert(roblox_unit_cube_generation_data,model_id);
model_id model_id
}; };
modeldatas[model_id].instances.push(ModelInstance { indexed_models[model_id].instances.push(ModelInstance {
model_transform, model_transform,
color: glam::Vec4::ONE,//glam::vec4(color3.r as f32/255f32, color3.g as f32/255f32, color3.b as f32/255f32, 1.0-*transparency), color: glam::Vec4::ONE,//glam::vec4(color3.r as f32/255f32, color3.g as f32/255f32, color3.b as f32/255f32, 1.0-*transparency),
}); });
} }
} }
} }
} Ok((IndexedModelInstances{
Ok((modeldatas,asset_id_from_texture_id.iter().map(|t|t.to_string()).collect(),spawn_point)) textures:asset_id_from_texture_id.iter().map(|t|t.to_string()).collect(),
models:indexed_models,
},spawn_point))
} }