From 03c1ec34053e7fa1ca1ed09e901f7b2034f75831 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Wed, 4 Dec 2019 00:11:14 -0800 Subject: [PATCH] we glam now --- Cargo.toml | 4 +- examples/simple.rs | 31 +++-- src/application.rs | 13 +- .../src/components/rotation.rs | 29 ----- .../src/components/translation.rs | 36 ----- src/lib.rs | 2 +- src/render/camera.rs | 16 +-- src/render/light.rs | 7 +- .../.gitignore | 0 .../.travis.yml | 0 .../Cargo.toml | 2 +- src/{legion_transform => transform}/LICENSE | 0 src/{legion_transform => transform}/README.md | 0 .../benches/local_to_world.rs | 0 .../examples/hierarchy.rs | 0 .../examples/types_of_transforms.rs | 0 .../src/components/children.rs | 0 .../src/components/local_to_parent.rs | 6 +- .../src/components/local_to_world.rs | 6 +- .../src/components/mod.rs | 0 .../src/components/non_uniform_scale.rs | 21 +-- .../src/components/parent.rs | 0 src/transform/src/components/rotation.rs | 29 +++++ .../src/components/scale.rs | 0 src/transform/src/components/translation.rs | 30 +++++ .../src/hierarchy_maintenance_system.rs | 0 .../src/lib.rs | 2 +- .../src/local_to_parent_system.rs | 123 ++++++++++-------- .../src/local_to_world_propagate_system.rs | 9 +- .../src/local_to_world_system.rs | 119 ++++++++++------- .../src/transform_system_bundle.rs | 0 31 files changed, 261 insertions(+), 224 deletions(-) delete mode 100644 src/legion_transform/src/components/rotation.rs delete mode 100644 src/legion_transform/src/components/translation.rs rename src/{legion_transform => transform}/.gitignore (100%) rename src/{legion_transform => transform}/.travis.yml (100%) rename src/{legion_transform => transform}/Cargo.toml (92%) rename src/{legion_transform => transform}/LICENSE (100%) rename src/{legion_transform => transform}/README.md (100%) rename src/{legion_transform => transform}/benches/local_to_world.rs (100%) rename src/{legion_transform => transform}/examples/hierarchy.rs (100%) rename src/{legion_transform => transform}/examples/types_of_transforms.rs (100%) rename src/{legion_transform => transform}/src/components/children.rs (100%) rename src/{legion_transform => transform}/src/components/local_to_parent.rs (79%) rename src/{legion_transform => transform}/src/components/local_to_world.rs (80%) rename src/{legion_transform => transform}/src/components/mod.rs (100%) rename src/{legion_transform => transform}/src/components/non_uniform_scale.rs (53%) rename src/{legion_transform => transform}/src/components/parent.rs (100%) create mode 100644 src/transform/src/components/rotation.rs rename src/{legion_transform => transform}/src/components/scale.rs (100%) create mode 100644 src/transform/src/components/translation.rs rename src/{legion_transform => transform}/src/hierarchy_maintenance_system.rs (100%) rename src/{legion_transform => transform}/src/lib.rs (95%) rename src/{legion_transform => transform}/src/local_to_parent_system.rs (76%) rename src/{legion_transform => transform}/src/local_to_world_propagate_system.rs (93%) rename src/{legion_transform => transform}/src/local_to_world_system.rs (77%) rename src/{legion_transform => transform}/src/transform_system_bundle.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 48a24c66d1..f6ce8fa0c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,9 @@ edition = "2018" [dependencies] legion = { git = "https://github.com/TomGillen/legion.git", rev = "8628b227bcbe57582fffb5e80e73c634ec4eebd9" } -legion_transform = { path = "src/legion_transform" } -nalgebra-glm = "0.5.0" +legion_transform = { path = "src/transform" } wgpu = { git = "https://github.com/gfx-rs/wgpu-rs.git", rev = "44fa1bc2fa208fa92f80944253e0da56cb7ac1fe"} +glam = "0.8.3" winit = "0.20.0-alpha4" glsl-to-spirv = "0.1" zerocopy = "0.2" diff --git a/examples/simple.rs b/examples/simple.rs index 00c51d0fac..c86aaf2d7c 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,5 +1,5 @@ use bevy::*; -use bevy::{render::*, asset::{Asset, AssetStorage, Handle}, math, Schedulable, Parent}; +use bevy::{render::*, asset::{Asset, AssetStorage, Handle}, math::{Mat4, Quat, Vec3}, Schedulable, Parent}; use rand::{rngs::StdRng, Rng, SeedableRng, random}; fn build_wander_system() -> Box { @@ -25,10 +25,10 @@ fn build_wander_system() -> Box { rng.gen_range(-1.0, 1.0), rng.gen_range(0.0, 0.001), ).normalize(); - let distance = rng.gen_range(wander.distance_bounds.x, wander.distance_bounds.y); - navigation_point.target = translation.vector + direction * distance; + let distance = rng.gen_range(wander.distance_bounds.x(), wander.distance_bounds.y()); + navigation_point.target = translation.0 + direction * distance; wander.elapsed = 0.0; - wander.duration = rng.gen_range(wander.duration_bounds.x, wander.duration_bounds.y); + wander.duration = rng.gen_range(wander.duration_bounds.x(), wander.duration_bounds.y()); } } }) @@ -46,8 +46,8 @@ fn build_navigate_system() -> Box { _, world, _, person_query| { for (_, translation, mut velocity, navigation_point) in person_query.iter(world) { - let distance = navigation_point.target - translation.vector; - if math::length(&distance) > 0.01 { + let distance = navigation_point.target - translation.0; + if distance.length() > 0.01 { let direction = distance.normalize(); velocity.value = direction * 2.0; } else { @@ -66,7 +66,7 @@ fn build_move_system() -> Box { )>::query()) .build(move |_, world, time , person_query| { for (mut translation, velocity) in person_query.iter(world) { - translation.vector += velocity.value * time.delta_seconds; + translation.0 += velocity.value * time.delta_seconds; } }) } @@ -122,9 +122,8 @@ fn build_light_rotator_system() -> Box { Write, )>::query()) .build(move |_, world, time , light_query| { - for (mut light, mut rotation) in light_query.iter(world) { - let euler = math::quat_euler_angles(rotation.quaternion()); - *rotation = Rotation::from_euler_angles(euler.z + time.delta_seconds, euler.y, euler.x); + for (_, mut rotation) in light_query.iter(world) { + rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds); } }) } @@ -235,15 +234,15 @@ fn main() { // camera ( Camera::new(CameraType::Projection { - fov: math::quarter_pi(), + fov: std::f32::consts::PI / 4.0, near: 1.0, - far: 20.0, + far: 1000.0, aspect_ratio: 1.0, }), - LocalToWorld(math::look_at_rh( - &math::vec3(6.0, -40.0, 20.0), - &math::vec3(0.0, 0.0, 0.0), - &math::vec3(0.0, 0.0, 1.0),)), + LocalToWorld(Mat4::look_at_rh( + Vec3::new(6.0, -40.0, 20.0), + Vec3::new(0.0, 0.0, 0.0), + Vec3::new(0.0, 0.0, 1.0),)), // Translation::new(0.0, 0.0, 0.0), ) ]); diff --git a/src/application.rs b/src/application.rs index e543cb5534..dd20808faa 100644 --- a/src/application.rs +++ b/src/application.rs @@ -58,7 +58,7 @@ impl Application { let light_count = >::query().iter(&mut self.world).count(); let forward_uniforms = ForwardUniforms { - proj: math::Mat4::identity().into(), + proj: math::Mat4::identity().to_cols_array_2d(), num_lights: [light_count as u32, 0, 0, 0], }; @@ -110,7 +110,7 @@ impl Application { for (mut camera, local_to_world) in <(Write, Read)>::query().iter(&mut self.world) { camera.update(self.swap_chain_descriptor.width, self.swap_chain_descriptor.height); - let camera_matrix: [[f32; 4]; 4] = (camera.view_matrix * local_to_world.0).into(); + let camera_matrix: [[f32; 4]; 4] = (camera.view_matrix * local_to_world.0).to_cols_array_2d(); let temp_buf = self.device.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC); for pass in self.render_passes.iter() { @@ -152,13 +152,8 @@ impl Application { { slot.copy_from_slice( MaterialUniforms { - model: transform.0.into(), - color: [ - material.color.x as f32, - material.color.y as f32, - material.color.z as f32, - material.color.w as f32, - ], + model: transform.0.to_cols_array_2d(), + color: material.color.into(), } .as_bytes(), ); diff --git a/src/legion_transform/src/components/rotation.rs b/src/legion_transform/src/components/rotation.rs deleted file mode 100644 index 82e4b0b272..0000000000 --- a/src/legion_transform/src/components/rotation.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::math::UnitQuaternion; -use shrinkwraprs::Shrinkwrap; - -#[derive(Shrinkwrap, Debug, PartialEq, Clone, Copy)] -#[shrinkwrap(mutable)] -pub struct Rotation(pub UnitQuaternion); -impl Rotation { - #[inline(always)] - pub fn identity() -> Self { - Self(UnitQuaternion::identity()) - } - - #[inline(always)] - pub fn from_euler_angles(roll: f32, pitch: f32, yaw: f32) -> Self { - Self(UnitQuaternion::from_euler_angles(roll, pitch, yaw)) - } -} - -impl Default for Rotation { - fn default() -> Self { - Self::identity() - } -} - -impl From> for Rotation { - fn from(rotation: UnitQuaternion) -> Self { - Self(rotation) - } -} diff --git a/src/legion_transform/src/components/translation.rs b/src/legion_transform/src/components/translation.rs deleted file mode 100644 index 8760755369..0000000000 --- a/src/legion_transform/src/components/translation.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::math::{Translation3, Vector3}; -use shrinkwraprs::Shrinkwrap; - -#[derive(Shrinkwrap, Debug, PartialEq, Clone, Copy)] -#[shrinkwrap(mutable)] -pub struct Translation(pub Translation3); - -impl Translation { - #[inline(always)] - pub fn identity() -> Self { - Self(Translation3::identity()) - } - - #[inline(always)] - pub fn new(x: f32, y: f32, z: f32) -> Self { - Self(Translation3::new(x, y, z)) - } -} - -impl Default for Translation { - fn default() -> Self { - Self::identity() - } -} - -impl From> for Translation { - fn from(translation: Vector3) -> Self { - Self(Translation3::from(translation)) - } -} - -impl From> for Translation { - fn from(translation: Translation3) -> Self { - Self(translation) - } -} diff --git a/src/lib.rs b/src/lib.rs index 2d28baface..8b02414242 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,4 +14,4 @@ pub use legion::prelude::*; pub use legion::schedule::Schedulable; pub use legion_transform::prelude::*; pub use legion_transform::transform_system_bundle; -pub use nalgebra_glm as math; \ No newline at end of file +pub use glam as math; \ No newline at end of file diff --git a/src/render/camera.rs b/src/render/camera.rs index fd324da68a..7a4a469958 100644 --- a/src/render/camera.rs +++ b/src/render/camera.rs @@ -1,4 +1,4 @@ -use crate::math; +use crate::math::Mat4; pub enum CameraType { Projection { @@ -10,14 +10,14 @@ pub enum CameraType { } pub struct Camera { - pub view_matrix: math::Mat4, + pub view_matrix: Mat4, pub camera_type: CameraType, } impl Camera { pub fn new(camera_type: CameraType) -> Self { Camera { - view_matrix: math::identity(), + view_matrix: Mat4::identity(), camera_type, } } @@ -32,17 +32,17 @@ impl Camera { } } -pub fn get_projection_matrix(fov: f32, aspect_ratio: f32, near: f32, far: f32) -> math::Mat4 { - let projection = math::perspective(aspect_ratio, fov, near, far); +pub fn get_projection_matrix(fov: f32, aspect_ratio: f32, near: f32, far: f32) -> Mat4 { + let projection = Mat4::perspective_rh_gl(fov, aspect_ratio, near, far); opengl_to_wgpu_matrix() * projection } -pub fn opengl_to_wgpu_matrix() -> math::Mat4 { - math::mat4( +pub fn opengl_to_wgpu_matrix() -> Mat4 { + Mat4::from_cols_array(&[ 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 1.0, - ) + ]) } \ No newline at end of file diff --git a/src/render/light.rs b/src/render/light.rs index 2264be6a91..d9d618eb55 100644 --- a/src/render/light.rs +++ b/src/render/light.rs @@ -20,10 +20,11 @@ pub struct LightRaw { impl LightRaw { pub fn from(light: &Light, transform: &math::Mat4, translation: &Translation) -> LightRaw { - let proj = camera::get_projection_matrix(light.fov, 1.0, light.depth.start, light.depth.end) * transform; + let proj = camera::get_projection_matrix(light.fov, 1.0, light.depth.start, light.depth.end) * *transform; + let (x, y, z) = translation.0.into(); LightRaw { - proj: proj.into(), - pos: [translation.vector.x, translation.vector.y, translation.vector.z, 1.0], + proj: proj.to_cols_array_2d(), + pos: [x, y, z, 1.0], color: [ light.color.r as f32, light.color.g as f32, diff --git a/src/legion_transform/.gitignore b/src/transform/.gitignore similarity index 100% rename from src/legion_transform/.gitignore rename to src/transform/.gitignore diff --git a/src/legion_transform/.travis.yml b/src/transform/.travis.yml similarity index 100% rename from src/legion_transform/.travis.yml rename to src/transform/.travis.yml diff --git a/src/legion_transform/Cargo.toml b/src/transform/Cargo.toml similarity index 92% rename from src/legion_transform/Cargo.toml rename to src/transform/Cargo.toml index c051b229a4..baf1b33739 100644 --- a/src/legion_transform/Cargo.toml +++ b/src/transform/Cargo.toml @@ -9,8 +9,8 @@ license = "MIT" [dependencies] legion = { git = "https://github.com/TomGillen/legion.git", rev = "8628b227bcbe57582fffb5e80e73c634ec4eebd9" } #legion = { path = "../legion" } +glam = "0.8.3" log = "0.4" -nalgebra = { version = "0.19.0" } rayon = "1.2" serde = { version = "1", features = ["derive"] } smallvec = "0.6" diff --git a/src/legion_transform/LICENSE b/src/transform/LICENSE similarity index 100% rename from src/legion_transform/LICENSE rename to src/transform/LICENSE diff --git a/src/legion_transform/README.md b/src/transform/README.md similarity index 100% rename from src/legion_transform/README.md rename to src/transform/README.md diff --git a/src/legion_transform/benches/local_to_world.rs b/src/transform/benches/local_to_world.rs similarity index 100% rename from src/legion_transform/benches/local_to_world.rs rename to src/transform/benches/local_to_world.rs diff --git a/src/legion_transform/examples/hierarchy.rs b/src/transform/examples/hierarchy.rs similarity index 100% rename from src/legion_transform/examples/hierarchy.rs rename to src/transform/examples/hierarchy.rs diff --git a/src/legion_transform/examples/types_of_transforms.rs b/src/transform/examples/types_of_transforms.rs similarity index 100% rename from src/legion_transform/examples/types_of_transforms.rs rename to src/transform/examples/types_of_transforms.rs diff --git a/src/legion_transform/src/components/children.rs b/src/transform/src/components/children.rs similarity index 100% rename from src/legion_transform/src/components/children.rs rename to src/transform/src/components/children.rs diff --git a/src/legion_transform/src/components/local_to_parent.rs b/src/transform/src/components/local_to_parent.rs similarity index 79% rename from src/legion_transform/src/components/local_to_parent.rs rename to src/transform/src/components/local_to_parent.rs index 67b82afdc3..f9ce8783f0 100644 --- a/src/legion_transform/src/components/local_to_parent.rs +++ b/src/transform/src/components/local_to_parent.rs @@ -1,14 +1,14 @@ -use crate::math::Matrix4; +use crate::math::Mat4; use shrinkwraprs::Shrinkwrap; use std::fmt; #[derive(Shrinkwrap, Debug, PartialEq, Clone, Copy)] #[shrinkwrap(mutable)] -pub struct LocalToParent(pub Matrix4); +pub struct LocalToParent(pub Mat4); impl LocalToParent { pub fn identity() -> Self { - Self(Matrix4::identity()) + Self(Mat4::identity()) } } diff --git a/src/legion_transform/src/components/local_to_world.rs b/src/transform/src/components/local_to_world.rs similarity index 80% rename from src/legion_transform/src/components/local_to_world.rs rename to src/transform/src/components/local_to_world.rs index d8075b9934..05d230d3a0 100644 --- a/src/legion_transform/src/components/local_to_world.rs +++ b/src/transform/src/components/local_to_world.rs @@ -1,15 +1,15 @@ -use crate::math::Matrix4; +use crate::math::Mat4; use shrinkwraprs::Shrinkwrap; use std::fmt; #[derive(Shrinkwrap, Debug, PartialEq, Clone, Copy)] #[shrinkwrap(mutable)] -pub struct LocalToWorld(pub Matrix4); +pub struct LocalToWorld(pub Mat4); impl LocalToWorld { #[inline(always)] pub fn identity() -> Self { - Self(Matrix4::identity()) + Self(Mat4::identity()) } } diff --git a/src/legion_transform/src/components/mod.rs b/src/transform/src/components/mod.rs similarity index 100% rename from src/legion_transform/src/components/mod.rs rename to src/transform/src/components/mod.rs diff --git a/src/legion_transform/src/components/non_uniform_scale.rs b/src/transform/src/components/non_uniform_scale.rs similarity index 53% rename from src/legion_transform/src/components/non_uniform_scale.rs rename to src/transform/src/components/non_uniform_scale.rs index 32ebbaa096..240a9690b8 100644 --- a/src/legion_transform/src/components/non_uniform_scale.rs +++ b/src/transform/src/components/non_uniform_scale.rs @@ -1,41 +1,42 @@ -use crate::math::Vector3; +use crate::math::Vec3; use shrinkwraprs::Shrinkwrap; use std::fmt; #[derive(Shrinkwrap, Debug, PartialEq, Clone, Copy)] #[shrinkwrap(mutable)] -pub struct NonUniformScale(pub Vector3); +pub struct NonUniformScale(pub Vec3); impl NonUniformScale { pub fn new(x: f32, y: f32, z: f32) -> Self { - Self(Vector3::new(x, y, z)) + Self(Vec3::new(x, y, z)) } } -impl From> for NonUniformScale { - fn from(scale: Vector3) -> Self { +impl From for NonUniformScale { + fn from(scale: Vec3) -> Self { Self(scale) } } -impl From<&Vector3> for NonUniformScale { - fn from(scale: &Vector3) -> Self { +impl From<&Vec3> for NonUniformScale { + fn from(scale: &Vec3) -> Self { Self(*scale) } } -impl From<&mut Vector3> for NonUniformScale { - fn from(scale: &mut Vector3) -> Self { +impl From<&mut Vec3> for NonUniformScale { + fn from(scale: &mut Vec3) -> Self { Self(*scale) } } impl fmt::Display for NonUniformScale { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let (x, y, z) = self.0.into(); write!( f, "NonUniformScale({}, {}, {})", - self.0.x, self.0.y, self.0.z + x, y, z ) } } diff --git a/src/legion_transform/src/components/parent.rs b/src/transform/src/components/parent.rs similarity index 100% rename from src/legion_transform/src/components/parent.rs rename to src/transform/src/components/parent.rs diff --git a/src/transform/src/components/rotation.rs b/src/transform/src/components/rotation.rs new file mode 100644 index 0000000000..f2e97b2cd6 --- /dev/null +++ b/src/transform/src/components/rotation.rs @@ -0,0 +1,29 @@ +use crate::math::Quat; +use shrinkwraprs::Shrinkwrap; + +#[derive(Shrinkwrap, Debug, PartialEq, Clone, Copy)] +#[shrinkwrap(mutable)] +pub struct Rotation(pub Quat); +impl Rotation { + #[inline(always)] + pub fn identity() -> Self { + Self(Quat::identity()) + } + + #[inline(always)] + pub fn from_euler_angles(yaw: f32, pitch: f32, roll: f32) -> Self { + Self(Quat::from_rotation_ypr(yaw, pitch, roll)) + } +} + +impl Default for Rotation { + fn default() -> Self { + Self::identity() + } +} + +impl From for Rotation { + fn from(rotation: Quat) -> Self { + Self(rotation) + } +} diff --git a/src/legion_transform/src/components/scale.rs b/src/transform/src/components/scale.rs similarity index 100% rename from src/legion_transform/src/components/scale.rs rename to src/transform/src/components/scale.rs diff --git a/src/transform/src/components/translation.rs b/src/transform/src/components/translation.rs new file mode 100644 index 0000000000..ba8db7290e --- /dev/null +++ b/src/transform/src/components/translation.rs @@ -0,0 +1,30 @@ +use crate::math::Vec3; +use shrinkwraprs::Shrinkwrap; + +#[derive(Shrinkwrap, Debug, PartialEq, Clone, Copy)] +#[shrinkwrap(mutable)] +pub struct Translation(pub Vec3); + +impl Translation { + #[inline(always)] + pub fn identity() -> Self { + Self(Vec3::default()) + } + + #[inline(always)] + pub fn new(x: f32, y: f32, z: f32) -> Self { + Self(Vec3::new(x, y, z)) + } +} + +impl Default for Translation { + fn default() -> Self { + Self::identity() + } +} + +impl From for Translation { + fn from(translation: Vec3) -> Self { + Self(Vec3::from(translation)) + } +} \ No newline at end of file diff --git a/src/legion_transform/src/hierarchy_maintenance_system.rs b/src/transform/src/hierarchy_maintenance_system.rs similarity index 100% rename from src/legion_transform/src/hierarchy_maintenance_system.rs rename to src/transform/src/hierarchy_maintenance_system.rs diff --git a/src/legion_transform/src/lib.rs b/src/transform/src/lib.rs similarity index 95% rename from src/legion_transform/src/lib.rs rename to src/transform/src/lib.rs index d92766e464..09b0438a2c 100644 --- a/src/legion_transform/src/lib.rs +++ b/src/transform/src/lib.rs @@ -1,5 +1,5 @@ pub use legion as ecs; -pub use nalgebra as math; +pub use glam as math; pub mod components; pub mod hierarchy_maintenance_system; diff --git a/src/legion_transform/src/local_to_parent_system.rs b/src/transform/src/local_to_parent_system.rs similarity index 76% rename from src/legion_transform/src/local_to_parent_system.rs rename to src/transform/src/local_to_parent_system.rs index 74261c1da6..d348d05892 100644 --- a/src/legion_transform/src/local_to_parent_system.rs +++ b/src/transform/src/local_to_parent_system.rs @@ -1,5 +1,5 @@ #![allow(dead_code)] -use crate::{components::*, ecs::prelude::*, math::Matrix4}; +use crate::{components::*, ecs::prelude::*, math::{Mat4, Vec3, Quat}}; pub fn build(_: &mut World) -> Box { SystemBuilder::<()>::new("LocalToParentUpdateSystem") @@ -114,76 +114,81 @@ pub fn build(_: &mut World) -> Box { s.spawn(|_| unsafe { // Translation a.for_each_unchecked(world, |(mut ltw, translation)| { - *ltw = LocalToParent(translation.to_homogeneous()); + *ltw = LocalToParent(Mat4::from_translation(translation.0)); }); }); s.spawn(|_| unsafe { // Rotation b.for_each_unchecked(world, |(mut ltw, rotation)| { - *ltw = LocalToParent(rotation.to_homogeneous()); + *ltw = LocalToParent(Mat4::from_quat(rotation.0)); }); }); s.spawn(|_| unsafe { // Scale c.for_each_unchecked(world, |(mut ltw, scale)| { - *ltw = LocalToParent(Matrix4::new_scaling(scale.0)); + *ltw = LocalToParent(Mat4::from_scale(Vec3::new(scale.0, scale.0, scale.0))); }); }); s.spawn(|_| unsafe { // NonUniformScale d.for_each_unchecked(world, |(mut ltw, non_uniform_scale)| { - *ltw = LocalToParent(Matrix4::new_nonuniform_scaling(&non_uniform_scale.0)); + *ltw = LocalToParent(Mat4::from_scale(non_uniform_scale.0)); }); // Translation + Rotation e.for_each_unchecked(world, |(mut ltw, translation, rotation)| { *ltw = LocalToParent( - rotation - .to_homogeneous() - .append_translation(&translation.vector), + Mat4::from_rotation_translation(rotation.0, translation.0) ); }); }); s.spawn(|_| unsafe { // Translation + Scale f.for_each_unchecked(world, |(mut ltw, translation, scale)| { - *ltw = LocalToParent(translation.to_homogeneous().prepend_scaling(scale.0)); + *ltw = LocalToParent(Mat4::from_scale_rotation_translation( + Vec3::new(scale.0, scale.0, scale.0), + Quat::default(), + translation.0, + )); }); // Translation + NonUniformScale g.for_each_unchecked(world, |(mut ltw, translation, non_uniform_scale)| { - *ltw = LocalToParent( - translation - .to_homogeneous() - .prepend_nonuniform_scaling(&non_uniform_scale.0), - ); + *ltw = LocalToParent(Mat4::from_scale_rotation_translation( + non_uniform_scale.0, + Quat::default(), + translation.0, + )); }); }); s.spawn(|_| unsafe { // Rotation + Scale h.for_each_unchecked(world, |(mut ltw, rotation, scale)| { - *ltw = LocalToParent(rotation.to_homogeneous().prepend_scaling(scale.0)); + *ltw = LocalToParent(Mat4::from_scale_rotation_translation( + Vec3::new(scale.0, scale.0, scale.0), + rotation.0, + Vec3::default(), + )); }); }); s.spawn(|_| unsafe { // Rotation + NonUniformScale i.for_each_unchecked(world, |(mut ltw, rotation, non_uniform_scale)| { - *ltw = LocalToParent( - rotation - .to_homogeneous() - .prepend_nonuniform_scaling(&non_uniform_scale.0), - ); + *ltw = LocalToParent(Mat4::from_scale_rotation_translation( + non_uniform_scale.0, + rotation.0, + Vec3::default(), + )); }); }); s.spawn(|_| unsafe { // Translation + Rotation + Scale j.for_each_unchecked(world, |(mut ltw, translation, rotation, scale)| { - *ltw = LocalToParent( - rotation - .to_homogeneous() - .append_translation(&translation.vector) - .prepend_scaling(scale.0), - ); + *ltw = LocalToParent(Mat4::from_scale_rotation_translation( + Vec3::new(scale.0, scale.0, scale.0), + rotation.0, + translation.0, + )); }); }); s.spawn(|_| unsafe { @@ -191,12 +196,11 @@ pub fn build(_: &mut World) -> Box { k.for_each_unchecked( world, |(mut ltw, translation, rotation, non_uniform_scale)| { - *ltw = LocalToParent( - rotation - .to_homogeneous() - .append_translation(&translation.vector) - .prepend_nonuniform_scaling(&non_uniform_scale.0), - ); + *ltw = LocalToParent(Mat4::from_scale_rotation_translation( + non_uniform_scale.0, + rotation.0, + translation.0, + )); }, ); }); @@ -249,75 +253,92 @@ mod test { // Verify that each was transformed correctly. assert_eq!( world.get_component::(translation).unwrap().0, - t.to_homogeneous() + Mat4::from_translation(t.0) ); assert_eq!( world.get_component::(rotation).unwrap().0, - r.to_homogeneous() + Mat4::from_quat(r.0) ); assert_eq!( world.get_component::(scale).unwrap().0, - Matrix4::new_scaling(s.0), + Mat4::from_scale(Vec3::new(s.0, s.0, s.0)) ); assert_eq!( world .get_component::(non_uniform_scale) .unwrap() .0, - Matrix4::new_nonuniform_scaling(&nus.0), + Mat4::from_scale(nus.0) ); assert_eq!( world .get_component::(translation_and_rotation) .unwrap() .0, - r.to_homogeneous().append_translation(&t.vector), + Mat4::from_rotation_translation(r.0, t.0) ); assert_eq!( world .get_component::(translation_and_scale) .unwrap() .0, - t.to_homogeneous().prepend_scaling(s.0), + Mat4::from_scale_rotation_translation( + Vec3::new(s.0, s.0, s.0), + Quat::default(), + t.0 + ) ); assert_eq!( world .get_component::(translation_and_nus) .unwrap() .0, - t.to_homogeneous().prepend_nonuniform_scaling(&nus.0), + Mat4::from_scale_rotation_translation( + nus.0, + Quat::default(), + t.0 + ) ); assert_eq!( world .get_component::(rotation_scale) .unwrap() .0, - r.to_homogeneous().prepend_scaling(s.0) + Mat4::from_scale_rotation_translation( + Vec3::new(s.0, s.0, s.0), + r.0, + Vec3::default() + ) ); assert_eq!( - world - .get_component::(rotation_nus) - .unwrap() - .0, - r.to_homogeneous().prepend_nonuniform_scaling(&nus.0) + world.get_component::(rotation_nus).unwrap().0, + Mat4::from_scale_rotation_translation( + nus.0, + r.0, + Vec3::default() + ) ); assert_eq!( world .get_component::(translation_rotation_scale) .unwrap() .0, - r.to_homogeneous() - .append_translation(&t.vector) - .prepend_scaling(s.0) + Mat4::from_scale_rotation_translation( + Vec3::new(s.0, s.0, s.0), + r.0, + t.0 + ) ); assert_eq!( world .get_component::(translation_rotation_nus) .unwrap() .0, - r.to_homogeneous() - .append_translation(&t.vector) - .prepend_nonuniform_scaling(&nus.0) + Mat4::from_scale_rotation_translation( + nus.0, + r.0, + t.0 + ) ); } } diff --git a/src/legion_transform/src/local_to_world_propagate_system.rs b/src/transform/src/local_to_world_propagate_system.rs similarity index 93% rename from src/legion_transform/src/local_to_world_propagate_system.rs rename to src/transform/src/local_to_world_propagate_system.rs index 767daa1f82..3865af14dd 100644 --- a/src/legion_transform/src/local_to_world_propagate_system.rs +++ b/src/transform/src/local_to_world_propagate_system.rs @@ -59,6 +59,7 @@ mod test { use crate::{ hierarchy_maintenance_system, local_to_parent_system, local_to_world_propagate_system, local_to_world_system, + math::{Vec3, Mat4} }; #[test] @@ -120,14 +121,14 @@ mod test { assert_eq!( world.get_component::(e1).unwrap().0, - Translation::new(1.0, 0.0, 0.0).to_homogeneous() - * Translation::new(0.0, 2.0, 0.0).to_homogeneous() + Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) + * Mat4::from_translation(Vec3::new(0.0, 2.0, 0.0)) ); assert_eq!( world.get_component::(e2).unwrap().0, - Translation::new(1.0, 0.0, 0.0).to_homogeneous() - * Translation::new(0.0, 0.0, 3.0).to_homogeneous() + Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) + * Mat4::from_translation(Vec3::new(0.0, 0.0, 3.0)) ); } } diff --git a/src/legion_transform/src/local_to_world_system.rs b/src/transform/src/local_to_world_system.rs similarity index 77% rename from src/legion_transform/src/local_to_world_system.rs rename to src/transform/src/local_to_world_system.rs index 08a45e3628..110a3fd819 100644 --- a/src/legion_transform/src/local_to_world_system.rs +++ b/src/transform/src/local_to_world_system.rs @@ -1,5 +1,5 @@ #![allow(dead_code)] -use crate::{components::*, ecs::prelude::*, math::Matrix4}; +use crate::{components::*, ecs::prelude::*, math::{Mat4, Vec3, Quat}}; pub fn build(_: &mut World) -> Box { SystemBuilder::<()>::new("LocalToWorldUpdateSystem") @@ -128,78 +128,83 @@ pub fn build(_: &mut World) -> Box { s.spawn(|_| unsafe { // Translation a.for_each_unchecked(world, |(mut ltw, translation)| { - *ltw = LocalToWorld(translation.to_homogeneous()); + *ltw = LocalToWorld(Mat4::from_translation(translation.0)); }); }); s.spawn(|_| unsafe { // Rotation b.for_each_unchecked(world, |(mut ltw, rotation)| { - *ltw = LocalToWorld(rotation.to_homogeneous()); + *ltw = LocalToWorld(Mat4::from_quat(rotation.0)); }); }); s.spawn(|_| unsafe { // Scale c.for_each_unchecked(world, |(mut ltw, scale)| { - *ltw = LocalToWorld(Matrix4::new_scaling(scale.0)); + *ltw = LocalToWorld(Mat4::from_scale(Vec3::new(scale.0, scale.0, scale.0))); }); }); s.spawn(|_| unsafe { // NonUniformScale d.for_each_unchecked(world, |(mut ltw, non_uniform_scale)| { - *ltw = LocalToWorld(Matrix4::new_nonuniform_scaling(&non_uniform_scale.0)); + *ltw = LocalToWorld(Mat4::from_scale(non_uniform_scale.0)); }); }); s.spawn(|_| unsafe { // Translation + Rotation e.for_each_unchecked(world, |(mut ltw, translation, rotation)| { *ltw = LocalToWorld( - rotation - .to_homogeneous() - .append_translation(&translation.vector), + Mat4::from_rotation_translation(rotation.0, translation.0) ); }); }); s.spawn(|_| unsafe { // Translation + Scale f.for_each_unchecked(world, |(mut ltw, translation, scale)| { - *ltw = LocalToWorld(translation.to_homogeneous().prepend_scaling(scale.0)); + *ltw = LocalToWorld(Mat4::from_scale_rotation_translation( + Vec3::new(scale.0, scale.0, scale.0), + Quat::default(), + translation.0, + )); }); }); s.spawn(|_| unsafe { // Translation + NonUniformScale g.for_each_unchecked(world, |(mut ltw, translation, non_uniform_scale)| { - *ltw = LocalToWorld( - translation - .to_homogeneous() - .prepend_nonuniform_scaling(&non_uniform_scale.0), - ); + *ltw = LocalToWorld(Mat4::from_scale_rotation_translation( + non_uniform_scale.0, + Quat::default(), + translation.0, + )); }); }); s.spawn(|_| unsafe { // Rotation + Scale h.for_each_unchecked(world, |(mut ltw, rotation, scale)| { - *ltw = LocalToWorld(rotation.to_homogeneous().prepend_scaling(scale.0)); + *ltw = LocalToWorld(Mat4::from_scale_rotation_translation( + Vec3::new(scale.0, scale.0, scale.0), + rotation.0, + Vec3::default(), + )); }); }); s.spawn(|_| unsafe { // Rotation + NonUniformScale i.for_each_unchecked(world, |(mut ltw, rotation, non_uniform_scale)| { - *ltw = LocalToWorld( - rotation - .to_homogeneous() - .prepend_nonuniform_scaling(&non_uniform_scale.0), - ); + *ltw = LocalToWorld(Mat4::from_scale_rotation_translation( + non_uniform_scale.0, + rotation.0, + Vec3::default(), + )); }); }); s.spawn(|_| unsafe { // Translation + Rotation + Scale j.for_each_unchecked(world, |(mut ltw, translation, rotation, scale)| { - *ltw = LocalToWorld( - rotation - .to_homogeneous() - .append_translation(&translation.vector) - .prepend_scaling(scale.0), - ); + *ltw = LocalToWorld(Mat4::from_scale_rotation_translation( + Vec3::new(scale.0, scale.0, scale.0), + rotation.0, + translation.0, + )); }); }); s.spawn(|_| unsafe { @@ -207,12 +212,11 @@ pub fn build(_: &mut World) -> Box { k.for_each_unchecked( world, |(mut ltw, translation, rotation, non_uniform_scale)| { - *ltw = LocalToWorld( - rotation - .to_homogeneous() - .append_translation(&translation.vector) - .prepend_nonuniform_scaling(&non_uniform_scale.0), - ); + *ltw = LocalToWorld(Mat4::from_scale_rotation_translation( + non_uniform_scale.0, + rotation.0, + translation.0, + )); }, ); }); @@ -236,6 +240,7 @@ pub fn build(_: &mut World) -> Box { #[cfg(test)] mod test { use super::*; + use crate::math::{Mat4, Quat, Vec3}; #[test] fn correct_world_transformation() { @@ -270,72 +275,92 @@ mod test { // Verify that each was transformed correctly. assert_eq!( world.get_component::(translation).unwrap().0, - t.to_homogeneous() + Mat4::from_translation(t.0) ); assert_eq!( world.get_component::(rotation).unwrap().0, - r.to_homogeneous() + Mat4::from_quat(r.0) ); assert_eq!( world.get_component::(scale).unwrap().0, - Matrix4::new_scaling(s.0), + Mat4::from_scale(Vec3::new(s.0, s.0, s.0)) ); assert_eq!( world .get_component::(non_uniform_scale) .unwrap() .0, - Matrix4::new_nonuniform_scaling(&nus.0), + Mat4::from_scale(nus.0) ); assert_eq!( world .get_component::(translation_and_rotation) .unwrap() .0, - r.to_homogeneous().append_translation(&t.vector), + Mat4::from_rotation_translation(r.0, t.0) ); assert_eq!( world .get_component::(translation_and_scale) .unwrap() .0, - t.to_homogeneous().prepend_scaling(s.0), + Mat4::from_scale_rotation_translation( + Vec3::new(s.0, s.0, s.0), + Quat::default(), + t.0 + ) ); assert_eq!( world .get_component::(translation_and_nus) .unwrap() .0, - t.to_homogeneous().prepend_nonuniform_scaling(&nus.0), + Mat4::from_scale_rotation_translation( + nus.0, + Quat::default(), + t.0 + ) ); assert_eq!( world .get_component::(rotation_scale) .unwrap() .0, - r.to_homogeneous().prepend_scaling(s.0) + Mat4::from_scale_rotation_translation( + Vec3::new(s.0, s.0, s.0), + r.0, + Vec3::default() + ) ); assert_eq!( world.get_component::(rotation_nus).unwrap().0, - r.to_homogeneous().prepend_nonuniform_scaling(&nus.0) + Mat4::from_scale_rotation_translation( + nus.0, + r.0, + Vec3::default() + ) ); assert_eq!( world .get_component::(translation_rotation_scale) .unwrap() .0, - r.to_homogeneous() - .append_translation(&t.vector) - .prepend_scaling(s.0) + Mat4::from_scale_rotation_translation( + Vec3::new(s.0, s.0, s.0), + r.0, + t.0 + ) ); assert_eq!( world .get_component::(translation_rotation_nus) .unwrap() .0, - r.to_homogeneous() - .append_translation(&t.vector) - .prepend_nonuniform_scaling(&nus.0) + Mat4::from_scale_rotation_translation( + nus.0, + r.0, + t.0 + ) ); } } diff --git a/src/legion_transform/src/transform_system_bundle.rs b/src/transform/src/transform_system_bundle.rs similarity index 100% rename from src/legion_transform/src/transform_system_bundle.rs rename to src/transform/src/transform_system_bundle.rs