Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/many_sprites.rs
6849 views
1
//! Renders a lot of sprites to allow performance testing.
2
//! See <https://github.com/bevyengine/bevy/pull/1492>
3
//!
4
//! This example sets up many sprites in different sizes, rotations, and scales in the world.
5
//! It also moves the camera over them to see how well frustum culling works.
6
//!
7
//! Add the `--colored` arg to run with color tinted sprites. This will cause the sprites to be rendered
8
//! in multiple batches, reducing performance but useful for testing.
9
10
use bevy::{
11
color::palettes::css::*,
12
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
13
prelude::*,
14
window::{PresentMode, WindowResolution},
15
winit::WinitSettings,
16
};
17
18
use rand::Rng;
19
20
const CAMERA_SPEED: f32 = 1000.0;
21
22
const COLORS: [Color; 3] = [Color::Srgba(BLUE), Color::Srgba(WHITE), Color::Srgba(RED)];
23
24
#[derive(Resource)]
25
struct ColorTint(bool);
26
27
fn main() {
28
App::new()
29
.insert_resource(ColorTint(
30
std::env::args().nth(1).unwrap_or_default() == "--colored",
31
))
32
// Since this is also used as a benchmark, we want it to display performance data.
33
.add_plugins((
34
LogDiagnosticsPlugin::default(),
35
FrameTimeDiagnosticsPlugin::default(),
36
DefaultPlugins.set(WindowPlugin {
37
primary_window: Some(Window {
38
present_mode: PresentMode::AutoNoVsync,
39
resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
40
..default()
41
}),
42
..default()
43
}),
44
))
45
.insert_resource(WinitSettings::continuous())
46
.add_systems(Startup, setup)
47
.add_systems(
48
Update,
49
(print_sprite_count, move_camera.after(print_sprite_count)),
50
)
51
.run();
52
}
53
54
fn setup(mut commands: Commands, assets: Res<AssetServer>, color_tint: Res<ColorTint>) {
55
warn!(include_str!("warning_string.txt"));
56
57
let mut rng = rand::rng();
58
59
let tile_size = Vec2::splat(64.0);
60
let map_size = Vec2::splat(320.0);
61
62
let half_x = (map_size.x / 2.0) as i32;
63
let half_y = (map_size.y / 2.0) as i32;
64
65
let sprite_handle = assets.load("branding/icon.png");
66
67
// Spawns the camera
68
69
commands.spawn(Camera2d);
70
71
// Builds and spawns the sprites
72
let mut sprites = vec![];
73
for y in -half_y..half_y {
74
for x in -half_x..half_x {
75
let position = Vec2::new(x as f32, y as f32);
76
let translation = (position * tile_size).extend(rng.random::<f32>());
77
let rotation = Quat::from_rotation_z(rng.random::<f32>());
78
let scale = Vec3::splat(rng.random::<f32>() * 2.0);
79
80
sprites.push((
81
Sprite {
82
image: sprite_handle.clone(),
83
custom_size: Some(tile_size),
84
color: if color_tint.0 {
85
COLORS[rng.random_range(0..3)]
86
} else {
87
Color::WHITE
88
},
89
..default()
90
},
91
Transform {
92
translation,
93
rotation,
94
scale,
95
},
96
));
97
}
98
}
99
commands.spawn_batch(sprites);
100
}
101
102
// System for rotating and translating the camera
103
fn move_camera(time: Res<Time>, mut camera_transform: Single<&mut Transform, With<Camera>>) {
104
camera_transform.rotate_z(time.delta_secs() * 0.5);
105
**camera_transform = **camera_transform
106
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());
107
}
108
109
#[derive(Deref, DerefMut)]
110
struct PrintingTimer(Timer);
111
112
impl Default for PrintingTimer {
113
fn default() -> Self {
114
Self(Timer::from_seconds(1.0, TimerMode::Repeating))
115
}
116
}
117
118
// System for printing the number of sprites on every tick of the timer
119
fn print_sprite_count(time: Res<Time>, mut timer: Local<PrintingTimer>, sprites: Query<&Sprite>) {
120
timer.tick(time.delta());
121
122
if timer.just_finished() {
123
info!("Sprites: {}", sprites.iter().count());
124
}
125
}
126
127