refactor model gen

This commit is contained in:
Quaternions 2023-09-27 17:24:25 -07:00
parent 23a1a8690b
commit 5b770fc8a9
4 changed files with 201 additions and 146 deletions

View File

@ -1,4 +1,4 @@
use crate::model::{ModelData,ModelInstance}; use crate::model::{IndexedModelInstances,ModelInstance};
use crate::primitives; use crate::primitives;
@ -129,12 +129,13 @@ impl RobloxUnitCubeGenerationData{
} }
} }
} }
pub fn generate_modeldatas_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(Vec<ModelData>,Vec<String>,glam::Vec3), Box<dyn std::error::Error>>{ 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 //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 //TODO: generate unit Block, Wedge, etc. after based on part shape lists
let mut modeldatas=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();

View File

@ -17,8 +17,12 @@ struct Entity {
index_buf: wgpu::Buffer, index_buf: wgpu::Buffer,
} }
struct ModelGraphicsInstance {
model_transform: glam::Affine3A,
color: glam::Vec4,
}
struct ModelGraphics { struct ModelGraphics {
instances: Vec<ModelInstance>, instances: Vec<ModelGraphicsInstance>,
vertex_buf: wgpu::Buffer, vertex_buf: wgpu::Buffer,
entities: Vec<Entity>, entities: Vec<Entity>,
bind_group: wgpu::BindGroup, bind_group: wgpu::BindGroup,
@ -90,13 +94,13 @@ impl GraphicsData {
).flatten().collect()); ).flatten().collect());
println!("Physics Objects: {}",self.physics.models.len()); println!("Physics Objects: {}",self.physics.models.len());
} }
fn generate_model_graphics(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,mut modeldatas:Vec<ModelData>,textures:Vec<String>){ fn generate_model_graphics(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,mut indexed_models:model::IndexedModelInstances){
//generate texture view per texture //generate texture view per texture
//idk how to do this gooder lol //idk how to do this gooder lol
let mut double_map=std::collections::HashMap::<u32,u32>::new(); let mut double_map=std::collections::HashMap::<u32,u32>::new();
let mut texture_views:Vec<wgpu::TextureView>=Vec::with_capacity(textures.len()); let mut texture_views:Vec<wgpu::TextureView>=Vec::with_capacity(indexed_models.textures.len());
for (i,t) in textures.iter().enumerate(){ for (i,t) in indexed_models.textures.iter().enumerate(){
if let Ok(mut file) = std::fs::File::open(std::path::Path::new(&format!("textures/{}.dds",t))){ if let Ok(mut file) = std::fs::File::open(std::path::Path::new(&format!("textures/{}.dds",t))){
let image = ddsfile::Dds::read(&mut file).unwrap(); let image = ddsfile::Dds::read(&mut file).unwrap();
@ -254,12 +258,11 @@ impl framework::Example for GraphicsData {
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue, queue: &wgpu::Queue,
) -> Self { ) -> Self {
let unit_cube=primitives::the_unit_cube_lol();
let mut modeldatas = Vec::<ModelData>::new(); let mut modeldatas = Vec::<ModelData>::new();
modeldatas.append(&mut model::generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE)); modeldatas.append(&mut model::generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE));
modeldatas.append(&mut model::generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/suzanne.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE)); modeldatas.append(&mut model::generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/suzanne.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE));
modeldatas.append(&mut model::generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE)); modeldatas.append(&mut model::generate_modeldatas(obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap(),ModelData::COLOR_FLOATS_WHITE));
modeldatas.append(&mut model::generate_modeldatas(unit_cube.clone(),ModelData::COLOR_FLOATS_WHITE)); modeldatas.push(primitives::the_unit_cube_lol());
println!("models.len = {:?}", modeldatas.len()); println!("models.len = {:?}", modeldatas.len());
modeldatas[0].instances.push(ModelInstance{ modeldatas[0].instances.push(ModelInstance{
model_transform:glam::Affine3A::from_translation(glam::vec3(10.,0.,-10.)), model_transform:glam::Affine3A::from_translation(glam::vec3(10.,0.,-10.)),

View File

@ -7,63 +7,77 @@ pub struct Vertex {
pub normal: [f32; 3], pub normal: [f32; 3],
pub color: [f32; 4], pub color: [f32; 4],
} }
#[derive(Hash)]
pub struct IndexedVertex{
pub pos:u32,
pub texture:u32,
pub normal:u32,
pub color:u32,
}
pub struct IndexedPolygon{
pub vertices:Vec<u32>,
}
pub struct IndexedGroup{
pub texture:Option<u32>,//RenderPattern? material/texture/shader/flat color
pub polys:Vec<IndexedPolygon>,
}
pub struct IndexedModel{
pub unique_pos:Vec<[f32; 3]>,
pub unique_texture:Vec<[f32; 2]>,
pub unique_normal:Vec<[f32; 3]>,
pub unique_color:Vec<[f32; 4]>,
pub unique_vertices:Vec<IndexedVertex>,
pub groups: Vec<IndexedGroup>,
}
#[derive(Clone)] #[derive(Clone)]
pub struct ModelInstance { pub struct ModelInstance{
pub model_transform: glam::Affine3A, pub model:u32,
pub color: glam::Vec4, pub model_transform:glam::Affine3A,
pub color:glam::Vec4,
}
pub struct IndexedModelInstances{
pub textures:Vec<String>,//RenderPattern
pub models:Vec<IndexedModel>,
pub instances:Vec<ModelInstance>,
//object_index for spawns, triggers etc?
} }
#[derive(Clone)] pub fn generate_indexed_model_from_obj(data:obj::ObjData,color:[f32;4]) -> Vec<IndexedModel>{
pub struct ModelData { let mut unique_vertex_index = std::collections::HashMap::<obj::IndexTuple,u32>::new();
pub instances: Vec<ModelInstance>, return data.objects.iter().map(|object|{
pub vertices: Vec<Vertex>, unique_vertex_index.clear();
pub entities: Vec<Vec<u16>>, let mut unique_vertices = Vec::new();
pub texture: Option<u32>, let groups = object.groups.iter().map(|group|{
} IndexedGroup{
texture:None,
impl ModelData { polys:group.polys.iter().map(|poly|{
pub const COLOR_FLOATS_WHITE: [f32;4] = [1.0,1.0,1.0,1.0]; IndexedPolygon{
pub const COLOR_VEC4_WHITE: glam::Vec4 = glam::vec4(1.0,1.0,1.0,1.0); vertices:poly.0.iter().map(|&tup|{
} if let Some(&i)=unique_vertex_index.get(&tup){
i
pub fn generate_modeldatas(data:obj::ObjData,color:[f32;4]) -> Vec<ModelData>{
let mut modeldatas=Vec::new();
let mut vertices = Vec::new();
let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new();
for object in data.objects {
vertices.clear();
vertex_index.clear();
let mut entities = Vec::new();
for group in object.groups {
let mut indices = Vec::new();
for poly in group.polys {
for end_index in 2..poly.0.len() {
for &index in &[0, end_index - 1, end_index] {
let vert = poly.0[index];
if let Some(&i)=vertex_index.get(&vert){
indices.push(i);
}else{ }else{
let i=vertices.len() as u16; let i=unique_vertices.len() as u32;
vertices.push(Vertex { unique_vertices.push(IndexedVertex{
pos: data.position[vert.0], pos: tup.0 as u32,
texture: data.texture[vert.1.unwrap()], texture: tup.1.unwrap() as u32,
normal: data.normal[vert.2.unwrap()], normal: tup.2.unwrap() as u32,
color, color: 0,
}); });
vertex_index.insert(vert,i); unique_vertex_index.insert(tup,i);
indices.push(i); i
} }
}).collect()
} }
}).collect()
} }
}).collect();
IndexedModel{
unique_pos: data.position.clone(),
unique_texture: data.texture.clone(),
unique_normal: data.normal.clone(),
unique_color: vec![color],
unique_vertices,
groups,
} }
entities.push(indices); }).collect()
}
modeldatas.push(ModelData {
instances: Vec::new(),
vertices:vertices.clone(),
entities,
texture: None,
});
}
modeldatas
} }

View File

@ -1,53 +1,88 @@
pub fn the_unit_cube_lol() -> obj::ObjData{ const CUBE_DEFAULT_TEXTURE_COORDS:[[f32;2];4]=[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]];
generate_partial_unit_cube([Some(glam::Affine2::IDENTITY);6]) const CUBE_DEFAULT_VERTICES:[[f32;3];8]=[
} [-1.,-1., 1.],//0 left bottom back
pub fn generate_partial_unit_cube(face_transforms:[Option<glam::Affine2>;6])->obj::ObjData{ [ 1.,-1., 1.],//1 right bottom back
let default_polys=[ [ 1., 1., 1.],//2 right top back
[-1., 1., 1.],//3 left top back
[-1., 1.,-1.],//4 left top front
[ 1., 1.,-1.],//5 right top front
[ 1.,-1.,-1.],//6 right bottom front
[-1.,-1.,-1.],//7 left bottom front
];
const CUBE_DEFAULT_NORMALS:[[f32;3];6]=[
[ 1., 0., 0.],//AabbFace::Right
[ 0., 1., 0.],//AabbFace::Top
[ 0., 0., 1.],//AabbFace::Back
[-1., 0., 0.],//AabbFace::Left
[ 0.,-1., 0.],//AabbFace::Bottom
[ 0., 0.,-1.],//AabbFace::Front
];
const CUBE_DEFAULT_POLYS:[[[u32;3];4];6]=[
// right (1, 0, 0) // right (1, 0, 0)
obj::SimplePolygon(vec![ [
obj::IndexTuple(6,Some(2),Some(0)), [6,2,0],//[vert,tex,norm]
obj::IndexTuple(5,Some(1),Some(0)), [5,1,0],
obj::IndexTuple(2,Some(0),Some(0)), [2,0,0],
obj::IndexTuple(1,Some(3),Some(0)), [1,3,0],
]), ],
// top (0, 1, 0) // top (0, 1, 0)
obj::SimplePolygon(vec![ [
obj::IndexTuple(5,Some(3),Some(1)), [5,3,1],
obj::IndexTuple(4,Some(2),Some(1)), [4,2,1],
obj::IndexTuple(3,Some(1),Some(1)), [3,1,1],
obj::IndexTuple(2,Some(0),Some(1)), [2,0,1],
]), ],
// back (0, 0, 1) // back (0, 0, 1)
obj::SimplePolygon(vec![ [
obj::IndexTuple(0,Some(3),Some(2)), [0,3,2],
obj::IndexTuple(1,Some(2),Some(2)), [1,2,2],
obj::IndexTuple(2,Some(1),Some(2)), [2,1,2],
obj::IndexTuple(3,Some(0),Some(2)), [3,0,2],
]), ],
// left (-1, 0, 0) // left (-1, 0, 0)
obj::SimplePolygon(vec![ [
obj::IndexTuple(0,Some(2),Some(3)), [0,2,3],
obj::IndexTuple(3,Some(1),Some(3)), [3,1,3],
obj::IndexTuple(4,Some(0),Some(3)), [4,0,3],
obj::IndexTuple(7,Some(3),Some(3)), [7,3,3],
]), ],
// bottom (0,-1, 0) // bottom (0,-1, 0)
obj::SimplePolygon(vec![ [
obj::IndexTuple(1,Some(1),Some(4)), [1,1,4],
obj::IndexTuple(0,Some(0),Some(4)), [0,0,4],
obj::IndexTuple(7,Some(3),Some(4)), [7,3,4],
obj::IndexTuple(6,Some(2),Some(4)), [6,2,4],
]), ],
// front (0, 0,-1) // front (0, 0,-1)
obj::SimplePolygon(vec![ [
obj::IndexTuple(4,Some(1),Some(5)), [4,1,5],
obj::IndexTuple(5,Some(0),Some(5)), [5,0,5],
obj::IndexTuple(6,Some(3),Some(5)), [6,3,5],
obj::IndexTuple(7,Some(2),Some(5)), [7,2,5],
]), ],
]; ];
let default_verts=[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]]; pub fn the_unit_cube_lol() -> crate::model::ModelData{
//generate transformed vertices generate_partial_unit_cube([Some(FaceDescription::default());6],None)
}
pub struct FaceDescription{
transform:glam::Affine2,
color:glam::Vec4,
}
impl std::default::Default for FaceDescription{
fn default() -> Self {
Self{
transform:glam::Affine2::IDENTITY,
color:glam::Vec4::ONE,
}
}
}
impl FaceDescription{
pub fn new(transform:glam::Affine2,color:glam::Vec4)->Self{
Self{transform,color}
}
}
pub fn generate_partial_unit_cube(face_transforms:[Option<FaceDescription>;6],texture:Option<u32>) -> crate::model::ModelData{ //generate transformed vertices
let mut generated_verts=Vec::new(); let mut generated_verts=Vec::new();
let mut transforms=Vec::new(); let mut transforms=Vec::new();
let mut generated_polys=Vec::new(); let mut generated_polys=Vec::new();
@ -71,35 +106,37 @@ pub fn generate_partial_unit_cube(face_transforms:[Option<glam::Affine2>;6])->ob
)); ));
} }
} }
obj::ObjData{ let mut vertices = Vec::new();
position: vec![ let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new();
[-1.,-1., 1.],//0 left bottom back let mut entities = Vec::new();
[ 1.,-1., 1.],//1 right bottom back for group in object.groups {
[ 1., 1., 1.],//2 right top back let mut indices = Vec::new();
[-1., 1., 1.],//3 left top back for poly in group.polys {
[-1., 1.,-1.],//4 left top front for end_index in 2..poly.0.len() {
[ 1., 1.,-1.],//5 right top front for &index in &[0, end_index - 1, end_index] {
[ 1.,-1.,-1.],//6 right bottom front let vert = poly.0[index];
[-1.,-1.,-1.],//7 left bottom front if let Some(&i)=vertex_index.get(&vert){
], indices.push(i);
texture: generated_verts, }else{
normal: vec![ let i=vertices.len() as u16;
[ 1., 0., 0.],//AabbFace::Right vertices.push(Vertex {
[ 0., 1., 0.],//AabbFace::Top pos: data.position[vert.0],
[ 0., 0., 1.],//AabbFace::Back texture: data.texture[vert.1.unwrap()],
[-1., 0., 0.],//AabbFace::Left normal: data.normal[vert.2.unwrap()],
[ 0.,-1., 0.],//AabbFace::Bottom color,
[ 0., 0.,-1.],//AabbFace::Front });
], vertex_index.insert(vert,i);
objects: vec![obj::Object{ indices.push(i);
name: "Unit Cube".to_owned(), }
groups: vec![obj::Group{ }
name: "Cube Vertices".to_owned(), }
index: 0, }
material: None, entities.push(indices);
polys: generated_polys, }
}] ModelData {
}], instances: Vec::new(),
material_libs: Vec::new(), vertices,
entities,
texture,
} }
} }