resource: load multiple gltf scenes

This commit is contained in:
SeanOMik 2024-03-04 18:31:25 -05:00 committed by SeanOMik
parent 556b603f83
commit 7db913d15b
3 changed files with 38 additions and 23 deletions

View File

@ -173,8 +173,7 @@ impl ResourceLoader for ModelLoader {
.map_err(ModelLoaderError::UriDecodingError), .map_err(ModelLoaderError::UriDecodingError),
}).collect(); }).collect();
// TODO: Read in multiple scenes let mut gltf_out = super::Gltf::default();
let scene = gltf.scenes().next().unwrap();
let mut context = GltfLoadContext { let mut context = GltfLoadContext {
resource_manager, resource_manager,
@ -189,22 +188,27 @@ impl ResourceLoader for ModelLoader {
.map(|mat| Material::from_gltf(&mut context, mat)) .map(|mat| Material::from_gltf(&mut context, mat))
.collect(); .collect();
let mat_time = Instant::now() - start_inst; let mat_time = Instant::now() - start_inst;
debug!("Loaded {} materials in {}s", materials.len(), mat_time.as_secs_f32());
for scene in gltf.scenes() {
let start_inst = Instant::now(); let start_inst = Instant::now();
let nodes: Vec<GltfNode> = scene.nodes() let nodes: Vec<GltfNode> = scene.nodes()
.map(|node| ModelLoader::process_node(&mut context, &materials, node)) .map(|node| ModelLoader::process_node(&mut context, &materials, node))
.collect(); .collect();
let node_time = Instant::now() - start_inst; let node_time = Instant::now() - start_inst;
debug!("Loaded {} nodes (in {}s), and {} materials (in {}s) from '{}'", nodes.len(), debug!("Loaded {} nodes in the scene in {}s", nodes.len(), node_time.as_secs_f32());
node_time.as_secs_f32(), materials.len(), mat_time.as_secs_f32(), path);
/* debug!("Loaded {} nodes (in {}s), and {} materials (in {}s) from '{}'", nodes.len(),
node_time.as_secs_f32(), materials.len(), mat_time.as_secs_f32(), path); */
let scene = GltfScene { let scene = GltfScene {
nodes, nodes,
materials,
}; };
gltf_out.scenes.push(scene);
}
Ok(Arc::new(ResHandle::with_data(path, scene))) Ok(Arc::new(ResHandle::with_data(path, gltf_out)))
} }
#[allow(unused_variables)] #[allow(unused_variables)]
@ -215,7 +219,7 @@ impl ResourceLoader for ModelLoader {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::ResourceLoader; use crate::{gltf::Gltf, ResourceLoader};
use super::*; use super::*;
fn test_file_path(path: &str) -> String { fn test_file_path(path: &str) -> String {
@ -230,13 +234,16 @@ mod tests {
let mut manager = ResourceManager::new(); let mut manager = ResourceManager::new();
let loader = ModelLoader::default(); let loader = ModelLoader::default();
let scene = loader.load(&mut manager, &path).unwrap(); let gltf = loader.load(&mut manager, &path).unwrap();
let scene = Arc::downcast::<ResHandle<GltfScene>>(scene.as_arc_any()).unwrap(); let gltf = Arc::downcast::<ResHandle<Gltf>>(gltf.as_arc_any()).unwrap();
let scene = scene.data_ref(); let gltf = gltf.data_ref();
assert_eq!(gltf.scenes.len(), 1);
let scene = &gltf.scenes[0];
assert_eq!(scene.nodes.len(), 1); assert_eq!(scene.nodes.len(), 1);
let mnode = &scene.nodes[0]; let mnode = &scene.nodes[0];
assert!(mnode.mesh.is_some()); assert!(mnode.mesh.is_some());
assert_eq!(mnode.transform, Transform::from_xyz(0.0, 0.0, 0.0)); assert_eq!(mnode.transform, Transform::from_xyz(0.0, 0.0, 0.0));
assert_eq!(mnode.children.len(), 0); assert_eq!(mnode.children.len(), 0);

View File

@ -9,3 +9,10 @@ pub use mesh::*;
pub mod scene; pub mod scene;
pub use scene::*; pub use scene::*;
/// A loaded Gltf file
#[derive(Clone, Default)]
pub struct Gltf {
pub scenes: Vec<GltfScene>,
pub materials: Vec<Material>,
}

View File

@ -6,6 +6,7 @@ use lyra_math::Transform;
use super::{Material, Mesh}; use super::{Material, Mesh};
use crate::{lyra_engine, ResHandle}; use crate::{lyra_engine, ResHandle};
/// A Node in the Gltf file
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct GltfNode { pub struct GltfNode {
pub name: Option<String>, pub name: Option<String>,
@ -14,8 +15,8 @@ pub struct GltfNode {
pub children: Vec<GltfNode>, pub children: Vec<GltfNode>,
} }
/// A Scene in a Gltf file
#[derive(Clone)] #[derive(Clone)]
pub struct GltfScene { pub struct GltfScene {
pub nodes: Vec<GltfNode>, pub nodes: Vec<GltfNode>,
pub materials: Vec<Material>,
} }