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