From fd2b1683cc7c575ac0cfad29fef8072517ac19f4 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Mon, 4 Mar 2024 18:31:25 -0500 Subject: [PATCH] resource: load multiple gltf scenes --- lyra-resource/src/gltf/loader.rs | 49 ++++++++++++++++++-------------- lyra-resource/src/gltf/mod.rs | 9 +++++- lyra-resource/src/gltf/scene.rs | 3 +- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/lyra-resource/src/gltf/loader.rs b/lyra-resource/src/gltf/loader.rs index 2b26645..20ff5ad 100644 --- a/lyra-resource/src/gltf/loader.rs +++ b/lyra-resource/src/gltf/loader.rs @@ -173,8 +173,7 @@ impl ResourceLoader for ModelLoader { .map_err(ModelLoaderError::UriDecodingError), }).collect(); - // TODO: Read in multiple scenes - let scene = gltf.scenes().next().unwrap(); + let mut gltf_out = super::Gltf::default(); let mut context = GltfLoadContext { resource_manager, @@ -189,22 +188,27 @@ impl ResourceLoader for ModelLoader { .map(|mat| Material::from_gltf(&mut context, mat)) .collect(); let mat_time = Instant::now() - start_inst; + debug!("Loaded {} materials in {}s", materials.len(), mat_time.as_secs_f32()); - let start_inst = Instant::now(); - let nodes: Vec = scene.nodes() - .map(|node| ModelLoader::process_node(&mut context, &materials, node)) - .collect(); - let node_time = Instant::now() - start_inst; + for scene in gltf.scenes() { + let start_inst = Instant::now(); + let nodes: Vec = scene.nodes() + .map(|node| ModelLoader::process_node(&mut context, &materials, node)) + .collect(); + let node_time = Instant::now() - start_inst; + + debug!("Loaded {} nodes in the scene in {}s", nodes.len(), node_time.as_secs_f32()); - 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 { - nodes, - materials, - }; - - Ok(Arc::new(ResHandle::with_data(path, scene))) + /* 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 { + nodes, + }; + gltf_out.scenes.push(scene); + } + + Ok(Arc::new(ResHandle::with_data(path, gltf_out))) } #[allow(unused_variables)] @@ -215,7 +219,7 @@ impl ResourceLoader for ModelLoader { #[cfg(test)] mod tests { - use crate::ResourceLoader; + use crate::{gltf::Gltf, ResourceLoader}; use super::*; fn test_file_path(path: &str) -> String { @@ -230,13 +234,16 @@ mod tests { let mut manager = ResourceManager::new(); let loader = ModelLoader::default(); - let scene = loader.load(&mut manager, &path).unwrap(); - let scene = Arc::downcast::>(scene.as_arc_any()).unwrap(); - let scene = scene.data_ref(); + let gltf = loader.load(&mut manager, &path).unwrap(); + let gltf = Arc::downcast::>(gltf.as_arc_any()).unwrap(); + let gltf = gltf.data_ref(); + + assert_eq!(gltf.scenes.len(), 1); + let scene = &gltf.scenes[0]; assert_eq!(scene.nodes.len(), 1); - let mnode = &scene.nodes[0]; + assert!(mnode.mesh.is_some()); assert_eq!(mnode.transform, Transform::from_xyz(0.0, 0.0, 0.0)); assert_eq!(mnode.children.len(), 0); diff --git a/lyra-resource/src/gltf/mod.rs b/lyra-resource/src/gltf/mod.rs index 18772fc..0f7c8ac 100644 --- a/lyra-resource/src/gltf/mod.rs +++ b/lyra-resource/src/gltf/mod.rs @@ -8,4 +8,11 @@ pub mod mesh; pub use mesh::*; pub mod scene; -pub use scene::*; \ No newline at end of file +pub use scene::*; + +/// A loaded Gltf file +#[derive(Clone, Default)] +pub struct Gltf { + pub scenes: Vec, + pub materials: Vec, +} \ No newline at end of file diff --git a/lyra-resource/src/gltf/scene.rs b/lyra-resource/src/gltf/scene.rs index cd64df7..8b14456 100644 --- a/lyra-resource/src/gltf/scene.rs +++ b/lyra-resource/src/gltf/scene.rs @@ -6,6 +6,7 @@ use lyra_math::Transform; use super::{Material, Mesh}; use crate::{lyra_engine, ResHandle}; +/// A Node in the Gltf file #[derive(Clone, Default)] pub struct GltfNode { pub name: Option, @@ -14,8 +15,8 @@ pub struct GltfNode { pub children: Vec, } +/// A Scene in a Gltf file #[derive(Clone)] pub struct GltfScene { pub nodes: Vec, - pub materials: Vec, } \ No newline at end of file