Repository: bevyengine/bevy
File: crates/bevy_camera/src/camera.rs (Rust)

Code before:

        (!ndc_point.is_nan()).then_some(ndc_point.into())
    }

    /// Given a position in Normalized Device Coordinates,
    /// use the camera's viewport to compute the world space position.
    ///
    /// When the position is within the viewport the values returned will be between -1.0 and 1.0 on the X and Y axes,
    /// and between 0.0 and 1.0 on the Z axis.
    /// To get the world space coordinates with the viewport position, you should use
    /// [`world_to_viewport`](Self::world_to_viewport).
    ///
    /// Returns `None` if the `camera_transform`, the `world_position`, or the projection matrix defined by
    /// [`Projection`](super::projection::Projection) contain `NAN`.
    ///
    /// # Panics
    ///
    /// Will panic if the projection matrix is invalid (has a determinant of 0) and `glam_assert` is enabled.
    pub fn ndc_to_world<V: Into<Vec3A> + From<Vec3A>>(
        &self,
        camera_transform: &GlobalTransform,
        ndc_point: V,
    ) -> Option<V> {
        let view_point = self
            .computed
            .clip_from_view
            .inverse()
            .project_point3a(ndc_point.into());
        let world_point = camera_transform.affine().transform_point3a(view_point);

        (!world_point.is_nan()).then_some(world_point.into())
    }

    /// Converts the depth in Normalized Device Coordinates
    /// to linear view z for perspective projections.
    ///
    /// Note: Depth values in front of the camera will be negative as -z is forward
    pub fn depth_ndc_to_view_z(&self, ndc_depth: f32) -> f32 {
        let near = self.clip_from_view().w_axis.z; // [3][2]
        -near / ndc_depth
    }

    /// Converts the depth in Normalized Device Coordinates
    /// to linear view z for orthographic projections.
    ///
    /// Note: Depth values in front of the camera will be negative as -z is forward
    pub fn depth_ndc_to_view_z_2d(&self, ndc_depth: f32) -> f32 {
        -(self.clip_from_view().w_axis.z - ndc_depth) / self.clip_from_view().z_axis.z
        //                       [3][2]                                         [2][2]
    }
}

Review comment (performance):
```suggestion
        // We multiply the point by `view_from_clip` and then `world_from_view` in sequence to avoid the precision loss
        // (and performance penalty) incurred by pre-composing an affine transform with a projective transform.
        let view_point = self
```

Code after:

        (!ndc_point.is_nan()).then_some(ndc_point.into())
    }

    /// Given a position in Normalized Device Coordinates,
    /// use the camera's viewport to compute the world space position.
    ///
    /// When the position is within the viewport the values returned will be between -1.0 and 1.0 on the X and Y axes,
    /// and between 0.0 and 1.0 on the Z axis.
    /// To get the world space coordinates with the viewport position, you should use
    /// [`world_to_viewport`](Self::world_to_viewport).
    ///
    /// Returns `None` if the `camera_transform`, the `world_position`, or the projection matrix defined by
    /// [`Projection`](super::projection::Projection) contain `NAN`.
    ///
    /// # Panics
    ///
    /// Will panic if the projection matrix is invalid (has a determinant of 0) and `glam_assert` is enabled.
    pub fn ndc_to_world<V: Into<Vec3A> + From<Vec3A>>(
        &self,
        camera_transform: &GlobalTransform,
        ndc_point: V,
    ) -> Option<V> {
        // We multiply the point by `view_from_clip` and then `world_from_view` in sequence to avoid the precision loss
        // (and performance penalty) incurred by pre-composing an affine transform with a projective transform.
        let view_point = self
            .computed
            .clip_from_view
            .inverse()
            .project_point3a(ndc_point.into());
        let world_point = camera_transform.affine().transform_point3a(view_point);

        (!world_point.is_nan()).then_some(world_point.into())
    }

    /// Converts the depth in Normalized Device Coordinates
    /// to linear view z for perspective projections.
    ///
    /// Note: Depth values in front of the camera will be negative as -z is forward
    pub fn depth_ndc_to_view_z(&self, ndc_depth: f32) -> f32 {
        let near = self.clip_from_view().w_axis.z; // [3][2]
        -near / ndc_depth
    }

    /// Converts the depth in Normalized Device Coordinates
    /// to linear view z for orthographic projections.
    ///
    /// Note: Depth values in front of the camera will be negative as -z is forward
    pub fn depth_ndc_to_view_z_2d(&self, ndc_depth: f32) -> f32 {
        -(self.clip_from_view().w_axis.z - ndc_depth) / self.clip_from_view().z_axis.z
        //                       [3][2]                                         [2][2]