Some gltf improvements #4

Merged
SeanOMik merged 13 commits from feature/gltf-scene-fixes into main 2024-03-09 05:46:45 +00:00
3 changed files with 38 additions and 23 deletions
Showing only changes of commit fd2b1683cc - Show all commits

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());
let start_inst = Instant::now(); for scene in gltf.scenes() {
let nodes: Vec<GltfNode> = scene.nodes() let start_inst = Instant::now();
.map(|node| ModelLoader::process_node(&mut context, &materials, node)) let nodes: Vec<GltfNode> = scene.nodes()
.collect(); .map(|node| ModelLoader::process_node(&mut context, &materials, node))
let node_time = Instant::now() - start_inst; .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(), /* 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); 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

@ -8,4 +8,11 @@ pub mod mesh;
pub use mesh::*; 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>,
} }