forked from BlackPhlox/bevy_dolly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
split.rs
172 lines (153 loc) · 4.88 KB
/
split.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
#![allow(clippy::type_complexity)]
//! Renders two cameras to the same window to accomplish "split screen".
use bevy::{
core_pipeline::clear_color::ClearColorConfig,
prelude::*,
render::camera::Viewport,
window::{PrimaryWindow, WindowResized},
};
use bevy_dolly::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(
Update,
(
Dolly::<LeftCamera>::update_active,
Dolly::<RightCamera>::update_active,
set_camera_viewports,
update_camera_1,
update_camera_2,
),
)
.run();
}
#[derive(Component)]
struct LeftCamera;
#[derive(Component)]
struct RightCamera;
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane {
size: 100.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
commands.spawn(SceneBundle {
scene: asset_server.load("poly_fox.glb#Scene0"),
..default()
});
commands.spawn(SceneBundle {
scene: asset_server.load("poly_dolly.gltf#Scene0"),
..default()
});
// Light
commands.spawn(DirectionalLightBundle {
transform: Transform::from_rotation(Quat::from_euler(
EulerRot::ZYX,
0.0,
1.0,
-std::f32::consts::FRAC_PI_4,
)),
directional_light: DirectionalLight {
shadows_enabled: true,
..default()
},
..default()
});
// camera
commands.spawn((
LeftCamera,
Camera3dBundle {
transform: Transform::from_xyz(0.0, 200.0, -100.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
));
commands.spawn((
LeftCamera,
Rig::builder()
.with(YawPitch::new().yaw_degrees(45.0).pitch_degrees(-30.0))
.with(Smooth::new_rotation(1.5))
.with(Arm::new(Vec3::Z * 4.0))
.build(),
));
commands.spawn((
RightCamera,
Rig::builder()
.with(YawPitch::new().yaw_degrees(45.0).pitch_degrees(-30.0))
.with(Smooth::new_rotation(1.5))
.with(Arm::new(Vec3::Z * 200.0))
.build(),
));
// camera
commands
.spawn(Camera3dBundle {
transform: Transform::from_xyz(100.0, 100., 150.0).looking_at(Vec3::ZERO, Vec3::Y),
camera_3d: Camera3d {
clear_color: ClearColorConfig::None,
..default()
},
camera: Camera {
order: 1,
..default()
},
..default()
})
.insert(RightCamera);
}
fn set_camera_viewports(
windows: Query<(Entity, &Window), With<PrimaryWindow>>,
mut resize_events: EventReader<WindowResized>,
mut left_camera: Query<&mut Camera, (With<LeftCamera>, Without<RightCamera>)>,
mut right_camera: Query<&mut Camera, With<RightCamera>>,
) {
for resize_event in resize_events.read() {
if let Ok((entity, window)) = windows.get_single() {
if resize_event.window == entity {
let mut left_camera = left_camera.single_mut();
left_camera.viewport = Some(Viewport {
physical_position: UVec2::new(0, 0),
physical_size: UVec2::new(
window.physical_width() / 2,
window.physical_height(),
),
depth: 0.0..1.0,
});
let mut right_camera = right_camera.single_mut();
right_camera.viewport = Some(Viewport {
physical_position: UVec2::new(window.physical_width() / 2, 0),
physical_size: UVec2::new(
window.physical_width() / 2,
window.physical_height(),
),
depth: 0.0..1.0,
});
}
}
}
}
fn update_camera_1(mut query: Query<&mut Rig, (With<LeftCamera>, Without<RightCamera>)>) {
let mut rig = query.single_mut();
let camera_driver = rig.driver_mut::<YawPitch>();
camera_driver.rotate_yaw_pitch(1.0, 0.0);
}
fn update_camera_2(
time: Res<Time>,
mut query: Query<(&mut Rig, (With<RightCamera>, Without<LeftCamera>))>,
) {
let (mut rig, _a) = query.single_mut();
let camera_driver = rig.driver_mut::<YawPitch>();
camera_driver.rotate_yaw_pitch(-1.0, 0.0);
let a = rig.driver_mut::<Arm>();
a.offset = Vec3::Z * ((time.delta_seconds() * 0.2).sin().cos().abs() * 400. - 200.);
}