From 1208be73bda32d84eb48d0862736635da38b44fe Mon Sep 17 00:00:00 2001 From: mars Date: Sat, 7 May 2022 22:17:10 -0600 Subject: [PATCH] LOTS of skinned meshes --- src/mesh.rs | 1 + src/pass/mesh.rs | 35 +++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/mesh.rs b/src/mesh.rs index c9c83a5..8f44798 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -341,6 +341,7 @@ pub struct MeshAlloc { /// A handle to an allocated mesh. #[repr(transparent)] +#[derive(Copy, Clone, Debug)] pub struct MeshHandle(usize); /// A reusable set of [AttrIds][AttrId], for use with querying compatible meshes. diff --git a/src/pass/mesh.rs b/src/pass/mesh.rs index 949e44d..0e52bb6 100644 --- a/src/pass/mesh.rs +++ b/src/pass/mesh.rs @@ -66,6 +66,11 @@ struct MeshGroupCommands { meshes: Vec, } +pub struct MeshInstance { + pub transform: glam::Mat4, + pub mesh: MeshHandle, +} + pub struct FrameData { skinned_vertices: GpuVec, skinning_uniforms: GpuVec, @@ -87,6 +92,7 @@ pub struct MeshPass { depth_pipeline: wgpu::RenderPipeline, opaque_pipeline: wgpu::RenderPipeline, target_info: ViewportInfo, + instances: Vec, } impl MeshPass { @@ -146,6 +152,21 @@ impl MeshPass { example_mesh.attributes.push(example_indices); let example_mesh = mesh_pool.load(example_mesh).unwrap(); + let mut instances = Vec::new(); + let r = 10; + for x in -r..r { + for y in -r..r { + for z in -r..r { + let translation = glam::Vec3::new(x as f32, y as f32, z as f32); + let transform = glam::Mat4::from_translation(translation); + instances.push(MeshInstance { + transform, + mesh: example_mesh.clone(), + }); + } + } + } + let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("MeshPass Pipeline Layout"), @@ -282,6 +303,7 @@ impl MeshPass { depth_pipeline, opaque_pipeline, target_info, + instances, } } } @@ -319,11 +341,9 @@ impl RenderPass for MeshPass { data.groups.clear(); data.skinning_uniforms.clear(); - let meshes = &[&self.example_mesh, &self.example_mesh]; - let mesh_bindings = self .mesh_pool - .iter_meshes(self.mesh_layout_id, meshes.iter(), |v| v) + .iter_meshes(self.mesh_layout_id, self.instances.iter(), |i| &i.mesh) .unwrap(); let mut skinned_cursor = 0; @@ -380,7 +400,7 @@ impl RenderPass for MeshPass { bind_group, }; - for (_mesh, infos) in instances { + for (mesh, infos) in instances { let vertices = infos.iter().find(|i| i.0 == self.vertex_attr_id).unwrap().1; let indices = infos.iter().find(|i| i.0 == self.index_attr_id).unwrap().1; @@ -394,12 +414,7 @@ impl RenderPass for MeshPass { }); data.skinning_uniforms.push(SkinningUniform { - transform: [ - [1., 0., 0., 0.], - [0., 1., 0., 0.], - [0., 0., 1., 0.], - [0., 0., 0., 1.], - ], + transform: mesh.transform.to_cols_array_2d(), src_offset: vertices.offset as u32, dst_offset: skinned_cursor as u32, count: vertices.count as u32,