Skip to content
Closed
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
2 changes: 1 addition & 1 deletion crates/bevy_gizmos/src/gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ where
/// # use bevy_math::{bounding::Aabb3d, Vec3};
/// # use bevy_color::palettes::basic::GREEN;
/// fn system(mut gizmos: Gizmos) {
/// gizmos.aabb_3d(Aabb3d::new(Vec3::ZERO, Vec3::ONE), Transform::IDENTITY, GREEN);
/// gizmos.aabb_3d(Aabb3d::from_half_size(Vec3::ZERO, Vec3::ONE), Transform::IDENTITY, GREEN);
/// }
/// # bevy_ecs::system::assert_is_system(system);
/// ```
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_math/src/bounding/bounded3d/extrusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl BoundedExtrusion for Ellipse {
});

let half_size = Vec3A::new(max_x.x, max_y.y, max_z.z).abs() + (normal * half_depth).abs();
Aabb3d::new(isometry.translation, half_size)
Aabb3d::from_half_size(isometry.translation, half_size)
}
}

Expand All @@ -82,7 +82,7 @@ impl BoundedExtrusion for Line2d {
if dir.z == 0. { half_depth.z } else { max },
);

Aabb3d::new(isometry.translation, half_size)
Aabb3d::from_half_size(isometry.translation, half_size)
}
}

Expand All @@ -92,7 +92,7 @@ impl BoundedExtrusion for Segment2d {
let half_size = isometry.rotation * Vec3A::from(self.point1().extend(0.));
let depth = isometry.rotation * Vec3A::new(0., 0., half_depth);

Aabb3d::new(isometry.translation, half_size.abs() + depth.abs())
Aabb3d::from_half_size(isometry.translation, half_size.abs() + depth.abs())
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ impl BoundedExtrusion for Capsule2d {

let up = isometry.rotation * Vec3A::new(0., self.half_length, 0.);
let half_size = aabb.max + up.abs();
Aabb3d::new(isometry.translation, half_size)
Aabb3d::from_half_size(isometry.translation, half_size)
}
}

Expand Down Expand Up @@ -230,7 +230,7 @@ pub trait BoundedExtrusion: Primitive2d + Bounded2d {
let cap_size = Vec3A::from_array(axis_values.map(|(max_val, _)| max_val)).abs();
let depth = isometry.rotation * Vec3A::new(0., 0., half_depth);

Aabb3d::new(isometry.translation - offset, cap_size + depth.abs())
Aabb3d::from_half_size(isometry.translation - offset, cap_size + depth.abs())
}

/// Get a bounding sphere for an extrusion of the `base_shape` with the given `half_depth` with the given translation and rotation
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_math/src/bounding/bounded3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,17 @@ pub struct Aabb3d {
}

