diff --git a/Cargo.lock b/Cargo.lock index 6a482f6..179240b 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -303,6 +303,12 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +[[package]] +name = "base64" +version = "0.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ba43ea6f343b788c8764558649e08df62f86c6ef251fdaeb1ffd010a9ae50a2" + [[package]] name = "bit-set" version = "0.5.3" @@ -902,7 +908,7 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad2dcfb6dd7a66f9eb3d181a29dcfb22d146b0bcdc2e1ed1713cbf03939a88ea" dependencies = [ - "base64", + "base64 0.13.1", "byteorder", "gltf-json", "image", @@ -1291,8 +1297,12 @@ name = "lyra-resource" version = "0.0.1" dependencies = [ "anyhow", + "base64 0.21.4", + "edict", + "glam", "gltf", "image", + "percent-encoding", "thiserror", "uuid", ] diff --git a/lyra-resource/Cargo.toml b/lyra-resource/Cargo.toml index 43b0d9c..25cea1b 100644 --- a/lyra-resource/Cargo.toml +++ b/lyra-resource/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] anyhow = "1.0.75" base64 = "0.21.4" +edict = "0.5.0" glam = "0.24.1" gltf = "1.3.0" image = "0.24.7" diff --git a/lyra-resource/src/loader/mod.rs b/lyra-resource/src/loader/mod.rs index fb0b7eb..1b26c7b 100644 --- a/lyra-resource/src/loader/mod.rs +++ b/lyra-resource/src/loader/mod.rs @@ -1,7 +1,7 @@ pub mod texture; pub mod model; -use std::{io, sync::Arc, fs::File, path::Path, ffi::OsStr}; +use std::{io, sync::Arc, path::Path, ffi::OsStr}; use thiserror::Error; diff --git a/lyra-resource/src/loader/model.rs b/lyra-resource/src/loader/model.rs index 1952c81..a85f12e 100644 --- a/lyra-resource/src/loader/model.rs +++ b/lyra-resource/src/loader/model.rs @@ -1,9 +1,8 @@ use std::sync::Arc; use base64::Engine; -use glam::Vec3; -use crate::{ResourceLoader, LoaderError, Mesh, Vertex, Model, MeshVertexAttribute, VertexAttributeData}; +use crate::{ResourceLoader, LoaderError, Mesh, Model, MeshVertexAttribute, VertexAttributeData, Resource}; impl From for LoaderError { fn from(value: gltf::Error) -> Self { @@ -116,7 +115,7 @@ impl ResourceLoader for ModelLoader { .map(|node| self.process_node(&buffers, node)) .flatten().collect(); - Ok(Arc::new(Model::new(meshes))) + Ok(Arc::new(Resource::with_data(path, Model::new(meshes)))) } } diff --git a/lyra-resource/src/loader/texture.rs b/lyra-resource/src/loader/texture.rs index c77ed80..66e5c17 100644 --- a/lyra-resource/src/loader/texture.rs +++ b/lyra-resource/src/loader/texture.rs @@ -1,4 +1,4 @@ -use std::{fs::File, sync::Arc, path::Path, ffi::OsStr, io::Read}; +use std::{fs::File, sync::Arc, io::Read}; use image::ImageError; diff --git a/lyra-resource/src/model.rs b/lyra-resource/src/model.rs index a7c7a10..f181efc 100644 --- a/lyra-resource/src/model.rs +++ b/lyra-resource/src/model.rs @@ -1,22 +1,5 @@ use std::collections::HashMap; - -#[repr(C)] -#[derive(Copy, Clone, Debug)] -pub struct Vertex { - pub position: glam::Vec3, - pub tex_coords: glam::Vec2 -} - -impl Vertex { - pub fn new(position: glam::Vec3, tex_coords: glam::Vec2) -> Self { - Self { - position, - tex_coords, - } - } -} - #[repr(C)] #[derive(Clone, Debug, PartialEq)] pub enum VertexAttributeData { @@ -47,7 +30,7 @@ pub enum MeshVertexAttribute { Position, Normals, Tangents, - // Colors, // TODO: Store data in VertexAttributeData + Colors, // TODO: Figure out best way to store color data Joints, // TODO: Animation TexCoords, Weights, // TODO: Animation @@ -55,7 +38,7 @@ pub enum MeshVertexAttribute { Other(String), } -#[derive(Clone, Default)] +#[derive(Clone, Default, edict::Component)] pub struct Mesh { pub attributes: HashMap, pub indices: Option>, diff --git a/lyra-resource/src/resource_manager.rs b/lyra-resource/src/resource_manager.rs index 03853d5..0204dee 100644 --- a/lyra-resource/src/resource_manager.rs +++ b/lyra-resource/src/resource_manager.rs @@ -2,7 +2,7 @@ use std::{sync::Arc, collections::{HashMap, hash_map::DefaultHasher}, hash::{Has use thiserror::Error; -use crate::{resource::Resource, loader::{ResourceLoader, LoaderError, texture::TextureLoader}}; +use crate::{resource::Resource, loader::{ResourceLoader, LoaderError, texture::TextureLoader, model::ModelLoader}}; pub trait ResourceStorage: Send + Sync + Any + 'static { fn as_any(&self) -> &dyn Any; @@ -48,7 +48,7 @@ impl ResourceManager { pub fn new() -> Self { Self { resources: HashMap::new(), - loaders: vec![ Box::new(TextureLoader::default()) ], + loaders: vec![ Box::new(TextureLoader::default()), Box::new(ModelLoader::default()) ], } } @@ -70,7 +70,8 @@ impl ResourceManager { // convert Arc to Arc let res = res.as_arc_any(); - let res = res.downcast::>().expect("Failure to downcast resource"); + let res = res.downcast::>() + .expect("Failure to downcast resource! Does the loader return an `Arc>`?"); Ok(res) } else { diff --git a/src/ecs/components/mod.rs b/src/ecs/components/mod.rs index 93b9a2b..4a474dd 100755 --- a/src/ecs/components/mod.rs +++ b/src/ecs/components/mod.rs @@ -1,3 +1,4 @@ pub mod mesh; +pub mod model; pub mod transform; pub mod camera; \ No newline at end of file diff --git a/src/ecs/components/model.rs b/src/ecs/components/model.rs new file mode 100644 index 0000000..5f68790 --- /dev/null +++ b/src/ecs/components/model.rs @@ -0,0 +1,39 @@ +use crate::assets::{Model, Resource}; + +use std::sync::Arc; + +#[derive(Clone, edict::Component)] +pub struct ModelComponent(pub Arc>); + +impl From>> for ModelComponent { + fn from(value: Arc>) -> Self { + ModelComponent(value) + } +} + +/* impl From> for ModelComponent { + +} */ + +impl std::ops::Deref for ModelComponent { + type Target = Arc>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl std::ops::DerefMut for ModelComponent { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +/* impl ModelComponent { + pub fn new(model, material: Material) -> Self { + Self { + mesh, + material + } + } +} */ \ No newline at end of file