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

Allow camera viewports to be specified as a percentage of the parent viewport #5185

Closed
wants to merge 7 commits into from

Conversation

LoipesMas
Copy link
Contributor

@LoipesMas LoipesMas commented Jul 3, 2022

Objective

Solution

  • Use enums for viewport position and size, and convert when needed

Changelog

Added:

  • ViewportSize and ViewportPosition enums
  • Camera::viewport_absolute_size function to reduce code repetition

Changed:

  • Viewport's position and size to use new enums
  • TrackedRenderPass::set_camera_viewport function to take ExtractedCamera as argument (needed to calculate absolute values)

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jul 3, 2022
crates/bevy_render/src/camera/camera.rs Outdated Show resolved Hide resolved
@@ -340,12 +340,24 @@ impl<'a> TrackedRenderPass<'a> {
/// Set the rendering viewport to the given [`Camera`](crate::camera::Viewport) [`Viewport`].
///
/// Subsequent draw calls will be projected into that viewport.
pub fn set_camera_viewport(&mut self, viewport: &Viewport) {
pub fn set_camera_viewport(&mut self, viewport: &Viewport, camera: &ExtractedCamera) {
Copy link
Contributor Author

@LoipesMas LoipesMas Jul 3, 2022

Choose a reason for hiding this comment

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

I had to change the signature of this function, because I needed camera to calculate absolute values of viewport.
Alternatives would be:

  • have caller clone the viewport and calculate those values themselves (or make it a method on Viewport) and then pass that new "derived" viewport to this function. but then someone could forget to do that
  • "bake in" absolute values into viewport (i.e. only use absolutes in viewport) but tbf I'm not sure when would this have to happen (and it wouldn't be responsive)

Copy link
Member

Choose a reason for hiding this comment

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

I definitely don't want the second option; that seems much harder to maintain and responsivity is important.

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of passing in the entire extracted camera, could you instead just pass in the physical target size as that is the only thing that is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that works too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@alice-i-cecile
Copy link
Member

Suggested PR title change: "Allow camera viewports to be specified as a percentage of the parent viewport"

@alice-i-cecile alice-i-cecile added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Jul 3, 2022
@LoipesMas LoipesMas changed the title Viewport perc Allow camera viewports to be specified as a percentage of the parent viewport Jul 3, 2022
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@LoipesMas
Copy link
Contributor Author

LoipesMas commented Jul 9, 2022

Updated the PR to use two separate enums for size and position.

I'm still not fully satisfied with this solution, but it can be changed later, maybe when doing similar thing for bevy_ui

@alice-i-cecile
Copy link
Member

PR description needs updating now :)

@LoipesMas LoipesMas requested a review from superdump July 14, 2022 09:30
let physical_size = viewport
.physical_size
.try_as_absolute(physical_target_size)
.expect("Couldn't get camera.physical_target_size (needed for relative viewport size)");
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I feel like if this method takes an Option<> then it should handle it gracefully, not panic. So I guess either we need to find a way to handle this gracefully, or this shouldn’t take an Option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

First of all, I'm not sure when physical_target_size can be None.
I think this is the only place where it's set:

physical_target_size: Some(target_size),

So it looks like it's always Some.
Maybe it shouldn't be an Option at all?

Also, try_as_absolute takes an Option because this value isn't required if the size/position is an absolute value. So requiring this to always be some value isn't great either. (Because absolute value without context is still valid)

Copy link
Contributor

Choose a reason for hiding this comment

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

It does appear that it's always Some as far as I can tell, since we wouldn't even extract the window if it's not. I think this can be refactored on ExtractedView.

@NthTensor
Copy link
Contributor

This appears to be stale, but I think this feature is wanted. I propose we close this an open an issue for the feature.

@alice-i-cecile
Copy link
Member

Agreed. Close once the issue is up.

Copy link
Contributor

@tychedelia tychedelia left a comment

Choose a reason for hiding this comment

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

Looks good sans the one comment about accepting a UVec2 instead of an option.

let physical_size = viewport
.physical_size
.try_as_absolute(physical_target_size)
.expect("Couldn't get camera.physical_target_size (needed for relative viewport size)");
Copy link
Contributor

Choose a reason for hiding this comment

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

It does appear that it's always Some as far as I can tell, since we wouldn't even extract the window if it's not. I think this can be refactored on ExtractedView.

@tychedelia tychedelia added the S-Adopt-Me The original PR author has no intent to complete this work. Pick me up! label Aug 16, 2024
@NthTensor NthTensor removed the S-Adopt-Me The original PR author has no intent to complete this work. Pick me up! label Aug 22, 2024
@NthTensor NthTensor added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it S-Needs-Review Needs reviewer attention (from anyone!) to move forward 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 Aug 22, 2024
@bas-ie
Copy link
Contributor

bas-ie commented Oct 13, 2024

Backlog cleanup: closing due to inactivity, although looks like we got pretty close here! Can be reopened via tracking issue #4960 if anyone feels like adopting.

@bas-ie bas-ie closed this Oct 13, 2024
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-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Viewport size and position to be defined through percentages or absolute values
6 participants