2021-12-18 19:38:05 +00:00
|
|
|
use bevy::{
|
2022-04-01 21:11:07 +00:00
|
|
|
core_pipeline::{self, AlphaMask3d, Opaque3d, Transparent3d},
|
2021-12-18 19:38:05 +00:00
|
|
|
prelude::*,
|
2022-04-01 21:11:07 +00:00
|
|
|
render::{
|
|
|
|
camera::{ActiveCamera, CameraTypePlugin, RenderTarget},
|
|
|
|
render_graph::{self, NodeRunError, RenderGraph, RenderGraphContext, SlotValue},
|
|
|
|
render_phase::RenderPhase,
|
|
|
|
renderer::RenderContext,
|
|
|
|
RenderApp, RenderStage,
|
|
|
|
},
|
2022-02-04 03:37:44 +00:00
|
|
|
window::{CreateWindow, PresentMode, WindowId},
|
2021-12-18 19:38:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// This example creates a second window and draws a mesh from two different cameras, one in each window
|
|
|
|
fn main() {
|
2022-03-12 00:41:06 +00:00
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
2022-04-01 21:11:07 +00:00
|
|
|
.add_plugin(SecondWindowCameraPlugin)
|
2021-12-18 19:38:05 +00:00
|
|
|
.add_startup_system(setup)
|
2022-03-12 00:41:06 +00:00
|
|
|
.add_startup_system(create_new_window)
|
|
|
|
.run();
|
2021-12-18 19:38:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 21:11:07 +00:00
|
|
|
struct SecondWindowCameraPlugin;
|
|
|
|
impl Plugin for SecondWindowCameraPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
// adds the `ActiveCamera<SecondWindowCamera3d>` resource and extracts the camera into the render world
|
|
|
|
app.add_plugin(CameraTypePlugin::<SecondWindowCamera3d>::default());
|
|
|
|
|
|
|
|
let render_app = app.sub_app_mut(RenderApp);
|
|
|
|
|
|
|
|
// add `RenderPhase<Opaque3d>`, `RenderPhase<AlphaMask3d>` and `RenderPhase<Transparent3d>` camera phases
|
|
|
|
render_app.add_system_to_stage(RenderStage::Extract, extract_second_camera_phases);
|
|
|
|
|
|
|
|
// add a render graph node that executes the 3d subgraph
|
|
|
|
let mut render_graph = render_app.world.resource_mut::<RenderGraph>();
|
|
|
|
let second_window_node = render_graph.add_node("second_window_cam", SecondWindowDriverNode);
|
|
|
|
render_graph
|
|
|
|
.add_node_edge(
|
|
|
|
core_pipeline::node::MAIN_PASS_DEPENDENCIES,
|
|
|
|
second_window_node,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
render_graph
|
|
|
|
.add_node_edge(core_pipeline::node::CLEAR_PASS_DRIVER, second_window_node)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SecondWindowDriverNode;
|
|
|
|
impl render_graph::Node for SecondWindowDriverNode {
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
graph: &mut RenderGraphContext,
|
|
|
|
_: &mut RenderContext,
|
|
|
|
world: &World,
|
|
|
|
) -> Result<(), NodeRunError> {
|
|
|
|
if let Some(camera) = world.resource::<ActiveCamera<SecondWindowCamera3d>>().get() {
|
|
|
|
graph.run_sub_graph(
|
|
|
|
core_pipeline::draw_3d_graph::NAME,
|
|
|
|
vec![SlotValue::Entity(camera)],
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_second_camera_phases(
|
|
|
|
mut commands: Commands,
|
|
|
|
active: Res<ActiveCamera<SecondWindowCamera3d>>,
|
|
|
|
) {
|
|
|
|
if let Some(entity) = active.get() {
|
|
|
|
commands.get_or_spawn(entity).insert_bundle((
|
|
|
|
RenderPhase::<Opaque3d>::default(),
|
|
|
|
RenderPhase::<AlphaMask3d>::default(),
|
|
|
|
RenderPhase::<Transparent3d>::default(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component, Default)]
|
|
|
|
struct SecondWindowCamera3d;
|
|
|
|
|
2022-03-12 00:41:06 +00:00
|
|
|
fn create_new_window(mut create_window_events: EventWriter<CreateWindow>, mut commands: Commands) {
|
2021-12-18 19:38:05 +00:00
|
|
|
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.,
|
2022-02-04 03:37:44 +00:00
|
|
|
present_mode: PresentMode::Immediate,
|
2021-12-18 19:38:05 +00:00
|
|
|
title: "Second window".to_string(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-18 19:38:05 +00:00
|
|
|
},
|
|
|
|
});
|
2022-03-12 00:41:06 +00:00
|
|
|
|
2021-12-18 19:38:05 +00:00
|
|
|
// second window camera
|
|
|
|
commands.spawn_bundle(PerspectiveCameraBundle {
|
|
|
|
camera: Camera {
|
2022-02-24 00:40:24 +00:00
|
|
|
target: RenderTarget::Window(window_id),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-18 19:38:05 +00:00
|
|
|
},
|
|
|
|
transform: Transform::from_xyz(6.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
|
2022-04-01 21:11:07 +00:00
|
|
|
marker: SecondWindowCamera3d,
|
|
|
|
..PerspectiveCameraBundle::new()
|
2021-12-18 19:38:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-18 19:38:05 +00:00
|
|
|
});
|
|
|
|
// main camera
|
|
|
|
commands.spawn_bundle(PerspectiveCameraBundle {
|
|
|
|
transform: Transform::from_xyz(0.0, 0.0, 6.0).looking_at(Vec3::ZERO, Vec3::Y),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-18 19:38:05 +00:00
|
|
|
});
|
|
|
|
}
|