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),
}).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<GltfNode> = 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<GltfNode> = 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::<ResHandle<GltfScene>>(scene.as_arc_any()).unwrap();
let scene = scene.data_ref();
let gltf = loader.load(&mut manager, &path).unwrap();
let gltf = Arc::downcast::<ResHandle<Gltf>>(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);

View File

@ -8,4 +8,11 @@ pub mod mesh;
pub use mesh::*;
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 crate::{lyra_engine, ResHandle};
/// A Node in the Gltf file
#[derive(Clone, Default)]
pub struct GltfNode {
pub name: Option<String>,
@ -14,8 +15,8 @@ pub struct GltfNode {
pub children: Vec<GltfNode>,
}
/// A Scene in a Gltf file
#[derive(Clone)]
pub struct GltfScene {
pub nodes: Vec<GltfNode>,
pub materials: Vec<Material>,
}