-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Deduplicate world_to_view logic #21715
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
Open
Breakdown-Dog
wants to merge
5
commits into
bevyengine:main
Choose a base branch
from
Breakdown-Dog:world_to_view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+32
−34
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -489,21 +489,17 @@ impl Camera { | |
| self.computed.clip_from_view | ||
| } | ||
|
|
||
| /// Given a position in world space, use the camera to compute the viewport-space coordinates. | ||
| /// | ||
| /// To get the coordinates in Normalized Device Coordinates, you should use | ||
| /// [`world_to_ndc`](Self::world_to_ndc). | ||
| /// Core conversion logic to compute viewport coordinates | ||
| /// | ||
| /// # Panics | ||
| /// This function is shared by `world_to_viewport` and `world_to_viewport_with_depth` | ||
| /// to avoid code duplication. | ||
| /// | ||
| /// Will panic if `glam_assert` is enabled and the `camera_transform` contains `NAN` | ||
| /// (see [`world_to_ndc`][Self::world_to_ndc]). | ||
| #[doc(alias = "world_to_screen")] | ||
| pub fn world_to_viewport( | ||
| /// Returns a tuple `(viewport_position, ndc_space_coords.z)`. | ||
| fn world_to_viewport_core( | ||
| &self, | ||
| camera_transform: &GlobalTransform, | ||
| world_position: Vec3, | ||
| ) -> Result<Vec2, ViewportConversionError> { | ||
| ) -> Result<(Vec2, f32), ViewportConversionError> { | ||
| let target_rect = self | ||
| .logical_viewport_rect() | ||
| .ok_or(ViewportConversionError::NoViewportSize)?; | ||
|
|
@@ -524,7 +520,27 @@ impl Camera { | |
| // Once in NDC space, we can discard the z element and map x/y to the viewport rect | ||
| let viewport_position = | ||
| (ndc_space_coords.truncate() + Vec2::ONE) / 2.0 * target_rect.size() + target_rect.min; | ||
| Ok(viewport_position) | ||
| Ok((viewport_position, ndc_space_coords.z)) | ||
| } | ||
|
|
||
| /// Given a position in world space, use the camera to compute the viewport-space coordinates. | ||
| /// | ||
| /// To get the coordinates in Normalized Device Coordinates, you should use | ||
| /// [`world_to_ndc`](Self::world_to_ndc). | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Will panic if `glam_assert` is enabled and the `camera_transform` contains `NAN` | ||
| /// (see [`world_to_ndc`][Self::world_to_ndc]). | ||
| #[doc(alias = "world_to_screen")] | ||
| pub fn world_to_viewport( | ||
| &self, | ||
| camera_transform: &GlobalTransform, | ||
| world_position: Vec3, | ||
| ) -> Result<Vec2, ViewportConversionError> { | ||
| Ok(self | ||
| .world_to_viewport_core(camera_transform, world_position)? | ||
| .0) | ||
| } | ||
|
|
||
| /// Given a position in world space, use the camera to compute the viewport-space coordinates and depth. | ||
|
|
@@ -542,30 +558,9 @@ impl Camera { | |
| camera_transform: &GlobalTransform, | ||
| world_position: Vec3, | ||
| ) -> Result<Vec3, ViewportConversionError> { | ||
| let target_rect = self | ||
| .logical_viewport_rect() | ||
| .ok_or(ViewportConversionError::NoViewportSize)?; | ||
| let mut ndc_space_coords = self | ||
| .world_to_ndc(camera_transform, world_position) | ||
| .ok_or(ViewportConversionError::InvalidData)?; | ||
| // NDC z-values outside of 0 < z < 1 are outside the (implicit) camera frustum and are thus not in viewport-space | ||
| if ndc_space_coords.z < 0.0 { | ||
| return Err(ViewportConversionError::PastFarPlane); | ||
| } | ||
| if ndc_space_coords.z > 1.0 { | ||
| return Err(ViewportConversionError::PastNearPlane); | ||
| } | ||
|
|
||
| // Stretching ndc depth to value via near plane and negating result to be in positive room again. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you intend to delete this comment as well? If not, you can place it back above |
||
| let depth = -self.depth_ndc_to_view_z(ndc_space_coords.z); | ||
|
|
||
| // Flip the Y co-ordinate origin from the bottom to the top. | ||
| ndc_space_coords.y = -ndc_space_coords.y; | ||
|
|
||
| // Once in NDC space, we can discard the z element and map x/y to the viewport rect | ||
| let viewport_position = | ||
| (ndc_space_coords.truncate() + Vec2::ONE) / 2.0 * target_rect.size() + target_rect.min; | ||
| Ok(viewport_position.extend(depth)) | ||
| let result = self.world_to_viewport_core(camera_transform, world_position)?; | ||
| let depth = -self.depth_ndc_to_view_z(result.1); | ||
| Ok(result.0.extend(depth)) | ||
| } | ||
|
|
||
| /// Returns a ray originating from the camera, that passes through everything beyond `viewport_position`. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
truncate()when creatingviewport_positionmutatendc_space_coordsat all, specifically the z-component? if it does, the value for z might not be what is expected here, and it might be safer for this function to returndepth(sincedepthin the previous logic seemed to be created beforeviewport_position)(New here so take this with a grain of salt!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The z component’s value isn’t changed, but I understand why it’s confusing.
The truncate() function consumes the entire ndc_space_coords struct. The reason it still works is due to Rust’s Non-Lexicographical Lifetimes (NLL). The compiler is smart enough to recognize that even though the whole struct is moved, the z field was never actually needed by truncate(), so it remains valid for the final Ok return.
However, I completely agree with your sentiment. Relying on this advanced compiler behavior is not ideal practice. It makes the code less clear and more fragile.