Skip to content

Commit 9290a6a

Browse files
committed
correct AABB in take_gpu_data
1 parent e203add commit 9290a6a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

crates/bevy_math/src/bounding/bounded3d/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ impl Aabb3d {
7272
}
7373
}
7474

75+
/// Constructs an AABB from the its minimum and maximum extent.
76+
#[inline]
77+
pub fn from_min_max(min: impl Into<Vec3A>, max: impl Into<Vec3A>) -> Self {
78+
let (min, max) = (min.into(), max.into());
79+
debug_assert!(min.x <= max.x && min.y <= max.y && min.z <= max.z);
80+
Self { min, max }
81+
}
82+
7583
/// Computes the smallest [`Aabb3d`] containing the given set of points,
7684
/// transformed by the rotation and translation of the given isometry.
7785
///

crates/bevy_mesh/src/mesh.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ impl Mesh {
21652165
min = Vec3::min(min, v);
21662166
max = Vec3::max(max, v);
21672167
}
2168-
self.final_aabb = Some(Aabb3d::new(min, max));
2168+
self.final_aabb = Some(Aabb3d::from_min_max(min, max));
21692169
}
21702170

21712171
Ok(Self {
@@ -2523,6 +2523,7 @@ mod tests {
25232523
use crate::mesh::{Indices, MeshWindingInvertError, VertexAttributeValues};
25242524
use crate::PrimitiveTopology;
25252525
use bevy_asset::RenderAssetUsages;
2526+
use bevy_math::bounding::Aabb3d;
25262527
use bevy_math::primitives::Triangle3d;
25272528
use bevy_math::Vec3;
25282529
use bevy_transform::components::Transform;
@@ -2878,6 +2879,29 @@ mod tests {
28782879
);
28792880
}
28802881

2882+
#[test]
2883+
fn take_gpu_data_calculates_aabb() {
2884+
let mut mesh = Mesh::new(
2885+
PrimitiveTopology::TriangleList,
2886+
RenderAssetUsages::default(),
2887+
);
2888+
mesh.insert_attribute(
2889+
Mesh::ATTRIBUTE_POSITION,
2890+
vec![
2891+
[-0.5, 0., 0.],
2892+
[-1., 0., 0.],
2893+
[-1., -1., 0.],
2894+
[-0.5, -1., 0.],
2895+
],
2896+
);
2897+
mesh.insert_indices(Indices::U32(vec![0, 1, 2, 2, 3, 0]));
2898+
mesh = mesh.take_gpu_data().unwrap();
2899+
assert_eq!(
2900+
mesh.final_aabb,
2901+
Some(Aabb3d::from_min_max([-1., -1., 0.], [-0.5, 0., 0.]))
2902+
);
2903+
}
2904+
28812905
#[cfg(feature = "serialize")]
28822906
#[test]
28832907
fn serialize_deserialize_mesh() {

0 commit comments

Comments
 (0)