Skip to content

Move Frustum, HalfSpace from bevy_render to bevy_math#13882

Closed
torsteingrindvik wants to merge 4 commits intobevyengine:mainfrom
torsteingrindvik:move-math-stuff
Closed

Move Frustum, HalfSpace from bevy_render to bevy_math#13882
torsteingrindvik wants to merge 4 commits intobevyengine:mainfrom
torsteingrindvik:move-math-stuff

Conversation

@torsteingrindvik
Copy link
Copy Markdown
Contributor

@torsteingrindvik torsteingrindvik commented Jun 16, 2024

Also move necessary baggage.

Objective

Fixes #13878

Solution

  • Move Frustum and HalfSpace from bevy_render to bevy_math
  • Move the Sphere and Aabb structs from bevy_render to a legacy module in bevy_math, to be refactored later

Changelog

  • Move Frustum and HalfSpace from bevy_render to bevy_math

Migration Guide

  • Frustum and HalfSpace must now be imported from bevy_math::primitives::{Frustum, HalfSpace} instead of from bevy_render.

@torsteingrindvik
Copy link
Copy Markdown
Contributor Author

When moving baggage into the legacy module (as suggested in #13878 (comment)) I had to also make docs for those, but since it's a legacy module I just mocked the docs. Let me know what should be done there (if not that).

Also the bevy_render/src/lib.rs registering of types seem kinda weird 🤔

@torsteingrindvik torsteingrindvik changed the title Initial pass move Frustum, HalfSpace from bevy_render to bevy_math. A… Move Frustum, HalfSpace from bevy_render to bevy_math Jun 16, 2024
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Code-Quality A section of code that is hard to understand or change A-Math Fundamental domain-agnostic mathematical operations D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Jun 16, 2024
pub use frustum::Frustum;
mod half_space;
pub use half_space::HalfSpace;
/// Clean me up!
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs real docs explaining why this exists, and linking to an issue made to track their clean-up.

Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Good, just a few notes:

  1. IMO we should remove the legacy notes docs and simply blanket allow missing docs on the legacy_bevy_render module.
  2. We should also remove Sphere from bevy_render during this PR rather than duplicating it, and note that in the migration guide.
  3. We should make an issue specifically for eliminating bevy_render::primitives and link it in the module docs of both the bevy_math and bevy_render modules.

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 16, 2024
@torsteingrindvik
Copy link
Copy Markdown
Contributor Author

We should also remove Sphere from bevy_render during this PR rather than duplicating it, and note that in the migration guide.

Perhaps I misunderstood the goal/scope of point 1. and 2. in the issue, but could you elaborate?
Sphere is deleted from bevy_render here.

Linking to changes seems a bit buggy, but it's deleted from bevy_render/src/primitives/mod.rs.

If you mean it's duplicated because it's now in the legacy module and we should remove that definition altogether then I would need to figure out the implications since the Sphere definition from bevy_render has a center field and the one in bevy_math doesn't.
I was probably missing some context from the conversation on Discord- I'm feeling a bit zapped for energy currently so depending on the scope I might put this up for adaption or close. But if I can get a clear picture it might be ok.

@alice-i-cecile
Copy link
Copy Markdown
Member

alice-i-cecile commented Jun 18, 2024

Sphere is deleted from bevy_render here.

I just misread the diff, sorry for the confusion! I can make the follow-up issue, so the only thing I want from you is the missing_docs change.

Torstein Grindvik added 3 commits June 19, 2024 19:58
…lso move necessary baggage.

Signed-off-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
Signed-off-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
CI
Signed-off-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
…flict for rust lints

Signed-off-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
@torsteingrindvik
Copy link
Copy Markdown
Contributor Author

Sphere is deleted from bevy_render here.

I just misread the diff, sorry for the confusion! I can make the follow-up issue, so the only thing I want from you is the missing_docs change.

Was questioning my sanity for a bit there 😅

I made a placeholder issue and linked to it in the PR: #13931 , will you edit it when needed?

Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Great: I'll merge this as soon as 0.14 is out the door. Merging refactors like this will make cherrypicking fixes harder, so I'm going to hold off for just a bit.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jun 19, 2024
@Jondolf
Copy link
Copy Markdown
Contributor

Jondolf commented Jun 20, 2024

If you mean it's duplicated because it's now in the legacy module and we should remove that definition altogether then I would need to figure out the implications since the Sphere definition from bevy_render has a center field and the one in bevy_math doesn't.

Sphere is functionally like a bounding sphere, and its equivalent in bevy_math is the aptly named BoundingSphere. The two types have basically identical representations, except BoundingSphere has a bunch more functionality. Afaik the only missing method is intersects_obb.


As for the Aabb, we can't just remove it from bevy_render or deprecate it without a replacement since it's an actual component used during frustum culling, so moving it to a legacy module doesn't feel very useful. It should either (1) be a wrapper over Aabb3d living in bevy_render, or (2) remain roughly the same, but probably renamed to have a more specific name like CullingAabb or just RenderAabb.

The main "issue" with option (1) is that Aabb has a center + half-extents representation while Aabb3d uses min + max (which is usually better), so we need benchmarks if we intend to change it to use Aabb3d. I guess this might be fine to leave to a follow-up though, as long as it's not merged for 0.14.

Copy link
Copy Markdown
Contributor

@Jondolf Jondolf left a comment

Choose a reason for hiding this comment

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

Personally, I don't really like adding a legacy rendering module like this to bevy_math especially since it's not gated behind feature flags, and I don't really see the utility. It just feels like extra complexity and is not something bevy_math should have at all, even momentarily imo.

Sphere is a type that we should pretty easily be able to just remove, and Aabb is a type we need to keep around in some form in bevy_render anyway. I'd just add an aabb module (or similar) in bevy_render, or even leave it where it was, and leave refactoring/renaming the type to a follow-up. Similarly for Frustum, if it needs to be a component, it just means that we need a newtype in bevy_render.

Comment on lines +37 to +51
#[cfg_attr(feature = "bevy_ecs", derive(Component))]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Debug, PartialEq, Default)
)]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(
all(feature = "bevy_ecs", feature = "bevy_reflect"),
reflect(Component)
)]
pub struct HalfSpace {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should not be a component. The old one isn't either.

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Jun 20, 2024
@alice-i-cecile
Copy link
Copy Markdown
Member

@Jondolf how do you think we should tackle this broader initiative of "don't have weird primitives in bevy_render" as a whole? I really value your judgement here, and it's not clear what can / should be done first.

@Jondolf
Copy link
Copy Markdown
Contributor

Jondolf commented Jun 20, 2024

My response became so long that I just wrote it up into its own issue 😅 See #13945 for the full context.

TLDR concerning this PR: The Aabb and Sphere changes aren't as trivial and shouldn't be done here imo, and I think that adding the legacy module and bevy_ecs dependency in bevy_math just isn't a great idea even if they're temporary, especially given that we don't seem to have a clear plan of how we'd move forward afterwards.

My opinion is that this PR should just do what the PR title says:

