Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/many_animated_sprites.rs
6592 views
1
//! Renders a lot of animated sprites to allow performance testing.
2
//!
3
//! This example sets up many animated sprites in different sizes, rotations, and scales in the world.
4
//! It also moves the camera over them to see how well frustum culling works.
5
6
use std::time::Duration;
7
8
use bevy::{
9
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
10
prelude::*,
11
window::{PresentMode, WindowResolution},
12
winit::{UpdateMode, WinitSettings},
13
};
14
15
use rand::Rng;
16
17
const CAMERA_SPEED: f32 = 1000.0;
18
19
fn main() {
20
App::new()
21
// Since this is also used as a benchmark, we want it to display performance data.
22
.add_plugins((
23
LogDiagnosticsPlugin::default(),
24
FrameTimeDiagnosticsPlugin::default(),
25
DefaultPlugins.set(WindowPlugin {
26
primary_window: Some(Window {
27
present_mode: PresentMode::AutoNoVsync,
28
resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
29
..default()
30
}),
31
..default()
32
}),
33
))
34
.insert_resource(WinitSettings {
35
focused_mode: UpdateMode::Continuous,
36
unfocused_mode: UpdateMode::Continuous,
37
})
38
.add_systems(Startup, setup)
39
.add_systems(
40
Update,
41
(
42
animate_sprite,
43
print_sprite_count,
44
move_camera.after(print_sprite_count),
45
),
46
)
47
.run();
48
}
49
50
fn setup(
51
mut commands: Commands,
52
assets: Res<AssetServer>,
53
mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
54
) {
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 texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png");
66
let texture_atlas = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);
67
let texture_atlas_handle = texture_atlases.add(texture_atlas);
68
69
// Spawns the camera
70
71
commands.spawn(Camera2d);
72
73
// Builds and spawns the sprites
74
for y in -half_y..half_y {
75
for x in -half_x..half_x {
76
let position = Vec2::new(x as f32, y as f32);
77
let translation = (position * tile_size).extend(rng.random::<f32>());
78
let rotation = Quat::from_rotation_z(rng.random::<f32>());
79
let scale = Vec3::splat(rng.random::<f32>() * 2.0);
80
let mut timer = Timer::from_seconds(0.1, TimerMode::Repeating);
81
timer.set_elapsed(Duration::from_secs_f32(rng.random::<f32>()));
82
83
commands.spawn((
84
Sprite {
85
image: texture_handle.clone(),
86
texture_atlas: Some(TextureAtlas::from(texture_atlas_handle.clone())),
87
custom_size: Some(tile_size),
88
..default()
89
},
90
Transform {
91
translation,
92
rotation,
93
scale,
94
},
95
AnimationTimer(timer),
96
));
97
}
98
}
99
}
100
101
// System for rotating and translating the camera
102
fn move_camera(time: Res<Time>, mut camera_transform: Single<&mut Transform, With<Camera>>) {
103
camera_transform.rotate(Quat::from_rotation_z(time.delta_secs() * 0.5));
104
**camera_transform = **camera_transform
105
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());
106
}
107
108
#[derive(Component, Deref, DerefMut)]
109
struct AnimationTimer(Timer);
110
111
fn animate_sprite(
112
time: Res<Time>,
113
texture_atlases: Res<Assets<TextureAtlasLayout>>,
114
mut query: Query<(&mut AnimationTimer, &mut Sprite)>,
115
) {
116
for (mut timer, mut sprite) in query.iter_mut() {
117
timer.tick(time.delta());
118
if timer.just_finished() {
119
let Some(atlas) = &mut sprite.texture_atlas else {
120
continue;
121
};
122
let texture_atlas = texture_atlases.get(&atlas.layout).unwrap();
123
atlas.index = (atlas.index + 1) % texture_atlas.textures.len();
124
}
125
}
126
}
127
128
#[derive(Deref, DerefMut)]
129
struct PrintingTimer(Timer);
130
131
impl Default for PrintingTimer {
132
fn default() -> Self {
133
Self(Timer::from_seconds(1.0, TimerMode::Repeating))
134
}
135
}
136
137
// System for printing the number of sprites on every tick of the timer
138
fn print_sprite_count(time: Res<Time>, mut timer: Local<PrintingTimer>, sprites: Query<&Sprite>) {
139
timer.tick(time.delta());
140
141
if timer.just_finished() {
142
info!("Sprites: {}", sprites.iter().count());
143
}
144
}
145
146