@@ -15,13 +15,13 @@ use bevy_ecs::{
15
15
removal_detection:: RemovedComponents ,
16
16
resource:: Resource ,
17
17
schedule:: IntoScheduleConfigs as _,
18
- system:: { Query , Res , ResMut } ,
18
+ system:: { Local , Query , Res , ResMut } ,
19
19
} ;
20
20
use bevy_math:: { vec4, FloatOrd , Vec4 } ;
21
21
use bevy_platform_support:: collections:: HashMap ;
22
22
use bevy_reflect:: Reflect ;
23
23
use bevy_transform:: components:: GlobalTransform ;
24
- use bevy_utils:: prelude:: default;
24
+ use bevy_utils:: { prelude:: default, Parallel } ;
25
25
use nonmax:: NonMaxU16 ;
26
26
use wgpu:: { BufferBindingType , BufferUsages } ;
27
27
@@ -385,7 +385,8 @@ impl VisibleEntityRanges {
385
385
pub fn check_visibility_ranges (
386
386
mut visible_entity_ranges : ResMut < VisibleEntityRanges > ,
387
387
view_query : Query < ( Entity , & GlobalTransform ) , With < Camera > > ,
388
- mut entity_query : Query < ( Entity , & GlobalTransform , Option < & Aabb > , & VisibilityRange ) > ,
388
+ mut par_local : Local < Parallel < Vec < ( Entity , u32 ) > > > ,
389
+ entity_query : Query < ( Entity , & GlobalTransform , Option < & Aabb > , & VisibilityRange ) > ,
389
390
) {
390
391
visible_entity_ranges. clear ( ) ;
391
392
@@ -404,30 +405,34 @@ pub fn check_visibility_ranges(
404
405
405
406
// Check each entity/view pair. Only consider entities with
406
407
// [`VisibilityRange`] components.
407
- for ( entity, entity_transform, maybe_model_aabb, visibility_range) in entity_query. iter_mut ( ) {
408
- let mut visibility = 0 ;
409
- for ( view_index, & ( _, view_position) ) in views. iter ( ) . enumerate ( ) {
410
- // If instructed to use the AABB and the model has one, use its
411
- // center as the model position. Otherwise, use the model's
412
- // translation.
413
- let model_position = match ( visibility_range. use_aabb , maybe_model_aabb) {
414
- ( true , Some ( model_aabb) ) => entity_transform
415
- . affine ( )
416
- . transform_point3a ( model_aabb. center ) ,
417
- _ => entity_transform. translation_vec3a ( ) ,
418
- } ;
419
-
420
- if visibility_range. is_visible_at_all ( ( view_position - model_position) . length ( ) ) {
421
- visibility |= 1 << view_index;
408
+ entity_query. par_iter ( ) . for_each (
409
+ |( entity, entity_transform, maybe_model_aabb, visibility_range) | {
410
+ let mut visibility = 0 ;
411
+ for ( view_index, & ( _, view_position) ) in views. iter ( ) . enumerate ( ) {
412
+ // If instructed to use the AABB and the model has one, use its
413
+ // center as the model position. Otherwise, use the model's
414
+ // translation.
415
+ let model_position = match ( visibility_range. use_aabb , maybe_model_aabb) {
416
+ ( true , Some ( model_aabb) ) => entity_transform
417
+ . affine ( )
418
+ . transform_point3a ( model_aabb. center ) ,
419
+ _ => entity_transform. translation_vec3a ( ) ,
420
+ } ;
421
+
422
+ if visibility_range. is_visible_at_all ( ( view_position - model_position) . length ( ) ) {
423
+ visibility |= 1 << view_index;
424
+ }
422
425
}
423
- }
424
426
425
- // Invisible entities have no entry at all in the hash map. This speeds
426
- // up checks slightly in this common case.
427
- if visibility != 0 {
428
- visible_entity_ranges. entities . insert ( entity, visibility) ;
429
- }
430
- }
427
+ // Invisible entities have no entry at all in the hash map. This speeds
428
+ // up checks slightly in this common case.
429
+ if visibility != 0 {
430
+ par_local. borrow_local_mut ( ) . push ( ( entity, visibility) ) ;
431
+ }
432
+ } ,
433
+ ) ;
434
+
435
+ visible_entity_ranges. entities . extend ( par_local. drain ( ) ) ;
431
436
}
432
437
433
438
/// Extracts all [`VisibilityRange`] components from the main world to the
0 commit comments