impl Aabb3d {
/// Constructs an AABB from the its minimum and maximum extent.
#[inline]
pub fn from_min_max(min: impl Into<Vec3A>, max: impl Into<Vec3A>) -> Self {
let (min, max) = (min.into(), max.into());
debug_assert!(min.x <= max.x && min.y <= max.y && min.z <= max.z);
Self { min, max }
}

/// Constructs an AABB from its center and half-size.
#[inline]
pub fn new(center: impl Into<Vec3A>, half_size: impl Into<Vec3A>) -> Self {
pub fn from_half_size(center: impl Into<Vec3A>, half_size: impl Into<Vec3A>) -> Self {
let (center, half_size) = (center.into(), half_size.into());
debug_assert!(half_size.x >= 0.0 && half_size.y >= 0.0 && half_size.z >= 0.0);
Self {
Expand Down Expand Up @@ -261,7 +269,7 @@ impl BoundingVolume for Aabb3d {
fn rotate_by(&mut self, rotation: impl Into<Self::Rotation>) {
let rot_mat = Mat3::from_quat(rotation.into());
let half_size = rot_mat.abs() * self.half_size();
*self = Self::new(rot_mat * self.center(), half_size);
*self = Self::from_half_size(rot_mat * self.center(), half_size);
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::{Aabb3d, Bounded3d, BoundingSphere};
impl Bounded3d for Sphere {
fn aabb_3d(&self, isometry: impl Into<Isometry3d>) -> Aabb3d {
let isometry = isometry.into();
Aabb3d::new(isometry.translation, Vec3::splat(self.radius))
Aabb3d::from_half_size(isometry.translation, Vec3::splat(self.radius))
}

fn bounding_sphere(&self, isometry: impl Into<Isometry3d>) -> BoundingSphere {
Expand All @@ -43,7 +43,7 @@ impl Bounded3d for InfinitePlane3d {
let half_depth = if facing_z { 0.0 } else { f32::MAX / 2.0 };
let half_size = Vec3A::new(half_width, half_height, half_depth);

Aabb3d::new(isometry.translation, half_size)
Aabb3d::from_half_size(isometry.translation, half_size)
}

fn bounding_sphere(&self, isometry: impl Into<Isometry3d>) -> BoundingSphere {
Expand All @@ -65,7 +65,7 @@ impl Bounded3d for Line3d {
let half_depth = if direction.z == 0.0 { 0.0 } else { max };
let half_size = Vec3A::new(half_width, half_height, half_depth);

Aabb3d::new(isometry.translation, half_size)
Aabb3d::from_half_size(isometry.translation, half_size)
}

fn bounding_sphere(&self, isometry: impl Into<Isometry3d>) -> BoundingSphere {
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Bounded3d for Cuboid {
);
let half_size = abs_rot_mat * self.half_size;

Aabb3d::new(isometry.translation, half_size)
Aabb3d::from_half_size(isometry.translation, half_size)
}

fn bounding_sphere(&self, isometry: impl Into<Isometry3d>) -> BoundingSphere {
Expand Down Expand Up @@ -312,7 +312,7 @@ impl Bounded3d for Torus {
// Expand the disc by the minor radius to get the torus half-size
let half_size = disc_half_size + Vec3A::splat(self.minor_radius);

Aabb3d::new(isometry.translation, half_size)
Aabb3d::from_half_size(isometry.translation, half_size)
}

fn bounding_sphere(&self, isometry: impl Into<Isometry3d>) -> BoundingSphere {
Expand All @@ -337,7 +337,7 @@ impl Bounded3d for Triangle3d {
let bounding_center = (max + min) / 2.0 + isometry.translation;
let half_extents = (max - min) / 2.0;

Aabb3d::new(bounding_center, half_extents)
Aabb3d::from_half_size(bounding_center, half_extents)
}

/// Get the bounding sphere of the triangle.
Expand Down
41 changes: 23 additions & 18 deletions crates/bevy_math/src/bounding/raycast3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,37 +315,37 @@ mod tests {
(
// Hit the center of a centered aabb
RayCast3d::new(Vec3::Y * -5., Dir3::Y, 90.),
Aabb3d::new(Vec3::ZERO, Vec3::ONE),
Aabb3d::from_half_size(Vec3::ZERO, Vec3::ONE),
4.,
),
(
// Hit the center of a centered aabb, but from the other side
RayCast3d::new(Vec3::Y * 5., -Dir3::Y, 90.),
Aabb3d::new(Vec3::ZERO, Vec3::ONE),
Aabb3d::from_half_size(Vec3::ZERO, Vec3::ONE),
4.,
),
(
// Hit the center of an offset aabb
RayCast3d::new(Vec3::ZERO, Dir3::Y, 90.),
Aabb3d::new(Vec3::Y * 3., Vec3::splat(2.)),
Aabb3d::from_half_size(Vec3::Y * 3., Vec3::splat(2.)),
1.,
),
(
// Just barely hit the aabb before the max distance
RayCast3d::new(Vec3::X, Dir3::Y, 1.),
Aabb3d::new(Vec3::new(1., 1., 0.), Vec3::splat(0.01)),
Aabb3d::from_half_size(Vec3::new(1., 1., 0.), Vec3::splat(0.01)),
0.99,
),
(
// Hit an aabb off-center
RayCast3d::new(Vec3::X, Dir3::Y, 90.),
Aabb3d::new(Vec3::Y * 5., Vec3::splat(2.)),
Aabb3d::from_half_size(Vec3::Y * 5., Vec3::splat(2.)),
3.,
),
(
// Barely hit an aabb on corner
RayCast3d::new(Vec3::X * -0.001, Dir3::from_xyz(1., 1., 1.).unwrap(), 90.),
Aabb3d::new(Vec3::Y * 2., Vec3::ONE),
Aabb3d::from_half_size(Vec3::Y * 2., Vec3::ONE),
1.732,
),
] {
Expand Down Expand Up @@ -373,17 +373,17 @@ mod tests {
(
// The ray doesn't go in the right direction
RayCast3d::new(Vec3::ZERO, Dir3::X, 90.),
Aabb3d::new(Vec3::Y * 2., Vec3::ONE),
Aabb3d::from_half_size(Vec3::Y * 2., Vec3::ONE),
),
(
// Ray's alignment isn't enough to hit the aabb
RayCast3d::new(Vec3::ZERO, Dir3::from_xyz(1., 0.99, 1.).unwrap(), 90.),
Aabb3d::new(Vec3::Y * 2., Vec3::ONE),
Aabb3d::from_half_size(Vec3::Y * 2., Vec3::ONE),
),
(
// The ray's maximum distance isn't high enough
RayCast3d::new(Vec3::ZERO, Dir3::Y, 0.5),
Aabb3d::new(Vec3::Y * 2., Vec3::ONE),
Aabb3d::from_half_size(Vec3::Y * 2., Vec3::ONE),
),
] {
assert!(
Expand All @@ -395,7 +395,7 @@ mod tests {

#[test]
fn test_ray_intersection_aabb_inside() {
let volume = Aabb3d::new(Vec3::splat(0.5), Vec3::ONE);
let volume = Aabb3d::from_half_size(Vec3::splat(0.5), Vec3::ONE);
for origin in &[Vec3::X, Vec3::Y, Vec3::ONE, Vec3::ZERO] {
for direction in &[Dir3::X, Dir3::Y, Dir3::Z, -Dir3::X, -Dir3::Y, -Dir3::Z] {
for max in &[0., 1., 900.] {
Expand All @@ -422,41 +422,46 @@ mod tests {
for (test, volume, expected_distance) in &[
(
// Hit the center of the aabb, that a ray would've also hit
AabbCast3d::new(Aabb3d::new(Vec3::ZERO, Vec3::ONE), Vec3::ZERO, Dir3::Y, 90.),
Aabb3d::new(Vec3::Y * 5., Vec3::ONE),
AabbCast3d::new(
Aabb3d::from_half_size(Vec3::ZERO, Vec3::ONE),
Vec3::ZERO,
Dir3::Y,
90.,
),
Aabb3d::from_half_size(Vec3::Y * 5., Vec3::ONE),
3.,
),
(
// Hit the center of the aabb, but from the other side
AabbCast3d::new(
Aabb3d::new(Vec3::ZERO, Vec3::ONE),
Aabb3d::from_half_size(Vec3::ZERO, Vec3::ONE),
Vec3::Y * 10.,
-Dir3::Y,
90.,
),
Aabb3d::new(Vec3::Y * 5., Vec3::ONE),
Aabb3d::from_half_size(Vec3::Y * 5., Vec3::ONE),
3.,
),
(
// Hit the edge of the aabb, that a ray would've missed
AabbCast3d::new(
Aabb3d::new(Vec3::ZERO, Vec3::ONE),
Aabb3d::from_half_size(Vec3::ZERO, Vec3::ONE),
Vec3::X * 1.5,
Dir3::Y,
90.,
),
Aabb3d::new(Vec3::Y * 5., Vec3::ONE),
Aabb3d::from_half_size(Vec3::Y * 5., Vec3::ONE),
3.,
),
(
// Hit the edge of the aabb, by casting an off-center AABB
AabbCast3d::new(
Aabb3d::new(Vec3::X * -2., Vec3::ONE),
Aabb3d::from_half_size(Vec3::X * -2., Vec3::ONE),
Vec3::X * 3.,
Dir3::Y,
90.,
),
Aabb3d::new(Vec3::Y * 5., Vec3::ONE),
Aabb3d::from_half_size(Vec3::Y * 5., Vec3::ONE),
3.,
),
] {
Expand Down
26 changes: 25 additions & 1 deletion crates/bevy_mesh/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ impl Mesh {
min = Vec3::min(min, v);
max = Vec3::max(max, v);
}
self.final_aabb = Some(Aabb3d::new(min, max));
self.final_aabb = Some(Aabb3d::from_min_max(min, max));
}

Ok(Self {
Expand Down Expand Up @@ -2523,6 +2523,7 @@ mod tests {
use crate::mesh::{Indices, MeshWindingInvertError, VertexAttributeValues};
use crate::PrimitiveTopology;
use bevy_asset::RenderAssetUsages;
use bevy_math::bounding::Aabb3d;
use bevy_math::primitives::Triangle3d;
use bevy_math::Vec3;
use bevy_transform::components::Transform;
Expand Down Expand Up @@ -2878,6 +2879,29 @@ mod tests {
);
}

#[test]
fn take_gpu_data_calculates_aabb() {
let mut mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
);
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
[-0.5, 0., 0.],
[-1., 0., 0.],
[-1., -1., 0.],
[-0.5, -1., 0.],
],
);
mesh.insert_indices(Indices::U32(vec![0, 1, 2, 2, 3, 0]));
mesh = mesh.take_gpu_data().unwrap();
assert_eq!(
mesh.final_aabb,
Some(Aabb3d::from_min_max([-1., -1., 0.], [-0.5, 0., 0.]))
);
}

#[cfg(feature = "serialize")]
#[test]
fn serialize_deserialize_mesh() {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/meshlet/from_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ impl BvhBuilder {
break;
}

aabb = aabb.merge(&Aabb3d::new(
aabb = aabb.merge(&Aabb3d::from_half_size(
child.aabbs[i].center,
child.aabbs[i].half_extent,
));
Expand Down Expand Up @@ -963,7 +963,7 @@ impl BvhBuilder {
break;
}

aabb = aabb.merge(&Aabb3d::new(
aabb = aabb.merge(&Aabb3d::from_half_size(
root.aabbs[i].center,
root.aabbs[i].half_extent,
));
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_picking/src/mesh_picking/ray_cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl<'w, 's> MeshRayCast<'w, 's> {
if should_ray_cast
&& let Some(distance) = ray_aabb_intersection_3d(
ray,
&Aabb3d::new(aabb.center, aabb.half_extents),
&Aabb3d::from_half_size(aabb.center, aabb.half_extents),
&transform.affine(),
)
{
Expand Down
6 changes: 6 additions & 0 deletions release-content/migration-guides/aabb3d_new_renamed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: "`Aabb3d::new` renamed to `Aabb3d::from_half_size`"
pull_requests: [22176]
---

`Aabb3d::new` has been renamed to `Aabb3d::from_half_size`, and a new constructor has been added `Aabb3d::from_min_max`.