fix: update AABB calculation to use local transformations (#18)
Co-authored-by: not-elm <elmgameinfo@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use bevy::camera::primitives::Aabb;
|
use bevy::camera::primitives::Aabb;
|
||||||
use bevy::ecs::system::SystemParam;
|
use bevy::ecs::system::SystemParam;
|
||||||
use bevy::math::Vec3;
|
use bevy::math::{Affine3A, Vec3};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(SystemParam)]
|
#[derive(SystemParam)]
|
||||||
@@ -17,15 +17,20 @@ pub struct MeshAabb<'w, 's> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MeshAabb<'_, '_> {
|
impl MeshAabb<'_, '_> {
|
||||||
pub fn calculate(&self, mesh_root: Entity) -> (Vec3, Vec3) {
|
pub fn calculate_local(&self, mesh_root: Entity) -> (Vec3, Vec3) {
|
||||||
calculate_aabb(&[mesh_root], true, &self.meshes)
|
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],
|
entities: &[Entity],
|
||||||
include_children: bool,
|
include_children: bool,
|
||||||
entities_query: &Query<(&GlobalTransform, Option<&Aabb>, Option<&Children>)>,
|
entities_query: &Query<(&GlobalTransform, Option<&Aabb>, Option<&Children>)>,
|
||||||
|
root_inv: &Affine3A,
|
||||||
) -> (Vec3, Vec3) {
|
) -> (Vec3, Vec3) {
|
||||||
let combine_bounds = |(a_min, a_max): (Vec3, Vec3), (b_min, b_max): (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))
|
(a_min.min(b_min), a_max.max(b_max))
|
||||||
@@ -37,12 +42,20 @@ fn calculate_aabb(
|
|||||||
entities_query
|
entities_query
|
||||||
.get(entity)
|
.get(entity)
|
||||||
.map(|(&tf, bounds, children)| {
|
.map(|(&tf, bounds, children)| {
|
||||||
|
let local_tf = *root_inv * tf.affine();
|
||||||
let mut entity_bounds = bounds.map_or(default_bounds, |bounds| {
|
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 {
|
if include_children && let Some(children) = children {
|
||||||
let children_bounds =
|
let children_bounds = calculate_aabb_with_root_inv(
|
||||||
calculate_aabb(children, include_children, entities_query);
|
children,
|
||||||
|
include_children,
|
||||||
|
entities_query,
|
||||||
|
root_inv,
|
||||||
|
);
|
||||||
entity_bounds = combine_bounds(entity_bounds, children_bounds);
|
entity_bounds = combine_bounds(entity_bounds, children_bounds);
|
||||||
}
|
}
|
||||||
entity_bounds
|
entity_bounds
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ impl<C: Component> WebviewPointer<'_, '_, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn pointer_pos(&self, webview: Entity, viewport_pos: Vec2) -> Option<Vec2> {
|
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 aabb_size = Vec2::new(max.x - min.x, max.y - min.y);
|
||||||
let (webview_gtf, webview_size) = self.webviews.get(webview).ok()?;
|
let (webview_gtf, webview_size) = self.webviews.get(webview).ok()?;
|
||||||
self.cameras.iter().find_map(|(camera, camera_gtf)| {
|
self.cameras.iter().find_map(|(camera, camera_gtf)| {
|
||||||
|
|||||||
Reference in New Issue
Block a user