Skip to content

Commit 0ed08d6

Browse files
fix multiple_windows example (#4389)
The example was broken in #3635 when the `ActiveCamera` logic was introduced, after which there could only be one active `Camera3d` globally. Ideally there could be one `Camera3d` per render target, not globally, but that isn't the case yet. To fix the example, we need to - don't use `Camera3d` twice, add a new `SecondWindowCamera3d` marker - add the `CameraTypePlugin::<SecondWindowCamera3d>` - extract the correct `RenderPhase`s - add a 3d pass driver node for the secondary camera Fixes #4378 Co-authored-by: Jakob Hellermann <[email protected]>
1 parent c26be39 commit 0ed08d6

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

examples/window/multiple_windows.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,87 @@
11
use bevy::{
2+
core_pipeline::{self, AlphaMask3d, Opaque3d, Transparent3d},
23
prelude::*,
3-
render::camera::RenderTarget,
4+
render::{
5+
camera::{ActiveCamera, CameraTypePlugin, RenderTarget},
6+
render_graph::{self, NodeRunError, RenderGraph, RenderGraphContext, SlotValue},
7+
render_phase::RenderPhase,
8+
renderer::RenderContext,
9+
RenderApp, RenderStage,
10+
},
411
window::{CreateWindow, PresentMode, WindowId},
512
};
613

714
/// This example creates a second window and draws a mesh from two different cameras, one in each window
815
fn main() {
916
App::new()
1017
.add_plugins(DefaultPlugins)
18+
.add_plugin(SecondWindowCameraPlugin)
1119
.add_startup_system(setup)
1220
.add_startup_system(create_new_window)
1321
.run();
1422
}
1523

24+
struct SecondWindowCameraPlugin;
25+
impl Plugin for SecondWindowCameraPlugin {
26+
fn build(&self, app: &mut App) {
27+
// adds the `ActiveCamera<SecondWindowCamera3d>` resource and extracts the camera into the render world
28+
app.add_plugin(CameraTypePlugin::<SecondWindowCamera3d>::default());
29+
30+
let render_app = app.sub_app_mut(RenderApp);
31+
32+
// add `RenderPhase<Opaque3d>`, `RenderPhase<AlphaMask3d>` and `RenderPhase<Transparent3d>` camera phases
33+
render_app.add_system_to_stage(RenderStage::Extract, extract_second_camera_phases);
34+
35+
// add a render graph node that executes the 3d subgraph
36+
let mut render_graph = render_app.world.resource_mut::<RenderGraph>();
37+
let second_window_node = render_graph.add_node("second_window_cam", SecondWindowDriverNode);
38+
render_graph
39+
.add_node_edge(
40+
core_pipeline::node::MAIN_PASS_DEPENDENCIES,
41+
second_window_node,
42+
)
43+
.unwrap();
44+
render_graph
45+
.add_node_edge(core_pipeline::node::CLEAR_PASS_DRIVER, second_window_node)
46+
.unwrap();
47+
}
48+
}
49+
50+
struct SecondWindowDriverNode;
51+
impl render_graph::Node for SecondWindowDriverNode {
52+
fn run(
53+
&self,
54+
graph: &mut RenderGraphContext,
55+
_: &mut RenderContext,
56+
world: &World,
57+
) -> Result<(), NodeRunError> {
58+
if let Some(camera) = world.resource::<ActiveCamera<SecondWindowCamera3d>>().get() {
59+
graph.run_sub_graph(
60+
core_pipeline::draw_3d_graph::NAME,
61+
vec![SlotValue::Entity(camera)],
62+
)?;
63+
}
64+
65+
Ok(())
66+
}
67+
}
68+
69+
fn extract_second_camera_phases(
70+
mut commands: Commands,
71+
active: Res<ActiveCamera<SecondWindowCamera3d>>,
72+
) {
73+
if let Some(entity) = active.get() {
74+
commands.get_or_spawn(entity).insert_bundle((
75+
RenderPhase::<Opaque3d>::default(),
76+
RenderPhase::<AlphaMask3d>::default(),
77+
RenderPhase::<Transparent3d>::default(),
78+
));
79+
}
80+
}
81+
82+
#[derive(Component, Default)]
83+
struct SecondWindowCamera3d;
84+
1685
fn create_new_window(mut create_window_events: EventWriter<CreateWindow>, mut commands: Commands) {
1786
let window_id = WindowId::new();
1887

@@ -35,7 +104,8 @@ fn create_new_window(mut create_window_events: EventWriter<CreateWindow>, mut co
35104
..default()
36105
},
37106
transform: Transform::from_xyz(6.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
38-
..default()
107+
marker: SecondWindowCamera3d,
108+
..PerspectiveCameraBundle::new()
39109
});
40110
}
41111

0 commit comments

Comments
 (0)