wgpu-renderer/src/core/model.rs

222 lines
6.5 KiB
Rust

use std::ops::Range;
use crate::core::texture::Texture;
pub struct Material {
pub name: String,
pub diffuse_texture: Texture,
pub normal_texture: Texture,
pub metallic_roughness_texture: Texture,
pub metallic_factor: f32, // TODO pass to shader
pub roughness_factor: f32, // TODO pass to shader
pub bind_group: wgpu::BindGroup,
}
impl Material {
pub fn new(
device: &wgpu::Device,
name: &str,
diffuse_texture: Texture,
normal_texture: Texture,
metallic_roughness_texture: Texture,
metallic_factor: f32,
roughness_factor: f32,
layout: &wgpu::BindGroupLayout,
) -> Self {
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout,
entries: &[
// diffuse
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
},
// normal
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::TextureView(&normal_texture.view),
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(&normal_texture.sampler),
},
// metallic roughness
wgpu::BindGroupEntry {
binding: 4,
resource: wgpu::BindingResource::TextureView(&metallic_roughness_texture.view),
},
wgpu::BindGroupEntry {
binding: 5,
resource: wgpu::BindingResource::Sampler(&metallic_roughness_texture.sampler),
},
],
label: None,
});
Self {
name: String::from(name),
diffuse_texture,
normal_texture,
metallic_roughness_texture,
metallic_factor,
roughness_factor,
bind_group,
}
}
}
pub struct Mesh {
pub name: String,
pub vertex_buffer: wgpu::Buffer,
pub index_buffer: wgpu::Buffer,
pub num_elements: u32,
pub material: usize,
}
pub struct Model {
pub meshes: Vec<Mesh>,
pub materials: Vec<Material>,
}
pub trait Vertex {
fn desc<'a>() -> wgpu::VertexBufferLayout<'a>;
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ModelVertex {
pub position: [f32; 3],
pub tex_coords: [f32; 2],
pub normal: [f32; 3],
pub tangent: [f32; 3],
pub bitangent: [f32; 3],
}
impl Vertex for ModelVertex {
fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
use std::mem;
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<ModelVertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
// position
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
// tex_coords
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x2,
},
// normal
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress,
shader_location: 2,
format: wgpu::VertexFormat::Float32x3,
},
// tangent
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 8]>() as wgpu::BufferAddress,
shader_location: 3,
format: wgpu::VertexFormat::Float32x3,
},
// bitangent
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 11]>() as wgpu::BufferAddress,
shader_location: 4,
format: wgpu::VertexFormat::Float32x3,
},
],
}
}
}
pub trait DrawModel<'a> {
fn draw_mesh(
&mut self,
mesh: &'a Mesh,
material: &'a Material,
bind_groups: Vec<&'a wgpu::BindGroup>,
);
fn draw_mesh_instanced(
&mut self,
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
bind_groups: Vec<&'a wgpu::BindGroup>,
);
fn draw_model(
&mut self,
model: &'a Model,
bind_groups: Vec<&'a wgpu::BindGroup>,
);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
bind_groups: Vec<&'a wgpu::BindGroup>,
);
}
impl<'a, 'b> DrawModel<'b> for wgpu::RenderPass<'a>
where
'b: 'a,
{
fn draw_mesh(
&mut self,
mesh: &'b Mesh,
material: &'b Material,
bind_groups: Vec<&'a wgpu::BindGroup>,
) {
self.draw_mesh_instanced(mesh, material, 0..1, bind_groups);
}
fn draw_mesh_instanced(
&mut self,
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
bind_groups: Vec<&'a wgpu::BindGroup>,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
for i in 0..bind_groups.len() {
self.set_bind_group(i as u32, bind_groups[i], &[]);
}
self.set_bind_group(bind_groups.len() as u32, &material.bind_group, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
fn draw_model(
&mut self,
model: &'b Model,
bind_groups: Vec<&'a wgpu::BindGroup>,
) {
self.draw_model_instanced(model, 0..1, bind_groups);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
bind_groups: Vec<&'a wgpu::BindGroup>,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(
mesh,
material,
instances.clone(),
bind_groups.clone(),
);
}
}
}