diff --git a/Cargo.lock b/Cargo.lock index e2e17a2..091300d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1258,6 +1258,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.10" @@ -1413,6 +1422,7 @@ version = "0.1.0" dependencies = [ "anyhow", "lyra-ecs-derive", + "lyra-math", "rand", "thiserror", ] @@ -1447,8 +1457,9 @@ dependencies = [ "glam", "image", "instant", - "itertools", + "itertools 0.11.0", "lyra-ecs", + "lyra-math", "lyra-reflect", "lyra-resource", "quote", @@ -1463,11 +1474,19 @@ dependencies = [ "winit", ] +[[package]] +name = "lyra-math" +version = "0.1.0" +dependencies = [ + "glam", +] + [[package]] name = "lyra-reflect" version = "0.1.0" dependencies = [ "lyra-ecs", + "lyra-math", "lyra-reflect-derive", ] @@ -1503,16 +1522,27 @@ name = "lyra-scripting" version = "0.1.0" dependencies = [ "anyhow", + "itertools 0.12.0", "lyra-ecs", "lyra-game", "lyra-reflect", "lyra-resource", + "lyra-scripting-derive", "mlua", "thiserror", "tracing", "tracing-subscriber", ] +[[package]] +name = "lyra-scripting-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "mach2" version = "0.4.2" diff --git a/Cargo.toml b/Cargo.toml index 7bdf3a1..36a4517 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ members = [ "lyra-ecs", "lyra-reflect", "lyra-scripting", - "lyra-game"] + "lyra-game", "lyra-math"] [features] scripting = ["dep:lyra-scripting"] @@ -18,4 +18,4 @@ lua_scripting = ["scripting", "lyra-scripting/lua"] [dependencies] lyra-game = { path = "lyra-game" } -lyra-scripting = { path = "lyra-scripting", optional = true } \ No newline at end of file +lyra-scripting = { path = "lyra-scripting", optional = true } diff --git a/lyra-ecs/Cargo.toml b/lyra-ecs/Cargo.toml index 8249429..9e49722 100644 --- a/lyra-ecs/Cargo.toml +++ b/lyra-ecs/Cargo.toml @@ -6,13 +6,13 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -math = ["dep:glam"] +math = ["dep:lyra-math"] [dependencies] lyra-ecs-derive = { path = "./lyra-ecs-derive" } +lyra-math = { path = "../lyra-math", optional = true } anyhow = "1.0.75" thiserror = "1.0.50" -glam = { version = "0.24.0", optional = true } [dev-dependencies] rand = "0.8.5" # used for tests diff --git a/lyra-ecs/src/math.rs b/lyra-ecs/src/math.rs index 045b70f..43c1a90 100644 --- a/lyra-ecs/src/math.rs +++ b/lyra-ecs/src/math.rs @@ -1,8 +1,8 @@ use crate::Component; -use glam::{Vec3, Quat, Vec2, Vec4, Mat4}; +use lyra_math::{Vec3, Quat, Vec2, Vec4, Mat4, Transform}; -macro_rules! impl_component_math { +macro_rules! impl_external_component { ($type: ident) => { impl Component for $type { fn name() -> &'static str { @@ -12,8 +12,9 @@ macro_rules! impl_component_math { }; } -impl_component_math!(Vec2); -impl_component_math!(Vec3); -impl_component_math!(Vec4); -impl_component_math!(Mat4); -impl_component_math!(Quat); \ No newline at end of file +impl_external_component!(Vec2); +impl_external_component!(Vec3); +impl_external_component!(Vec4); +impl_external_component!(Mat4); +impl_external_component!(Quat); +impl_external_component!(Transform); \ No newline at end of file diff --git a/lyra-game/Cargo.toml b/lyra-game/Cargo.toml index 3579672..070fc85 100644 --- a/lyra-game/Cargo.toml +++ b/lyra-game/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" lyra-resource = { path = "../lyra-resource" } lyra-ecs = { path = "../lyra-ecs", features = [ "math" ] } lyra-reflect = { path = "../lyra-reflect", features = [ "math" ] } +lyra-math = { path = "../lyra-math" } winit = "0.28.1" tracing = "0.1.37" diff --git a/lyra-game/src/lib.rs b/lyra-game/src/lib.rs index 35ab932..3c70bc4 100644 --- a/lyra-game/src/lib.rs +++ b/lyra-game/src/lib.rs @@ -7,7 +7,6 @@ extern crate self as lyra_engine; pub mod game; pub mod render; pub mod resources; -pub mod math; pub mod input; pub mod castable_any; pub mod plugin; @@ -26,6 +25,7 @@ pub mod scene; pub use lyra_resource as assets; pub use lyra_ecs as ecs; +pub use lyra_math as math; #[cfg(feature = "scripting")] pub use lyra_scripting as script; diff --git a/lyra-game/src/render/light/mod.rs b/lyra-game/src/render/light/mod.rs index d89cbd2..fb4728b 100644 --- a/lyra-game/src/render/light/mod.rs +++ b/lyra-game/src/render/light/mod.rs @@ -12,7 +12,7 @@ use tracing::debug; use std::mem; -use crate::{math::Transform, scene::TransformComponent}; +use crate::math::Transform; use self::directional::DirectionalLight; @@ -169,29 +169,29 @@ impl LightUniformBuffers { pub fn update_lights(&mut self, queue: &wgpu::Queue, world_tick: Tick, world: &World) { for (entity, point_light, transform, light_epoch, transform_epoch) - in world.view_iter::<(Entities, &PointLight, &TransformComponent, TickOf, TickOf)>() { + in world.view_iter::<(Entities, &PointLight, &Transform, TickOf, TickOf)>() { if !self.point_lights.has_light(entity) || light_epoch == world_tick || transform_epoch == world_tick { - let uniform = PointLightUniform::from_bundle(&point_light, &transform.transform); + let uniform = PointLightUniform::from_bundle(&point_light, &transform); self.point_lights.update_or_add(&mut self.lights_uniform.point_lights, entity, uniform); debug!("Updated point light"); } } for (entity, spot_light, transform, light_epoch, transform_epoch) - in world.view_iter::<(Entities, &SpotLight, &TransformComponent, TickOf, TickOf)>() { + in world.view_iter::<(Entities, &SpotLight, &Transform, TickOf, TickOf)>() { if !self.spot_lights.has_light(entity) || light_epoch == world_tick || transform_epoch == world_tick { - let uniform = SpotLightUniform::from_bundle(&spot_light, &transform.transform); + let uniform = SpotLightUniform::from_bundle(&spot_light, &transform); self.spot_lights.update_or_add(&mut self.lights_uniform.spot_lights, entity, uniform); //debug!("Updated spot light"); } } if let Some((dir_light, transform)) = - world.view_iter::<(&DirectionalLight, &TransformComponent)>().next() { + world.view_iter::<(&DirectionalLight, &Transform)>().next() { - let uniform = DirectionalLightUniform::from_bundle(&dir_light, &transform.transform); + let uniform = DirectionalLightUniform::from_bundle(&dir_light, &transform); self.lights_uniform.directional_light = uniform; } diff --git a/lyra-game/src/render/renderer.rs b/lyra-game/src/render/renderer.rs index 9e250ee..b7aa4a4 100755 --- a/lyra-game/src/render/renderer.rs +++ b/lyra-game/src/render/renderer.rs @@ -16,7 +16,7 @@ use winit::window::Window; use crate::math::Transform; use crate::render::material::MaterialUniform; use crate::render::render_buffer::BufferWrapperBuilder; -use crate::scene::{ModelComponent, TransformComponent, CameraComponent}; +use crate::scene::{ModelComponent, CameraComponent}; use super::camera::{RenderCamera, CameraUniform}; use super::desc_buf_lay::DescVertexBufferLayout; @@ -395,13 +395,13 @@ impl Renderer for BasicRenderer { let now_inst = Instant::now(); - for (entity, model, model_epoch, transform, transform_epoch) in main_world.view_iter::<(Entities, &ModelComponent, TickOf, &TransformComponent, TickOf)>() { + for (entity, model, model_epoch, transform, transform_epoch) in main_world.view_iter::<(Entities, &ModelComponent, TickOf, &Transform, TickOf)>() { alive_entities.insert(entity); let cached = match self.entity_last_transforms.get_mut(&entity) { Some(last) if transform_epoch == last_epoch => { last.from_transform = last.to_transform; - last.to_transform = transform.transform; + last.to_transform = *transform; last.last_updated_at = Some(last.cached_at); last.cached_at = now_inst; @@ -412,8 +412,8 @@ impl Renderer for BasicRenderer { let cached = CachedTransform { last_updated_at: None, cached_at: now_inst, - from_transform: transform.transform, - to_transform: transform.transform, + from_transform: *transform, + to_transform: *transform, }; self.entity_last_transforms.insert(entity, cached.clone()); cached diff --git a/lyra-game/src/scene/mod.rs b/lyra-game/src/scene/mod.rs index 5d743fa..47898a1 100644 --- a/lyra-game/src/scene/mod.rs +++ b/lyra-game/src/scene/mod.rs @@ -4,9 +4,6 @@ pub use mesh::*; pub mod model; pub use model::*; -pub mod transform; -pub use transform::*; - pub mod camera; pub use camera::*; diff --git a/lyra-game/src/scene/transform.rs b/lyra-game/src/scene/transform.rs deleted file mode 100755 index 516f6f8..0000000 --- a/lyra-game/src/scene/transform.rs +++ /dev/null @@ -1,26 +0,0 @@ -use lyra_ecs::Component; - -use crate::math::Transform; - -#[derive(Clone, Component, Default)] -pub struct TransformComponent { - pub transform: Transform, -} - -impl From for TransformComponent { - fn from(transform: Transform) -> Self { - Self { - transform - } - } -} - -impl TransformComponent { - pub fn new() -> Self { - Self::default() - } - - pub fn from_transform(transform: Transform) -> Self { - Self::from(transform) - } -} \ No newline at end of file diff --git a/lyra-math/Cargo.toml b/lyra-math/Cargo.toml new file mode 100644 index 0000000..463fe9b --- /dev/null +++ b/lyra-math/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "lyra-math" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +glam = { version = "0.24.0" } \ No newline at end of file diff --git a/lyra-game/src/math/angle.rs b/lyra-math/src/angle.rs similarity index 100% rename from lyra-game/src/math/angle.rs rename to lyra-math/src/angle.rs diff --git a/lyra-game/src/math/mod.rs b/lyra-math/src/lib.rs old mode 100755 new mode 100644 similarity index 100% rename from lyra-game/src/math/mod.rs rename to lyra-math/src/lib.rs diff --git a/lyra-game/src/math/transform.rs b/lyra-math/src/transform.rs similarity index 86% rename from lyra-game/src/math/transform.rs rename to lyra-math/src/transform.rs index f2aa027..f154423 100755 --- a/lyra-game/src/math/transform.rs +++ b/lyra-math/src/transform.rs @@ -1,9 +1,9 @@ use glam::{Vec3, Mat4, Quat}; -use crate::math::angle::Angle; +use super::Angle; #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq)] pub struct Transform { pub translation: Vec3, pub rotation: Quat, @@ -103,4 +103,18 @@ impl Transform { *self } } +} + +/// Adds a transform to another one +/// +/// The Translations of each transform is added and rotation and scale is multiplied. +impl std::ops::Add for Transform { + type Output = Transform; + + fn add(mut self, rhs: Self) -> Self::Output { + self.translation += rhs.translation; + self.rotation *= rhs.rotation; + self.scale *= rhs.scale; + self + } } \ No newline at end of file diff --git a/lyra-reflect/Cargo.toml b/lyra-reflect/Cargo.toml index 89643d2..0977970 100644 --- a/lyra-reflect/Cargo.toml +++ b/lyra-reflect/Cargo.toml @@ -6,9 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -math = ["dep:glam"] +math = ["dep:lyra-math"] [dependencies] lyra-reflect-derive = { path = "lyra-reflect-derive" } lyra-ecs = { path = "../lyra-ecs" } -glam = { version = "0.24.0", optional = true } \ No newline at end of file +lyra-math = { path = "../lyra-math", optional = true } \ No newline at end of file diff --git a/lyra-reflect/lyra-reflect-derive/src/struct_macro.rs b/lyra-reflect/lyra-reflect-derive/src/struct_macro.rs index d1c9b5a..d4971f8 100644 --- a/lyra-reflect/lyra-reflect-derive/src/struct_macro.rs +++ b/lyra-reflect/lyra-reflect-derive/src/struct_macro.rs @@ -4,7 +4,7 @@ use syn::{Token, parenthesized, punctuated::Punctuated}; struct Field { name: syn::Ident, _eq: Token![=], - ty: syn::Ident, + ty: syn::Path, } impl syn::parse::Parse for Field { diff --git a/lyra-reflect/src/impls/impl_math.rs b/lyra-reflect/src/impls/impl_math.rs index 27a3e0f..2381203 100644 --- a/lyra-reflect/src/impls/impl_math.rs +++ b/lyra-reflect/src/impls/impl_math.rs @@ -1,10 +1,19 @@ use lyra_reflect_derive::{impl_reflect_simple_struct, impl_reflect_trait_value}; -use crate::{lyra_engine, Reflect, Method}; +use crate::{lyra_engine, Method, Reflect}; -impl_reflect_simple_struct!(glam::Vec2, fields(x=f32, y=f32)); -impl_reflect_simple_struct!(glam::Vec3, fields(x=f32, y=f32, z=f32)); -impl_reflect_simple_struct!(glam::Vec4, fields(x=f32, y=f32, z=f32, w=f32)); -impl_reflect_simple_struct!(glam::Quat, fields(x=f32, y=f32, z=f32, w=f32)); +impl_reflect_simple_struct!(lyra_math::Vec2, fields(x = f32, y = f32)); +impl_reflect_simple_struct!(lyra_math::Vec3, fields(x = f32, y = f32, z = f32)); +impl_reflect_simple_struct!(lyra_math::Vec4, fields(x = f32, y = f32, z = f32, w = f32)); +impl_reflect_simple_struct!(lyra_math::Quat, fields(x = f32, y = f32, z = f32, w = f32)); -impl_reflect_trait_value!(glam::Mat4); \ No newline at end of file +impl_reflect_simple_struct!( + lyra_math::Transform, + fields( + translation = lyra_math::Vec3, + rotation = lyra_math::Quat, + scale = lyra_math::Vec3 + ) +); + +impl_reflect_trait_value!(lyra_math::Mat4);