Path: blob/main/examples/stress_tests/many_animated_sprites.rs
6592 views
//! Renders a lot of animated sprites to allow performance testing.1//!2//! This example sets up many animated sprites in different sizes, rotations, and scales in the world.3//! It also moves the camera over them to see how well frustum culling works.45use std::time::Duration;67use bevy::{8diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},9prelude::*,10window::{PresentMode, WindowResolution},11winit::{UpdateMode, WinitSettings},12};1314use rand::Rng;1516const CAMERA_SPEED: f32 = 1000.0;1718fn main() {19App::new()20// Since this is also used as a benchmark, we want it to display performance data.21.add_plugins((22LogDiagnosticsPlugin::default(),23FrameTimeDiagnosticsPlugin::default(),24DefaultPlugins.set(WindowPlugin {25primary_window: Some(Window {26present_mode: PresentMode::AutoNoVsync,27resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),28..default()29}),30..default()31}),32))33.insert_resource(WinitSettings {34focused_mode: UpdateMode::Continuous,35unfocused_mode: UpdateMode::Continuous,36})37.add_systems(Startup, setup)38.add_systems(39Update,40(41animate_sprite,42print_sprite_count,43move_camera.after(print_sprite_count),44),45)46.run();47}4849fn setup(50mut commands: Commands,51assets: Res<AssetServer>,52mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,53) {54warn!(include_str!("warning_string.txt"));5556let mut rng = rand::rng();5758let tile_size = Vec2::splat(64.0);59let map_size = Vec2::splat(320.0);6061let half_x = (map_size.x / 2.0) as i32;62let half_y = (map_size.y / 2.0) as i32;6364let texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png");65let texture_atlas = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);66let texture_atlas_handle = texture_atlases.add(texture_atlas);6768// Spawns the camera6970commands.spawn(Camera2d);7172// Builds and spawns the sprites73for y in -half_y..half_y {74for x in -half_x..half_x {75let position = Vec2::new(x as f32, y as f32);76let translation = (position * tile_size).extend(rng.random::<f32>());77let rotation = Quat::from_rotation_z(rng.random::<f32>());78let scale = Vec3::splat(rng.random::<f32>() * 2.0);79let mut timer = Timer::from_seconds(0.1, TimerMode::Repeating);80timer.set_elapsed(Duration::from_secs_f32(rng.random::<f32>()));8182commands.spawn((83Sprite {84image: texture_handle.clone(),85texture_atlas: Some(TextureAtlas::from(texture_atlas_handle.clone())),86custom_size: Some(tile_size),87..default()88},89Transform {90translation,91rotation,92scale,93},94AnimationTimer(timer),95));96}97}98}99100// System for rotating and translating the camera101fn move_camera(time: Res<Time>, mut camera_transform: Single<&mut Transform, With<Camera>>) {102camera_transform.rotate(Quat::from_rotation_z(time.delta_secs() * 0.5));103**camera_transform = **camera_transform104* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());105}106107#[derive(Component, Deref, DerefMut)]108struct AnimationTimer(Timer);109110fn animate_sprite(111time: Res<Time>,112texture_atlases: Res<Assets<TextureAtlasLayout>>,113mut query: Query<(&mut AnimationTimer, &mut Sprite)>,114) {115for (mut timer, mut sprite) in query.iter_mut() {116timer.tick(time.delta());117if timer.just_finished() {118let Some(atlas) = &mut sprite.texture_atlas else {119continue;120};121let texture_atlas = texture_atlases.get(&atlas.layout).unwrap();122atlas.index = (atlas.index + 1) % texture_atlas.textures.len();123}124}125}126127#[derive(Deref, DerefMut)]128struct PrintingTimer(Timer);129130impl Default for PrintingTimer {131fn default() -> Self {132Self(Timer::from_seconds(1.0, TimerMode::Repeating))133}134}135136// System for printing the number of sprites on every tick of the timer137fn print_sprite_count(time: Res<Time>, mut timer: Local<PrintingTimer>, sprites: Query<&Sprite>) {138timer.tick(time.delta());139140if timer.just_finished() {141info!("Sprites: {}", sprites.iter().count());142}143}144145146