  1. Move HalfSpace into bevy_math. (done)
  2. (Optional) Move Frustum into bevy_math. But:
    • Rename it to something like ViewFrustum to be more accurate and avoid overlap with e.g. ConicalFrustum.
    • Add a wrapper component in bevy_render. Not sure what name fits best since both lights and cameras can have the frustum component. Rendering people might be able to give input here. Either way, I don't really want components in bevy_math, even behind feature flags.
    • Add intersects_obb as a method on the wrapper component in bevy_render for now. It'd be nice to have a similar method in bevy_math, but again I don't really like adding a duplicate Aabb or a dependency on bevy_render there. And this is just simpler imo :)

No legacy modules or other unrelated changes, just these two (or just (1)). The Aabb and Sphere refactor can be done in a separate PR imo.

Feel free to disagree, these are just my thoughts on this :)

@torsteingrindvik
Copy link
Copy Markdown
Contributor Author

My response became so long that I just wrote it up into its own issue 😅 See #13945 for the full context.

TLDR concerning this PR: The Aabb and Sphere changes aren't as trivial and shouldn't be done here imo, and I think that adding the legacy module and bevy_ecs dependency in bevy_math just isn't a great idea even if they're temporary, especially given that we don't seem to have a clear plan of how we'd move forward afterwards.

My opinion is that this PR should just do what the PR title says:

1. Move `HalfSpace` into `bevy_math`. (done)

2. (Optional) Move `Frustum` into `bevy_math`. But:
   
   * Rename it to something like `ViewFrustum` to be more accurate and avoid overlap with e.g. `ConicalFrustum`.
   * Add a wrapper component in `bevy_render`. Not sure what name fits best since both lights and cameras can have the frustum component. Rendering people might be able to give input here. Either way, I don't really want components in `bevy_math`, even behind feature flags.
   * Add `intersects_obb` as a method on the wrapper component in `bevy_render` for now. It'd be nice to have a similar method in `bevy_math`, but again I don't really like adding a duplicate `Aabb` or a dependency on `bevy_render` there. And this is just simpler imo :)

No legacy modules or other unrelated changes, just these two (or just (1)). The Aabb and Sphere refactor can be done in a separate PR imo.

Feel free to disagree, these are just my thoughts on this :)

Thanks for the explanation. This PR was originally just to address a chore when I had some extra time but there are more moving parts involved- so I think it's simplest for me to close this out for now.
The issue you created I think will serve the goals better.

github-merge-queue bot pushed a commit that referenced this pull request Jan 27, 2026
…m `bevy_camera` to `bevy_math` (#22684)

# Objective

- Does the latter half of #13945 
- Does the first two thirds of #13878 
- Finish some of what #13882 started

## Solution

- Moves `bevy_camera::primitives::HalfSpace` to
`bevy_math::primitives::HalfSpace` (first commit)
- Moves some of `bevy_camera::primitives::Frustum` to
`bevy_math::primitives::ViewFrustum` (second commit)
- I basically followed Jondolf’s directions on the two refactorings.
aabb, obb, and sphere stuff stays in bevy_camera (for now? If sphere is
refactored, it has been stressed to be done as a follow up )

## Testing

I ran the `lighting`, `2d_gizmos`, and `3d_gizmos` example just to make
sure the lights… and camera… (and action!) were still working.
Everything seems to be fine there compared to main. If there are any
other examples I should run to make sure things look good, let me know.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Math Fundamental domain-agnostic mathematical operations A-Rendering Drawing game state to the screen C-Code-Quality A section of code that is hard to understand or change D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bevy_render::Frustum should live in bevy_math

4 participants