diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 25ce82195d4953..d2570156deaef3 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -235,6 +235,15 @@ impl Camera { self.computed.projection_matrix } + #[inline] + fn finite_projection_matrix(&self) -> Result { + let projection_matrix = self.computed.projection_matrix; + if !projection_matrix.is_finite() { + return Err(ProjectionMatrixNotFiniteError(projection_matrix)); + } + Ok(projection_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 @@ -295,13 +304,10 @@ impl Camera { camera_transform_matrix, )); } - // TODO: Do some code deduplication of these two snippets.. - let projection_matrix = self.computed.projection_matrix; - if !projection_matrix.is_finite() { - return Err(ViewportToWorldError::ProjectionMatrixNotFiniteError( - projection_matrix, - )); - } + // TODO: Do some code deduplication of the above snippet.. + let projection_matrix = self + .finite_projection_matrix() + .map_err(ViewportToWorldError::ProjectionMatrixNotFiniteError)?; let ndc_to_world = camera_transform_matrix * projection_matrix.inverse(); let world_near_plane = ndc_to_world.project_point3(ndc.extend(1.)); @@ -371,12 +377,9 @@ impl Camera { )); } - let projection_matrix = self.computed.projection_matrix; - if !projection_matrix.is_finite() { - return Err(WorldToNdcError::ProjectionMatrixNotFiniteError( - projection_matrix, - )); - } + let projection_matrix = self + .finite_projection_matrix() + .map_err(WorldToNdcError::ProjectionMatrixNotFiniteError)?; // Build a transformation matrix to convert from world space to NDC using camera data let world_to_ndc: Mat4 = projection_matrix * camera_transform_matrix.inverse(); @@ -412,12 +415,9 @@ impl Camera { )); } - let projection_matrix = self.computed.projection_matrix; - if !projection_matrix.is_finite() { - return Err(NdcToWorldError::ProjectionMatrixNotFiniteError( - projection_matrix, - )); - } + let projection_matrix = self + .finite_projection_matrix() + .map_err(NdcToWorldError::ProjectionMatrixNotFiniteError)?; // Build a transformation matrix to convert from NDC to world space using camera data let ndc_to_world = camera_transform_matrix * projection_matrix.inverse(); @@ -437,14 +437,14 @@ impl Camera { pub enum NdcToWorldError { WorldSpaceCoordsNotFiniteError(Vec3), CameraTransformNotFiniteError(Mat4), - ProjectionMatrixNotFiniteError(Mat4), + ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError), } #[derive(Debug)] pub enum WorldToNdcError { NdcSpaceCoordsNotFiniteError(Vec3), CameraTransformNotFiniteError(Mat4), - ProjectionMatrixNotFiniteError(Mat4), + ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError), } #[derive(Debug)] @@ -458,7 +458,7 @@ pub struct LogicalViewportSizeError { pub enum ViewportToWorldError { LogicalViewportSizeError(LogicalViewportSizeError), CameraTransformNotFiniteError(Mat4), - ProjectionMatrixNotFiniteError(Mat4), + ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError), WorldNearPlaneNotFiniteError(Vec3), WorldFarPlaneNanError(Vec3), //Presumably this being infinite is sometimes ok? } @@ -476,6 +476,9 @@ pub enum WorldToViewportError { NdcCoordsOutsideFrustumError(Vec3), } +#[derive(Debug)] +pub struct ProjectionMatrixNotFiniteError(Mat4); + /// Control how this camera outputs once rendering is completed. #[derive(Debug, Clone, Copy)] pub enum CameraOutputMode {