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

Return a Result from Camera::viewport_to_world #9770

Closed
wants to merge 4 commits into from

Conversation

tormeh
Copy link
Contributor

@tormeh tormeh commented Sep 11, 2023

Objective

This is a follow-up to 8841

The objective is to create a better UX when something goes wrong in Camera::viewport_to_world and related methods.

Solution

Move from using Option to detailed Result variants. The user no longer has to guess what the error was.


Changelog

Improve errors related to functions converting coordinates between camera, viewport, and world.

Migration Guide

Replace code that expects Option with code that expects Result. An easy way is to append a .ok() to the returned Result to transform it into an Option.

Change references to Camera::projection_matrix to Camera::projection_matrix_unchecked.

@tormeh

This comment was marked as outdated.

@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 Sep 11, 2023
@alice-i-cecile
Copy link
Member

Good stuff: I really like the direction that this is going.

Copy link
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

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

It's a bit unfortunate that it makes the code more verbose but I think these changes make sense.

I wonder if the new is_nan checks can affect performance though. Probably not, but I'm still a bit curious.

ndc: Vec3,
) -> Result<Vec3, NdcToWorldError> {
let cam_trans_mat = camera_transform.compute_matrix();
if cam_trans_mat.is_nan() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this maybe check for is_finite instead of is_nan? I'm not sure if infinite makes sense here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes me think; Do we want infinite anywhere? Can/Should I use is_finite everywhere instead of is_nan?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have done some effort to judge when to do this, but unfortunately I've all but forgotten all the graphics-related knowledge I acquired for the first PR and I'm to lazy to go back to the YouTube videos. Hope I got it right 😅

crates/bevy_render/src/camera/camera.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/camera/camera.rs Show resolved Hide resolved
@tormeh
Copy link
Contributor Author

tormeh commented Sep 12, 2023

It's a bit unfortunate that it makes the code more verbose but I think these changes make sense.

I wonder if the new is_nan checks can affect performance though. Probably not, but I'm still a bit curious.

My limited knowledge says that since the numbers being checked are probably in a fast CPU cache it should be pretty fast, but you never know.

EDIT: Just realized that this doesn't help if the code to check for nulls is not also in the CPU cache... I guess this would vary based on usage.

@alice-i-cecile alice-i-cecile changed the title Tormod/none to result Return a Result from Camera::viewport_to_world Sep 14, 2023
@tormeh tormeh force-pushed the tormod/none-to-result branch 4 times, most recently from c809f75 to f2aba94 Compare September 18, 2023 15:04
@tormeh tormeh force-pushed the tormod/none-to-result branch from 68f78a4 to f0196cc Compare September 18, 2023 17:03
@tormeh tormeh marked this pull request as ready for review September 18, 2023 17:32
@tormeh
Copy link
Contributor Author

tormeh commented Sep 18, 2023

@Selene-Amanita Fixed some of the stuff you remarked on in #8841

@tormeh
Copy link
Contributor Author

tormeh commented Sep 18, 2023

I've tried to apply a bit of common sense as to where infinity should be allowed and not (almost nowhere), but I'm honestly fumbling in the dark, so please give that a quick scan

@Selene-Amanita Selene-Amanita self-requested a review September 18, 2023 17:53
@alice-i-cecile alice-i-cecile added the C-Code-Quality A section of code that is hard to understand or change label Sep 18, 2023
tormeh added a commit to tormeh/bevy that referenced this pull request Sep 18, 2023
tormeh added a commit to tormeh/bevy that referenced this pull request Sep 18, 2023
Copy link
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.

Alright, the code quality is up to snuff now :) This is a much nicer and more informative approach to error handling here, but I need to defer to people with more rendering expertise to know if the edge cases are all handled correctly.

@tormeh
Copy link
Contributor Author

tormeh commented Sep 18, 2023

Alright, the code quality is up to snuff now :) This is a much nicer and more informative approach to error handling here, but I need to defer to people with more rendering expertise to know if the edge cases are all handled correctly.

Same lol

@tormeh tormeh force-pushed the tormod/none-to-result branch from 3817ed0 to 3eedaff Compare September 26, 2023 20:51
tormeh added a commit to tormeh/bevy that referenced this pull request Sep 26, 2023
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
@tormeh tormeh force-pushed the tormod/none-to-result branch from ad4acd5 to 78afe0a Compare August 19, 2024 22:52
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
@tormeh tormeh force-pushed the tormod/none-to-result branch from 78afe0a to c5fd79a Compare August 19, 2024 23:05
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
@tormeh tormeh force-pushed the tormod/none-to-result branch from c5fd79a to 43ffaa2 Compare August 19, 2024 23:06
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
@tormeh tormeh force-pushed the tormod/none-to-result branch from 43ffaa2 to 40563fc Compare August 19, 2024 23:10
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
tormeh added a commit to tormeh/bevy that referenced this pull request Aug 19, 2024
@tormeh tormeh force-pushed the tormod/none-to-result branch from 40563fc to 3d90c02 Compare August 19, 2024 23:27
@tormeh
Copy link
Contributor Author

tormeh commented Aug 19, 2024

@tormeh Thanks for picking this back up!

Appreciate it. Have never rebased on top of an entire year's worth of changes before 😅

@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 M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Aug 19, 2024
@alice-i-cecile
Copy link
Member

Bah, still listing merge conflicts 🤔 Ping me when this is ready please!

@tormeh
Copy link
Contributor Author

tormeh commented Aug 19, 2024

It's getting late here, so the rest will have to be done later. I've gotten to June.

@alice-i-cecile
Copy link
Member

Oof :( Feel free to remake the PR if it's easier: rebasing can be quite painful.

@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 Aug 26, 2024
@alice-i-cecile
Copy link
Member

Adopted in #14931.

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-Code-Quality A section of code that is hard to understand or change 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-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

7 participants