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