bevy/crates
Joseph 02b520b4e8
Split ComputedVisibility into two components to allow for accurate change detection and speed up visibility propagation (#9497)
# Objective

Fix #8267.
Fixes half of #7840.

The `ComputedVisibility` component contains two flags: hierarchy
visibility, and view visibility (whether its visible to any cameras).
Due to the modular and open-ended way that view visibility is computed,
it triggers change detection every single frame, even when the value
does not change. Since hierarchy visibility is stored in the same
component as view visibility, this means that change detection for
inherited visibility is completely broken.

At the company I work for, this has become a real issue. We are using
change detection to only re-render scenes when necessary. The broken
state of change detection for computed visibility means that we have to
to rely on the non-inherited `Visibility` component for now. This is
workable in the early stages of our project, but since we will
inevitably want to use the hierarchy, we will have to either:

1. Roll our own solution for computed visibility.
2. Fix the issue for everyone.

## Solution

Split the `ComputedVisibility` component into two: `InheritedVisibilty`
and `ViewVisibility`.
This allows change detection to behave properly for
`InheritedVisibility`.
View visiblity is still erratic, although it is less useful to be able
to detect changes
for this flavor of visibility.

Overall, this actually simplifies the API. Since the visibility system
consists of
self-explaining components, it is much easier to document the behavior
and usage.
This approach is more modular and "ECS-like" -- one could
strip out the `ViewVisibility` component entirely if it's not needed,
and rely only on inherited visibility.

---

## Changelog

- `ComputedVisibility` has been removed in favor of:
`InheritedVisibility` and `ViewVisiblity`.

## Migration Guide

The `ComputedVisibilty` component has been split into
`InheritedVisiblity` and
`ViewVisibility`. Replace any usages of
`ComputedVisibility::is_visible_in_hierarchy`
with `InheritedVisibility::get`, and replace
`ComputedVisibility::is_visible_in_view`
 with `ViewVisibility::get`.
 
 ```rust
 // Before:
 commands.spawn(VisibilityBundle {
     visibility: Visibility::Inherited,
     computed_visibility: ComputedVisibility::default(),
 });
 
 // After:
 commands.spawn(VisibilityBundle {
     visibility: Visibility::Inherited,
     inherited_visibility: InheritedVisibility::default(),
     view_visibility: ViewVisibility::default(),
 });
 ```
 
 ```rust
 // Before:
 fn my_system(q: Query<&ComputedVisibilty>) {
     for vis in &q {
         if vis.is_visible_in_hierarchy() {
     
 // After:
 fn my_system(q: Query<&InheritedVisibility>) {
     for inherited_visibility in &q {
         if inherited_visibility.get() {
 ```
 
 ```rust
 // Before:
 fn my_system(q: Query<&ComputedVisibilty>) {
     for vis in &q {
         if vis.is_visible_in_view() {
     
 // After:
 fn my_system(q: Query<&ViewVisibility>) {
     for view_visibility in &q {
         if view_visibility.get() {
 ```
 
 ```rust
 // Before:
 fn my_system(mut q: Query<&mut ComputedVisibilty>) {
     for vis in &mut q {
         vis.set_visible_in_view();
     
 // After:
 fn my_system(mut q: Query<&mut ViewVisibility>) {
     for view_visibility in &mut q {
         view_visibility.set();
 ```

---------

Co-authored-by: Robert Swain <robert.swain@gmail.com>
2023-09-01 13:00:18 +00:00
..
bevy_a11y Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_animation Fixing some doc comments (#9646) 2023-08-31 19:05:49 +00:00
bevy_app Refactor EventReader::iter to read (#9631) 2023-08-30 14:20:03 +00:00
bevy_asset Fixing some doc comments (#9646) 2023-08-31 19:05:49 +00:00
bevy_audio Fixing some doc comments (#9646) 2023-08-31 19:05:49 +00:00
bevy_core Remove Resource and add Debug to TaskPoolOptions (#9485) 2023-08-20 22:32:41 +00:00
bevy_core_pipeline Reorder render sets, refactor bevy_sprite to take advantage (#9236) 2023-08-27 14:33:49 +00:00
bevy_derive bevy_derive: Fix #[deref] breaking other attributes (#9551) 2023-08-28 17:36:18 +00:00
bevy_diagnostic Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_dylib Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_dynamic_plugin Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_ecs Fix anonymous set name stack overflow (#9650) 2023-08-31 22:52:59 +00:00
bevy_ecs_compile_fail_tests Fix CI for Rust 1.72 (#9562) 2023-08-25 12:34:24 +00:00
bevy_encase_derive Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_gilrs Refactor EventReader::iter to read (#9631) 2023-08-30 14:20:03 +00:00
bevy_gizmos Reorder render sets, refactor bevy_sprite to take advantage (#9236) 2023-08-27 14:33:49 +00:00
bevy_gltf only take up to the max number of joints (#9351) 2023-08-28 16:58:45 +00:00
bevy_hierarchy Split ComputedVisibility into two components to allow for accurate change detection and speed up visibility propagation (#9497) 2023-09-01 13:00:18 +00:00
bevy_input Refactor EventReader::iter to read (#9631) 2023-08-30 14:20:03 +00:00
bevy_internal fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
bevy_log Update tracy-client requirement from 0.15 to 0.16 (#9436) 2023-08-15 07:45:21 +00:00
bevy_macro_utils Add some more helpful errors to BevyManifest when it doesn't find Cargo.toml (#9207) 2023-07-19 12:05:04 +00:00
bevy_macros_compile_fail_tests bevy_derive: Fix #[deref] breaking other attributes (#9551) 2023-08-28 17:36:18 +00:00
bevy_math Fix erronenous glam version (#9653) 2023-08-31 12:55:17 +00:00
bevy_mikktspace Fix erronenous glam version (#9653) 2023-08-31 12:55:17 +00:00
bevy_pbr Split ComputedVisibility into two components to allow for accurate change detection and speed up visibility propagation (#9497) 2023-09-01 13:00:18 +00:00
bevy_ptr Put #[repr(transparent)] attr to bevy_ptr types (#9068) 2023-07-14 18:55:15 +00:00
bevy_reflect Fix erronenous glam version (#9653) 2023-08-31 12:55:17 +00:00
bevy_reflect_compile_fail_tests Fix CI for Rust 1.72 (#9562) 2023-08-25 12:34:24 +00:00
bevy_render Split ComputedVisibility into two components to allow for accurate change detection and speed up visibility propagation (#9497) 2023-09-01 13:00:18 +00:00
bevy_scene Split ComputedVisibility into two components to allow for accurate change detection and speed up visibility propagation (#9497) 2023-09-01 13:00:18 +00:00
bevy_sprite Split ComputedVisibility into two components to allow for accurate change detection and speed up visibility propagation (#9497) 2023-09-01 13:00:18 +00:00
bevy_tasks elaborate on TaskPool and bevy tasks (#8750) 2023-08-11 21:07:28 +00:00
bevy_text Split ComputedVisibility into two components to allow for accurate change detection and speed up visibility propagation (#9497) 2023-09-01 13:00:18 +00:00
bevy_time Move schedule name into Schedule (#9600) 2023-08-28 20:44:48 +00:00
bevy_transform Fixing some doc comments (#9646) 2023-08-31 19:05:49 +00:00
bevy_ui Split ComputedVisibility into two components to allow for accurate change detection and speed up visibility propagation (#9497) 2023-09-01 13:00:18 +00:00
bevy_utils Add system.map(...) for transforming the output of a system (#8526) 2023-08-28 16:36:46 +00:00
bevy_window Change Window::physical_cursor_position to use the physical size of the window (#9657) 2023-08-31 19:02:33 +00:00
bevy_winit Fixing some doc comments (#9646) 2023-08-31 19:05:49 +00:00