2020-06-26 06:04:08 +00:00
|
|
|
use bevy::{
|
|
|
|
prelude::*,
|
|
|
|
render::{
|
2020-07-17 01:26:21 +00:00
|
|
|
camera::{ActiveCameras, Camera},
|
|
|
|
pass::*,
|
|
|
|
render_graph::{CameraNode, PassNode, RenderGraph, WindowSwapChainNode, WindowTextureNode},
|
2020-06-26 06:04:08 +00:00
|
|
|
texture::{TextureDescriptor, TextureFormat, TextureUsage},
|
|
|
|
},
|
2020-07-26 19:27:09 +00:00
|
|
|
window::{CreateWindow, WindowDescriptor, WindowId},
|
2020-06-26 06:04:08 +00:00
|
|
|
};
|
2020-03-30 21:53:32 +00:00
|
|
|
|
2020-07-28 21:24:03 +00:00
|
|
|
/// This example creates a second window and draws a mesh from two different cameras.
|
2020-03-30 21:53:32 +00:00
|
|
|
fn main() {
|
2020-04-16 02:40:24 +00:00
|
|
|
App::build()
|
|
|
|
.add_default_plugins()
|
2020-06-26 06:04:08 +00:00
|
|
|
.add_startup_system(setup.system())
|
2020-04-16 02:40:24 +00:00
|
|
|
.run();
|
2020-03-30 21:53:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 06:04:08 +00:00
|
|
|
fn setup(
|
2020-07-10 04:18:35 +00:00
|
|
|
mut commands: Commands,
|
2020-06-25 23:02:21 +00:00
|
|
|
mut create_window_events: ResMut<Events<CreateWindow>>,
|
2020-06-26 06:04:08 +00:00
|
|
|
mut active_cameras: ResMut<ActiveCameras>,
|
2020-06-25 23:02:21 +00:00
|
|
|
mut render_graph: ResMut<RenderGraph>,
|
2020-06-26 06:04:08 +00:00
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
asset_server: Res<AssetServer>,
|
2020-06-25 23:02:21 +00:00
|
|
|
) {
|
|
|
|
let window_id = WindowId::new();
|
|
|
|
|
2020-04-07 04:32:19 +00:00
|
|
|
// sends out a "CreateWindow" event, which will be received by the windowing backend
|
2020-03-30 21:53:32 +00:00
|
|
|
create_window_events.send(CreateWindow {
|
2020-06-25 23:02:21 +00:00
|
|
|
id: window_id,
|
2020-03-30 21:53:32 +00:00
|
|
|
descriptor: WindowDescriptor {
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
vsync: false,
|
2020-06-25 23:02:21 +00:00
|
|
|
title: "second window".to_string(),
|
2020-04-04 19:40:32 +00:00
|
|
|
},
|
2020-03-30 21:53:32 +00:00
|
|
|
});
|
2020-06-25 23:02:21 +00:00
|
|
|
|
|
|
|
// here we setup our render graph to draw our second camera to the new window's swap chain
|
|
|
|
|
|
|
|
// add a swapchain node for our new window
|
|
|
|
render_graph.add_node(
|
|
|
|
"second_window_swap_chain",
|
2020-07-26 19:27:09 +00:00
|
|
|
WindowSwapChainNode::new(window_id),
|
2020-06-25 23:02:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// add a new depth texture node for our new window
|
|
|
|
render_graph.add_node(
|
|
|
|
"second_window_depth_texture",
|
|
|
|
WindowTextureNode::new(
|
2020-07-26 19:27:09 +00:00
|
|
|
window_id,
|
2020-06-25 23:02:21 +00:00
|
|
|
TextureDescriptor {
|
|
|
|
format: TextureFormat::Depth32Float,
|
|
|
|
usage: TextureUsage::OUTPUT_ATTACHMENT,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2020-07-28 20:43:07 +00:00
|
|
|
// add a new camera node for our new window
|
2020-06-26 06:04:08 +00:00
|
|
|
render_graph.add_system_node("secondary_camera", CameraNode::new("Secondary"));
|
|
|
|
|
|
|
|
// add a new render pass for our new camera
|
2020-06-25 23:02:21 +00:00
|
|
|
let mut second_window_pass = PassNode::new(PassDescriptor {
|
|
|
|
color_attachments: vec![RenderPassColorAttachmentDescriptor {
|
|
|
|
attachment: TextureAttachment::Input("color".to_string()),
|
|
|
|
resolve_target: None,
|
2020-07-10 20:47:31 +00:00
|
|
|
ops: Operations {
|
|
|
|
load: LoadOp::Clear(Color::rgb(0.1, 0.1, 0.1)),
|
|
|
|
store: true,
|
|
|
|
},
|
2020-06-25 23:02:21 +00:00
|
|
|
}],
|
|
|
|
depth_stencil_attachment: Some(RenderPassDepthStencilAttachmentDescriptor {
|
|
|
|
attachment: TextureAttachment::Input("depth".to_string()),
|
2020-07-10 20:47:31 +00:00
|
|
|
depth_ops: Some(Operations {
|
|
|
|
load: LoadOp::Clear(1.0),
|
|
|
|
store: true,
|
|
|
|
}),
|
|
|
|
stencil_ops: None,
|
2020-06-25 23:02:21 +00:00
|
|
|
}),
|
|
|
|
sample_count: 1,
|
|
|
|
});
|
|
|
|
|
2020-06-26 06:04:08 +00:00
|
|
|
second_window_pass.add_camera("Secondary");
|
|
|
|
active_cameras.add("Secondary");
|
2020-06-25 23:02:21 +00:00
|
|
|
|
2020-06-26 06:04:08 +00:00
|
|
|
render_graph.add_node("second_window_pass", second_window_pass);
|
2020-06-25 23:02:21 +00:00
|
|
|
|
|
|
|
render_graph
|
|
|
|
.add_slot_edge(
|
|
|
|
"second_window_swap_chain",
|
|
|
|
WindowSwapChainNode::OUT_TEXTURE,
|
|
|
|
"second_window_pass",
|
|
|
|
"color",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
render_graph
|
|
|
|
.add_slot_edge(
|
|
|
|
"second_window_depth_texture",
|
|
|
|
WindowTextureNode::OUT_TEXTURE,
|
|
|
|
"second_window_pass",
|
|
|
|
"depth",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2020-06-26 06:04:08 +00:00
|
|
|
render_graph
|
|
|
|
.add_node_edge("secondary_camera", "second_window_pass")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// SETUP SCENE
|
|
|
|
|
2020-06-25 23:02:21 +00:00
|
|
|
// load the mesh
|
|
|
|
let mesh_handle = asset_server
|
|
|
|
.load("assets/models/monkey/Monkey.gltf")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// create a material for the mesh
|
|
|
|
let material_handle = materials.add(StandardMaterial {
|
|
|
|
albedo: Color::rgb(0.5, 0.4, 0.3),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
// add entities to the world
|
2020-07-10 04:18:35 +00:00
|
|
|
commands
|
2020-06-25 23:02:21 +00:00
|
|
|
// mesh
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(PbrComponents {
|
2020-06-25 23:02:21 +00:00
|
|
|
mesh: mesh_handle,
|
|
|
|
material: material_handle,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
// light
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(LightComponents {
|
2020-06-25 23:02:21 +00:00
|
|
|
translation: Translation::new(4.0, 5.0, 4.0),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
// main camera
|
2020-07-20 00:00:08 +00:00
|
|
|
.spawn(Camera3dComponents {
|
2020-06-25 23:02:21 +00:00
|
|
|
transform: Transform::new_sync_disabled(Mat4::face_toward(
|
|
|
|
Vec3::new(0.0, 0.0, 6.0),
|
|
|
|
Vec3::new(0.0, 0.0, 0.0),
|
|
|
|
Vec3::new(0.0, 1.0, 0.0),
|
|
|
|
)),
|
|
|
|
..Default::default()
|
2020-06-26 06:04:08 +00:00
|
|
|
})
|
|
|
|
// second window camera
|
2020-07-20 00:00:08 +00:00
|
|
|
.spawn(Camera3dComponents {
|
2020-06-26 06:04:08 +00:00
|
|
|
camera: Camera {
|
|
|
|
name: Some("Secondary".to_string()),
|
2020-07-26 19:27:09 +00:00
|
|
|
window: window_id,
|
2020-06-26 06:04:08 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
transform: Transform::new_sync_disabled(Mat4::face_toward(
|
|
|
|
Vec3::new(6.0, 0.0, 0.0),
|
|
|
|
Vec3::new(0.0, 0.0, 0.0),
|
|
|
|
Vec3::new(0.0, 1.0, 0.0),
|
|
|
|
)),
|
|
|
|
..Default::default()
|
2020-06-25 23:02:21 +00:00
|
|
|
});
|
2020-04-04 19:40:32 +00:00
|
|
|
}
|