#Working with Viewports

3 messages · Page 1 of 1 (latest)

modern falcon
#

I don't quite get how can I create what I'm trying to do with several cameras and Viewports.

I would like to render a backdrop and some static sprite on three different slices of the window. I tried creating three cameras and setting their viewports to target the primary window and then offset their positions, something like this:

let viewport_size = UVec2::new(window_w, window_h / 3);

commands.spawn(Camera2dBundle {
        camera: Camera {
            target: bevy::render::camera::RenderTarget::Window(bevy::window::WindowRef::Primary),
            viewport: Some(Viewport {
                physical_position: UVec2::new(0, 0),
                physical_size: viewport_size,
                ..default()
            }),
            order: -1,
            ..default()
        },
        ..default()
    });
    commands.spawn(Camera2dBundle {
        camera: Camera {
            target: bevy::render::camera::RenderTarget::Window(bevy::window::WindowRef::Primary),
            viewport: Some(Viewport {
                physical_position: UVec2::new(0, viewport_size.y),
                physical_size: viewport_size,
                ..default()
            }),
            order: -2,
            ..default()
        },
        ..default()
    });
    commands.spawn(Camera2dBundle {
        camera: Camera {
            target: bevy::render::camera::RenderTarget::Window(bevy::window::WindowRef::Primary),
            viewport: Some(Viewport {
                physical_position: UVec2::new(0, viewport_size.y * 2),
                physical_size: viewport_size,
                ..default()
            }),
            order: -3,
            ..default()
        },
        ..default()
    });

tried tweaking the order and changing the viewport positions but I can't seem to make it work. Any ideas?

#

Something I forgot to mention is I want these sprites to move up and down without overlapping the other window sections, that's why I thought of this approach. What I'm getting is a single slice of camera at either the bottom or the top of the window, depending on how I sort their order.

ancient bronze
#

@modern falcon
I think you need to put the ClearColorConfig of all camera except the first one to render to ClearColorConfig::None
That's what this example of bevy_hanabi seem to be doing at least (it's in 3D but ClearColorConfig exists on Camera2d too): https://github.com/djeedai/bevy_hanabi/blob/main/examples/multicam.rs

GitHub

🎆 Hanabi — a GPU particle system plugin for the Bevy game engine. - bevy_hanabi/examples/multicam.rs at main · djeedai/bevy_hanabi