From d0204fda2d1dfc2e3ca4445384e0cce79f6d8b97 Mon Sep 17 00:00:00 2001 From: Opstic <46141527+opstic@users.noreply.github.com> Date: Fri, 3 Mar 2023 12:07:53 -0500 Subject: [PATCH 1/7] Add `Aabb` calculation for `Sprite`, `TextureAtlasSprite` and `Mesh2d` --- crates/bevy_sprite/src/lib.rs | 57 +++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index d7a02c3ff780b..a5215e7f6ebe0 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -27,13 +27,17 @@ pub use texture_atlas::*; pub use texture_atlas_builder::*; use bevy_app::prelude::*; -use bevy_asset::{AddAsset, Assets, HandleUntyped}; +use bevy_asset::{AddAsset, Assets, Handle, HandleUntyped}; use bevy_core_pipeline::core_2d::Transparent2d; use bevy_ecs::prelude::*; use bevy_reflect::TypeUuid; use bevy_render::{ + mesh::Mesh, + primitives::Aabb, render_phase::AddRenderCommand, render_resource::{Shader, SpecializedRenderPipelines}, + texture::Image, + view::{NoFrustumCulling, VisibilitySystems}, ExtractSchedule, RenderApp, RenderSet, }; @@ -59,7 +63,8 @@ impl Plugin for SpritePlugin { .register_type::() .register_type::() .add_plugin(Mesh2dRenderPlugin) - .add_plugin(ColorMaterialPlugin); + .add_plugin(ColorMaterialPlugin) + .add_system(calculate_bounds.in_set(VisibilitySystems::CalculateBounds)); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { render_app @@ -85,3 +90,51 @@ impl Plugin for SpritePlugin { }; } } + +pub fn calculate_bounds( + mut commands: Commands, + meshes: Res>, + images: Res>, + atlases: Res>, + meshes_without_aabb: Query<(Entity, &Mesh2dHandle), (Without, Without)>, + sprites_without_aabb: Query< + (Entity, &Sprite, &Handle), + (Without, Without), + >, + atlases_without_aabb: Query< + (Entity, &TextureAtlasSprite, &Handle), + (Without, Without), + >, +) { + for (entity, mesh_handle) in meshes_without_aabb.iter() { + if let Some(mesh) = meshes.get(&mesh_handle.0) { + if let Some(aabb) = mesh.compute_aabb() { + commands.entity(entity).insert(aabb); + } + } + } + for (entity, sprite, texture_handle) in sprites_without_aabb.iter() { + if let Some(image) = images.get(texture_handle) { + let size = sprite.custom_size.unwrap_or_else(|| image.size()); + let aabb = Aabb { + center: (-sprite.anchor.as_vec()).extend(0.0).into(), + half_extents: (0.5 * size).extend(0.0).into(), + }; + commands.entity(entity).insert(aabb); + } + } + for (entity, atlas_sprite, atlas_handle) in atlases_without_aabb.iter() { + if let Some(atlas) = atlases.get(atlas_handle) { + if let Some(rect) = atlas.textures.get(atlas_sprite.index) { + let size = atlas_sprite + .custom_size + .unwrap_or_else(|| (rect.min - rect.max).abs()); + let aabb = Aabb { + center: (-atlas_sprite.anchor.as_vec()).extend(0.0).into(), + half_extents: (0.5 * size).extend(0.0).into(), + }; + commands.entity(entity).insert(aabb); + } + } + } +} From 70de2c990de6aa5ebe6b9e5329fc4a36a467f123 Mon Sep 17 00:00:00 2001 From: Opstic <46141527+opstic@users.noreply.github.com> Date: Mon, 10 Apr 2023 12:10:08 -0400 Subject: [PATCH 2/7] Fix compile --- crates/bevy_sprite/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index e45a5bd9730a6..91979af67b0d6 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -64,7 +64,7 @@ impl Plugin for SpritePlugin { .register_type::() .add_plugin(Mesh2dRenderPlugin) .add_plugin(ColorMaterialPlugin) - .add_system(calculate_bounds.in_set(VisibilitySystems::CalculateBounds)); + .add_systems(PostUpdate, calculate_bounds.in_set(VisibilitySystems::CalculateBounds)); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { render_app From caba6357eda388859f21ad4cd797880ac54e1d66 Mon Sep 17 00:00:00 2001 From: Opstic <46141527+opstic@users.noreply.github.com> Date: Mon, 10 Apr 2023 12:21:21 -0400 Subject: [PATCH 3/7] Make center scale with size --- crates/bevy_sprite/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index 91979af67b0d6..41941bdfa2045 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -118,7 +118,7 @@ pub fn calculate_bounds( if let Some(image) = images.get(texture_handle) { let size = sprite.custom_size.unwrap_or_else(|| image.size()); let aabb = Aabb { - center: (-sprite.anchor.as_vec()).extend(0.0).into(), + center: (-sprite.anchor.as_vec() * size).extend(0.0).into(), half_extents: (0.5 * size).extend(0.0).into(), }; commands.entity(entity).insert(aabb); @@ -131,7 +131,7 @@ pub fn calculate_bounds( .custom_size .unwrap_or_else(|| (rect.min - rect.max).abs()); let aabb = Aabb { - center: (-atlas_sprite.anchor.as_vec()).extend(0.0).into(), + center: (-atlas_sprite.anchor.as_vec() * size).extend(0.0).into(), half_extents: (0.5 * size).extend(0.0).into(), }; commands.entity(entity).insert(aabb); From ef384f073bd01c4983a1a980dde5f6a2488a5119 Mon Sep 17 00:00:00 2001 From: Opstic <46141527+opstic@users.noreply.github.com> Date: Mon, 10 Apr 2023 12:22:55 -0400 Subject: [PATCH 4/7] Run `cargo fmt` --- crates/bevy_sprite/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index 41941bdfa2045..aaae0bac7696a 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -64,7 +64,10 @@ impl Plugin for SpritePlugin { .register_type::() .add_plugin(Mesh2dRenderPlugin) .add_plugin(ColorMaterialPlugin) - .add_systems(PostUpdate, calculate_bounds.in_set(VisibilitySystems::CalculateBounds)); + .add_systems( + PostUpdate, + calculate_bounds.in_set(VisibilitySystems::CalculateBounds), + ); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { render_app From dd12281eafb157d8459083a450550327de6a0f81 Mon Sep 17 00:00:00 2001 From: Opstic <46141527+opstic@users.noreply.github.com> Date: Fri, 21 Apr 2023 09:55:53 -0400 Subject: [PATCH 5/7] Apply suggestions from @devil-ira Co-authored-by: ira --- crates/bevy_sprite/src/lib.rs | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index aaae0bac7696a..6e48f1ae2868e 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -95,7 +95,7 @@ impl Plugin for SpritePlugin { } } -pub fn calculate_bounds( +pub fn calculate_bounds_2d( mut commands: Commands, meshes: Res>, images: Res>, @@ -110,16 +110,18 @@ pub fn calculate_bounds( (Without, Without), >, ) { - for (entity, mesh_handle) in meshes_without_aabb.iter() { + for (entity, mesh_handle) in &meshes_without_aabb { if let Some(mesh) = meshes.get(&mesh_handle.0) { if let Some(aabb) = mesh.compute_aabb() { commands.entity(entity).insert(aabb); } } } - for (entity, sprite, texture_handle) in sprites_without_aabb.iter() { - if let Some(image) = images.get(texture_handle) { - let size = sprite.custom_size.unwrap_or_else(|| image.size()); + for (entity, sprite, texture_handle) in &sprites_without_aabb { + if let Some(size) = sprite + .custom_size + .or_else(|| images.get(texture_handle).map(|image| image.size())) + { let aabb = Aabb { center: (-sprite.anchor.as_vec() * size).extend(0.0).into(), half_extents: (0.5 * size).extend(0.0).into(), @@ -127,17 +129,18 @@ pub fn calculate_bounds( commands.entity(entity).insert(aabb); } } - for (entity, atlas_sprite, atlas_handle) in atlases_without_aabb.iter() { - if let Some(atlas) = atlases.get(atlas_handle) { - if let Some(rect) = atlas.textures.get(atlas_sprite.index) { - let size = atlas_sprite - .custom_size - .unwrap_or_else(|| (rect.min - rect.max).abs()); - let aabb = Aabb { - center: (-atlas_sprite.anchor.as_vec() * size).extend(0.0).into(), - half_extents: (0.5 * size).extend(0.0).into(), - }; - commands.entity(entity).insert(aabb); + for (entity, atlas_sprite, atlas_handle) in &atlases_without_aabb { + if let Some(size) = atlas_sprite.custom_size.or_else(|| { + atlases + .get(atlas_handle) + .and_then(|atlas| atlas.textures.get(atlas_sprite.index)) + .map(|rect| (rect.min - rect.max).abs()) + }) { + let aabb = Aabb { + center: (-atlas_sprite.anchor.as_vec() * size).extend(0.0).into(), + half_extents: (0.5 * size).extend(0.0).into(), + }; + commands.entity(entity).insert(aabb); } } } From 2d50da9330f86905fa8c5641b9aa3d11dbd3d010 Mon Sep 17 00:00:00 2001 From: Opstic <46141527+opstic@users.noreply.github.com> Date: Fri, 21 Apr 2023 10:05:58 -0400 Subject: [PATCH 6/7] Fix compile --- crates/bevy_sprite/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index 6e48f1ae2868e..af8322f31b487 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -31,6 +31,7 @@ use bevy_asset::{AddAsset, Assets, Handle, HandleUntyped}; use bevy_core_pipeline::core_2d::Transparent2d; use bevy_ecs::prelude::*; use bevy_reflect::TypeUuid; +use bevy_render::view::calculate_bounds; use bevy_render::{ mesh::Mesh, primitives::Aabb, @@ -141,7 +142,6 @@ pub fn calculate_bounds_2d( half_extents: (0.5 * size).extend(0.0).into(), }; commands.entity(entity).insert(aabb); - } } } } From d428491469a8130981cd9120f42d2ef23e581af4 Mon Sep 17 00:00:00 2001 From: Opstic <46141527+opstic@users.noreply.github.com> Date: Fri, 21 Apr 2023 11:54:43 -0400 Subject: [PATCH 7/7] Add the proper system --- crates/bevy_sprite/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index af8322f31b487..b7ab49a2cebd2 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -31,7 +31,6 @@ use bevy_asset::{AddAsset, Assets, Handle, HandleUntyped}; use bevy_core_pipeline::core_2d::Transparent2d; use bevy_ecs::prelude::*; use bevy_reflect::TypeUuid; -use bevy_render::view::calculate_bounds; use bevy_render::{ mesh::Mesh, primitives::Aabb, @@ -67,7 +66,7 @@ impl Plugin for SpritePlugin { .add_plugin(ColorMaterialPlugin) .add_systems( PostUpdate, - calculate_bounds.in_set(VisibilitySystems::CalculateBounds), + calculate_bounds_2d.in_set(VisibilitySystems::CalculateBounds), ); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {