Skip to content

Commit 1ed4ace

Browse files
committed
Add many_cameras_lights stress test
1 parent 0f8c397 commit 1ed4ace

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,6 +2485,17 @@ description = "Test rendering of many UI elements"
24852485
category = "Stress Tests"
24862486
wasm = true
24872487

2488+
[[example]]
2489+
name = "many_cameras_lights"
2490+
path = "examples/stress_tests/many_cameras_lights.rs"
2491+
doc-scrape-examples = true
2492+
2493+
[package.metadata.example.many_cameras_lights]
2494+
name = "Many Cameras & Lights"
2495+
description = "Test rendering of many cameras and lights"
2496+
category = "Stress Tests"
2497+
wasm = true
2498+
24882499
[[example]]
24892500
name = "many_cubes"
24902501
path = "examples/stress_tests/many_cubes.rs"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ Example | Description
433433
[Bevymark](../examples/stress_tests/bevymark.rs) | A heavy sprite rendering workload to benchmark your system with Bevy
434434
[Many Animated Sprites](../examples/stress_tests/many_animated_sprites.rs) | Displays many animated sprites in a grid arrangement with slight offsets to their animation timers. Used for performance testing.
435435
[Many Buttons](../examples/stress_tests/many_buttons.rs) | Test rendering of many UI elements
436+
[Many Cameras & Lights](../examples/stress_tests/many_cameras_lights.rs) | Test rendering of many cameras and lights
436437
[Many Cubes](../examples/stress_tests/many_cubes.rs) | Simple benchmark to test per-entity draw overhead. Run with the `sphere` argument to test frustum culling
437438
[Many Foxes](../examples/stress_tests/many_foxes.rs) | Loads an animated fox model and spawns lots of them. Good for testing skinned mesh performance. Takes an unsigned integer argument for the number of foxes to spawn. Defaults to 1000
438439
[Many Gizmos](../examples/stress_tests/many_gizmos.rs) | Test rendering of many gizmos
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//! Test rendering of many cameras and lights
2+
3+
use std::f32::consts::PI;
4+
5+
use bevy::prelude::*;
6+
use bevy_render::camera::Viewport;
7+
8+
fn main() {
9+
App::new()
10+
.add_plugins(DefaultPlugins)
11+
.add_systems(Startup, setup)
12+
.add_systems(Update, rotate_cameras)
13+
.run();
14+
}
15+
16+
const CAMERA_ROWS: usize = 4;
17+
const CAMERA_COLS: usize = 4;
18+
const NUM_LIGHTS: usize = 5;
19+
20+
/// set up a simple 3D scene
21+
fn setup(
22+
mut commands: Commands,
23+
mut meshes: ResMut<Assets<Mesh>>,
24+
mut materials: ResMut<Assets<StandardMaterial>>,
25+
window: Query<&Window>,
26+
) {
27+
// circular base
28+
commands.spawn(PbrBundle {
29+
mesh: meshes.add(Circle::new(4.0)),
30+
material: materials.add(Color::WHITE),
31+
transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
32+
..default()
33+
});
34+
35+
// cube
36+
commands.spawn(PbrBundle {
37+
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
38+
material: materials.add(Color::WHITE),
39+
transform: Transform::from_xyz(0.0, 0.5, 0.0),
40+
..default()
41+
});
42+
43+
// lights
44+
for i in 0..NUM_LIGHTS {
45+
let angle = (i as f32) / (NUM_LIGHTS as f32) * PI * 2.0;
46+
commands.spawn(PointLightBundle {
47+
point_light: PointLight {
48+
color: Color::hsv(angle.to_degrees(), 1.0, 1.0),
49+
intensity: 2_000_000.0 / NUM_LIGHTS as f32,
50+
shadows_enabled: true,
51+
..default()
52+
},
53+
transform: Transform::from_xyz(angle.sin() * 4.0, 2.0, angle.cos() * 4.0),
54+
..default()
55+
});
56+
}
57+
58+
// cameras
59+
let window = window.single();
60+
let width = window.resolution.width() / CAMERA_COLS as f32 * window.resolution.scale_factor();
61+
let height = window.resolution.height() / CAMERA_ROWS as f32 * window.resolution.scale_factor();
62+
let mut i = 0;
63+
for y in 0..CAMERA_COLS {
64+
for x in 0..CAMERA_ROWS {
65+
let angle = i as f32 / (CAMERA_ROWS * CAMERA_COLS) as f32 * PI * 2.0;
66+
commands.spawn(Camera3dBundle {
67+
transform: Transform::from_xyz(angle.sin() * 4.0, 2.5, angle.cos() * 4.0)
68+
.looking_at(Vec3::ZERO, Vec3::Y),
69+
camera: Camera {
70+
viewport: Some(Viewport {
71+
physical_position: UVec2::new(
72+
(x as f32 * width) as u32,
73+
(y as f32 * height) as u32,
74+
),
75+
physical_size: UVec2::new(width as u32, height as u32),
76+
..default()
77+
}),
78+
order: i,
79+
..default()
80+
},
81+
..default()
82+
});
83+
i += 1;
84+
}
85+
}
86+
}
87+
88+
fn rotate_cameras(time: Res<Time>, mut query: Query<&mut Transform, With<Camera>>) {
89+
for mut transform in query.iter_mut() {
90+
transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(time.delta_seconds()));
91+
}
92+
}

0 commit comments

Comments
 (0)