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

87 lines
2.3 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
#[repr(C)]
#[derive(Clone, Debug, PartialEq)]
pub enum VertexAttributeData {
Vec2(Vec<glam::Vec2>),
Vec3(Vec<glam::Vec3>),
Vec4(Vec<glam::Vec4>),
}
impl VertexAttributeData {
/// Take self as a list of Vec2's
pub fn as_vec2(&self) -> &Vec<glam::Vec2> {
match self {
VertexAttributeData::Vec2(v) => v,
_ => panic!("Attempt to get {self:?} as `Vec2`"),
}
}
pub fn as_vec3(&self) -> &Vec<glam::Vec3> {
match self {
VertexAttributeData::Vec3(v) => v,
_ => panic!("Attempt to get {self:?} as `Vec3`"),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum MeshVertexAttribute {
Position,
Normals,
Tangents,
Colors, // TODO: Figure out best way to store color data
Joints, // TODO: Animation
TexCoords,
Weights, // TODO: Animation
MorphTargets, // TODO: Animation
Other(String),
}
#[derive(Clone, Default, edict::Component)]
2023-09-21 18:22:46 +00:00
pub struct Mesh {
pub attributes: HashMap<MeshVertexAttribute, VertexAttributeData>,
2023-09-22 03:11:09 +00:00
pub indices: Option<Vec<u32>>,
2023-09-21 18:22:46 +00:00
}
impl Mesh {
pub fn add_attribute(&mut self, attribute: MeshVertexAttribute, data: VertexAttributeData) {
self.attributes.insert(attribute, data);
}
/// Try to get the position attribute of the Mesh
pub fn position(&self) -> Option<&Vec<glam::Vec3>> {
self.attributes.get(&MeshVertexAttribute::Position)
.map(|p| p.as_vec3())
}
pub fn add_position(&mut self, pos: Vec<glam::Vec3>) {
self.attributes.insert(MeshVertexAttribute::Position, VertexAttributeData::Vec3(pos));
}
/// Try to get the normals attribute of the Mesh
pub fn normals(&self) -> Option<&Vec<glam::Vec3>> {
self.attributes.get(&MeshVertexAttribute::Normals)
.map(|p| p.as_vec3())
}
/// Try to get the texture coordinates attribute of the Mesh
pub fn tex_coords(&self) -> Option<&Vec<glam::Vec2>> {
self.attributes.get(&MeshVertexAttribute::TexCoords)
.map(|p| p.as_vec2())
}
}
#[derive(Clone, Default)]
2023-09-21 18:22:46 +00:00
pub struct Model {
2023-09-22 03:11:09 +00:00
pub meshes: Vec<Mesh>,
2023-09-21 18:22:46 +00:00
//pub material
2023-09-22 03:11:09 +00:00
}
impl Model {
pub fn new(meshes: Vec<Mesh>) -> Self {
Self {
meshes,
}
}
2023-09-21 18:22:46 +00:00
}