split the math module into a lyra-math crate, implement Reflect and Component for Transform

This commit is contained in:
SeanOMik 2024-01-13 11:51:16 -05:00
parent d14abcc3e5
commit 189d05e323
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
17 changed files with 100 additions and 65 deletions

32
Cargo.lock generated
View File

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

View File

@ -10,7 +10,7 @@ members = [
"lyra-ecs",
"lyra-reflect",
"lyra-scripting",
"lyra-game"]
"lyra-game", "lyra-math"]
[features]
scripting = ["dep:lyra-scripting"]

View File

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

View File

@ -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);
impl_external_component!(Vec2);
impl_external_component!(Vec3);
impl_external_component!(Vec4);
impl_external_component!(Mat4);
impl_external_component!(Quat);
impl_external_component!(Transform);

View File

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

View File

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

View File

@ -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<PointLight>, TickOf<TransformComponent>)>() {
in world.view_iter::<(Entities, &PointLight, &Transform, TickOf<PointLight>, TickOf<Transform>)>() {
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<SpotLight>, TickOf<TransformComponent>)>() {
in world.view_iter::<(Entities, &SpotLight, &Transform, TickOf<SpotLight>, TickOf<Transform>)>() {
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;
}

View File

@ -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<ModelComponent>, &TransformComponent, TickOf<TransformComponent>)>() {
for (entity, model, model_epoch, transform, transform_epoch) in main_world.view_iter::<(Entities, &ModelComponent, TickOf<ModelComponent>, &Transform, TickOf<Transform>)>() {
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

View File

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

View File

@ -1,26 +0,0 @@
use lyra_ecs::Component;
use crate::math::Transform;
#[derive(Clone, Component, Default)]
pub struct TransformComponent {
pub transform: Transform,
}
impl From<Transform> 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)
}
}

9
lyra-math/Cargo.toml Normal file
View File

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

0
lyra-game/src/math/mod.rs → lyra-math/src/lib.rs Executable file → Normal file
View File

View File

@ -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,
@ -104,3 +104,17 @@ impl Transform {
}
}
}
/// 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
}
}

View File

@ -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 }
lyra-math = { path = "../lyra-math", optional = true }

View File

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

View File

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