From ed9d45fae57e8f01d13ca3a6c12a5f3005b6c610 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 18 Dec 2021 19:38:05 +0000 Subject: [PATCH] Add an example 'showcasing' using multiple windows (#3367) # Objective - The multiple windows example which was viciously murdered in #3175. - cart asked me to ## Solution - Rework the example to work on pipelined-rendering, based on the work from #2898 --- Cargo.toml | 6 +- crates/bevy_render/src/camera/mod.rs | 2 +- examples/README.md | 1 + examples/window/multiple_windows.rs | 111 +++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 examples/window/multiple_windows.rs diff --git a/Cargo.toml b/Cargo.toml index d159b08f21..5918458d2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ default = [ "hdr", "mp3", "x11", - "filesystem_watcher" + "filesystem_watcher", ] # Force dynamic linking, which improves iterative compile times @@ -441,6 +441,10 @@ path = "examples/ui/ui.rs" name = "clear_color" path = "examples/window/clear_color.rs" +[[example]] +name = "multiple_windows" +path = "examples/window/multiple_windows.rs" + [[example]] name = "scale_factor_override" path = "examples/window/scale_factor_override.rs" diff --git a/crates/bevy_render/src/camera/mod.rs b/crates/bevy_render/src/camera/mod.rs index 8629d8022d..ef1e3d19ac 100644 --- a/crates/bevy_render/src/camera/mod.rs +++ b/crates/bevy_render/src/camera/mod.rs @@ -82,8 +82,8 @@ fn extract_cameras( if let Some((entity, camera, transform, visible_entities)) = camera.entity.and_then(|e| query.get(e).ok()) { - entities.insert(name.clone(), entity); if let Some(window) = windows.get(camera.window) { + entities.insert(name.clone(), entity); commands.get_or_spawn(entity).insert_bundle(( ExtractedCamera { window_id: camera.window, diff --git a/examples/README.md b/examples/README.md index f63b874fcc..ca7bb98fe8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -243,6 +243,7 @@ Example | File | Description Example | File | Description --- | --- | --- `clear_color` | [`window/clear_color.rs`](./window/clear_color.rs) | Creates a solid color window +`multiple_windows` | [`window/multiple_windows.rs`](./window/multiple_windows.rs) | Demonstrates creating multiple windows, and rendering to them `scale_factor_override` | [`window/scale_factor_override.rs`](./window/scale_factor_override.rs) | Illustrates how to customize the default window settings `transparent_window` | [`window/transparent_window.rs`](./window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration `window_settings` | [`window/window_settings.rs`](./window/window_settings.rs) | Demonstrates customizing default window settings diff --git a/examples/window/multiple_windows.rs b/examples/window/multiple_windows.rs new file mode 100644 index 0000000000..3bfb07a958 --- /dev/null +++ b/examples/window/multiple_windows.rs @@ -0,0 +1,111 @@ +use bevy::{ + core_pipeline::{draw_3d_graph, node, AlphaMask3d, Opaque3d, Transparent3d}, + prelude::*, + render::{ + camera::{ActiveCameras, ExtractedCameraNames}, + render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext, SlotValue}, + render_phase::RenderPhase, + renderer::RenderContext, + RenderApp, RenderStage, + }, + window::{CreateWindow, WindowId}, +}; + +/// This example creates a second window and draws a mesh from two different cameras, one in each window +fn main() { + let mut app = App::new(); + app.add_plugins(DefaultPlugins) + .add_startup_system(setup) + .add_startup_system(create_new_window); + + let render_app = app.sub_app(RenderApp); + render_app.add_system_to_stage(RenderStage::Extract, extract_secondary_camera_phases); + let mut graph = render_app.world.get_resource_mut::().unwrap(); + graph.add_node(SECONDARY_PASS_DRIVER, SecondaryCameraDriver); + graph + .add_node_edge(node::MAIN_PASS_DEPENDENCIES, SECONDARY_PASS_DRIVER) + .unwrap(); + app.run(); +} + +fn extract_secondary_camera_phases(mut commands: Commands, active_cameras: Res) { + if let Some(secondary) = active_cameras.get(SECONDARY_CAMERA_NAME) { + if let Some(entity) = secondary.entity { + commands.get_or_spawn(entity).insert_bundle(( + RenderPhase::::default(), + RenderPhase::::default(), + RenderPhase::::default(), + )); + } + } +} + +const SECONDARY_CAMERA_NAME: &str = "Secondary"; +const SECONDARY_PASS_DRIVER: &str = "secondary_pass_driver"; + +fn create_new_window( + mut create_window_events: EventWriter, + + mut commands: Commands, + mut active_cameras: ResMut, +) { + 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., + vsync: false, + title: "Second window".to_string(), + ..Default::default() + }, + }); + // second window camera + commands.spawn_bundle(PerspectiveCameraBundle { + camera: Camera { + window: window_id, + name: Some(SECONDARY_CAMERA_NAME.into()), + ..Default::default() + }, + transform: Transform::from_xyz(6.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y), + ..Default::default() + }); + + active_cameras.add(SECONDARY_CAMERA_NAME); +} + +struct SecondaryCameraDriver; +impl Node for SecondaryCameraDriver { + fn run( + &self, + graph: &mut RenderGraphContext, + _render_context: &mut RenderContext, + world: &World, + ) -> Result<(), NodeRunError> { + let extracted_cameras = world.get_resource::().unwrap(); + if let Some(camera_3d) = extracted_cameras.entities.get(SECONDARY_CAMERA_NAME) { + graph.run_sub_graph( + crate::draw_3d_graph::NAME, + vec![SlotValue::Entity(*camera_3d)], + )?; + } + Ok(()) + } +} + +fn setup(mut commands: Commands, asset_server: Res) { + // 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::default() + }); + // main camera + commands.spawn_bundle(PerspectiveCameraBundle { + transform: Transform::from_xyz(0.0, 0.0, 6.0).looking_at(Vec3::ZERO, Vec3::Y), + ..Default::default() + }); +}