diff --git a/src/body.rs b/src/body.rs index 27ff28c..37c9d22 100644 --- a/src/body.rs +++ b/src/body.rs @@ -269,12 +269,12 @@ type TreyMesh = Aabb; pub struct ModelPhysics { //A model is a thing that has a hitbox. can be represented by a list of TreyMesh-es //in this iteration, all it needs is extents. - transform: glam::Mat4, + model_transform: glam::Affine3A, } impl ModelPhysics { - pub fn new(transform:glam::Mat4) -> Self { - Self{transform} + pub fn new(model_transform:glam::Affine3A) -> Self { + Self{model_transform} } pub fn unit_vertices(&self) -> [glam::Vec3;8] { Aabb::unit_vertices() @@ -282,7 +282,7 @@ impl ModelPhysics { pub fn mesh(&self) -> TreyMesh { let mut aabb=Aabb::new(); for &vertex in self.unit_vertices().iter() { - aabb.grow(glam::Vec4Swizzles::xyz(self.transform*vertex.extend(1.0))); + aabb.grow(self.model_transform.transform_point3(vertex)); } return aabb; } @@ -303,7 +303,7 @@ impl ModelPhysics { return aabb; } pub fn face_normal(&self,face:TreyMeshFace) -> glam::Vec3 { - glam::Vec4Swizzles::xyz(Aabb::normal(face).extend(0.0))//this is wrong for scale + Aabb::normal(face)//this is wrong for scale } } diff --git a/src/load_roblox.rs b/src/load_roblox.rs index 0403c07..7d2ddc9 100644 --- a/src/load_roblox.rs +++ b/src/load_roblox.rs @@ -79,24 +79,21 @@ pub fn generate_modeldatas_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(Vec Result<(Vec(){ if let Some(&texture_id)=texture_id_from_asset_id.get(&asset_id.0){ i_can_only_load_one_texture_per_model=Some(texture_id); @@ -125,6 +146,11 @@ pub fn generate_modeldatas_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(Vecmodeldatas[(texture_id+1) as usize].instances.push(model_instance), diff --git a/src/main.rs b/src/main.rs index 95a5b6b..ee707fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -164,7 +164,7 @@ impl GraphicsData { fn generate_model_physics(&mut self,modeldatas:&Vec){ self.physics.models.append(&mut modeldatas.iter().map(|m| //make aabb and run vertices to get realistic bounds - m.instances.iter().map(|t|body::ModelPhysics::new(t.transform)) + m.instances.iter().map(|t|body::ModelPhysics::new(t.model_transform)) ).flatten().collect()); println!("Physics Objects: {}",self.physics.models.len()); } @@ -285,12 +285,19 @@ impl GraphicsData { } } -fn get_instances_buffer_data(instances:&Vec) -> Vec { - const SIZE: usize=4*4+4;//let size=std::mem::size_of::(); - let mut raw = Vec::with_capacity(SIZE*instances.len()); +const MODEL_BUFFER_SIZE:usize=4*4 + 2*2 + 2+2 + 4;//let size=std::mem::size_of::(); +fn get_instances_buffer_data(instances:&[ModelInstance]) -> Vec { + let mut raw = Vec::with_capacity(MODEL_BUFFER_SIZE*instances.len()); for (i,mi) in instances.iter().enumerate(){ - let mut v = raw.split_off(SIZE*i); - raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&mi.transform)[..]); + let mut v = raw.split_off(MODEL_BUFFER_SIZE*i); + //model_transform + raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&glam::Mat4::from(mi.model_transform))[..]); + //texture_matrix + raw.extend_from_slice(&AsRef::<[f32; 2*2]>::as_ref(&mi.texture_transform.matrix2)[..]); + //texture_offset + raw.extend_from_slice(AsRef::<[f32; 2]>::as_ref(&mi.texture_transform.translation)); + raw.extend_from_slice(&[0.0,0.0]); + //color raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color)); raw.append(&mut v); } @@ -320,34 +327,41 @@ impl framework::Example for GraphicsData { modeldatas.append(&mut model::generate_modeldatas(unit_cube.clone(),ModelData::COLOR_FLOATS_WHITE)); println!("models.len = {:?}", modeldatas.len()); modeldatas[0].instances.push(ModelInstance{ - transform:glam::Mat4::from_translation(glam::vec3(10.,0.,-10.)), + model_transform:glam::Affine3A::from_translation(glam::vec3(10.,0.,-10.)), + texture_transform:glam::Affine2::IDENTITY, color:ModelData::COLOR_VEC4_WHITE, }); //quad monkeys modeldatas[1].instances.push(ModelInstance{ - transform:glam::Mat4::from_translation(glam::vec3(10.,5.,10.)), + model_transform:glam::Affine3A::from_translation(glam::vec3(10.,5.,10.)), + texture_transform:glam::Affine2::IDENTITY, color:ModelData::COLOR_VEC4_WHITE, }); modeldatas[1].instances.push(ModelInstance{ - transform:glam::Mat4::from_translation(glam::vec3(20.,5.,10.)), + model_transform:glam::Affine3A::from_translation(glam::vec3(20.,5.,10.)), + texture_transform:glam::Affine2::IDENTITY, color:glam::vec4(1.0,0.0,0.0,1.0), }); modeldatas[1].instances.push(ModelInstance{ - transform:glam::Mat4::from_translation(glam::vec3(10.,5.,20.)), + model_transform:glam::Affine3A::from_translation(glam::vec3(10.,5.,20.)), + texture_transform:glam::Affine2::IDENTITY, color:glam::vec4(0.0,1.0,0.0,1.0), }); modeldatas[1].instances.push(ModelInstance{ - transform:glam::Mat4::from_translation(glam::vec3(20.,5.,20.)), + model_transform:glam::Affine3A::from_translation(glam::vec3(20.,5.,20.)), + texture_transform:glam::Affine2::IDENTITY, color:glam::vec4(0.0,0.0,1.0,1.0), }); //teapot modeldatas[2].instances.push(ModelInstance{ - transform:glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)), + model_transform:glam::Affine3A::from_translation(glam::vec3(-10.,5.,10.)), + texture_transform:glam::Affine2::IDENTITY, color:ModelData::COLOR_VEC4_WHITE, }); //ground modeldatas[3].instances.push(ModelInstance{ - transform:glam::Mat4::from_translation(glam::vec3(0.,0.,0.))*glam::Mat4::from_scale(glam::vec3(160.0, 1.0, 160.0)), + model_transform:glam::Affine3A::from_translation(glam::vec3(0.,0.,0.))*glam::Affine3A::from_scale(glam::vec3(160.0, 1.0, 160.0)), + texture_transform:glam::Affine2::IDENTITY, color:ModelData::COLOR_VEC4_WHITE, }); diff --git a/src/model.rs b/src/model.rs index 99f4c0f..5fcac1d 100644 --- a/src/model.rs +++ b/src/model.rs @@ -7,10 +7,10 @@ pub struct Vertex { pub normal: [f32; 3], pub color: [f32; 4], } - #[derive(Clone)] pub struct ModelInstance { - pub transform: glam::Mat4, + pub model_transform: glam::Affine3A, + pub texture_transform: glam::Affine2, pub color: glam::Vec4, } diff --git a/src/shader.wgsl b/src/shader.wgsl index e289c02..d2a3165 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -42,8 +42,10 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput { } struct ModelInstance{ - transform:mat4x4, - //texture_transform:mat3x3, + model_transform:mat4x4, + //simple to save 4 bytes, so do it. above would need to be spit out into components. + texture_matrix:mat2x2, + texture_offset:vec2, color:vec4, } //my fancy idea is to create a megatexture for each model that includes all the textures each intance will need @@ -75,11 +77,11 @@ fn vs_entity_texture( @location(2) normal: vec3, @location(3) color: vec4, ) -> EntityOutputTexture { - var position: vec4 = model_instances[instance].transform * vec4(pos, 1.0); + var position: vec4 = model_instances[instance].model_transform * vec4(pos, 1.0); var result: EntityOutputTexture; - result.normal = (model_instances[instance].transform * vec4(normal, 0.0)).xyz; - result.texture=texture;//(model_instances[instance].texture_transform * vec3(texture, 1.0)).xy; - result.color=model_instances[instance].color * color; + result.normal = (model_instances[instance].model_transform * vec4(normal, 0.0)).xyz; + result.texture = model_instances[instance].texture_matrix * texture + model_instances[instance].texture_offset; + result.color = model_instances[instance].color * color; result.view = position.xyz - camera.cam_pos.xyz; result.position = camera.proj * camera.view * position; return result;