Skip to content

Commit

Permalink
Handle undefined projection matrix.
Browse files Browse the repository at this point in the history
  • Loading branch information
tormeh committed Sep 18, 2023
1 parent ab37ed9 commit 74cc340
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct RenderTargetInfo {
/// Holds internally computed [`Camera`] values.
#[derive(Default, Debug, Clone)]
pub struct ComputedCameraValues {
projection_matrix: Mat4,
projection_matrix: Option<Mat4>,
target_info: Option<RenderTargetInfo>,
// position and size of the `Viewport`
old_viewport_size: Option<UVec2>,
Expand Down Expand Up @@ -230,17 +230,24 @@ impl Camera {
/// The projection matrix computed using this camera's [`CameraProjection`].
#[inline]
pub fn projection_matrix(&self) -> Mat4 {
self.computed.projection_matrix
self.computed.projection_matrix.unwrap_or_default()
}

#[inline]
fn finite_projection_matrix(&self) -> Result<Mat4, BadProjectionMatrixError> {
let projection_matrix = self.computed.projection_matrix;
let projection_matrix = self
.computed
.projection_matrix
.ok_or(BadProjectionMatrixError::ProjectionMatrixUndefinedError)?;
match projection_matrix.is_finite() {
false => Err(BadProjectionMatrixError(projection_matrix)),
false => Err(BadProjectionMatrixError::BadProjectionMatrixError(
projection_matrix,
)),
true => {
if projection_matrix == Mat4::ZERO {
Err(BadProjectionMatrixError(projection_matrix))
Err(BadProjectionMatrixError::BadProjectionMatrixError(
projection_matrix,
))
} else {
Ok(projection_matrix)
}
Expand Down Expand Up @@ -478,7 +485,10 @@ pub enum WorldToViewportError {
}

#[derive(Debug)]
pub struct BadProjectionMatrixError(Mat4);
pub enum BadProjectionMatrixError {
BadProjectionMatrixError(Mat4),
ProjectionMatrixUndefinedError,
}

#[derive(Debug)]
pub struct CameraTransformNotFiniteError(Mat4);
Expand Down Expand Up @@ -735,7 +745,8 @@ pub fn camera_system<T: CameraProjection + Component>(
);
if let Ok(size) = camera.logical_viewport_size() {
camera_projection.update(size.x, size.y);
camera.computed.projection_matrix = camera_projection.get_projection_matrix();
camera.computed.projection_matrix =
Some(camera_projection.get_projection_matrix());
}
}
}
Expand Down

0 comments on commit 74cc340

Please sign in to comment.