Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double xforms feature flag & type aliasing approach #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ default = [
"vorbis",
"x11",
"filesystem_watcher",
# "xform_64"
]

# Force dynamic linking, which improves iterative compile times
Expand Down Expand Up @@ -88,6 +89,8 @@ subpixel_glyph_atlas = ["bevy_internal/subpixel_glyph_atlas"]
# enable systems that allow for automated testing on CI
bevy_ci_testing = ["bevy_internal/bevy_ci_testing"]

xform_64 = ["bevy_internal/xform_64"]

[dependencies]
bevy_dylib = { path = "crates/bevy_dylib", version = "0.5.0", default-features = false, optional = true }
bevy_internal = { path = "crates/bevy_internal", version = "0.5.0", default-features = false }
Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy_asset::{
use bevy_core::Name;
use bevy_ecs::world::World;
use bevy_log::warn;
use bevy_math::{Mat4, Vec3};
use bevy_math::{DefaultPrecisionConvert, Mat4};
use bevy_pbr::{AlphaMode, PbrBundle, StandardMaterial};
use bevy_render::{
camera::{
Expand Down Expand Up @@ -208,17 +208,17 @@ async fn load_gltf<'a, 'b>(
.map(|mesh| mesh.index())
.and_then(|i| meshes.get(i).cloned()),
transform: match node.transform() {
gltf::scene::Transform::Matrix { matrix } => {
Transform::from_matrix(bevy_math::Mat4::from_cols_array_2d(&matrix))
}
gltf::scene::Transform::Matrix { matrix } => Transform::from_matrix(
Mat4::from_cols_array_2d(&matrix).default_precision(),
),
gltf::scene::Transform::Decomposed {
translation,
rotation,
scale,
} => Transform {
translation: bevy_math::Vec3::from(translation),
rotation: bevy_math::Quat::from_vec4(rotation.into()),
scale: bevy_math::Vec3::from(scale),
translation: bevy_math::Vec3::from(translation).default_precision(),
rotation: bevy_math::Quat::from_vec4(rotation.into()).default_precision(),
scale: bevy_math::Vec3::from(scale).default_precision(),
},
},
},
Expand Down Expand Up @@ -456,7 +456,7 @@ fn load_node(
let transform = gltf_node.transform();
let mut gltf_error = None;
let mut node = world_builder.spawn_bundle((
Transform::from_matrix(Mat4::from_cols_array_2d(&transform.matrix())),
Transform::from_matrix(Mat4::from_cols_array_2d(&transform.matrix()).default_precision()),
GlobalTransform::identity(),
));

Expand Down Expand Up @@ -544,8 +544,8 @@ fn load_node(
..Default::default()
})
.insert(Aabb::from_min_max(
Vec3::from_slice(&bounds.min),
Vec3::from_slice(&bounds.max),
bevy_math::Vec3::from_slice(&bounds.min),
bevy_math::Vec3::from_slice(&bounds.max),
));
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ webgl = ["bevy_pbr/webgl", "bevy_render/webgl"]
# enable systems that allow for automated testing on CI
bevy_ci_testing = ["bevy_app/bevy_ci_testing", "bevy_render/ci_limits"]

xform_64 = [
"bevy_transform/xform_64",
"bevy_math/xform_64",
]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.5.0" }
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ keywords = ["bevy"]
[dependencies]
glam = { version = "0.20.0", features = ["serde", "bytemuck"] }
bevy_reflect = { path = "../bevy_reflect", version = "0.5.0", features = ["bevy"] }

[features]
xform_64 = []
37 changes: 35 additions & 2 deletions crates/bevy_math/src/face_toward.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::{Mat4, Vec3};
use crate::{DMat4, DVec3, Mat4, Vec3};

/// Generates a translation / rotation matrix that faces a given target
pub trait FaceToward {
type Vec3;
/// Generates a translation / rotation matrix that faces a given target
fn face_toward(eye: Vec3, center: Vec3, up: Vec3) -> Self;
fn face_toward(eye: Self::Vec3, center: Self::Vec3, up: Self::Vec3) -> Self;
}

impl FaceToward for Mat4 {
type Vec3 = Vec3;
fn face_toward(eye: Vec3, center: Vec3, up: Vec3) -> Self {
let forward = (eye - center).normalize();
let right = up.cross(forward).normalize();
Expand All @@ -20,6 +22,21 @@ impl FaceToward for Mat4 {
}
}

impl FaceToward for DMat4 {
type Vec3 = DVec3;
fn face_toward(eye: DVec3, center: DVec3, up: DVec3) -> Self {
let forward = (eye - center).normalize();
let right = up.cross(forward).normalize();
let up = forward.cross(right);
DMat4::from_cols(
right.extend(0.0),
up.extend(0.0),
forward.extend(0.0),
eye.extend(1.0),
)
}
}

#[cfg(test)]
mod test {
#[test]
Expand All @@ -38,4 +55,20 @@ mod test {
assert_eq!(matrix.z_axis, Vec4::new(0.6401844, 0.7682213, 0.0, 0.0));
assert_eq!(matrix.w_axis, Vec4::new(50.0, 60.0, 0.0, 1.0));
}
#[test]
fn face_toward_dmat4() {
use crate::{DMat4, DVec3, DVec4, FaceToward};

// Completely arbitrary arguments
let matrix = Mat4::face_toward(
DVec3::new(50.0, 60.0, 0.0),
DVec3::new(0.0, 0.0, 0.0),
DVec3::new(0.0, 1.0, 0.0),
);

assert_eq!(matrix.x_axis, DVec4::new(0.0, 0.0, -1.0, -0.0));
assert_eq!(matrix.y_axis, DVec4::new(-0.7682213, 0.6401844, 0.0, 0.0));
assert_eq!(matrix.z_axis, DVec4::new(0.6401844, 0.7682213, 0.0, 0.0));
assert_eq!(matrix.w_axis, DVec4::new(50.0, 60.0, 0.0, 1.0));
}
}
4 changes: 4 additions & 0 deletions crates/bevy_math/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
mod face_toward;
mod geometry;
mod precision;

pub use face_toward::*;
pub use geometry::*;
pub use glam::*;
pub use precision::*;

pub mod prelude {
pub use super::precision::*;

#[doc(hidden)]
pub use crate::{
BVec2, BVec3, BVec4, EulerRot, FaceToward, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect,
Expand Down
Loading