Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Aabb calculation for Sprite, TextureAtlasSprite and Mesh2d #7885

Merged
merged 9 commits into from
Apr 24, 2023
Merged

Add Aabb calculation for Sprite, TextureAtlasSprite and Mesh2d #7885

merged 9 commits into from
Apr 24, 2023

Conversation

opstic
Copy link
Contributor

@opstic opstic commented Mar 3, 2023

Objective

  • Add Aabb calculation for Sprite, TextureAtlasSprite and Mesh2d.
  • Enable frustum culling for 2D entities since frustum culling requires a Aabb component in the entity to function.
  • Improve 2D performance massively when there are many sprites out of view. (ex: many_sprites)

Solution

  • Derived from @Weasy666's 2d frustum culling #3944 pull request, which had no activity since multiple months.
  • Adapted the code to the latest version of Bevy.
  • Added support for sprites with non-center Anchors to avoid culling prematurely when part of the sprite is still in view or not culling when sprite is already out of view.

Note

  • Gives 15.8x performance boosts in some scenarios. (5 fps vs 79 fps with 409600 sprites in many_sprites)

@james7132 james7132 added this to the 0.11 milestone Mar 3, 2023
@james7132 james7132 added A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times labels Mar 3, 2023
@james7132
Copy link
Member

Wondering if we should have a Aabb2D to reduce the memory usage and computation cost of running this. Not blocking, but probably a good idea as an optimization.

@Elabajaba
Copy link
Contributor

Elabajaba commented Apr 17, 2023

This gave me a huge fps boost on many_sprites with the default settings (102400 sprites), going from ~60fps to ~1100fps.

Bevymark performance seemed slightly faster with 10 batches of 10000 sprites (100k total), though that could've just been the normal run to run variation since I didn't bother closing everything first to reduce noise.

edit: Code looks good to me.

Copy link
Contributor

@irate-devil irate-devil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this doesn't negatively impact performance when all sprites are on screen like in bevymark, but that's good :)

crates/bevy_sprite/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_sprite/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_sprite/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_sprite/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_sprite/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_sprite/src/lib.rs Outdated Show resolved Hide resolved
@Weasy666
Copy link
Contributor

Weasy666 commented Apr 21, 2023

Oh... i absolutely forgot that my PR stil existed, if would just have pinged me, i would have invested the time to bring it up to date 🙃.
But I have absolutely nothing against the fact that someone else has invested the time to get this done 😆. I would just be happy to also get some form of attribution 🙂🙈

@opstic
Copy link
Contributor Author

opstic commented Apr 21, 2023

I would just be happy to also get some form of attribution 🙂🙈

I have this line in the main section of this pull request that references the original one:
"Derived from @Weasy666's 2d frustum culling #3944 pull request, which had no activity since multiple months."
Shouldn't this be included in the commit if this was merged? Because I've seen this behavior for other pull requests.
If that's not enough, please tell me how to add more attribution. I'm pretty new to contributing, after all 😅.

opstic and others added 2 commits April 21, 2023 09:55
Co-authored-by: ira <JustTheCoolDude@gmail.com>
@Weasy666 Weasy666 mentioned this pull request Apr 21, 2023
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Apr 21, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Apr 24, 2023
Merged via the queue into bevyengine:main with commit 9a3225d Apr 24, 2023
@opstic opstic deleted the 2d-frustum-culling branch April 25, 2023 00:12
alice-i-cecile pushed a commit that referenced this pull request May 9, 2023
# Objective

Frustum culling for 2D components has been enabled since #7885,
Fixes #8490 

## Solution

Re-introduced the comments about frustum culling in the
many_animated_sprites.rs and many_sprites.rs examples.

---------

Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
Co-authored-by: François <mockersf@gmail.com>
@Selene-Amanita Selene-Amanita added the C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide label Jul 12, 2023
@github-actions
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Could you add some context on what users should update when this change get released in a new version of Bevy?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

github-merge-queue bot pushed a commit that referenced this pull request Aug 28, 2023
# Objective

This PR's first aim is to fix a mistake in `HalfSpace`'s documentation.

