feat: implement material loading and rendering
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d9a0c8b43
commit
a315cbc543
7 files changed
+138
-68
No files matched your search
+16
-2
@@ -1,6 +1,5 @@
|
||||
use crate::assets::loaders::material::mat_loader::MaterialInfo;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use vulkano::memory::allocator::MemoryAllocator;
|
||||
|
||||
pub struct MaterialRegistry {
|
||||
materials: HashMap<String, Material>
|
||||
@@ -36,3 +35,18 @@ pub struct Material {
|
||||
/// The path of the base color's texture, if any.
|
||||
pub base_color_texture: Option<String>,
|
||||
}
|
||||
|
||||
impl From<MaterialInfo> for Material {
|
||||
/// Converts loader output into a storable `Material`. `MaterialInfo::roughness_map` has no
|
||||
/// equivalent here yet - glTF packs it with the metallic map, and there's no texture loading
|
||||
/// in place to make use of either.
|
||||
fn from(info: MaterialInfo) -> Self {
|
||||
Self {
|
||||
base_color: info.uniforms.base_color,
|
||||
metallic: info.uniforms.metalness_factor,
|
||||
roughness: info.uniforms.roughness_factor,
|
||||
normal_map: info.normal_map,
|
||||
base_color_texture: info.base_color_map,
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-38
@@ -1,5 +1,4 @@
|
||||
use crate::assets::loaders::mesh_loader::{MeshInfo, MeshLoader};
|
||||
use crate::common::scene::Scene;
|
||||
use crate::assets::loaders::mesh_loader::MeshInfo;
|
||||
use crate::gpu::vertex3::Vertex3;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
@@ -148,42 +147,6 @@ impl MeshRegistry {
|
||||
self.rebuild_buffers();
|
||||
}
|
||||
|
||||
/// Creates a `MeshRegistry` from the given [`Scene`].
|
||||
///
|
||||
/// This method will load all the meshes indicated by the scene and allocate the required
|
||||
/// buffers. This significantly simplifies the allocation process, as the buffers only need to
|
||||
/// be allocated once.
|
||||
pub fn load_scene(&mut self, scene: &Scene, allocator: Arc<dyn MemoryAllocator>) {
|
||||
// Load all the meshes into a single map.
|
||||
println!("Loading meshes from scene");
|
||||
self.allocator = allocator;
|
||||
|
||||
let mesh_loader = MeshLoader::new();
|
||||
let meshes = scene
|
||||
.mesh_paths
|
||||
.iter()
|
||||
.map(|(id, path)| {
|
||||
(
|
||||
id.clone(),
|
||||
mesh_loader
|
||||
.load_mesh(path)
|
||||
.expect("Couldn't read wavefront file"),
|
||||
)
|
||||
})
|
||||
.collect::<HashMap<String, MeshInfo>>();
|
||||
|
||||
self.raw_vertices.clear();
|
||||
self.raw_indices.clear();
|
||||
let mut mesh_list = HashMap::new();
|
||||
meshes.into_iter().for_each(|(id, info)| {
|
||||
let handle = Self::append_mesh(&id, info, &mut self.raw_vertices, &mut self.raw_indices);
|
||||
mesh_list.insert(id, handle);
|
||||
});
|
||||
|
||||
self.meshes = mesh_list;
|
||||
self.rebuild_buffers();
|
||||
}
|
||||
|
||||
/// (Re)allocates `vertex_buffer`/`index_buffer` from the currently accumulated
|
||||
/// `raw_vertices`/`raw_indices`.
|
||||
fn rebuild_buffers(&mut self) {
|
||||
|
||||
Reference in new issue
Block a user