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

Code before:
        let world_near_plane = self
            .ndc_to_world(camera_transform, ndc.extend(1.))
            .ok_or(ViewportConversionError::InvalidData)?;

        Ok(world_near_plane.truncate())
    }

    /// Given a point in world space, use the camera's viewport to compute the Normalized Device Coordinates of the point.
    ///
    /// When the point is within the viewport the values returned will be between -1.0 (bottom left) and 1.0 (top right)
    /// on the X and Y axes, and between 0.0 (far) and 1.0 (near) on the Z axis.
    /// To get the coordinates in the render target's viewport dimensions, 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 `camera_transform` contains `NAN` and the `glam_assert` feature is enabled.
    pub fn world_to_ndc<V: Into<Vec3A> + From<Vec3A>>(
        &self,
        camera_transform: &GlobalTransform,
        world_point: V,
    ) -> Option<V> {
        // Build a transformation matrix to convert from world space to NDC using camera data
        let view_point = camera_transform
            .affine()
            .inverse()
            .transform_point3a(world_point.into());
        let ndc_point = self.computed.clip_from_view.project_point3a(view_point);

        (!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(&self, camera_transform: &GlobalTransform, ndc: Vec3) -> Option<Vec3> {
        // Build a transformation matrix to convert from NDC to world space using camera data

Review comment (suggestion):
```suggestion
        // Convert from world space to NDC using camera data
```

The code doesn't really build a matrix anymore.

Code after:
        let world_near_plane = self
            .ndc_to_world(camera_transform, ndc.extend(1.))
            .ok_or(ViewportConversionError::InvalidData)?;

        Ok(world_near_plane.truncate())
    }

    /// Given a point in world space, use the camera's viewport to compute the Normalized Device Coordinates of the point.
    ///
    /// When the point is within the viewport the values returned will be between -1.0 (bottom left) and 1.0 (top right)
    /// on the X and Y axes, and between 0.0 (far) and 1.0 (near) on the Z axis.
    /// To get the coordinates in the render target's viewport dimensions, 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 `camera_transform` contains `NAN` and the `glam_assert` feature is enabled.
    pub fn world_to_ndc<V: Into<Vec3A> + From<Vec3A>>(
        &self,
        camera_transform: &GlobalTransform,
        world_point: V,
    ) -> Option<V> {
        let view_from_world = camera_transform.affine().inverse();
        let view_point = view_from_world.transform_point3a(world_point.into());
        let ndc_point = self.computed.clip_from_view.project_point3a(view_point);

        (!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(&self, camera_transform: &GlobalTransform, ndc: Vec3) -> Option<Vec3> {
        // Build a transformation matrix to convert from NDC to world space using camera data
        let ndc_to_world = camera_transform.to_matrix() * self.computed.clip_from_view.inverse();

        let world_space_coords = ndc_to_world.project_point3(ndc);