Move material & mesh to separate files

This commit is contained in:
Lauri Räsänen 2023-11-08 11:03:09 +02:00
parent 90a2e2646a
commit cfd0c07ac6
6 changed files with 92 additions and 85 deletions

View file

@ -2,7 +2,8 @@ use std::ops::Range;
use super::{
camera::{FAR_PLANE, NEAR_PLANE},
model::{Mesh, Model},
model::{Model},
mesh::{Mesh},
};
use cgmath::{Matrix4, Vector3};

70
src/core/material.rs Normal file
View file

@ -0,0 +1,70 @@
use crate::core::texture::Texture;
pub struct Material {
pub name: String,
pub diffuse_texture: Texture,
pub normal_texture: Texture,
pub metallic_roughness_texture: Texture,
// TODO pass to shader
pub metallic_factor: f32,
// TODO pass to shader
pub roughness_factor: f32,
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,
}
}
}

7
src/core/mesh.rs Normal file
View file

@ -0,0 +1,7 @@
pub struct Mesh {
pub name: String,
pub vertex_buffer: wgpu::Buffer,
pub index_buffer: wgpu::Buffer,
pub num_elements: u32,
pub material: usize,
}

View file

@ -7,3 +7,5 @@ pub mod resources;
pub mod state;
pub mod texture;
pub mod window;
pub mod material;
pub mod mesh;

View file

@ -1,81 +1,6 @@
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,
}
use crate::core::material::Material;
use crate::core::mesh::Mesh;
pub struct Model {
pub meshes: Vec<Mesh>,
@ -167,8 +92,8 @@ pub trait DrawModel<'a> {
}
impl<'a, 'b> DrawModel<'b> for wgpu::RenderPass<'a>
where
'b: 'a,
where
'b: 'a,
{
fn draw_mesh(
&mut self,

View file

@ -1,7 +1,9 @@
use wgpu::util::DeviceExt;
use rust_embed::RustEmbed;
use crate::core::model::{Material, Mesh, Model, ModelVertex};
use crate::core::model::{Model, ModelVertex};
use crate::core::mesh::Mesh;
use crate::core::material::Material;
use crate::core::texture::Texture;
#[derive(RustEmbed)]
@ -191,7 +193,7 @@ pub async fn load_model_gltf(
gltf_image_format_to_wgpu(diffuse_data.format, true),
Some(file_name),
)
.unwrap();
.unwrap();
// normal
let normal_index = material
@ -220,7 +222,7 @@ pub async fn load_model_gltf(
gltf_image_format_to_wgpu(normal_data.format, false),
Some(file_name),
)
.unwrap();
.unwrap();
// roughness-metalness
let rm_index = pbr
@ -250,7 +252,7 @@ pub async fn load_model_gltf(
gltf_image_format_to_wgpu(rm_data.format, false),
Some(file_name),
)
.unwrap();
.unwrap();
materials.push(Material::new(
device,
@ -294,7 +296,7 @@ fn gltf_image_format_to_wgpu(format: gltf::image::Format, srgb: bool) -> wgpu::T
gltf::image::Format::R16G16 => wgpu::TextureFormat::Rg16Unorm,
gltf::image::Format::R16G16B16 => wgpu::TextureFormat::Rgba16Unorm, // converted
gltf::image::Format::R16G16B16A16 => wgpu::TextureFormat::Rgba16Unorm,
gltf::image::Format::R32G32B32FLOAT => wgpu::TextureFormat::Rgba32Float ,
gltf::image::Format::R32G32B32FLOAT => wgpu::TextureFormat::Rgba32Float,
gltf::image::Format::R32G32B32A32FLOAT => wgpu::TextureFormat::Rgba32Float,
}
}