bevy/examples/window/multiple_windows.rs
Jakob Hellermann bf6de89622 use marker components for cameras instead of name strings (#3635)
**Problem**
- whenever you want more than one of the builtin cameras (for example multiple windows, split screen, portals), you need to add a render graph node that executes the correct sub graph, extract the camera into the render world and add the correct `RenderPhase<T>` components
- querying for the 3d camera is annoying because you need to compare the camera's name to e.g. `CameraPlugin::CAMERA_3d`

**Solution**
- Introduce the marker types `Camera3d`, `Camera2d` and `CameraUi`
-> `Query<&mut Transform, With<Camera3d>>` works
- `PerspectiveCameraBundle::new_3d()` and `PerspectiveCameraBundle::<Camera3d>::default()` contain the `Camera3d` marker
- `OrthographicCameraBundle::new_3d()` has `Camera3d`, `OrthographicCameraBundle::new_2d()` has `Camera2d`
- remove `ActiveCameras`, `ExtractedCameraNames`
- run 2d, 3d and ui passes for every camera of their respective marker
-> no custom setup for multiple windows example needed

**Open questions**
- do we need a replacement for `ActiveCameras`? What about a component `ActiveCamera { is_active: bool }` similar to `Visibility`?

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-03-12 00:41:06 +00:00

55 lines
1.7 KiB
Rust

use bevy::{
prelude::*,
render::camera::RenderTarget,
window::{CreateWindow, PresentMode, WindowId},
};
/// This example creates a second window and draws a mesh from two different cameras, one in each window
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_startup_system(create_new_window)
.run();
}
fn create_new_window(mut create_window_events: EventWriter<CreateWindow>, mut commands: Commands) {
let window_id = WindowId::new();
// sends out a "CreateWindow" event, which will be received by the windowing backend
create_window_events.send(CreateWindow {
id: window_id,
descriptor: WindowDescriptor {
width: 800.,
height: 600.,
present_mode: PresentMode::Immediate,
title: "Second window".to_string(),
..default()
},
});
// second window camera
commands.spawn_bundle(PerspectiveCameraBundle {
camera: Camera {
target: RenderTarget::Window(window_id),
..default()
},
transform: Transform::from_xyz(6.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// add entities to the world
commands.spawn_scene(asset_server.load("models/monkey/Monkey.gltf#Scene0"));
// light
commands.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(4.0, 5.0, 4.0),
..default()
});
// main camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 0.0, 6.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}