From f2aba94b39abbc6a076d14e644ef23c0464a7279 Mon Sep 17 00:00:00 2001 From: "Tormod G. Hellen" Date: Mon, 18 Sep 2023 17:01:37 +0200 Subject: [PATCH] Deduplicate camera transform matrix check. --- crates/bevy_render/src/camera/camera.rs | 45 +++++++++++++------------ 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index d2570156deaef3..add409a2ef1314 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -244,6 +244,17 @@ impl Camera { Ok(projection_matrix) } + #[inline] + fn finite_camera_transform_matrix( + camera_transform: &GlobalTransform, + ) -> Result { + 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 @@ -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)?; @@ -370,12 +376,8 @@ impl Camera { camera_transform: &GlobalTransform, world_position: Vec3, ) -> Result { - 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() @@ -408,12 +410,8 @@ impl Camera { camera_transform: &GlobalTransform, ndc: Vec3, ) -> Result { - 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() @@ -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), } @@ -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? @@ -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 {