Skip to content

Commit

Permalink
render to second window in multiple_windows example
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Jun 25, 2020
1 parent 8a8d01a commit ca4726e
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 6 deletions.
19 changes: 19 additions & 0 deletions crates/bevy_render/src/texture/texture_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,22 @@ impl From<&Texture> for TextureDescriptor {
}
}
}


impl Default for TextureDescriptor {
fn default() -> Self {
TextureDescriptor {
size: Extent3d {
width: 1,
height: 1,
depth: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rgba8UnormSrgb,
usage: TextureUsage::SAMPLED | TextureUsage::COPY_DST,
}
}

}
1 change: 1 addition & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct WindowResized {
/// An event that indicates that a new window should be created.
#[derive(Debug, Clone)]
pub struct CreateWindow {
pub id: WindowId,
pub descriptor: WindowDescriptor,
}

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl AppPlugin for WindowPlugin {
let mut create_window_event =
app.resources().get_mut::<Events<CreateWindow>>().unwrap();
create_window_event.send(CreateWindow {
id: WindowId::new(),
descriptor: primary_window_descriptor.clone(),
});
}
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub enum WindowReference {
pub struct WindowId(Uuid);

impl WindowId {
pub fn new() -> Self {
WindowId(Uuid::new_v4())
}

pub fn to_string(&self) -> String {
self.0.to_simple().to_string()
}
Expand All @@ -24,9 +28,9 @@ pub struct Window {
}

impl Window {
pub fn new(window_descriptor: &WindowDescriptor) -> Self {
pub fn new(id: WindowId, window_descriptor: &WindowDescriptor) -> Self {
Window {
id: WindowId(Uuid::new_v4()),
id,
height: window_descriptor.height,
width: window_descriptor.width,
title: window_descriptor.title.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn handle_create_window_events(
let create_window_events = resources.get::<Events<CreateWindow>>().unwrap();
let mut window_created_events = resources.get_mut::<Events<WindowCreated>>().unwrap();
for create_window_event in create_window_event_reader.iter(&create_window_events) {
let window = Window::new(&create_window_event.descriptor);
let window = Window::new(create_window_event.id, &create_window_event.descriptor);
winit_windows.create_window(event_loop, &window);
let window_id = window.id;
windows.add(window);
Expand Down
138 changes: 135 additions & 3 deletions examples/window/multiple_windows.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,152 @@
use bevy::{prelude::*, window::CreateWindow};
use bevy_render::{pass::{StoreOp, LoadOp, TextureAttachment, RenderPassColorAttachmentDescriptor, PassDescriptor, RenderPassDepthStencilAttachmentDescriptor}, texture::{TextureDescriptor, TextureFormat, TextureUsage}};
use bevy_window::{WindowId, WindowReference};

fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup.system())
.add_startup_system(create_second_window_system.system())
.add_startup_system(setup_scene.system())
.run();
}

fn setup(mut create_window_events: ResMut<Events<CreateWindow>>) {
fn create_second_window_system(
mut create_window_events: ResMut<Events<CreateWindow>>,
mut render_graph: ResMut<RenderGraph>,
) {
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: "another window".to_string(),
title: "second window".to_string(),
},
});

// 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",
WindowSwapChainNode::new(WindowReference::Id(window_id)),
);

// add a new depth texture node for our new window
render_graph.add_node(
"second_window_depth_texture",
WindowTextureNode::new(
WindowReference::Id(window_id),
TextureDescriptor {
format: TextureFormat::Depth32Float,
usage: TextureUsage::OUTPUT_ATTACHMENT,
..Default::default()
},
),
);

let mut second_window_pass = PassNode::new(PassDescriptor {
color_attachments: vec![RenderPassColorAttachmentDescriptor {
attachment: TextureAttachment::Input("color".to_string()),
resolve_target: None,
load_op: LoadOp::Clear,
store_op: StoreOp::Store,
clear_color: Color::rgb(0.1, 0.1, 0.1),
}],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachmentDescriptor {
attachment: TextureAttachment::Input("depth".to_string()),
depth_load_op: LoadOp::Clear,
depth_store_op: StoreOp::Store,
stencil_load_op: LoadOp::Clear,
stencil_store_op: StoreOp::Store,
stencil_read_only: false,
depth_read_only: false,
clear_depth: 1.0,
clear_stencil: 0,
}),
sample_count: 1,
});

// TODO: use different camera here
second_window_pass.add_camera(bevy::render::base_render_graph::camera::CAMERA);

render_graph.add_node(
"second_window_pass",
second_window_pass,
);

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();
}

fn setup_scene(
command_buffer: &mut CommandBuffer,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// 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
command_buffer
.build()
// mesh
.entity_with(MeshComponents {
mesh: mesh_handle,
material: material_handle,
..Default::default()
})
// light
.entity_with(LightComponents {
translation: Translation::new(4.0, 5.0, 4.0),
..Default::default()
})
// main camera
.entity_with(PerspectiveCameraComponents {
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()
});
// // second window camera
// .entity_with(PerspectiveCameraComponents {
// camera: Camera {
// name: Some("Secondary".to_string()),
// ..Default::default()
// },
// 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()
// });
}

0 comments on commit ca4726e

Please sign in to comment.