Skip to content

Commit

Permalink
change Transparent3d rangefinder to euclidean distance
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Nov 16, 2024
1 parent 7fed4fa commit 4e81d47
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions crates/bevy_render/src/render_phase/rangefinder.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
use bevy_math::{Mat4, Vec3, Vec4};
use bevy_math::{Mat4, Vec3};

/// A distance calculator for the draw order of [`PhaseItem`](crate::render_phase::PhaseItem)s.
pub struct ViewRangefinder3d {
view_from_world_row_2: Vec4,
translation: Vec3,
}

impl ViewRangefinder3d {
/// Creates a 3D rangefinder for a view matrix.
pub fn from_world_from_view(world_from_view: &Mat4) -> ViewRangefinder3d {
let view_from_world = world_from_view.inverse();

ViewRangefinder3d {
view_from_world_row_2: view_from_world.row(2),
translation: world_from_view.col(3).truncate(),
}
}

/// Calculates the distance, or view-space `Z` value, for the given `translation`.
/// Calculates the distance for the given `translation`.
#[inline]
pub fn distance_translation(&self, translation: &Vec3) -> f32 {
// NOTE: row 2 of the inverse view matrix dotted with the translation from the model matrix
// gives the z component of translation of the mesh in view-space
self.view_from_world_row_2.dot(translation.extend(1.0))
- (*translation - self.translation).length_squared()
}

/// Calculates the distance, or view-space `Z` value, for the given `transform`.
/// Calculates the distance for the given `transform`.
#[inline]
pub fn distance(&self, transform: &Mat4) -> f32 {
// 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.view_from_world_row_2.dot(transform.col(3))
(transform.col(3).truncate() - self.translation).length_squared()
}
}

Expand Down

0 comments on commit 4e81d47

Please sign in to comment.