When defining a `Frustum` myself in bevy_basic_portals, I realised that
the "distance" of the `HalfSpace` is not, as the current doc defines,
the "distance from the origin along the normal", but actually the
opposite of that.

See the example I gave in this PR.

This means one of two things:
1. The documentation about `HalfSpace` is wrong (it is either way
because of the `n.p + d > 0` formula given later anyway, which is how it
behaves, but in that formula `d` is indeed the opposite of the "distance
from the origin along the normal", otherwise it should be `n.p > d`)
2. The distance is supposed to be the "distance from the origin along
the normal" but when used in a Frustum it's used as the opposite, and it
is a mistake
3. Same as 2, but it is somehow intended

Since I think `HalfSpace` is only used for `Frustum`, and it's easier to
fix documentation than code, I assumed for this PR we're in case number
1. If we're in case number 3, the documentation of `Frustum` needs to
change, and in case number 2, the code needs to be fixed.

While I was at it, I also :
- Tried to improve the documentation for `Frustum`, `Aabb`, and
`VisibilitySystems`, among others, since they're all related to
`Frustum`.
- Fixed documentation about frustum culling not applying to 2d objects,
which is not true since #7885

## Remarks and questions

- What about a `HalfSpace` with an infinite distance, is it allowed and
does it represents the whole space? If so it should probably be
mentioned.
- I referenced the `update_frusta` system in
`bevy_render::view::visibility` directly instead of referencing its
system set, should I reference the system set instead? It's a bit
annoying since it's in 3 sets.
- `visibility_propagate` is not public for some reason, I think it
probably should be, but for now I only documented its system set, should
I make it public? I don't think that would count as a breaking change?
- Why is `Aabb` inserted by a system, with `NoFrustumCulling` as an
opt-out, instead of having it inserted by default in `PbrBundle` for
example and then the system calculating it when it's added? Is it
because there is still no way to have an optional component inside a
bundle?

---------

Co-authored-by: SpecificProtagonist <vincentjunge@posteo.net>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
…ne#9136)

# Objective

This PR's first aim is to fix a mistake in `HalfSpace`'s documentation.

When defining a `Frustum` myself in bevy_basic_portals, I realised that
the "distance" of the `HalfSpace` is not, as the current doc defines,
the "distance from the origin along the normal", but actually the
opposite of that.

See the example I gave in this PR.

This means one of two things:
1. The documentation about `HalfSpace` is wrong (it is either way
because of the `n.p + d > 0` formula given later anyway, which is how it
behaves, but in that formula `d` is indeed the opposite of the "distance
from the origin along the normal", otherwise it should be `n.p > d`)
2. The distance is supposed to be the "distance from the origin along
the normal" but when used in a Frustum it's used as the opposite, and it
is a mistake
3. Same as 2, but it is somehow intended

Since I think `HalfSpace` is only used for `Frustum`, and it's easier to
fix documentation than code, I assumed for this PR we're in case number
1. If we're in case number 3, the documentation of `Frustum` needs to
change, and in case number 2, the code needs to be fixed.

While I was at it, I also :
- Tried to improve the documentation for `Frustum`, `Aabb`, and
`VisibilitySystems`, among others, since they're all related to
`Frustum`.
- Fixed documentation about frustum culling not applying to 2d objects,
which is not true since bevyengine#7885

## Remarks and questions

- What about a `HalfSpace` with an infinite distance, is it allowed and
does it represents the whole space? If so it should probably be
mentioned.
- I referenced the `update_frusta` system in
`bevy_render::view::visibility` directly instead of referencing its
system set, should I reference the system set instead? It's a bit
annoying since it's in 3 sets.
- `visibility_propagate` is not public for some reason, I think it
probably should be, but for now I only documented its system set, should
I make it public? I don't think that would count as a breaking change?
- Why is `Aabb` inserted by a system, with `NoFrustumCulling` as an
opt-out, instead of having it inserted by default in `PbrBundle` for
example and then the system calculating it when it's added? Is it
because there is still no way to have an optional component inside a
bundle?

---------

Co-authored-by: SpecificProtagonist <vincentjunge@posteo.net>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide C-Performance A change motivated by improving speed, memory usage or compile times S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

7 participants