Skip to content

Commit 1d1774f

Browse files
committed
Recomptue Aabbs for updated Meshes
1 parent 91ce046 commit 1d1774f

File tree

1 file changed

+46
-4
lines changed
  • crates/bevy_render/src/view/visibility

1 file changed

+46
-4
lines changed

crates/bevy_render/src/view/visibility/mod.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ mod render_layers;
33
pub use render_layers::*;
44

55
use bevy_app::{CoreSet, Plugin};
6-
use bevy_asset::{Assets, Handle};
6+
use bevy_asset::{AssetEvent, Assets, Handle, HandleId};
77
use bevy_ecs::prelude::*;
88
use bevy_hierarchy::{Children, Parent};
99
use bevy_reflect::Reflect;
1010
use bevy_reflect::{std_traits::ReflectDefault, FromReflect};
1111
use bevy_transform::components::GlobalTransform;
1212
use bevy_transform::TransformSystem;
13+
use bevy_utils::{HashMap, HashSet};
1314
use std::cell::Cell;
1415
use thread_local::ThreadLocal;
1516

@@ -224,6 +225,7 @@ impl Plugin for VisibilityPlugin {
224225
.configure_set(CheckVisibility.in_base_set(CoreSet::PostUpdate))
225226
.configure_set(VisibilityPropagate.in_base_set(CoreSet::PostUpdate))
226227
.add_system(calculate_bounds.in_set(CalculateBounds))
228+
.add_system(update_bounds.in_set(CalculateBounds))
227229
.add_system(
228230
update_frusta::<OrthographicProjection>
229231
.in_set(UpdateOrthographicFrusta)
@@ -269,12 +271,52 @@ pub fn calculate_bounds(
269271
mut commands: Commands,
270272
meshes: Res<Assets<Mesh>>,
271273
without_aabb: Query<(Entity, &Handle<Mesh>), (Without<Aabb>, Without<NoFrustumCulling>)>,
274+
without_handle: Query<Entity, (With<Aabb>, Without<Handle<Mesh>>, Without<NoFrustumCulling>)>,
272275
) {
273276
for (entity, mesh_handle) in &without_aabb {
274-
if let Some(mesh) = meshes.get(mesh_handle) {
275-
if let Some(aabb) = mesh.compute_aabb() {
276-
commands.entity(entity).insert(aabb);
277+
let Some(mesh) = meshes.get(mesh_handle) else { continue };
278+
let Some(aabb) = mesh.compute_aabb() else { continue };
279+
commands.entity(entity).insert(aabb);
280+
}
281+
for entity in &without_handle {
282+
commands.entity(entity).remove::<Aabb>();
283+
}
284+
}
285+
286+
pub fn update_bounds(
287+
mut commands: Commands,
288+
meshes: Res<Assets<Mesh>>,
289+
mut events: EventReader<AssetEvent<Mesh>>,
290+
mut with_aabb: Query<(Entity, &Handle<Mesh>, &mut Aabb), Without<NoFrustumCulling>>,
291+
mut changed: Local<HashMap<HandleId, Aabb>>,
292+
mut removed: Local<HashSet<HandleId>>,
293+
) {
294+
changed.clear();
295+
removed.clear();
296+
297+
for mesh_event in &mut events {
298+
match mesh_event {
299+
AssetEvent::Created { handle } | AssetEvent::Modified { handle } => {
300+
let Some(mesh) = meshes.get(&handle) else { continue };
301+
let Some(aabb) = mesh.compute_aabb() else { continue };
302+
changed.insert(handle.id(), aabb);
277303
}
304+
AssetEvent::Removed { handle } => {
305+
removed.insert(handle.id());
306+
}
307+
};
308+
}
309+
310+
if changed.is_empty() && removed.is_empty() {
311+
return;
312+
}
313+
314+
// FIXME: Include entities that have changed their Handle<Mesh> component
315+
for (entity, mesh_handle, mut entity_aabb) in &mut with_aabb {
316+
if let Some(aabb) = changed.get(&mesh_handle.id()) {
317+
*entity_aabb = *aabb;
318+
} else if removed.contains(&mesh_handle.id()) {
319+
commands.entity(entity).remove::<Aabb>();
278320
}
279321
}
280322
}

0 commit comments

Comments
 (0)