split_screen/
split_screen.rs

1//! Renders two cameras to the same window to accomplish "split screen".
2
3use std::f32::consts::PI;
4
5use bevy::{
6    pbr::CascadeShadowConfigBuilder, prelude::*, render::camera::Viewport, window::WindowResized,
7};
8
9fn main() {
10    App::new()
11        .add_plugins(DefaultPlugins)
12        .add_systems(Startup, setup)
13        .add_systems(Update, (set_camera_viewports, button_system))
14        .run();
15}
16
17/// set up a simple 3D scene
18fn setup(
19    mut commands: Commands,
20    asset_server: Res<AssetServer>,
21    mut meshes: ResMut<Assets<Mesh>>,
22    mut materials: ResMut<Assets<StandardMaterial>>,
23) {
24    // plane
25    commands.spawn((
26        Mesh3d(meshes.add(Plane3d::default().mesh().size(100.0, 100.0))),
27        MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
28    ));
29
30    commands.spawn(SceneRoot(
31        asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/animated/Fox.glb")),
32    ));
33
34    // Light
35    commands.spawn((
36        Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
37        DirectionalLight {
38            shadows_enabled: true,
39            ..default()
40        },
41        CascadeShadowConfigBuilder {
42            num_cascades: if cfg!(all(
43                feature = "webgl2",
44                target_arch = "wasm32",
45                not(feature = "webgpu")
46            )) {
47                // Limited to 1 cascade in WebGL
48                1
49            } else {
50                2
51            },
52            first_cascade_far_bound: 200.0,
53            maximum_distance: 280.0,
54            ..default()
55        }
56        .build(),
57    ));
58
59    // Cameras and their dedicated UI
60    for (index, (camera_name, camera_pos)) in [
61        ("Player 1", Vec3::new(0.0, 200.0, -150.0)),
62        ("Player 2", Vec3::new(150.0, 150., 50.0)),
63        ("Player 3", Vec3::new(100.0, 150., -150.0)),
64        ("Player 4", Vec3::new(-100.0, 80., 150.0)),
65    ]
66    .iter()
67    .enumerate()
68    {
69        let camera = commands
70            .spawn((
71                Camera3d::default(),
72                Transform::from_translation(*camera_pos).looking_at(Vec3::ZERO, Vec3::Y),
73                Camera {
74                    // Renders cameras with different priorities to prevent ambiguities
75                    order: index as isize,
76                    ..default()
77                },
78                CameraPosition {
79                    pos: UVec2::new((index % 2) as u32, (index / 2) as u32),
80                },
81            ))
82            .id();
83
84        // Set up UI
85        commands
86            .spawn((
87                UiTargetCamera(camera),
88                Node {
89                    width: Val::Percent(100.),
90                    height: Val::Percent(100.),
91                    ..default()
92                },
93            ))
94            .with_children(|parent| {
95                parent.spawn((
96                    Text::new(*camera_name),
97                    Node {
98                        position_type: PositionType::Absolute,
99                        top: Val::Px(12.),
100                        left: Val::Px(12.),
101                        ..default()
102                    },
103                ));
104                buttons_panel(parent);
105            });
106    }
107
108    fn buttons_panel(parent: &mut ChildSpawnerCommands) {
109        parent
110            .spawn(Node {
111                position_type: PositionType::Absolute,
112                width: Val::Percent(100.),
113                height: Val::Percent(100.),
114                display: Display::Flex,
115                flex_direction: FlexDirection::Row,
116                justify_content: JustifyContent::SpaceBetween,
117                align_items: AlignItems::Center,
118                padding: UiRect::all(Val::Px(20.)),
119                ..default()
120            })
121            .with_children(|parent| {
122                rotate_button(parent, "<", Direction::Left);
123                rotate_button(parent, ">", Direction::Right);
124            });
125    }
126
127    fn rotate_button(parent: &mut ChildSpawnerCommands, caption: &str, direction: Direction) {
128        parent
129            .spawn((
130                RotateCamera(direction),
131                Button,
132                Node {
133                    width: Val::Px(40.),
134                    height: Val::Px(40.),
135                    border: UiRect::all(Val::Px(2.)),
136                    justify_content: JustifyContent::Center,
137                    align_items: AlignItems::Center,
138                    ..default()
139                },
140                BorderColor(Color::WHITE),
141                BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
142            ))
143            .with_children(|parent| {
144                parent.spawn(Text::new(caption));
145            });
146    }
147}
148
149#[derive(Component)]
150struct CameraPosition {
151    pos: UVec2,
152}
153
154#[derive(Component)]
155struct RotateCamera(Direction);
156
157enum Direction {
158    Left,
159    Right,
160}
161
162fn set_camera_viewports(
163    windows: Query<&Window>,
164    mut resize_events: EventReader<WindowResized>,
165    mut query: Query<(&CameraPosition, &mut Camera)>,
166) {
167    // We need to dynamically resize the camera's viewports whenever the window size changes
168    // so then each camera always takes up half the screen.
169    // A resize_event is sent when the window is first created, allowing us to reuse this system for initial setup.
170    for resize_event in resize_events.read() {
171        let window = windows.get(resize_event.window).unwrap();
172        let size = window.physical_size() / 2;
173
174        for (camera_position, mut camera) in &mut query {
175            camera.viewport = Some(Viewport {
176                physical_position: camera_position.pos * size,
177                physical_size: size,
178                ..default()
179            });
180        }
181    }
182}
183
184fn button_system(
185    interaction_query: Query<
186        (&Interaction, &ComputedNodeTarget, &RotateCamera),
187        (Changed<Interaction>, With<Button>),
188    >,
189    mut camera_query: Query<&mut Transform, With<Camera>>,
190) {
191    for (interaction, computed_target, RotateCamera(direction)) in &interaction_query {
192        if let Interaction::Pressed = *interaction {
193            // Since TargetCamera propagates to the children, we can use it to find
194            // which side of the screen the button is on.
195            if let Some(mut camera_transform) = computed_target
196                .camera()
197                .and_then(|camera| camera_query.get_mut(camera).ok())
198            {
199                let angle = match direction {
200                    Direction::Left => -0.1,
201                    Direction::Right => 0.1,
202                };
203                camera_transform.rotate_around(Vec3::ZERO, Quat::from_axis_angle(Vec3::Y, angle));
204            }
205        }
206    }
207}