Add ModelComponent, add model loader to resource manager's default loaders

This commit is contained in:
SeanOMik 2023-09-26 17:14:38 -04:00
parent 64817b6142
commit dabc051b58
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
9 changed files with 62 additions and 28 deletions

12
Cargo.lock generated
View File

@ -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",
]

View File

@ -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"

View File

@ -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;

View File

@ -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<gltf::Error> 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))))
}
}

View File

@ -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;

View File

@ -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<MeshVertexAttribute, VertexAttributeData>,
pub indices: Option<Vec<u32>>,

View File

@ -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<dyn ResourceStorage> to Arc<Resource<T>
let res = res.as_arc_any();
let res = res.downcast::<Resource<T>>().expect("Failure to downcast resource");
let res = res.downcast::<Resource<T>>()
.expect("Failure to downcast resource! Does the loader return an `Arc<Resource<T>>`?");
Ok(res)
} else {

View File

@ -1,3 +1,4 @@
pub mod mesh;
pub mod model;
pub mod transform;
pub mod camera;

View File

@ -0,0 +1,39 @@
use crate::assets::{Model, Resource};
use std::sync::Arc;
#[derive(Clone, edict::Component)]
pub struct ModelComponent(pub Arc<Resource<Model>>);
impl From<Arc<Resource<Model>>> for ModelComponent {
fn from(value: Arc<Resource<Model>>) -> Self {
ModelComponent(value)
}
}
/* impl From<Arc<Resource<Model>> for ModelComponent {
} */
impl std::ops::Deref for ModelComponent {
type Target = Arc<Resource<Model>>;
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
}
}
} */