From 436706bc4dd105778c3d47ee1f5e78d3a0fb3a94 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 6 Oct 2023 13:57:39 -0700 Subject: [PATCH] save 4 bytes per model + include camera matrix --- src/main.rs | 25 +++++++++++++++---------- src/model.rs | 2 +- src/shader.wgsl | 13 ++++++------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3b5c70e..0b81b78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,7 @@ impl GraphicsCamera{ pub fn proj(&self)->glam::Mat4{ perspective_rh(self.fov.x, self.fov.y, 0.5, 2000.0) } - pub fn view(&self,pos:glam::Vec3,angles:glam::Vec2)->glam::Mat4{ + pub fn world(&self,pos:glam::Vec3,angles:glam::Vec2)->glam::Mat4{ //f32 good enough for view matrix glam::Mat4::from_translation(pos) * glam::Mat4::from_euler(glam::EulerRot::YXZ, angles.x, angles.y, 0f32) } @@ -82,17 +82,17 @@ impl GraphicsCamera{ self.fov.x=self.fov.y*(screen_size.x as f32)/(screen_size.y as f32); } - pub fn to_uniform_data(&self,(pos,angles): (glam::Vec3,glam::Vec2)) -> [f32; 16 * 3 + 4] { + pub fn to_uniform_data(&self,(pos,angles): (glam::Vec3,glam::Vec2)) -> [f32; 16 * 4] { let proj=self.proj(); let proj_inv = proj.inverse(); - let view=self.view(pos,angles); - let view_inv = view.inverse(); + let view_inv=self.world(pos,angles); + let view=view_inv.inverse(); - let mut raw = [0f32; 16 * 3 + 4]; + let mut raw = [0f32; 16 * 4]; raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]); raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]); - raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]); - raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3))); + raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view)[..]); + raw[48..64].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]); raw } } @@ -226,7 +226,7 @@ impl GlobalState{ }else{ Some(ModelGraphicsInstance{ transform: glam::Mat4::from(instance.transform), - normal_transform: glam::Mat4::from(instance.transform.inverse()).transpose(), + normal_transform: glam::Mat3::from(instance.transform.matrix3.inverse().transpose()), color: instance.color, }) } @@ -374,7 +374,7 @@ impl GlobalState{ } } -const MODEL_BUFFER_SIZE:usize=4*4 + 4*4 + 4;//let size=std::mem::size_of::(); +const MODEL_BUFFER_SIZE:usize=4*4 + 12 + 4;//let size=std::mem::size_of::(); const MODEL_BUFFER_SIZE_BYTES:usize=MODEL_BUFFER_SIZE*4; fn get_instances_buffer_data(instances:&[ModelGraphicsInstance]) -> Vec { let mut raw = Vec::with_capacity(MODEL_BUFFER_SIZE*instances.len()); @@ -383,7 +383,12 @@ fn get_instances_buffer_data(instances:&[ModelGraphicsInstance]) -> Vec { //model transform raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&mi.transform)[..]); //normal transform - raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&mi.normal_transform)[..]); + raw.extend_from_slice(AsRef::<[f32; 3]>::as_ref(&mi.normal_transform.x_axis)); + raw.extend_from_slice(&[0.0]); + raw.extend_from_slice(AsRef::<[f32; 3]>::as_ref(&mi.normal_transform.y_axis)); + raw.extend_from_slice(&[0.0]); + raw.extend_from_slice(AsRef::<[f32; 3]>::as_ref(&mi.normal_transform.z_axis)); + raw.extend_from_slice(&[0.0]); //color raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color)); raw.append(&mut v); diff --git a/src/model.rs b/src/model.rs index 6434734..3040a61 100644 --- a/src/model.rs +++ b/src/model.rs @@ -52,7 +52,7 @@ pub struct ModelSingleTexture{ #[derive(Clone)] pub struct ModelGraphicsInstance{ pub transform:glam::Mat4, - pub normal_transform:glam::Mat4, + pub normal_transform:glam::Mat3, pub color:glam::Vec4, } pub struct ModelInstance{ diff --git a/src/shader.wgsl b/src/shader.wgsl index 5e2a7e1..f993df4 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -5,8 +5,8 @@ struct Camera { proj_inv: mat4x4, // from world to camera view: mat4x4, - // camera position - cam_pos: vec4, + // from camera to world + view_inv: mat4x4, }; //group 0 is the camera @@ -31,8 +31,7 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput { 1.0 ); - // transposition = inversion for this orthonormal matrix - let inv_model_view = transpose(mat3x3(camera.view[0].xyz, camera.view[1].xyz, camera.view[2].xyz)); + let inv_model_view = mat3x3(camera.view_inv[0].xyz, camera.view_inv[1].xyz, camera.view_inv[2].xyz); let unprojected = camera.proj_inv * pos; var result: SkyOutput; @@ -43,7 +42,7 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput { struct ModelInstance{ transform:mat4x4, - normal_transform:mat4x4, + normal_transform:mat3x3, color:vec4, } //my fancy idea is to create a megatexture for each model that includes all the textures each intance will need @@ -78,11 +77,11 @@ fn vs_entity_texture( ) -> EntityOutputTexture { var position: vec4 = model_instances[instance].transform * vec4(pos, 1.0); var result: EntityOutputTexture; - result.normal = (model_instances[instance].normal_transform * vec4(normal, 1.0)).xyz; + result.normal = model_instances[instance].normal_transform * normal; result.texture = texture; result.color = color; result.model_color = model_instances[instance].color; - result.view = position.xyz - camera.cam_pos.xyz; + result.view = position.xyz - camera.view_inv[3].xyz;//col(3) result.position = camera.proj * camera.view * position; return result; }