mirror of
https://github.com/guilhermewerner/wgpu-renderer
synced 2025-06-15 13:24:20 +00:00
67 lines
2.0 KiB
Rust
67 lines
2.0 KiB
Rust
use crate::{Material, Mesh, Model};
|
|
use std::ops::Range;
|
|
|
|
pub trait DrawModel<'a> {
|
|
fn draw_mesh(
|
|
&mut self,
|
|
mesh: &'a Mesh,
|
|
material: &'a Material,
|
|
camera_bind_group: &'a wgpu::BindGroup,
|
|
);
|
|
fn draw_mesh_instanced(
|
|
&mut self,
|
|
mesh: &'a Mesh,
|
|
material: &'a Material,
|
|
instances: Range<u32>,
|
|
camera_bind_group: &'a wgpu::BindGroup,
|
|
);
|
|
fn draw_model(&mut self, model: &'a Model, camera_bind_group: &'a wgpu::BindGroup);
|
|
fn draw_model_instanced(
|
|
&mut self,
|
|
model: &'a Model,
|
|
instances: Range<u32>,
|
|
camera_bind_group: &'a wgpu::BindGroup,
|
|
);
|
|
}
|
|
|
|
impl<'a> DrawModel<'a> for wgpu::RenderPass<'a> {
|
|
fn draw_mesh(
|
|
&mut self,
|
|
mesh: &'a Mesh,
|
|
material: &'a Material,
|
|
camera_bind_group: &'a wgpu::BindGroup,
|
|
) {
|
|
self.draw_mesh_instanced(mesh, material, 0..1, camera_bind_group);
|
|
}
|
|
|
|
fn draw_mesh_instanced(
|
|
&mut self,
|
|
mesh: &'a Mesh,
|
|
material: &'a Material,
|
|
instances: Range<u32>,
|
|
camera_bind_group: &'a wgpu::BindGroup,
|
|
) {
|
|
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
|
|
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
|
|
self.set_bind_group(0, &material.bind_group, &[]);
|
|
self.set_bind_group(1, camera_bind_group, &[]);
|
|
self.draw_indexed(0..mesh.num_elements, 0, instances);
|
|
}
|
|
|
|
fn draw_model(&mut self, model: &'a Model, camera_bind_group: &'a wgpu::BindGroup) {
|
|
self.draw_model_instanced(model, 0..1, camera_bind_group);
|
|
}
|
|
|
|
fn draw_model_instanced(
|
|
&mut self,
|
|
model: &'a Model,
|
|
instances: Range<u32>,
|
|
camera_bind_group: &'a wgpu::BindGroup,
|
|
) {
|
|
for mesh in &model.meshes {
|
|
let material = &model.materials[mesh.material];
|
|
self.draw_mesh_instanced(mesh, material, instances.clone(), camera_bind_group);
|
|
}
|
|
}
|
|
}
|