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

Code before:
    use bevy_math::{Vec2, Vec3};
    use bevy_transform::components::GlobalTransform;

    use crate::{
        Camera, OrthographicProjection, PerspectiveProjection, Projection, RenderTargetInfo,
        Viewport,
    };

    fn make_camera(mut projection: Projection, physical_size: Vec2) -> Camera {
        let viewport = Viewport {
            physical_size: physical_size.as_uvec2(),
            ..Default::default()
        };
        let mut camera = Camera {
            viewport: Some(viewport.clone()),
            ..Default::default()
        };
        camera.computed.target_info = Some(RenderTargetInfo {
            physical_size: viewport.physical_size,
            scale_factor: 1.0,
        });
        projection.update(
            viewport.physical_size.x as f32,
            viewport.physical_size.y as f32,
        );
        camera.computed.clip_from_view = match &camera.sub_camera_view {
            Some(sub_view) => projection.get_clip_from_view_for_sub(sub_view),
            None => projection.get_clip_from_view(),
        };
        camera
    }

    #[test]
    fn viewport_to_world_orthographic_3d_returns_forward() {
        let transform = GlobalTransform::default();
        let size = Vec2::new(1600.0, 900.0);
        let camera = make_camera(
            Projection::Orthographic(OrthographicProjection::default_3d()),
            size,
        );
        let ray = camera.viewport_to_world(&transform, Vec2::ZERO).unwrap();
        assert_eq!(ray.direction, transform.forward());
        assert!(ray
            .origin
            .abs_diff_eq(Vec3::new(-size.x * 0.5, size.y * 0.5, 0.0), 1e-4));
        let ray = camera.viewport_to_world(&transform, size).unwrap();
        assert_eq!(ray.direction, transform.forward());
        assert!(ray
            .origin
            .abs_diff_eq(Vec3::new(size.x * 0.5, -size.y * 0.5, 0.0), 1e-4));
    }

Review comment (suggestion):
The `match` is unnecessary. `camera.sub_camera_view` is always `None`. Unless we're worried that its default value might change in the future.

Code after:
    use bevy_math::{Vec2, Vec3};
    use bevy_transform::components::GlobalTransform;

    use crate::{
        Camera, OrthographicProjection, PerspectiveProjection, Projection, RenderTargetInfo,
        Viewport,
    };

    fn make_camera(mut projection: Projection, physical_size: Vec2) -> Camera {
        let viewport = Viewport {
            physical_size: physical_size.as_uvec2(),
            ..Default::default()
        };
        let mut camera = Camera {
            viewport: Some(viewport.clone()),
            ..Default::default()
        };
        camera.computed.target_info = Some(RenderTargetInfo {
            physical_size: viewport.physical_size,
            scale_factor: 1.0,
        });
        projection.update(
            viewport.physical_size.x as f32,
            viewport.physical_size.y as f32,
        );
        camera.computed.clip_from_view = projection.get_clip_from_view();
        camera
    }

    #[test]
    fn viewport_to_world_orthographic_3d_returns_forward() {
        let transform = GlobalTransform::default();
        let size = Vec2::new(1600.0, 900.0);
        let camera = make_camera(
            Projection::Orthographic(OrthographicProjection::default_3d()),
            size,
        );
        let ray = camera.viewport_to_world(&transform, Vec2::ZERO).unwrap();
        assert_eq!(ray.direction, transform.forward());
        assert!(ray
            .origin
            .abs_diff_eq(Vec3::new(-size.x * 0.5, size.y * 0.5, 0.0), 1e-4));
        let ray = camera.viewport_to_world(&transform, size).unwrap();
        assert_eq!(ray.direction, transform.forward());
        assert!(ray
            .origin
            .abs_diff_eq(Vec3::new(size.x * 0.5, -size.y * 0.5, 0.0), 1e-4));
    }

    #[test]
    fn viewport_to_world_orthographic_2d_returns_forward() {