Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/many_sprites.rs
6592 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::{UpdateMode, 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 {
46
focused_mode: UpdateMode::Continuous,
47
unfocused_mode: UpdateMode::Continuous,
48
})
49
.add_systems(Startup, setup)
50
.add_systems(
51
Update,
52
(print_sprite_count, move_camera.after(print_sprite_count)),
53
)
54
.run();
55
}
56
57
fn setup(mut commands: Commands, assets: Res<AssetServer>, color_tint: Res<ColorTint>) {
58
warn!(include_str!("warning_string.txt"));
59
60
let mut rng = rand::rng();
61
62
let tile_size = Vec2::splat(64.0);
63
let map_size = Vec2::splat(320.0);
64
65
let half_x = (map_size.x / 2.0) as i32;
66
let half_y = (map_size.y / 2.0) as i32;
67
68
let sprite_handle = assets.load("branding/icon.png");
69
70
// Spawns the camera
71
72
commands.spawn(Camera2d);
73
74
// Builds and spawns the sprites
75
let mut sprites = vec![];
76
for y in -half_y..half_y {
77
for x in -half_x..half_x {
78
let position = Vec2::new(x as f32, y as f32);
79
let translation = (position * tile_size).extend(rng.random::<f32>());
80
let rotation = Quat::from_rotation_z(rng.random::<f32>());
81
let scale = Vec3::splat(rng.random::<f32>() * 2.0);
82
83
sprites.push((
84
Sprite {
85
image: sprite_handle.clone(),
86
custom_size: Some(tile_size),
87
color: if color_tint.0 {
88
COLORS[rng.random_range(0..3)]
89
} else {
90
Color::WHITE
91
},
92
..default()
93
},
94
Transform {
95
translation,
96
rotation,
97
scale,
98
},
99
));
100
}
101
}
102
commands.spawn_batch(sprites);
103
}
104
105
// System for rotating and translating the camera
106
fn move_camera(time: Res<Time>, mut camera_transform: Single<&mut Transform, With<Camera>>) {
107
camera_transform.rotate_z(time.delta_secs() * 0.5);
108
**camera_transform = **camera_transform
109
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());
110
}
111
112
#[derive(Deref, DerefMut)]
113
struct PrintingTimer(Timer);
114
115
impl Default for PrintingTimer {
116
fn default() -> Self {
117
Self(Timer::from_seconds(1.0, TimerMode::Repeating))
118
}
119
}
120
121
// System for printing the number of sprites on every tick of the timer
122
fn print_sprite_count(time: Res<Time>, mut timer: Local<PrintingTimer>, sprites: Query<&Sprite>) {
123
timer.tick(time.delta());
124
125
if timer.just_finished() {
126
info!("Sprites: {}", sprites.iter().count());
127
}
128
}
129
130