-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Merged by Bors] - Add ViewRangefinder3d to reduce boilerplate when enqueuing standard 3D PhaseItems. #5014
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5bc615f
Add ViewRangefinder3d and update distance calculations to use it.
komadori 738d487
Add unit test for ViewRangefinder3d.
komadori d2a1d25
Improve documentation.
komadori 7df30aa
Fix clippy::doc-markdown lint.
komadori 40a84e5
Merge branch 'main' of github.com:komadori/bevy into view-rangefinder
komadori d2ff013
Fix new PhaseItem::sort() impls to match SortKey behaviour in this PR.
komadori beff79e
Merge branch 'main' of github.com:komadori/bevy into view-rangefinder
komadori 9b0a02d
Add inline annotation to distance() function.
komadori f687c3d
Add rangefinder3d() function to ExtractedView.
komadori 906790f
Remove accidently duplicated file.
komadori 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use bevy_math::{Mat4, Vec4}; | ||
|
||
/// A distance calculator for the draw order of [`PhaseItem`](crate::render_phase::PhaseItem)s. | ||
pub struct ViewRangefinder3d { | ||
komadori marked this conversation as resolved.
Show resolved
Hide resolved
|
||
inverse_view_row_2: Vec4, | ||
} | ||
|
||
impl ViewRangefinder3d { | ||
/// Creates a 3D rangefinder for a view matrix | ||
pub fn from_view_matrix(view_matrix: &Mat4) -> ViewRangefinder3d { | ||
let inverse_view_matrix = view_matrix.inverse(); | ||
ViewRangefinder3d { | ||
inverse_view_row_2: inverse_view_matrix.row(2), | ||
} | ||
} | ||
|
||
/// Calculates the distance, or view-space Z value, for a transform | ||
#[inline] | ||
pub fn distance(&self, transform: &Mat4) -> f32 { | ||
komadori marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// NOTE: row 2 of the inverse view matrix dotted with column 3 of the model matrix | ||
// gives the z component of translation of the mesh in view-space | ||
self.inverse_view_row_2.dot(transform.col(3)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::ViewRangefinder3d; | ||
use bevy_math::{Mat4, Vec3}; | ||
|
||
#[test] | ||
fn distance() { | ||
let view_matrix = Mat4::from_translation(Vec3::new(0.0, 0.0, -1.0)); | ||
let rangefinder = ViewRangefinder3d::from_view_matrix(&view_matrix); | ||
assert_eq!(rangefinder.distance(&Mat4::IDENTITY), 1.0); | ||
assert_eq!( | ||
rangefinder.distance(&Mat4::from_translation(Vec3::new(0.0, 0.0, 1.0))), | ||
2.0 | ||
); | ||
} | ||
} |
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
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
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
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.