save 4 bytes per model + include camera matrix

This commit is contained in:
Quaternions 2023-10-06 13:57:39 -07:00
parent bde24d35a2
commit 436706bc4d
3 changed files with 22 additions and 18 deletions

View File

@ -73,7 +73,7 @@ impl GraphicsCamera{
pub fn proj(&self)->glam::Mat4{ pub fn proj(&self)->glam::Mat4{
perspective_rh(self.fov.x, self.fov.y, 0.5, 2000.0) 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 //f32 good enough for view matrix
glam::Mat4::from_translation(pos) * glam::Mat4::from_euler(glam::EulerRot::YXZ, angles.x, angles.y, 0f32) 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); 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=self.proj();
let proj_inv = proj.inverse(); let proj_inv = proj.inverse();
let view=self.view(pos,angles); let view_inv=self.world(pos,angles);
let view_inv = view.inverse(); 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].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]);
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]); 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[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view)[..]);
raw[48..52].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&view.col(3))); raw[48..64].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]);
raw raw
} }
} }
@ -226,7 +226,7 @@ impl GlobalState{
}else{ }else{
Some(ModelGraphicsInstance{ Some(ModelGraphicsInstance{
transform: glam::Mat4::from(instance.transform), 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, 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::<ModelInstance>(); const MODEL_BUFFER_SIZE:usize=4*4 + 12 + 4;//let size=std::mem::size_of::<ModelInstance>();
const MODEL_BUFFER_SIZE_BYTES:usize=MODEL_BUFFER_SIZE*4; const MODEL_BUFFER_SIZE_BYTES:usize=MODEL_BUFFER_SIZE*4;
fn get_instances_buffer_data(instances:&[ModelGraphicsInstance]) -> Vec<f32> { fn get_instances_buffer_data(instances:&[ModelGraphicsInstance]) -> Vec<f32> {
let mut raw = Vec::with_capacity(MODEL_BUFFER_SIZE*instances.len()); let mut raw = Vec::with_capacity(MODEL_BUFFER_SIZE*instances.len());
@ -383,7 +383,12 @@ fn get_instances_buffer_data(instances:&[ModelGraphicsInstance]) -> Vec<f32> {
//model transform //model transform
raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&mi.transform)[..]); raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&mi.transform)[..]);
//normal 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 //color
raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color)); raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color));
raw.append(&mut v); raw.append(&mut v);

View File

@ -52,7 +52,7 @@ pub struct ModelSingleTexture{
#[derive(Clone)] #[derive(Clone)]
pub struct ModelGraphicsInstance{ pub struct ModelGraphicsInstance{
pub transform:glam::Mat4, pub transform:glam::Mat4,
pub normal_transform:glam::Mat4, pub normal_transform:glam::Mat3,
pub color:glam::Vec4, pub color:glam::Vec4,
} }
pub struct ModelInstance{ pub struct ModelInstance{

View File

@ -5,8 +5,8 @@ struct Camera {
proj_inv: mat4x4<f32>, proj_inv: mat4x4<f32>,
// from world to camera // from world to camera
view: mat4x4<f32>, view: mat4x4<f32>,
// camera position // from camera to world
cam_pos: vec4<f32>, view_inv: mat4x4<f32>,
}; };
//group 0 is the camera //group 0 is the camera
@ -31,8 +31,7 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput {
1.0 1.0
); );
// transposition = inversion for this orthonormal matrix let inv_model_view = mat3x3<f32>(camera.view_inv[0].xyz, camera.view_inv[1].xyz, camera.view_inv[2].xyz);
let inv_model_view = transpose(mat3x3<f32>(camera.view[0].xyz, camera.view[1].xyz, camera.view[2].xyz));
let unprojected = camera.proj_inv * pos; let unprojected = camera.proj_inv * pos;
var result: SkyOutput; var result: SkyOutput;
@ -43,7 +42,7 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput {
struct ModelInstance{ struct ModelInstance{
transform:mat4x4<f32>, transform:mat4x4<f32>,
normal_transform:mat4x4<f32>, normal_transform:mat3x3<f32>,
color:vec4<f32>, color:vec4<f32>,
} }
//my fancy idea is to create a megatexture for each model that includes all the textures each intance will need //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 { ) -> EntityOutputTexture {
var position: vec4<f32> = model_instances[instance].transform * vec4<f32>(pos, 1.0); var position: vec4<f32> = model_instances[instance].transform * vec4<f32>(pos, 1.0);
var result: EntityOutputTexture; var result: EntityOutputTexture;
result.normal = (model_instances[instance].normal_transform * vec4<f32>(normal, 1.0)).xyz; result.normal = model_instances[instance].normal_transform * normal;
result.texture = texture; result.texture = texture;
result.color = color; result.color = color;
result.model_color = model_instances[instance].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; result.position = camera.proj * camera.view * position;
return result; return result;
} }