//! Demonstrates usage with multiple viewports use bevy::prelude::*; use bevy::render::camera::Viewport; use bevy::window::WindowResized; use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(PanOrbitCameraPlugin) .add_systems(Startup, setup) .add_systems(Update, set_camera_viewports) .run(); } fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { // Ground commands.spawn(( Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))), MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))), )); // Cube commands.spawn(( Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))), MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))), Transform::from_xyz(0.0, 0.5, 0.0), )); // Light commands.spawn(( PointLight { shadows_enabled: true, ..default() }, Transform::from_xyz(4.0, 8.0, 4.0), )); // Main Camera commands.spawn(( Transform::from_translation(Vec3::new(0.0, 0.5, 5.0)), PanOrbitCamera::default(), )); // Minimap Camera commands.spawn(( Transform::from_translation(Vec3::new(1.0, 1.5, 4.0)), Camera { // Renders the minimap camera after the main camera, so it is rendered on top order: 1, // Don't clear on the second camera because the first camera already cleared the window clear_color: ClearColorConfig::None, ..default() }, PanOrbitCamera::default(), MinimapCamera, )); } #[derive(Component)] struct MinimapCamera; fn set_camera_viewports( windows: Query<&Window>, mut resize_events: EventReader, mut right_camera: Query<&mut Camera, With>, ) { for resize_event in resize_events.read() { let window = windows.get(resize_event.window).unwrap(); let mut right_camera = right_camera.single_mut(); let size = window.resolution.physical_width() / 5; right_camera.viewport = Some(Viewport { physical_position: UVec2::new(window.resolution.physical_width() - size, 0), physical_size: UVec2::new(size, size), ..default() }); } }