Path: blob/main/examples/stress_tests/many_animated_sprites.rs
6849 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::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::continuous())34.add_systems(Startup, setup)35.add_systems(36Update,37(38animate_sprite,39print_sprite_count,40move_camera.after(print_sprite_count),41),42)43.run();44}4546fn setup(47mut commands: Commands,48assets: Res<AssetServer>,49mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,50) {51warn!(include_str!("warning_string.txt"));5253let mut rng = rand::rng();5455let tile_size = Vec2::splat(64.0);56let map_size = Vec2::splat(320.0);5758let half_x = (map_size.x / 2.0) as i32;59let half_y = (map_size.y / 2.0) as i32;6061let texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png");62let texture_atlas = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);63let texture_atlas_handle = texture_atlases.add(texture_atlas);6465// Spawns the camera6667commands.spawn(Camera2d);6869// Builds and spawns the sprites70for y in -half_y..half_y {71for x in -half_x..half_x {72let position = Vec2::new(x as f32, y as f32);73let translation = (position * tile_size).extend(rng.random::<f32>());74let rotation = Quat::from_rotation_z(rng.random::<f32>());75let scale = Vec3::splat(rng.random::<f32>() * 2.0);76let mut timer = Timer::from_seconds(0.1, TimerMode::Repeating);77timer.set_elapsed(Duration::from_secs_f32(rng.random::<f32>()));7879commands.spawn((80Sprite {81image: texture_handle.clone(),82texture_atlas: Some(TextureAtlas::from(texture_atlas_handle.clone())),83custom_size: Some(tile_size),84..default()85},86Transform {87translation,88rotation,89scale,90},91AnimationTimer(timer),92));93}94}95}9697// System for rotating and translating the camera98fn move_camera(time: Res<Time>, mut camera_transform: Single<&mut Transform, With<Camera>>) {99camera_transform.rotate(Quat::from_rotation_z(time.delta_secs() * 0.5));100**camera_transform = **camera_transform101* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());102}103104#[derive(Component, Deref, DerefMut)]105struct AnimationTimer(Timer);106107fn animate_sprite(108time: Res<Time>,109texture_atlases: Res<Assets<TextureAtlasLayout>>,110mut query: Query<(&mut AnimationTimer, &mut Sprite)>,111) {112for (mut timer, mut sprite) in query.iter_mut() {113timer.tick(time.delta());114if timer.just_finished() {115let Some(atlas) = &mut sprite.texture_atlas else {116continue;117};118let texture_atlas = texture_atlases.get(&atlas.layout).unwrap();119atlas.index = (atlas.index + 1) % texture_atlas.textures.len();120}121}122}123124#[derive(Deref, DerefMut)]125struct PrintingTimer(Timer);126127impl Default for PrintingTimer {128fn default() -> Self {129Self(Timer::from_seconds(1.0, TimerMode::Repeating))130}131}132133// System for printing the number of sprites on every tick of the timer134fn print_sprite_count(time: Res<Time>, mut timer: Local<PrintingTimer>, sprites: Query<&Sprite>) {135timer.tick(time.delta());136137if timer.just_finished() {138info!("Sprites: {}", sprites.iter().count());139}140}141142143