Skip to main content

bevy_camera/visibility/
range.rs

1//! Specific distances from the camera in which entities are visible, also known
2//! as *hierarchical levels of detail* or *HLOD*s.
3
4use core::{
5    hash::{Hash, Hasher},
6    ops::Range,
7};
8
9use bevy_app::{App, Plugin, PostUpdate};
10use bevy_ecs::{
11    component::Component,
12    entity::{Entity, EntityHashMap},
13    query::{Or, With, Without},
14    reflect::ReflectComponent,
15    resource::Resource,
16    schedule::IntoScheduleConfigs as _,
17    system::{Local, Query, ResMut},
18};
19use bevy_math::FloatOrd;
20use bevy_reflect::Reflect;
21use bevy_transform::components::GlobalTransform;
22use bevy_utils::Parallel;
23
24use super::{check_visibility_cpu_culling, VisibilitySystems};
25use crate::{camera::Camera, primitives::Aabb, visibility::NoCpuCulling, ShadowLodOrigin};
26
27/// A plugin that enables [`VisibilityRange`]s, which allow entities to be
28/// hidden or shown based on distance to the camera.
29pub struct VisibilityRangePlugin;
30
31impl Plugin for VisibilityRangePlugin {
32    fn build(&self, app: &mut App) {
33        app.init_resource::<VisibleEntityRanges>().add_systems(
34            PostUpdate,
35            check_visibility_ranges
36                .in_set(VisibilitySystems::CheckVisibility)
37                .before(check_visibility_cpu_culling),
38        );
39    }
40}
41
42/// Specifies the range of distances that this entity must be from the camera in
43/// order to be rendered.
44///
45/// This is also known as *hierarchical level of detail* or *HLOD*.
46///
47/// Use this component when you want to render a high-polygon mesh when the
48/// camera is close and a lower-polygon mesh when the camera is far away. This
49/// is a common technique for improving performance, because fine details are
50/// hard to see in a mesh at a distance. To avoid an artifact known as *popping*
51/// between levels, each level has a *margin*, within which the object
52/// transitions gradually from invisible to visible using a dithering effect.
53///
54/// You can also use this feature to replace multiple meshes with a single mesh
55/// when the camera is distant. This is the reason for the term "*hierarchical*
56/// level of detail". Reducing the number of meshes can be useful for reducing
57/// drawcall count. Note that you must place the [`VisibilityRange`] component
58/// on each entity you want to be part of a LOD group, as [`VisibilityRange`]
59/// isn't automatically propagated down to children.
60///
61/// A typical use of this feature might look like this:
62///
63/// | Entity                  | `start_margin` | `end_margin` |
64/// |-------------------------|----------------|--------------|
65/// | Root                    | N/A            | N/A          |
66/// | ├─ High-poly mesh       | [0, 0)         | [20, 25)     |
67/// | ├─ Low-poly mesh        | [20, 25)       | [70, 75)     |
68/// | └─ Billboard *imposter* | [70, 75)       | [150, 160)   |
69///
70/// With this setup, the user will see a high-poly mesh when the camera is
71/// closer than 20 units. As the camera zooms out, between 20 units to 25 units,
72/// the high-poly mesh will gradually fade to a low-poly mesh. When the camera
73/// is 70 to 75 units away, the low-poly mesh will fade to a single textured
74/// quad. And between 150 and 160 units, the object fades away entirely. Note
75/// that the `end_margin` of a higher LOD is always identical to the
76/// `start_margin` of the next lower LOD; this is important for the crossfade
77/// effect to function properly.
78#[derive(impl bevy_ecs::component::Component for VisibilityRange where
    Self: ::core::marker::Send + ::core::marker::Sync + 'static {
    const STORAGE_TYPE: bevy_ecs::component::StorageType =
        bevy_ecs::component::StorageType::Table;
    type Mutability = bevy_ecs::component::Mutable;
    fn register_required_components(_requiree:
            bevy_ecs::component::ComponentId,
        required_components:
            &mut bevy_ecs::component::RequiredComponentsRegistrator) {}
    fn clone_behavior() -> bevy_ecs::component::ComponentCloneBehavior {
        use bevy_ecs::component::{
            DefaultCloneBehaviorBase, DefaultCloneBehaviorViaClone,
        };
        (&&&bevy_ecs::component::DefaultCloneBehaviorSpecialization::<Self>::default()).default_clone_behavior()
    }
    fn relationship_accessor()
        ->
            ::core::option::Option<bevy_ecs::relationship::ComponentRelationshipAccessor<Self>> {
        ::core::option::Option::None
    }
}Component, #[automatically_derived]
impl ::core::clone::Clone for VisibilityRange {
    #[inline]
    fn clone(&self) -> VisibilityRange {
        VisibilityRange {
            start_margin: ::core::clone::Clone::clone(&self.start_margin),
            end_margin: ::core::clone::Clone::clone(&self.end_margin),
            use_aabb: ::core::clone::Clone::clone(&self.use_aabb),
        }
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for VisibilityRange {
    #[inline]
    fn eq(&self, other: &VisibilityRange) -> bool {
        self.use_aabb == other.use_aabb &&
                self.start_margin == other.start_margin &&
            self.end_margin == other.end_margin
    }
}PartialEq, #[automatically_derived]
impl ::core::default::Default for VisibilityRange {
    #[inline]
    fn default() -> VisibilityRange {
        VisibilityRange {
            start_margin: ::core::default::Default::default(),
            end_margin: ::core::default::Default::default(),
            use_aabb: ::core::default::Default::default(),
        }
    }
}Default, const _: () =
    {
        impl bevy_reflect::GetTypeRegistration for VisibilityRange where  {
            fn get_type_registration() -> bevy_reflect::TypeRegistration {
                let mut registration =
                    bevy_reflect::TypeRegistration::of::<Self>();
                registration.insert::<bevy_reflect::ReflectFromPtr>(bevy_reflect::FromType::<Self>::from_type());
                registration.insert::<bevy_reflect::ReflectFromReflect>(bevy_reflect::FromType::<Self>::from_type());
                registration.register_type_data::<ReflectComponent, Self>();
                registration
            }
            #[inline(never)]
            fn register_type_dependencies(registry:
                    &mut bevy_reflect::TypeRegistry) {
                <Range<f32> as
                        bevy_reflect::__macro_exports::RegisterForReflection>::__register(registry);
                <bool as
                        bevy_reflect::__macro_exports::RegisterForReflection>::__register(registry);
            }
        }
        impl bevy_reflect::Typed for VisibilityRange where  {
            #[inline]
            fn type_info() -> &'static bevy_reflect::TypeInfo {
                static CELL: bevy_reflect::utility::NonGenericTypeInfoCell =
                    bevy_reflect::utility::NonGenericTypeInfoCell::new();
                CELL.get_or_set(||
                        {
                            bevy_reflect::TypeInfo::Struct(bevy_reflect::structs::StructInfo::new::<Self>(&[bevy_reflect::NamedField::new::<Range<f32>>("start_margin"),
                                                bevy_reflect::NamedField::new::<Range<f32>>("end_margin"),
                                                bevy_reflect::NamedField::new::<bool>("use_aabb")]))
                        })
            }
        }
        #[allow(deprecated, reason =
        "derives on a deprecated type shouldn't be considered a usage")]
        impl bevy_reflect::TypePath for VisibilityRange where  {
            fn type_path() -> &'static str {
                "bevy_camera::visibility::range::VisibilityRange"
            }
            fn short_type_path() -> &'static str { "VisibilityRange" }
            fn type_ident() -> ::core::option::Option<&'static str> {
                ::core::option::Option::Some("VisibilityRange")
            }
            fn crate_name() -> ::core::option::Option<&'static str> {
                ::core::option::Option::Some("bevy_camera::visibility::range".split(':').next().unwrap())
            }
            fn module_path() -> ::core::option::Option<&'static str> {
                ::core::option::Option::Some("bevy_camera::visibility::range")
            }
        }
        impl bevy_reflect::Reflect for VisibilityRange where  {
            #[inline]
            fn into_any(self:
                    bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
                ->
                    bevy_reflect::__macro_exports::alloc_utils::Box<dyn ::core::any::Any> {
                self
            }
            #[inline]
            fn as_any(&self) -> &dyn ::core::any::Any { self }
            #[inline]
            fn as_any_mut(&mut self) -> &mut dyn ::core::any::Any { self }
            #[inline]
            fn into_reflect(self:
                    bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
                ->
                    bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect> {
                self
            }
            #[inline]
            fn as_reflect(&self) -> &dyn bevy_reflect::Reflect { self }
            #[inline]
            fn as_reflect_mut(&mut self) -> &mut dyn bevy_reflect::Reflect {
                self
            }
            #[inline]
            fn set(&mut self,
                value:
                    bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect>)
                ->
                    ::core::result::Result<(),
                    bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect>> {
                *self = <dyn bevy_reflect::Reflect>::take(value)?;
                ::core::result::Result::Ok(())
            }
        }
        #[allow(non_upper_case_globals)]
        const _: () =
            {
                static __INVENTORY: ::inventory::Node =
                    ::inventory::Node {
                        value: &{
                                bevy_reflect::__macro_exports::auto_register::AutomaticReflectRegistrations(<VisibilityRange
                                        as
                                        bevy_reflect::__macro_exports::auto_register::RegisterForReflection>::__register)
                            },
                        next: ::inventory::__private::UnsafeCell::new(::inventory::__private::Option::None),
                    };
                #[link_section = ".text.startup"]
                unsafe extern "C" fn __ctor() {
                    unsafe {
                        ::inventory::ErasedNode::submit(__INVENTORY.value,
                            &__INVENTORY)
                    }
                }
                #[used]
                #[link_section = ".init_array"]
                static __CTOR: unsafe extern "C" fn() = __ctor;
            };
        impl bevy_reflect::structs::Struct for VisibilityRange where  {
            fn field(&self, name: &str)
                -> ::core::option::Option<&dyn bevy_reflect::PartialReflect> {
                match name {
                    "start_margin" =>
                        ::core::option::Option::Some(&self.start_margin),
                    "end_margin" =>
                        ::core::option::Option::Some(&self.end_margin),
                    "use_aabb" => ::core::option::Option::Some(&self.use_aabb),
                    _ => ::core::option::Option::None,
                }
            }
            fn field_mut(&mut self, name: &str)
                ->
                    ::core::option::Option<&mut dyn bevy_reflect::PartialReflect> {
                match name {
                    "start_margin" =>
                        ::core::option::Option::Some(&mut self.start_margin),
                    "end_margin" =>
                        ::core::option::Option::Some(&mut self.end_margin),
                    "use_aabb" =>
                        ::core::option::Option::Some(&mut self.use_aabb),
                    _ => ::core::option::Option::None,
                }
            }
            fn field_at(&self, index: usize)
                -> ::core::option::Option<&dyn bevy_reflect::PartialReflect> {
                match index {
                    0usize => ::core::option::Option::Some(&self.start_margin),
                    1usize => ::core::option::Option::Some(&self.end_margin),
                    2usize => ::core::option::Option::Some(&self.use_aabb),
                    _ => ::core::option::Option::None,
                }
            }
            fn field_at_mut(&mut self, index: usize)
                ->
                    ::core::option::Option<&mut dyn bevy_reflect::PartialReflect> {
                match index {
                    0usize =>
                        ::core::option::Option::Some(&mut self.start_margin),
                    1usize =>
                        ::core::option::Option::Some(&mut self.end_margin),
                    2usize => ::core::option::Option::Some(&mut self.use_aabb),
                    _ => ::core::option::Option::None,
                }
            }
            fn name_at(&self, index: usize) -> ::core::option::Option<&str> {
                match index {
                    0usize => ::core::option::Option::Some("start_margin"),
                    1usize => ::core::option::Option::Some("end_margin"),
                    2usize => ::core::option::Option::Some("use_aabb"),
                    _ => ::core::option::Option::None,
                }
            }
            fn index_of_name(&self, name: &str)
                -> ::core::option::Option<usize> {
                match name {
                    "start_margin" => ::core::option::Option::Some(0usize),
                    "end_margin" => ::core::option::Option::Some(1usize),
                    "use_aabb" => ::core::option::Option::Some(2usize),
                    _ => ::core::option::Option::None,
                }
            }
            fn field_len(&self) -> usize { 3usize }
            fn iter_fields(&self) -> bevy_reflect::structs::FieldIter {
                bevy_reflect::structs::FieldIter::new(self)
            }
            fn to_dynamic_struct(&self)
                -> bevy_reflect::structs::DynamicStruct {
                let mut dynamic: bevy_reflect::structs::DynamicStruct =
                    ::core::default::Default::default();
                dynamic.set_represented_type(bevy_reflect::PartialReflect::get_represented_type_info(self));
                dynamic.insert_boxed("start_margin",
                    bevy_reflect::PartialReflect::to_dynamic(&self.start_margin));
                dynamic.insert_boxed("end_margin",
                    bevy_reflect::PartialReflect::to_dynamic(&self.end_margin));
                dynamic.insert_boxed("use_aabb",
                    bevy_reflect::PartialReflect::to_dynamic(&self.use_aabb));
                dynamic
            }
        }
        impl bevy_reflect::PartialReflect for VisibilityRange where  {
            #[inline]
            fn get_represented_type_info(&self)
                -> ::core::option::Option<&'static bevy_reflect::TypeInfo> {
                ::core::option::Option::Some(<Self as
                            bevy_reflect::Typed>::type_info())
            }
            #[inline]
            fn try_apply(&mut self, value: &dyn bevy_reflect::PartialReflect)
                -> ::core::result::Result<(), bevy_reflect::ApplyError> {
                if let bevy_reflect::ReflectRef::Struct(struct_value) =
                        bevy_reflect::PartialReflect::reflect_ref(value) {
                    for (name, value) in
                        bevy_reflect::structs::Struct::iter_fields(struct_value) {
                        if let ::core::option::Option::Some(v) =
                                bevy_reflect::structs::Struct::field_mut(self, name) {
                            bevy_reflect::PartialReflect::try_apply(v, value)?;
                        }
                    }
                } else {
                    return ::core::result::Result::Err(bevy_reflect::ApplyError::MismatchedKinds {
                                from_kind: bevy_reflect::PartialReflect::reflect_kind(value),
                                to_kind: bevy_reflect::ReflectKind::Struct,
                            });
                }
                ::core::result::Result::Ok(())
            }
            #[inline]
            fn reflect_kind(&self) -> bevy_reflect::ReflectKind {
                bevy_reflect::ReflectKind::Struct
            }
            #[inline]
            fn reflect_ref(&self) -> bevy_reflect::ReflectRef {
                bevy_reflect::ReflectRef::Struct(self)
            }
            #[inline]
            fn reflect_mut(&mut self) -> bevy_reflect::ReflectMut {
                bevy_reflect::ReflectMut::Struct(self)
            }
            #[inline]
            fn reflect_owned(self:
                    bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
                -> bevy_reflect::ReflectOwned {
                bevy_reflect::ReflectOwned::Struct(self)
            }
            #[inline]
            fn try_into_reflect(self:
                    bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
                ->
                    ::core::result::Result<bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect>,
                    bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::PartialReflect>> {
                ::core::result::Result::Ok(self)
            }
            #[inline]
            fn try_as_reflect(&self)
                -> ::core::option::Option<&dyn bevy_reflect::Reflect> {
                ::core::option::Option::Some(self)
            }
            #[inline]
            fn try_as_reflect_mut(&mut self)
                -> ::core::option::Option<&mut dyn bevy_reflect::Reflect> {
                ::core::option::Option::Some(self)
            }
            #[inline]
            fn into_partial_reflect(self:
                    bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
                ->
                    bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::PartialReflect> {
                self
            }
            #[inline]
            fn as_partial_reflect(&self)
                -> &dyn bevy_reflect::PartialReflect {
                self
            }
            #[inline]
            fn as_partial_reflect_mut(&mut self)
                -> &mut dyn bevy_reflect::PartialReflect {
                self
            }
            fn reflect_hash(&self) -> ::core::option::Option<u64> {
                use ::core::hash::{Hash, Hasher};
                let mut hasher = bevy_reflect::utility::reflect_hasher();
                Hash::hash(&::core::any::Any::type_id(self), &mut hasher);
                Hash::hash(self, &mut hasher);
                ::core::option::Option::Some(Hasher::finish(&hasher))
            }
            fn reflect_partial_eq(&self,
                value: &dyn bevy_reflect::PartialReflect)
                -> ::core::option::Option<bool> {
                let value =
                    <dyn bevy_reflect::PartialReflect>::try_downcast_ref::<Self>(value);
                if let ::core::option::Option::Some(value) = value {
                    ::core::option::Option::Some(::core::cmp::PartialEq::eq(self,
                            value))
                } else { ::core::option::Option::Some(false) }
            }
            fn reflect_partial_cmp(&self,
                value: &dyn bevy_reflect::PartialReflect)
                -> ::core::option::Option<::core::cmp::Ordering> {
                (bevy_reflect::structs::struct_partial_cmp)(self, value)
            }
            #[inline]
            fn reflect_clone(&self)
                ->
                    ::core::result::Result<bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect>,
                    bevy_reflect::ReflectCloneError> {
                ::core::result::Result::Ok(bevy_reflect::__macro_exports::alloc_utils::Box::new(::core::clone::Clone::clone(self)))
            }
        }
        impl bevy_reflect::FromReflect for VisibilityRange where  {
            fn from_reflect(reflect: &dyn bevy_reflect::PartialReflect)
                -> ::core::option::Option<Self> {
                if let bevy_reflect::ReflectRef::Struct(__ref_struct) =
                        bevy_reflect::PartialReflect::reflect_ref(reflect) {
                    let __this =
                        Self {
                            start_margin: <Range<f32> as
                                        bevy_reflect::FromReflect>::from_reflect(bevy_reflect::structs::Struct::field(__ref_struct,
                                            "start_margin")?)?,
                            end_margin: <Range<f32> as
                                        bevy_reflect::FromReflect>::from_reflect(bevy_reflect::structs::Struct::field(__ref_struct,
                                            "end_margin")?)?,
                            use_aabb: <bool as
                                        bevy_reflect::FromReflect>::from_reflect(bevy_reflect::structs::Struct::field(__ref_struct,
                                            "use_aabb")?)?,
                        };
                    ::core::option::Option::Some(__this)
                } else { ::core::option::Option::None }
            }
        }
    };Reflect)]
79#[reflect(Component, PartialEq, Hash, Clone)]
80pub struct VisibilityRange {
81    /// The range of distances, in world units, between which this entity will
82    /// smoothly fade into view as the camera zooms out.
83    ///
84    /// If the start and end of this range are identical, the transition will be
85    /// abrupt, with no crossfading.
86    ///
87    /// `start_margin.end` must be less than or equal to `end_margin.start`.
88    pub start_margin: Range<f32>,
89
90    /// The range of distances, in world units, between which this entity will
91    /// smoothly fade out of view as the camera zooms out.
92    ///
93    /// If the start and end of this range are identical, the transition will be
94    /// abrupt, with no crossfading.
95    ///
96    /// `end_margin.start` must be greater than or equal to `start_margin.end`.
97    pub end_margin: Range<f32>,
98
99    /// If set to true, Bevy will use the center of the axis-aligned bounding
100    /// box ([`Aabb`]) as the position of the mesh for the purposes of
101    /// visibility range computation.
102    ///
103    /// Otherwise, if this field is set to false, Bevy will use the origin of
104    /// the mesh as the mesh's position.
105    ///
106    /// Usually you will want to leave this set to false, because different LODs
107    /// may have different AABBs, and smooth crossfades between LOD levels
108    /// require that all LODs of a mesh be at *precisely* the same position. If
109    /// you aren't using crossfading, however, and your meshes aren't centered
110    /// around their origins, then this flag may be useful.
111    pub use_aabb: bool,
112}
113
114impl Eq for VisibilityRange {}
115
116impl Hash for VisibilityRange {
117    fn hash<H>(&self, state: &mut H)
118    where
119        H: Hasher,
120    {
121        FloatOrd(self.start_margin.start).hash(state);
122        FloatOrd(self.start_margin.end).hash(state);
123        FloatOrd(self.end_margin.start).hash(state);
124        FloatOrd(self.end_margin.end).hash(state);
125    }
126}
127
128impl VisibilityRange {
129    /// Creates a new *abrupt* visibility range, with no crossfade.
130    ///
131    /// There will be no crossfade; the object will immediately vanish if the
132    /// camera is closer than `start` units or farther than `end` units from the
133    /// model.
134    ///
135    /// The `start` value must be less than or equal to the `end` value.
136    #[inline]
137    pub fn abrupt(start: f32, end: f32) -> Self {
138        Self {
139            start_margin: start..start,
140            end_margin: end..end,
141            use_aabb: false,
142        }
143    }
144
145    /// Returns true if both the start and end transitions for this range are
146    /// abrupt: that is, there is no crossfading.
147    #[inline]
148    pub fn is_abrupt(&self) -> bool {
149        self.start_margin.start == self.start_margin.end
150            && self.end_margin.start == self.end_margin.end
151    }
152
153    /// Returns true if the object will be visible at all, given a camera
154    /// `camera_distance` units away.
155    ///
156    /// Any amount of visibility, even with the heaviest dithering applied, is
157    /// considered visible according to this check.
158    #[inline]
159    pub fn is_visible_at_all(&self, camera_distance: f32) -> bool {
160        camera_distance >= self.start_margin.start && camera_distance < self.end_margin.end
161    }
162
163    /// Returns true if the object is completely invisible, given a camera
164    /// `camera_distance` units away.
165    ///
166    /// This is equivalent to `!VisibilityRange::is_visible_at_all()`.
167    #[inline]
168    pub fn is_culled(&self, camera_distance: f32) -> bool {
169        !self.is_visible_at_all(camera_distance)
170    }
171}
172
173/// Stores which entities are in within the [`VisibilityRange`]s of views.
174///
175/// This doesn't store the results of frustum or occlusion culling; use
176/// [`ViewVisibility`](`super::ViewVisibility`) for that. Thus entities in this list may not
177/// actually be visible.
178///
179/// For efficiency, these tables only store entities that have
180/// [`VisibilityRange`] components. Entities without such a component won't be
181/// in these tables at all.
182///
183/// The table is indexed by entity and stores a 32-bit bitmask with one bit for
184/// each camera, where a 0 bit corresponds to "out of range" and a 1 bit
185/// corresponds to "in range". Hence it's limited to storing information for 32
186/// views.
187#[derive(impl bevy_ecs::resource::Resource for VisibleEntityRanges where
    Self: ::core::marker::Send + ::core::marker::Sync + 'static {}Resource, #[automatically_derived]
impl ::core::default::Default for VisibleEntityRanges {
    #[inline]
    fn default() -> VisibleEntityRanges {
        VisibleEntityRanges {
            views: ::core::default::Default::default(),
            entities: ::core::default::Default::default(),
        }
    }
}Default)]
188pub struct VisibleEntityRanges {
189    /// Stores which bit index each view corresponds to.
190    views: EntityHashMap<u8>,
191
192    /// Stores a bitmask in which each view has a single bit.
193    ///
194    /// A 0 bit for a view corresponds to "out of range"; a 1 bit corresponds to
195    /// "in range".
196    entities: EntityHashMap<u32>,
197}
198
199impl VisibleEntityRanges {
200    /// Clears out the [`VisibleEntityRanges`] in preparation for a new frame.
201    fn clear(&mut self) {
202        self.views.clear();
203        self.entities.clear();
204    }
205
206    /// Returns true if the entity is in range of the given camera.
207    ///
208    /// This only checks [`VisibilityRange`]s and doesn't perform any frustum or
209    /// occlusion culling. Thus the entity might not *actually* be visible.
210    ///
211    /// The entity is assumed to have a [`VisibilityRange`] component. If the
212    /// entity doesn't have that component, this method will return false.
213    #[inline]
214    pub fn entity_is_in_range_of_view(&self, entity: Entity, view: Entity) -> bool {
215        let Some(visibility_bitmask) = self.entities.get(&entity) else {
216            return false;
217        };
218        let Some(view_index) = self.views.get(&view) else {
219            return false;
220        };
221        (visibility_bitmask & (1 << view_index)) != 0
222    }
223}
224
225/// Checks all entities against all views in order to determine which entities
226/// with [`VisibilityRange`]s are potentially visible.
227///
228/// This only checks distance from the camera and doesn't frustum or occlusion
229/// cull.
230pub fn check_visibility_ranges(
231    mut visible_entity_ranges: ResMut<VisibleEntityRanges>,
232    view_query: Query<(Entity, &GlobalTransform), Or<(With<Camera>, With<ShadowLodOrigin>)>>,
233    mut par_local: Local<Parallel<Vec<(Entity, u32)>>>,
234    entity_query: Query<
235        (Entity, &GlobalTransform, Option<&Aabb>, &VisibilityRange),
236        Without<NoCpuCulling>,
237    >,
238) {
239    visible_entity_ranges.clear();
240
241    // Early out if the visibility range feature isn't in use.
242    if entity_query.is_empty() {
243        return;
244    }
245
246    // Assign an index to each view.
247    let mut views = ::alloc::vec::Vec::new()vec![];
248    for (view, view_transform) in view_query.iter().take(32) {
249        let view_index = views.len() as u8;
250        visible_entity_ranges.views.insert(view, view_index);
251        views.push((view, view_transform.translation_vec3a()));
252    }
253
254    // Check each entity/view pair. Only consider entities with
255    // [`VisibilityRange`] components.
256    entity_query.par_iter().for_each(
257        |(entity, entity_transform, maybe_model_aabb, visibility_range)| {
258            let mut visibility = 0;
259            for (view_index, &(_, view_position)) in views.iter().enumerate() {
260                // If instructed to use the AABB and the model has one, use its
261                // center as the model position. Otherwise, use the model's
262                // translation.
263                let model_position = match (visibility_range.use_aabb, maybe_model_aabb) {
264                    (true, Some(model_aabb)) => entity_transform
265                        .affine()
266                        .transform_point3a(model_aabb.center),
267                    _ => entity_transform.translation_vec3a(),
268                };
269
270                if visibility_range.is_visible_at_all((view_position - model_position).length()) {
271                    visibility |= 1 << view_index;
272                }
273            }
274
275            // Invisible entities have no entry at all in the hash map. This speeds
276            // up checks slightly in this common case.
277            if visibility != 0 {
278                par_local.borrow_local_mut().push((entity, visibility));
279            }
280        },
281    );
282
283    visible_entity_ranges.entities.extend(par_local.drain());
284}