diff --git a/crates/bevy_math/src/lib.rs b/crates/bevy_math/src/lib.rs index 0955c6e04d..c906dd4c92 100644 --- a/crates/bevy_math/src/lib.rs +++ b/crates/bevy_math/src/lib.rs @@ -55,7 +55,7 @@ pub use direction::*; pub use float_ord::*; pub use isometry::{Isometry2d, Isometry3d}; pub use ops::FloatPow; -pub use ray::{Ray2d, Ray3d, PlaneIntersectionMode}; +pub use ray::{PlaneIntersectionMode, Ray2d, Ray3d}; pub use rects::*; pub use rotation2d::Rot2; @@ -78,8 +78,8 @@ pub mod prelude { primitives::*, quat, uvec2, uvec3, uvec4, vec2, vec3, vec3a, vec4, BVec2, BVec3, BVec3A, BVec4, BVec4A, EulerRot, FloatExt, IRect, IVec2, IVec3, IVec4, Isometry2d, Isometry3d, Mat2, Mat3, Mat3A, - Mat4, Quat, Ray2d, Ray3d, PlaneIntersectionMode, Rect, Rot2, StableInterpolate, URect, UVec2, UVec3, UVec4, Vec2, - Vec2Swizzles, Vec3, Vec3A, Vec3Swizzles, Vec4, Vec4Swizzles, + Mat4, PlaneIntersectionMode, Quat, Ray2d, Ray3d, Rect, Rot2, StableInterpolate, URect, + UVec2, UVec3, UVec4, Vec2, Vec2Swizzles, Vec3, Vec3A, Vec3Swizzles, Vec4, Vec4Swizzles, }; #[doc(hidden)] diff --git a/crates/bevy_math/src/ray.rs b/crates/bevy_math/src/ray.rs index af8f0a7e86..1f260f63c9 100644 --- a/crates/bevy_math/src/ray.rs +++ b/crates/bevy_math/src/ray.rs @@ -108,7 +108,12 @@ impl Ray3d { /// Get the distance to a plane if the ray intersects it /// `plane_hit_mode` specifies which face of the plane the ray should hit #[inline] - pub fn intersect_plane(&self, plane_origin: Vec3, plane: InfinitePlane3d, plane_hit_mode: PlaneIntersectionMode) -> Option { + pub fn intersect_plane( + &self, + plane_origin: Vec3, + plane: InfinitePlane3d, + plane_hit_mode: PlaneIntersectionMode, + ) -> Option { let denominator = plane.normal.dot(*self.direction); if ops::abs(denominator) > f32::EPSILON { let distance = (plane_origin - self.origin).dot(*plane.normal) / denominator; @@ -176,32 +181,60 @@ mod tests { // Orthogonal, and test that an inverse plane_normal has the same result assert_eq!( - ray.intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::Both), + ray.intersect_plane( + Vec3::Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::Both + ), Some(1.0) ); assert_eq!( - ray.intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::NEG_Z), PlaneIntersectionMode::Both), + ray.intersect_plane( + Vec3::Z, + InfinitePlane3d::new(Vec3::NEG_Z), + PlaneIntersectionMode::Both + ), Some(1.0) ); assert!(ray - .intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::Both) + .intersect_plane( + Vec3::NEG_Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::Both + ) .is_none()); assert!(ray - .intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::NEG_Z), PlaneIntersectionMode::Both) + .intersect_plane( + Vec3::NEG_Z, + InfinitePlane3d::new(Vec3::NEG_Z), + PlaneIntersectionMode::Both + ) .is_none()); // Diagonal assert_eq!( - ray.intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::ONE), PlaneIntersectionMode::Both), + ray.intersect_plane( + Vec3::Z, + InfinitePlane3d::new(Vec3::ONE), + PlaneIntersectionMode::Both + ), Some(1.0) ); assert!(ray - .intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::ONE), PlaneIntersectionMode::Both) + .intersect_plane( + Vec3::NEG_Z, + InfinitePlane3d::new(Vec3::ONE), + PlaneIntersectionMode::Both + ) .is_none()); // Parallel assert!(ray - .intersect_plane(Vec3::X, InfinitePlane3d::new(Vec3::X), PlaneIntersectionMode::Both) + .intersect_plane( + Vec3::X, + InfinitePlane3d::new(Vec3::X), + PlaneIntersectionMode::Both + ) .is_none()); // Parallel with simulated rounding error @@ -220,17 +253,33 @@ mod tests { // Orthogonal, and test that ray intersects only the front face assert_eq!( - ray.intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::FrontFaceOnly), + ray.intersect_plane( + Vec3::Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::FrontFaceOnly + ), Some(1.0) ); assert!(ray - .intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::NEG_Z), PlaneIntersectionMode::FrontFaceOnly) + .intersect_plane( + Vec3::Z, + InfinitePlane3d::new(Vec3::NEG_Z), + PlaneIntersectionMode::FrontFaceOnly + ) .is_none()); assert!(ray - .intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::FrontFaceOnly) + .intersect_plane( + Vec3::NEG_Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::FrontFaceOnly + ) .is_none()); assert!(ray - .intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::FrontFaceOnly) + .intersect_plane( + Vec3::NEG_Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::FrontFaceOnly + ) .is_none()); } @@ -240,17 +289,33 @@ mod tests { // Orthogonal, and test that ray intersects only the back face assert!(ray - .intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::BackFaceOnly) + .intersect_plane( + Vec3::Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::BackFaceOnly + ) .is_none()); assert_eq!( - ray.intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::BackFaceOnly), + ray.intersect_plane( + Vec3::Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::BackFaceOnly + ), Some(1.0) ); assert!(ray - .intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::BackFaceOnly) + .intersect_plane( + Vec3::NEG_Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::BackFaceOnly + ) .is_none()); assert!(ray - .intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::Z), PlaneIntersectionMode::BackFaceOnly) + .intersect_plane( + Vec3::NEG_Z, + InfinitePlane3d::new(Vec3::Z), + PlaneIntersectionMode::BackFaceOnly + ) .is_none()); } } diff --git a/examples/3d/irradiance_volumes.rs b/examples/3d/irradiance_volumes.rs index 4f8f2b9c18..8ccc7fe2c4 100644 --- a/examples/3d/irradiance_volumes.rs +++ b/examples/3d/irradiance_volumes.rs @@ -466,7 +466,11 @@ fn handle_mouse_clicks( let Ok(ray) = camera.viewport_to_world(camera_transform, mouse_position) else { return; }; - let Some(ray_distance) = ray.intersect_plane(Vec3::ZERO, InfinitePlane3d::new(Vec3::Y), PlaneIntersectionMode::Both) else { + let Some(ray_distance) = ray.intersect_plane( + Vec3::ZERO, + InfinitePlane3d::new(Vec3::Y), + PlaneIntersectionMode::Both, + ) else { return; }; let plane_intersection = ray.origin + ray.direction.normalize() * ray_distance;