LOTS of skinned meshes

This commit is contained in:
mars 2022-05-07 22:17:10 -06:00
parent aaa5ea4be0
commit 1208be73bd
2 changed files with 26 additions and 10 deletions

View File

@ -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.

View File

@ -66,6 +66,11 @@ struct MeshGroupCommands {
meshes: Vec<MeshCommand>,
}
pub struct MeshInstance {
pub transform: glam::Mat4,
pub mesh: MeshHandle,
}
pub struct FrameData {
skinned_vertices: GpuVec<Vertex>,
skinning_uniforms: GpuVec<SkinningUniform>,
@ -87,6 +92,7 @@ pub struct MeshPass {
depth_pipeline: wgpu::RenderPipeline,
opaque_pipeline: wgpu::RenderPipeline,
target_info: ViewportInfo,
instances: Vec<MeshInstance>,
}
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,