forked from vladbat00/bevy_egui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_windows.rs
327 lines (293 loc) · 9.85 KB
/
two_windows.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use bevy::{
prelude::*,
render::{
camera::{ActiveCameras, Camera},
pass::*,
render_graph::{
base::MainPass, CameraNode, PassNode, RenderGraph, WindowSwapChainNode,
WindowTextureNode,
},
texture::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage},
},
window::{CreateWindow, WindowDescriptor, WindowId},
};
use bevy_egui::{egui, EguiContext, EguiPlugin};
const BEVY_TEXTURE_ID: u64 = 0;
/// This example creates a second window and draws a mesh from two different cameras.
fn main() {
App::build()
.insert_resource(Msaa { samples: 4 })
.init_resource::<SharedUiState>()
.add_state(AppState::CreateWindow)
.add_plugins(DefaultPlugins)
.add_plugin(EguiPlugin)
.add_startup_system(load_assets.system())
.add_system_set(
SystemSet::on_update(AppState::CreateWindow).with_system(setup_window.system()),
)
.add_system_set(SystemSet::on_update(AppState::Setup).with_system(setup.system()))
.add_system_set(SystemSet::on_update(AppState::Done).with_system(ui_second_window.system()))
.add_system(ui_first_window.system())
.run();
}
struct SecondWindow {
id: WindowId,
}
// NOTE: this "state based" approach to multiple windows is a short term workaround.
// Future Bevy releases shouldn't require such a strict order of operations.
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
enum AppState {
CreateWindow,
Setup,
Done,
}
fn setup_window(
mut app_state: ResMut<State<AppState>>,
mut create_window_events: EventWriter<CreateWindow>,
) {
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()
},
});
app_state.set(AppState::Setup).unwrap();
}
mod second_window {
pub const SWAP_CHAIN: &str = "second_window_swap_chain";
pub const DEPTH_TEXTURE: &str = "second_window_depth_texture";
pub const CAMERA_NODE: &str = "secondary_camera";
pub const CAMERA_NAME: &str = "Secondary";
pub const SAMPLED_COLOR_ATTACHMENT: &str = "second_multi_sampled_color_attachment";
pub const PASS: &str = "second_window_pass";
}
fn load_assets(mut egui_context: ResMut<EguiContext>, assets: Res<AssetServer>) {
let texture_handle = assets.load("icon.png");
egui_context.set_egui_texture(BEVY_TEXTURE_ID, texture_handle);
}
fn setup_pipeline(
render_graph: &mut RenderGraph,
active_cameras: &mut ActiveCameras,
msaa: &Msaa,
window_id: WindowId,
) {
// 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(window_id),
);
// add a new depth texture node for our new window
render_graph.add_node(
second_window::DEPTH_TEXTURE,
WindowTextureNode::new(
window_id,
TextureDescriptor {
format: TextureFormat::Depth32Float,
usage: TextureUsage::OUTPUT_ATTACHMENT,
sample_count: msaa.samples,
..Default::default()
},
),
);
// add a new camera node for our new window
render_graph.add_system_node(
second_window::CAMERA_NODE,
CameraNode::new(second_window::CAMERA_NAME),
);
// add a new render pass for our new window / camera
let mut second_window_pass = PassNode::<&MainPass>::new(PassDescriptor {
color_attachments: vec![msaa.color_attachment_descriptor(
TextureAttachment::Input("color_attachment".to_string()),
TextureAttachment::Input("color_resolve_target".to_string()),
Operations {
load: LoadOp::Clear(Color::rgb(0.5, 0.5, 0.8)),
store: true,
},
)],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachmentDescriptor {
attachment: TextureAttachment::Input("depth".to_string()),
depth_ops: Some(Operations {
load: LoadOp::Clear(1.0),
store: true,
}),
stencil_ops: None,
}),
sample_count: msaa.samples,
});
second_window_pass.add_camera(second_window::CAMERA_NAME);
active_cameras.add(second_window::CAMERA_NAME);
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,
if msaa.samples > 1 {
"color_resolve_target"
} else {
"color_attachment"
},
)
.unwrap();
render_graph
.add_slot_edge(
second_window::DEPTH_TEXTURE,
WindowTextureNode::OUT_TEXTURE,
second_window::PASS,
"depth",
)
.unwrap();
render_graph
.add_node_edge(second_window::CAMERA_NODE, second_window::PASS)
.unwrap();
if msaa.samples > 1 {
render_graph.add_node(
second_window::SAMPLED_COLOR_ATTACHMENT,
WindowTextureNode::new(
window_id,
TextureDescriptor {
size: Extent3d {
depth: 1,
width: 1,
height: 1,
},
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::default(),
usage: TextureUsage::OUTPUT_ATTACHMENT,
},
),
);
render_graph
.add_slot_edge(
second_window::SAMPLED_COLOR_ATTACHMENT,
WindowSwapChainNode::OUT_TEXTURE,
second_window::PASS,
"color_attachment",
)
.unwrap();
}
bevy_egui::setup_pipeline(
render_graph,
msaa,
bevy_egui::RenderGraphConfig {
window_id,
egui_pass: "egui_pass2",
main_pass: second_window::PASS,
swap_chain_node: second_window::SWAP_CHAIN,
depth_texture: second_window::DEPTH_TEXTURE,
sampled_color_attachment: second_window::SAMPLED_COLOR_ATTACHMENT,
transform_node: "egui_transform2",
},
);
}
fn setup(
mut commands: Commands,
mut app_state: ResMut<State<AppState>>,
windows: Res<Windows>,
mut active_cameras: ResMut<ActiveCameras>,
mut render_graph: ResMut<RenderGraph>,
mut meshes: ResMut<Assets<Mesh>>,
msaa: Res<Msaa>,
) {
// get the non-default window id
let window_id = match windows
.iter()
.find(|w| w.id() != WindowId::default())
.map(|w| w.id())
{
Some(x) => x,
None => return,
};
setup_pipeline(&mut render_graph, &mut active_cameras, &msaa, window_id);
// SETUP SCENE
// add entities to the world
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
..Default::default()
});
// light
commands.spawn_bundle(LightBundle {
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()
});
// second window camera
commands.spawn_bundle(PerspectiveCameraBundle {
camera: Camera {
name: Some("Secondary".to_string()),
window: window_id,
..Default::default()
},
transform: Transform::from_xyz(6.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
commands.insert_resource(SecondWindow { id: window_id });
app_state.set(AppState::Done).unwrap();
}
#[derive(Default)]
struct UiState {
input: String,
}
#[derive(Default)]
struct SharedUiState {
shared_input: String,
}
fn ui_first_window(
egui_context: Res<EguiContext>,
mut ui_state: Local<UiState>,
mut shared_ui_state: ResMut<SharedUiState>,
) {
egui::Window::new("First Window")
.scroll(true)
.show(egui_context.ctx(), |ui| {
ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(&mut ui_state.input);
});
ui.horizontal(|ui| {
ui.label("Shared input: ");
ui.text_edit_singleline(&mut shared_ui_state.shared_input);
});
ui.add(egui::widgets::Image::new(
egui::TextureId::User(BEVY_TEXTURE_ID),
[256.0, 256.0],
));
});
}
fn ui_second_window(
egui_context: Res<EguiContext>,
second_window: Res<SecondWindow>,
mut ui_state: Local<UiState>,
mut shared_ui_state: ResMut<SharedUiState>,
) {
egui::Window::new("Second Window").scroll(true).show(
egui_context.ctx_for_window(second_window.id),
|ui| {
ui.horizontal(|ui| {
ui.label("Write something else: ");
ui.text_edit_singleline(&mut ui_state.input);
});
ui.horizontal(|ui| {
ui.label("Shared input: ");
ui.text_edit_singleline(&mut shared_ui_state.shared_input);
});
ui.add(egui::widgets::Image::new(
egui::TextureId::User(BEVY_TEXTURE_ID),
[256.0, 256.0],
));
},
);
}