Skip to main content

many_cameras_lights/
many_cameras_lights.rs

1//! Test rendering of many cameras and lights
2
3use std::f32::consts::PI;
4
5use bevy::{
6    camera::Viewport,
7    diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
8    math::ops::{cos, sin},
9    prelude::*,
10    window::{PresentMode, WindowResolution},
11    winit::WinitSettings,
12};
13
14fn main() {
15    App::new()
16        .add_plugins((
17            DefaultPlugins.set(WindowPlugin {
18                primary_window: Some(Window {
19                    present_mode: PresentMode::AutoNoVsync,
20                    resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
21                    ..default()
22                }),
23                ..default()
24            }),
25            FrameTimeDiagnosticsPlugin::default(),
26            LogDiagnosticsPlugin::default(),
27        ))
28        .insert_resource(WinitSettings::continuous())
29        .add_systems(Startup, setup)
30        .add_systems(Update, rotate_cameras)
31        .run();
32}
33
34const CAMERA_ROWS: usize = 4;
35const CAMERA_COLS: usize = 4;
36const NUM_LIGHTS: usize = 5;
37
38/// set up a simple 3D scene
39fn setup(
40    mut commands: Commands,
41    mut meshes: ResMut<Assets<Mesh>>,
42    mut materials: ResMut<Assets<StandardMaterial>>,
43    window: Query<&Window>,
44) -> Result {
45    warn!(include_str!("warning_string.txt"));
46
47    // circular base
48    commands.spawn((
49        Mesh3d(meshes.add(Circle::new(4.0))),
50        MeshMaterial3d(materials.add(Color::WHITE)),
51        Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
52    ));
53
54    // cube
55    commands.spawn((
56        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
57        MeshMaterial3d(materials.add(Color::WHITE)),
58        Transform::from_xyz(0.0, 0.5, 0.0),
59    ));
60
61    // lights
62    for i in 0..NUM_LIGHTS {
63        let angle = (i as f32) / (NUM_LIGHTS as f32) * PI * 2.0;
64        commands.spawn((
65            PointLight {
66                color: Color::hsv(angle.to_degrees(), 1.0, 1.0),
67                intensity: 2_000_000.0 / NUM_LIGHTS as f32,
68                shadow_maps_enabled: true,
69                ..default()
70            },
71            Transform::from_xyz(sin(angle) * 4.0, 2.0, cos(angle) * 4.0),
72        ));
73    }
74
75    // cameras
76    let window = window.single()?;
77    let width = window.resolution.width() / CAMERA_COLS as f32 * window.resolution.scale_factor();
78    let height = window.resolution.height() / CAMERA_ROWS as f32 * window.resolution.scale_factor();
79    let mut i = 0;
80    for y in 0..CAMERA_COLS {
81        for x in 0..CAMERA_ROWS {
82            let angle = i as f32 / (CAMERA_ROWS * CAMERA_COLS) as f32 * PI * 2.0;
83            commands.spawn((
84                Camera3d::default(),
85                Camera {
86                    viewport: Some(Viewport {
87                        physical_position: UVec2::new(
88                            (x as f32 * width) as u32,
89                            (y as f32 * height) as u32,
90                        ),
91                        physical_size: UVec2::new(width as u32, height as u32),
92                        ..default()
93                    }),
94                    order: i,
95                    ..default()
96                },
97                Transform::from_xyz(sin(angle) * 4.0, 2.5, cos(angle) * 4.0)
98                    .looking_at(Vec3::ZERO, Vec3::Y),
99            ));
100            i += 1;
101        }
102    }
103    Ok(())
104}
105
106fn rotate_cameras(time: Res<Time>, mut query: Query<&mut Transform, With<Camera>>) {
107    for mut transform in query.iter_mut() {
108        transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(time.delta_secs()));
109    }
110}