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;
@ -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
let mut spawn_point=glam::Vec3::ZERO;
//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 asset_id_from_texture_id=Vec::new();

View File

@ -17,8 +17,12 @@ struct Entity {
index_buf: wgpu::Buffer,
}
struct ModelGraphicsInstance {
model_transform: glam::Affine3A,
color: glam::Vec4,
}
struct ModelGraphics {
instances: Vec<ModelInstance>,
instances: Vec<ModelGraphicsInstance>,
vertex_buf: wgpu::Buffer,
entities: Vec<Entity>,
bind_group: wgpu::BindGroup,
@ -90,13 +94,13 @@ impl GraphicsData {
).flatten().collect());
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
//idk how to do this gooder lol
let mut double_map=std::collections::HashMap::<u32,u32>::new();
let mut texture_views:Vec<wgpu::TextureView>=Vec::with_capacity(textures.len());
for (i,t) in textures.iter().enumerate(){
let mut texture_views:Vec<wgpu::TextureView>=Vec::with_capacity(indexed_models.textures.len());
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))){
let image = ddsfile::Dds::read(&mut file).unwrap();
@ -254,12 +258,11 @@ impl framework::Example for GraphicsData {
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self {
let unit_cube=primitives::the_unit_cube_lol();
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/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(unit_cube.clone(),ModelData::COLOR_FLOATS_WHITE));
modeldatas.push(primitives::the_unit_cube_lol());
println!("models.len = {:?}", modeldatas.len());
modeldatas[0].instances.push(ModelInstance{
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 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)]
pub struct ModelInstance{
pub model:u32,
pub model_transform:glam::Affine3A,
pub color:glam::Vec4,
}
#[derive(Clone)]
pub struct ModelData {
pub struct IndexedModelInstances{
pub textures:Vec<String>,//RenderPattern
pub models:Vec<IndexedModel>,
pub instances:Vec<ModelInstance>,
pub vertices: Vec<Vertex>,
pub entities: Vec<Vec<u16>>,
pub texture: Option<u32>,
//object_index for spawns, triggers etc?
}
impl ModelData {
pub const COLOR_FLOATS_WHITE: [f32;4] = [1.0,1.0,1.0,1.0];
pub const COLOR_VEC4_WHITE: glam::Vec4 = glam::vec4(1.0,1.0,1.0,1.0);
}
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{
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);
}
}
}
}
entities.push(indices);
}
modeldatas.push(ModelData {
instances: Vec::new(),
vertices:vertices.clone(),
entities,
pub fn generate_indexed_model_from_obj(data:obj::ObjData,color:[f32;4]) -> Vec<IndexedModel>{
let mut unique_vertex_index = std::collections::HashMap::<obj::IndexTuple,u32>::new();
return data.objects.iter().map(|object|{
unique_vertex_index.clear();
let mut unique_vertices = Vec::new();
let groups = object.groups.iter().map(|group|{
IndexedGroup{
texture:None,
polys:group.polys.iter().map(|poly|{
IndexedPolygon{
vertices:poly.0.iter().map(|&tup|{
if let Some(&i)=unique_vertex_index.get(&tup){
i
}else{
let i=unique_vertices.len() as u32;
unique_vertices.push(IndexedVertex{
pos: tup.0 as u32,
texture: tup.1.unwrap() as u32,
normal: tup.2.unwrap() as u32,
color: 0,
});
unique_vertex_index.insert(tup,i);
i
}
modeldatas
}).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,
}
}).collect()
}

View File

@ -1,53 +1,88 @@
pub fn the_unit_cube_lol() -> obj::ObjData{
generate_partial_unit_cube([Some(glam::Affine2::IDENTITY);6])
}
pub fn generate_partial_unit_cube(face_transforms:[Option<glam::Affine2>;6])->obj::ObjData{
let default_polys=[
// right (1, 0, 0)
obj::SimplePolygon(vec![
obj::IndexTuple(6,Some(2),Some(0)),
obj::IndexTuple(5,Some(1),Some(0)),
obj::IndexTuple(2,Some(0),Some(0)),
obj::IndexTuple(1,Some(3),Some(0)),
]),
// top (0, 1, 0)
obj::SimplePolygon(vec![
obj::IndexTuple(5,Some(3),Some(1)),
obj::IndexTuple(4,Some(2),Some(1)),
obj::IndexTuple(3,Some(1),Some(1)),
obj::IndexTuple(2,Some(0),Some(1)),
]),
// back (0, 0, 1)
obj::SimplePolygon(vec![
obj::IndexTuple(0,Some(3),Some(2)),
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)),
]),
const CUBE_DEFAULT_TEXTURE_COORDS:[[f32;2];4]=[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]];
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
];
let default_verts=[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]];
//generate transformed vertices
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 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 transforms=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{
position: vec![
[-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
],
texture: generated_verts,
normal: vec![
[ 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
],
objects: vec![obj::Object{
name: "Unit Cube".to_owned(),
groups: vec![obj::Group{
name: "Cube Vertices".to_owned(),
index: 0,
material: None,
polys: generated_polys,
}]
}],
material_libs: Vec::new(),
let mut vertices = Vec::new();
let mut vertex_index = std::collections::HashMap::<obj::IndexTuple,u16>::new();
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{
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);
}
}
}
}
entities.push(indices);
}
ModelData {
instances: Vec::new(),
vertices,
entities,
texture,
}
}