lyra-engine/lyra-resource/src/loader/model.rs

75 lines
2.2 KiB
Rust
Raw Normal View History

2023-09-21 21:27:21 +00:00
use crate::{ResourceLoader, LoaderError, Mesh};
2023-09-21 18:22:46 +00:00
impl From<gltf::Error> for LoaderError {
fn from(value: gltf::Error) -> Self {
LoaderError::DecodingError(value.into())
}
}
#[derive(Default)]
pub struct ModelLoader;
2023-09-21 21:27:21 +00:00
impl ModelLoader {
fn process_node(&self, views: Vec<gltf::buffer::View<'_>>, node: gltf::Node<'_>) -> Vec<Mesh> {
let mut meshes = vec![];
if let Some(mesh) = node.mesh() {
for prim in mesh.primitives() {
let pos_accessor = prim.get(&gltf::Semantic::Positions).unwrap();
let mut new_mesh = Mesh::default();
assert_eq!(pos_accessor.dimensions(), gltf::accessor::Dimensions::Vec3); // TODO: dont do this
let view = pos_accessor.view().unwrap(); // TODO: handle sparse Accessor
let stride = view.stride().unwrap_or(0); // if stride is None, its tightly packed
let offset = view.offset();
let buffer = view.buffer();
let buf_len = buffer.length();
let view = views.iter().next().unwrap();
//gltf.views()
//vertices.data_type() == gltf::
//new_mesh.vertices = vertices.;
if let Some(indicies) = prim.indices() {
}
}
//meshes.push(mesh);
}
for child in node.children() {
let mut child_meshes = self.process_node(node);
meshes.append(&mut child_meshes);
}
todo!()
}
}
2023-09-21 18:22:46 +00:00
impl ResourceLoader for ModelLoader {
fn extensions(&self) -> &[&str] {
&[
"gltf"
]
}
fn load(&self, path: &str) -> Result<std::sync::Arc<dyn crate::ResourceStorage>, crate::LoaderError> {
// check if the file is supported by this loader
if !self.does_support_file(path) {
return Err(LoaderError::UnsupportedExtension(path.to_string()));
}
let gltf = gltf::Gltf::open(path)?;
2023-09-21 21:27:21 +00:00
let buffers: Vec<gltf::Buffer<'_>> = gltf.buffers().collect();
// TODO: Read in multiple scenes
let scene = gltf.scenes().next().unwrap();
for node in scene.nodes() {
}
2023-09-21 18:22:46 +00:00
todo!()
}
}