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;
@ -105,12 +105,12 @@ struct RobloxUnitCubeGenerationData{
} }
impl std::cmp::Eq for RobloxUnitCubeGenerationData{}//???? impl std::cmp::Eq for RobloxUnitCubeGenerationData{}//????
impl std::default::Default for RobloxUnitCubeGenerationData{ impl std::default::Default for RobloxUnitCubeGenerationData{
fn default() -> Self { fn default() -> Self {
Self{ Self{
texture:None, texture:None,
faces:[Some(RobloxColorAndTextureTransform::default());6], faces:[Some(RobloxColorAndTextureTransform::default());6],
} }
} }
} }
impl std::hash::Hash for RobloxUnitCubeGenerationData { impl std::hash::Hash for RobloxUnitCubeGenerationData {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
@ -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>{ }else{
let mut modeldatas=Vec::new(); let i=unique_vertices.len() as u32;
let mut vertices = Vec::new(); unique_vertices.push(IndexedVertex{
let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new(); pos: tup.0 as u32,
for object in data.objects { texture: tup.1.unwrap() as u32,
vertices.clear(); normal: tup.2.unwrap() as u32,
vertex_index.clear(); color: 0,
let mut entities = Vec::new(); });
for group in object.groups { unique_vertex_index.insert(tup,i);
let mut indices = Vec::new(); i
for poly in group.polys { }
for end_index in 2..poly.0.len() { }).collect()
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{
let i=vertices.len() as u16;
vertices.push(Vertex {
pos: data.position[vert.0],
texture: data.texture[vert.1.unwrap()],
normal: data.normal[vert.2.unwrap()],
color,
});
vertex_index.insert(vert,i);
indices.push(i);
}
} }
} }).collect()
} }
entities.push(indices); }).collect();
IndexedModel{
unique_pos: data.position.clone(),
unique_texture: data.texture.clone(),
unique_normal: data.normal.clone(),
unique_color: vec![color],
unique_vertices,
groups,
} }
modeldatas.push(ModelData { }).collect()
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
[ 1.,-1., 1.],//1 right bottom back
[ 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)
[
[6,2,0],//[vert,tex,norm]
[5,1,0],
[2,0,0],
[1,3,0],
],
// top (0, 1, 0)
[
[5,3,1],
[4,2,1],
[3,1,1],
[2,0,1],
],
// back (0, 0, 1)
[
[0,3,2],
[1,2,2],
[2,1,2],
[3,0,2],
],
// left (-1, 0, 0)
[
[0,2,3],
[3,1,3],
[4,0,3],
[7,3,3],
],
// bottom (0,-1, 0)
[
[1,1,4],
[0,0,4],
[7,3,4],
[6,2,4],
],
// front (0, 0,-1)
[
[4,1,5],
[5,0,5],
[6,3,5],
[7,2,5],
],
];
pub fn the_unit_cube_lol() -> crate::model::ModelData{
generate_partial_unit_cube([Some(FaceDescription::default());6],None)
} }
pub fn generate_partial_unit_cube(face_transforms:[Option<glam::Affine2>;6])->obj::ObjData{
let default_polys=[ pub struct FaceDescription{
// right (1, 0, 0) transform:glam::Affine2,
obj::SimplePolygon(vec![ color:glam::Vec4,
obj::IndexTuple(6,Some(2),Some(0)), }
obj::IndexTuple(5,Some(1),Some(0)), impl std::default::Default for FaceDescription{
obj::IndexTuple(2,Some(0),Some(0)), fn default() -> Self {
obj::IndexTuple(1,Some(3),Some(0)), Self{
]), transform:glam::Affine2::IDENTITY,
// top (0, 1, 0) color:glam::Vec4::ONE,
obj::SimplePolygon(vec![ }
obj::IndexTuple(5,Some(3),Some(1)), }
obj::IndexTuple(4,Some(2),Some(1)), }
obj::IndexTuple(3,Some(1),Some(1)), impl FaceDescription{
obj::IndexTuple(2,Some(0),Some(1)), pub fn new(transform:glam::Affine2,color:glam::Vec4)->Self{
]), Self{transform,color}
// back (0, 0, 1) }
obj::SimplePolygon(vec![ }
obj::IndexTuple(0,Some(3),Some(2)), pub fn generate_partial_unit_cube(face_transforms:[Option<FaceDescription>;6],texture:Option<u32>) -> crate::model::ModelData{ //generate transformed vertices
obj::IndexTuple(1,Some(2),Some(2)),
obj::IndexTuple(2,Some(1),Some(2)),
obj::IndexTuple(3,Some(0),Some(2)),
]),
// left (-1, 0, 0)
obj::SimplePolygon(vec![
obj::IndexTuple(0,Some(2),Some(3)),
obj::IndexTuple(3,Some(1),Some(3)),
obj::IndexTuple(4,Some(0),Some(3)),
obj::IndexTuple(7,Some(3),Some(3)),
]),
// bottom (0,-1, 0)
obj::SimplePolygon(vec![
obj::IndexTuple(1,Some(1),Some(4)),
obj::IndexTuple(0,Some(0),Some(4)),
obj::IndexTuple(7,Some(3),Some(4)),
obj::IndexTuple(6,Some(2),Some(4)),
]),
// front (0, 0,-1)
obj::SimplePolygon(vec![
obj::IndexTuple(4,Some(1),Some(5)),
obj::IndexTuple(5,Some(0),Some(5)),
obj::IndexTuple(6,Some(3),Some(5)),
obj::IndexTuple(7,Some(2),Some(5)),
]),
];
let default_verts=[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]];
//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,
}]
}],
material_libs: Vec::new(),
} }
} ModelData {
instances: Vec::new(),
vertices,
entities,
texture,
}
}