bevy/crates
m-edlund c323db02e0
Add sub_camera_view, enabling sheared projection (#15537)
# Objective

- This PR fixes #12488

## Solution

- This PR adds a new property to `Camera` that emulates the
functionality of the
[setViewOffset()](https://threejs.org/docs/#api/en/cameras/PerspectiveCamera.setViewOffset)
API in three.js.
- When set, the perspective and orthographic projections will restrict
the visible area of the camera to a part of the view frustum defined by
`offset` and `size`.

## Testing

- In the new `camera_sub_view` example, a fixed, moving and control sub
view is created for both perspective and orthographic projection
- Run the example with `cargo run --example camera_sub_view`
- The code can be tested by adding a `SubCameraView` to a camera

---

## Showcase


![image](https://github.com/user-attachments/assets/75ac45fc-d75d-4664-8ef6-ff7865297c25)

- Left Half: Perspective Projection
- Right Half: Orthographic Projection
- Small boxes in order:
  - Sub view of the left half of the full image
- Sub view moving from the top left to the bottom right of the full
image
  - Sub view of the full image (acting as a control)
- Large box: No sub view

<details>
  <summary>Shortened camera setup of `camera_sub_view` example</summary>

```rust
    // Main perspective Camera
    commands.spawn(Camera3dBundle {
        transform,
        ..default()
    });

    // Perspective camera left half
    commands.spawn(Camera3dBundle {
        camera: Camera {
            sub_camera_view: Some(SubCameraView {
                // Set the sub view camera to the left half of the full image
                full_size: uvec2(500, 500),
                offset: ivec2(0, 0),
                size: uvec2(250, 500),
            }),
            order: 1,
            ..default()
        },
        transform,
        ..default()
    });

    // Perspective camera moving
    commands.spawn((
        Camera3dBundle {
            camera: Camera {
                sub_camera_view: Some(SubCameraView {
                    // Set the sub view camera to a fifth of the full view and
                    // move it in another system
                    full_size: uvec2(500, 500),
                    offset: ivec2(0, 0),
                    size: uvec2(100, 100),
                }),
                order: 2,
                ..default()
            },
            transform,
            ..default()
        },
        MovingCameraMarker,
    ));

    // Perspective camera control
    commands.spawn(Camera3dBundle {
        camera: Camera {
            sub_camera_view: Some(SubCameraView {
                // Set the sub view to the full image, to ensure that it matches
                // the projection without sub view
                full_size: uvec2(450, 450),
                offset: ivec2(0, 0),
                size: uvec2(450, 450),
            }),
            order: 3,
            ..default()
        },
        transform,
        ..default()
    });

    // Main orthographic camera
    commands.spawn(Camera3dBundle {
        projection: OrthographicProjection {
          ...
        }
        .into(),
        camera: Camera {
            order: 4,
            ..default()
        },
        transform,
        ..default()
    });

    // Orthographic camera left half
    commands.spawn(Camera3dBundle {
        projection: OrthographicProjection {
          ...
        }
        .into(),
        camera: Camera {
            sub_camera_view: Some(SubCameraView {
                // Set the sub view camera to the left half of the full image
                full_size: uvec2(500, 500),
                offset: ivec2(0, 0),
                size: uvec2(250, 500),
            }),
            order: 5,
            ..default()
        },
        transform,
        ..default()
    });

    // Orthographic camera moving
    commands.spawn((
        Camera3dBundle {
            projection: OrthographicProjection {
              ...
            }
            .into(),
            camera: Camera {
                sub_camera_view: Some(SubCameraView {
                    // Set the sub view camera to a fifth of the full view and
                    // move it in another system
                    full_size: uvec2(500, 500),
                    offset: ivec2(0, 0),
                    size: uvec2(100, 100),
                }),
                order: 6,
                ..default()
            },
            transform,
            ..default()
        },
        MovingCameraMarker,
    ));

    // Orthographic camera control
    commands.spawn(Camera3dBundle {
        projection: OrthographicProjection {
          ...
        }
        .into(),
        camera: Camera {
            sub_camera_view: Some(SubCameraView {
                // Set the sub view to the full image, to ensure that it matches
                // the projection without sub view
                full_size: uvec2(450, 450),
                offset: ivec2(0, 0),
                size: uvec2(450, 450),
            }),
            order: 7,
            ..default()
        },
        transform,
        ..default()
    });
```

</details>
2024-10-01 14:11:24 +00:00
..
bevy_a11y Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_animation Cleanup unneeded lifetimes in bevy_asset (#15546) 2024-09-30 21:54:59 +00:00
bevy_app Runtime required components (#15458) 2024-09-30 19:20:16 +00:00
bevy_asset Add directory related functions to AndroidAssetReader (#11495) 2024-10-01 12:23:24 +00:00
bevy_audio Cleanup unneeded lifetimes in bevy_asset (#15546) 2024-09-30 21:54:59 +00:00
bevy_color Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_core Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_core_pipeline The Cooler 'Retain Rendering World' (#15320) 2024-09-30 18:51:43 +00:00
bevy_derive Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_dev_tools Simplified ui_stack_system (#9889) 2024-09-30 18:43:57 +00:00
bevy_diagnostic Runtime required components (#15458) 2024-09-30 19:20:16 +00:00
bevy_dylib Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_ecs 15540 Make World::flush_commands private (#15553) 2024-09-30 22:07:09 +00:00
bevy_encase_derive Update `glam to 0.29, encase` to 0.10. (#15249) 2024-09-23 19:44:02 +00:00
bevy_gilrs Implement gamepads as entities (#12770) 2024-09-27 20:07:20 +00:00
bevy_gizmos The Cooler 'Retain Rendering World' (#15320) 2024-09-30 18:51:43 +00:00
bevy_gltf Migrate lights to required components (#15554) 2024-10-01 03:20:43 +00:00
bevy_hierarchy Add VisitEntities for generic and reflectable Entity iteration (#15425) 2024-09-30 17:32:03 +00:00
bevy_input Fix window spawning triggering ButtonInput<KeyCode>::just_pressed/just_released (#12372) 2024-09-30 18:24:36 +00:00
bevy_internal Split TextureAtlasSources out of TextureAtlasLayout and make TextureAtlasLayout serializable (#15344) 2024-09-30 17:11:56 +00:00
bevy_log Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_macro_utils Modify derive_label to support no_std environments (#15465) 2024-09-27 20:23:26 +00:00
bevy_math Curve-based animation (#15434) 2024-09-30 19:56:55 +00:00
bevy_mikktspace Add no_std support to bevy_mikktspace (#15528) 2024-09-30 18:17:03 +00:00
bevy_pbr Migrate lights to required components (#15554) 2024-10-01 03:20:43 +00:00
bevy_picking Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_ptr Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_reflect bevy_reflect: Generic parameter info (#15475) 2024-09-30 17:58:37 +00:00
bevy_remote Add content-type header to BRP HTTP responses (#15552) 2024-09-30 21:23:55 +00:00
bevy_render Add sub_camera_view, enabling sheared projection (#15537) 2024-10-01 14:11:24 +00:00
bevy_scene Cleanup unneeded lifetimes in bevy_asset (#15546) 2024-09-30 21:54:59 +00:00
bevy_sprite The Cooler 'Retain Rendering World' (#15320) 2024-09-30 18:51:43 +00:00
bevy_state Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_tasks bump async-channel to 2.3.0 (#15497) 2024-09-28 19:21:59 +00:00
bevy_text Cleanup unneeded lifetimes in bevy_asset (#15546) 2024-09-30 21:54:59 +00:00
bevy_time Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_transform Migrate visibility to required components (#15474) 2024-09-27 19:06:16 +00:00
bevy_ui The Cooler 'Retain Rendering World' (#15320) 2024-09-30 18:51:43 +00:00
bevy_utils Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_window Add VisitEntities for generic and reflectable Entity iteration (#15425) 2024-09-30 17:32:03 +00:00
bevy_winit Fix window spawning triggering ButtonInput<KeyCode>::just_pressed/just_released (#12372) 2024-09-30 18:24:36 +00:00