Skip to content

Commit

Permalink
Deduplicate camera transform matrix check.
Browse files Browse the repository at this point in the history
  • Loading branch information
tormeh committed Sep 18, 2023
1 parent c11cf62 commit f2aba94
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ impl Camera {
Ok(projection_matrix)
}

#[inline]
fn finite_camera_transform_matrix(
camera_transform: &GlobalTransform,
) -> Result<Mat4, CameraTransformNotFiniteError> {
let camera_transform_matrix = camera_transform.compute_matrix();
if !camera_transform_matrix.is_finite() {
return Err(CameraTransformNotFiniteError(camera_transform_matrix));
}
Ok(camera_transform_matrix)
}

/// 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
Expand Down Expand Up @@ -298,13 +309,8 @@ impl Camera {
viewport_position.y = target_size.y - viewport_position.y;
let ndc = viewport_position * 2. / target_size - Vec2::ONE;

let camera_transform_matrix = camera_transform.compute_matrix();
if !camera_transform_matrix.is_finite() {
return Err(ViewportToWorldError::CameraTransformNotFiniteError(
camera_transform_matrix,
));
}
// TODO: Do some code deduplication of the above snippet..
let camera_transform_matrix = Self::finite_camera_transform_matrix(camera_transform)
.map_err(ViewportToWorldError::CameraTransformNotFiniteError)?;
let projection_matrix = self
.finite_projection_matrix()
.map_err(ViewportToWorldError::ProjectionMatrixNotFiniteError)?;
Expand Down Expand Up @@ -370,12 +376,8 @@ impl Camera {
camera_transform: &GlobalTransform,
world_position: Vec3,
) -> Result<Vec3, WorldToNdcError> {
let camera_transform_matrix = camera_transform.compute_matrix();
if !camera_transform_matrix.is_finite() {
return Err(WorldToNdcError::CameraTransformNotFiniteError(
camera_transform_matrix,
));
}
let camera_transform_matrix = Self::finite_camera_transform_matrix(camera_transform)
.map_err(WorldToNdcError::CameraTransformNotFiniteError)?;

let projection_matrix = self
.finite_projection_matrix()
Expand Down Expand Up @@ -408,12 +410,8 @@ impl Camera {
camera_transform: &GlobalTransform,
ndc: Vec3,
) -> Result<Vec3, NdcToWorldError> {
let camera_transform_matrix = camera_transform.compute_matrix();
if !camera_transform_matrix.is_finite() {
return Err(NdcToWorldError::CameraTransformNotFiniteError(
camera_transform_matrix,
));
}
let camera_transform_matrix = Self::finite_camera_transform_matrix(camera_transform)
.map_err(NdcToWorldError::CameraTransformNotFiniteError)?;

let projection_matrix = self
.finite_projection_matrix()
Expand All @@ -436,14 +434,14 @@ impl Camera {
#[derive(Debug)]
pub enum NdcToWorldError {
WorldSpaceCoordsNotFiniteError(Vec3),
CameraTransformNotFiniteError(Mat4),
CameraTransformNotFiniteError(CameraTransformNotFiniteError),
ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError),
}

#[derive(Debug)]
pub enum WorldToNdcError {
NdcSpaceCoordsNotFiniteError(Vec3),
CameraTransformNotFiniteError(Mat4),
CameraTransformNotFiniteError(CameraTransformNotFiniteError),
ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError),
}

Expand All @@ -457,7 +455,7 @@ pub struct LogicalViewportSizeError {
#[derive(Debug)]
pub enum ViewportToWorldError {
LogicalViewportSizeError(LogicalViewportSizeError),
CameraTransformNotFiniteError(Mat4),
CameraTransformNotFiniteError(CameraTransformNotFiniteError),
ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError),
WorldNearPlaneNotFiniteError(Vec3),
WorldFarPlaneNanError(Vec3), //Presumably this being infinite is sometimes ok?
Expand All @@ -479,6 +477,9 @@ pub enum WorldToViewportError {
#[derive(Debug)]
pub struct ProjectionMatrixNotFiniteError(Mat4);

#[derive(Debug)]
pub struct CameraTransformNotFiniteError(Mat4);

/// Control how this camera outputs once rendering is completed.
#[derive(Debug, Clone, Copy)]
pub enum CameraOutputMode {
Expand Down

0 comments on commit f2aba94

Please sign in to comment.