fix: update AABB calculation to use local transformations (#18)

Co-authored-by: not-elm <elmgameinfo@gmail.com>
This commit is contained in:
elm
2026-02-06 00:40:19 +09:00
committed by GitHub
parent 776e261d94
commit 22210c664b
2 changed files with 21 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
use bevy::camera::primitives::Aabb;
use bevy::ecs::system::SystemParam;
use bevy::math::Vec3;
use bevy::math::{Affine3A, Vec3};
use bevy::prelude::*;
#[derive(SystemParam)]
@@ -17,15 +17,20 @@ pub struct MeshAabb<'w, 's> {
}
impl MeshAabb<'_, '_> {
pub fn calculate(&self, mesh_root: Entity) -> (Vec3, Vec3) {
calculate_aabb(&[mesh_root], true, &self.meshes)
pub fn calculate_local(&self, mesh_root: Entity) -> (Vec3, Vec3) {
let Ok((root_tf, _, _)) = self.meshes.get(mesh_root) else {
return (Vec3::splat(f32::INFINITY), Vec3::splat(f32::NEG_INFINITY));
};
let root_inv = root_tf.affine().inverse();
calculate_aabb_with_root_inv(&[mesh_root], true, &self.meshes, &root_inv)
}
}
fn calculate_aabb(
fn calculate_aabb_with_root_inv(
entities: &[Entity],
include_children: bool,
entities_query: &Query<(&GlobalTransform, Option<&Aabb>, Option<&Children>)>,
root_inv: &Affine3A,
) -> (Vec3, Vec3) {
let combine_bounds = |(a_min, a_max): (Vec3, Vec3), (b_min, b_max): (Vec3, Vec3)| {
(a_min.min(b_min), a_max.max(b_max))
@@ -37,12 +42,20 @@ fn calculate_aabb(
entities_query
.get(entity)
.map(|(&tf, bounds, children)| {
let local_tf = *root_inv * tf.affine();
let mut entity_bounds = bounds.map_or(default_bounds, |bounds| {
(tf * Vec3::from(bounds.min()), tf * Vec3::from(bounds.max()))
(
local_tf.transform_point3(Vec3::from(bounds.min())),
local_tf.transform_point3(Vec3::from(bounds.max())),
)
});
if include_children && let Some(children) = children {
let children_bounds =
calculate_aabb(children, include_children, entities_query);
let children_bounds = calculate_aabb_with_root_inv(
children,
include_children,
entities_query,
root_inv,
);
entity_bounds = combine_bounds(entity_bounds, children_bounds);
}
entity_bounds

View File

@@ -28,7 +28,7 @@ impl<C: Component> WebviewPointer<'_, '_, C> {
}
pub fn pointer_pos(&self, webview: Entity, viewport_pos: Vec2) -> Option<Vec2> {
let (min, max) = self.aabb.calculate(webview);
let (min, max) = self.aabb.calculate_local(webview);
let aabb_size = Vec2::new(max.x - min.x, max.y - min.y);
let (webview_gtf, webview_size) = self.webviews.get(webview).ok()?;
self.cameras.iter().find_map(|(camera, camera_gtf)| {