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

192 lines
7.1 KiB
Rust
Raw Normal View History

use std::{sync::Arc, path::{Path, PathBuf}};
2023-09-22 03:11:09 +00:00
use base64::Engine;
use thiserror::Error;
2023-09-22 03:11:09 +00:00
use crate::{ResourceLoader, LoaderError, Mesh, Model, MeshVertexAttribute, VertexAttributeData, Resource, Material, MeshIndices, ResourceManager, util};
2023-09-21 18:22:46 +00:00
use tracing::debug;
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(Error, Debug)]
enum ModelLoaderError {
#[error("The model ({0}) is missing the BIN section in the gltf file")]
MissingBin(String),
#[error("There was an error with decoding a uri defined in the model: '{0}'")]
UriDecodingError(util::UriReadError),
}
impl From<ModelLoaderError> for LoaderError {
fn from(value: ModelLoaderError) -> Self {
LoaderError::DecodingError(value.into())
}
}
2023-09-21 18:22:46 +00:00
#[derive(Default)]
pub struct ModelLoader;
2023-09-21 21:27:21 +00:00
impl ModelLoader {
/* fn parse_uri(containing_path: &str, uri: &str) -> Option<Vec<u8>> {
2023-09-22 03:11:09 +00:00
let uri = uri.strip_prefix("data")?;
let (mime, data) = uri.split_once(",")?;
let (_mime, is_base64) = match mime.strip_suffix(";base64") {
Some(mime) => (mime, true),
None => (mime, false),
};
if is_base64 {
Some(base64::engine::general_purpose::STANDARD.decode(data).unwrap())
2023-09-22 03:11:09 +00:00
} else {
let full_path = format!("{containing_path}/{data}");
let buf = std::fs::read(&full_path).unwrap();
Some(buf)
2023-09-22 03:11:09 +00:00
}
} */
2023-09-22 03:11:09 +00:00
fn process_node(&self, buffers: &Vec<Vec<u8>>, materials: &Vec<Material>, node: gltf::Node<'_>) -> Vec<Mesh> {
2023-09-21 21:27:21 +00:00
let mut meshes = vec![];
if let Some(mesh) = node.mesh() {
for prim in mesh.primitives() {
2023-09-22 03:11:09 +00:00
let reader = prim.reader(|buf| Some(buffers[buf.index()].as_slice()));
2023-09-21 21:27:21 +00:00
let mut new_mesh = Mesh::default();
// read the positions
if let Some(pos) = reader.read_positions() {
debug!("Mesh mode is {:?}", prim.mode());
let pos: Vec<glam::Vec3> = pos.map(|t| t.into()).collect();
new_mesh.add_attribute(MeshVertexAttribute::Position, VertexAttributeData::Vec3(pos));
}
// read the normals
if let Some(norms) = reader.read_normals() {
let norms: Vec<glam::Vec3> = norms.map(|t| t.into()).collect();
new_mesh.add_attribute(MeshVertexAttribute::Normals, VertexAttributeData::Vec3(norms));
}
// read the tangents
if let Some(tangents) = reader.read_tangents() {
let tangents: Vec<glam::Vec4> = tangents.map(|t| t.into()).collect();
new_mesh.add_attribute(MeshVertexAttribute::Tangents, VertexAttributeData::Vec4(tangents));
}
// read tex coords
if let Some(tex_coords) = reader.read_tex_coords(0) {
let tex_coords: Vec<glam::Vec2> = tex_coords.into_f32().map(|t| t.into()).collect();
new_mesh.add_attribute(MeshVertexAttribute::TexCoords, VertexAttributeData::Vec2(tex_coords));
}
// read the indices
if let Some(indices) = reader.read_indices() {
let indices: MeshIndices = match indices {
// wpgu doesn't support u8 indices, so those must be converted to u16
gltf::mesh::util::ReadIndices::U8(i) => MeshIndices::U16(i.map(|i| i as u16).collect()),
gltf::mesh::util::ReadIndices::U16(i) => MeshIndices::U16(i.collect()),
gltf::mesh::util::ReadIndices::U32(i) => MeshIndices::U32(i.collect()),
};
2023-09-21 21:27:21 +00:00
new_mesh.indices = Some(indices);
}
2023-09-22 03:11:09 +00:00
2023-09-29 18:20:28 +00:00
let mat = materials.get(prim.material().index().unwrap()).unwrap();
new_mesh.set_material(mat.clone());
//prim.material().
2023-09-22 03:11:09 +00:00
meshes.push(new_mesh);
2023-09-21 21:27:21 +00:00
}
}
for child in node.children() {
let mut child_meshes = self.process_node(buffers, materials, child);
2023-09-21 21:27:21 +00:00
meshes.append(&mut child_meshes);
}
2023-09-22 03:11:09 +00:00
meshes
2023-09-21 21:27:21 +00:00
}
}
2023-09-21 18:22:46 +00:00
impl ResourceLoader for ModelLoader {
fn extensions(&self) -> &[&str] {
&[
"gltf"
]
}
fn mime_types(&self) -> &[&str] {
&[]
}
fn load(&self, resource_manager: &mut ResourceManager, path: &str) -> Result<std::sync::Arc<dyn crate::ResourceStorage>, crate::LoaderError> {
2023-09-21 18:22:46 +00:00
// check if the file is supported by this loader
if !self.does_support_file(path) {
return Err(LoaderError::UnsupportedExtension(path.to_string()));
}
let mut parent_path = PathBuf::from(path);
parent_path.pop();
let parent_path = parent_path.display().to_string();
2023-09-21 18:22:46 +00:00
let gltf = gltf::Gltf::open(path)?;
2023-09-22 03:11:09 +00:00
let buffers: Vec<Vec<u8>> = gltf.buffers().map(|b| match b.source() {
gltf::buffer::Source::Bin => gltf.blob.as_deref().map(|v| v.to_vec())
.ok_or(ModelLoaderError::MissingBin(path.to_string())),
gltf::buffer::Source::Uri(uri) => util::gltf_read_buffer_uri(&parent_path, uri)
.map_err(|e| ModelLoaderError::UriDecodingError(e)),
2023-09-22 03:11:09 +00:00
}).flatten().collect();
2023-09-21 21:27:21 +00:00
// TODO: Read in multiple scenes
let scene = gltf.scenes().next().unwrap();
// Load the materials
let materials: Vec<Material> = gltf.materials()
.map(|mat| Material::from_gltf(resource_manager, &parent_path, mat)).collect();
2023-09-22 03:11:09 +00:00
let meshes: Vec<Mesh> = scene.nodes()
.map(|node| self.process_node(&buffers, &materials, node))
2023-09-22 03:11:09 +00:00
.flatten().collect();
Ok(Arc::new(Resource::with_data(path, Model::new(meshes))))
2023-09-22 03:11:09 +00:00
}
fn load_bytes(&self, resource_manager: &mut ResourceManager, bytes: Vec<u8>, offset: usize, length: usize) -> Result<Arc<dyn crate::ResourceStorage>, LoaderError> {
todo!()
}
2023-09-22 03:11:09 +00:00
}
#[cfg(test)]
mod tests {
use crate::ResourceLoader;
use super::*;
fn test_file_path(path: &str) -> String {
let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap();
format!("{manifest}/test_files/gltf/{path}")
}
#[test]
fn test_loading() {
let path = test_file_path("texture-embedded.gltf");
2023-09-21 21:27:21 +00:00
let mut manager = ResourceManager::new();
2023-09-22 03:11:09 +00:00
let loader = ModelLoader::default();
let model = loader.load(&mut manager, &path).unwrap();
2023-09-29 18:20:28 +00:00
let model = Arc::downcast::<Resource<Model>>(model.as_arc_any()).unwrap();
let model = model.data.as_ref().unwrap();
assert_eq!(model.meshes.len(), 1); // There should only be 1 mesh
2023-09-22 03:11:09 +00:00
let mesh = &model.meshes[0];
assert!(mesh.position().unwrap().len() > 0);
assert!(mesh.normals().unwrap().len() > 0);
assert!(mesh.tex_coords().unwrap().len() > 0);
2023-09-22 03:11:09 +00:00
assert!(mesh.indices.clone().unwrap().len() > 0);
assert!(mesh.material().base_color_texture.is_some());
2023-09-29 18:20:28 +00:00
let _mesh_mat = mesh.material(); // inner panic if material was not loaded
2023-09-21 18:22:46 +00:00
}
}