Path: blob/main/examples/stress_tests/many_animated_sprite_meshes.rs
9374 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.4//!5//! This is a modified version of `many_animated_sprites` but based on [`SpriteMesh`]67use std::time::Duration;89use bevy::{10diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},11prelude::*,12window::{PresentMode, WindowResolution},13winit::WinitSettings,14};1516use rand::Rng;1718const CAMERA_SPEED: f32 = 1000.0;1920fn main() {21App::new()22// Since this is also used as a benchmark, we want it to display performance data.23.add_plugins((24LogDiagnosticsPlugin::default(),25FrameTimeDiagnosticsPlugin::default(),26DefaultPlugins.set(WindowPlugin {27primary_window: Some(Window {28present_mode: PresentMode::AutoNoVsync,29resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),30..default()31}),32..default()33}),34))35.insert_resource(WinitSettings::continuous())36.add_systems(Startup, setup)37.add_systems(38Update,39(40animate_sprite,41print_sprite_count,42move_camera.after(print_sprite_count),43),44)45.run();46}4748fn setup(49mut commands: Commands,50assets: Res<AssetServer>,51mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,52) {53warn!(include_str!("warning_string.txt"));5455let mut rng = rand::rng();5657let tile_size = Vec2::splat(64.0);58let map_size = Vec2::splat(320.0);5960let half_x = (map_size.x / 2.0) as i32;61let half_y = (map_size.y / 2.0) as i32;6263let texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png");64let texture_atlas = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);65let texture_atlas_handle = texture_atlases.add(texture_atlas);6667// Spawns the camera6869commands.spawn(Camera2d);7071// Builds and spawns the sprites72for y in -half_y..half_y {73for x in -half_x..half_x {74let position = Vec2::new(x as f32, y as f32);75let translation = (position * tile_size).extend(rng.random::<f32>());76let rotation = Quat::from_rotation_z(rng.random::<f32>());77let scale = Vec3::splat(rng.random::<f32>() * 2.0);78let mut timer = Timer::from_seconds(0.1, TimerMode::Repeating);79timer.set_elapsed(Duration::from_secs_f32(rng.random::<f32>()));8081commands.spawn((82SpriteMesh {83image: texture_handle.clone(),84texture_atlas: Some(TextureAtlas::from(texture_atlas_handle.clone())),85custom_size: Some(tile_size),86..default()87},88Transform {89translation,90rotation,91scale,92},93AnimationTimer(timer),94));95}96}97}9899// System for rotating and translating the camera100fn move_camera(time: Res<Time>, mut camera_transform: Single<&mut Transform, With<Camera>>) {101camera_transform.rotate(Quat::from_rotation_z(time.delta_secs() * 0.5));102**camera_transform = **camera_transform103* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());104}105106#[derive(Component, Deref, DerefMut)]107struct AnimationTimer(Timer);108109fn animate_sprite(110time: Res<Time>,111texture_atlases: Res<Assets<TextureAtlasLayout>>,112mut query: Query<(&mut AnimationTimer, &mut SpriteMesh)>,113) {114for (mut timer, mut sprite) in query.iter_mut() {115timer.tick(time.delta());116if timer.just_finished() {117let Some(atlas) = &mut sprite.texture_atlas else {118continue;119};120let texture_atlas = texture_atlases.get(&atlas.layout).unwrap();121atlas.index = (atlas.index + 1) % texture_atlas.textures.len();122}123}124}125126#[derive(Deref, DerefMut)]127struct PrintingTimer(Timer);128129impl Default for PrintingTimer {130fn default() -> Self {131Self(Timer::from_seconds(1.0, TimerMode::Repeating))132}133}134135// System for printing the number of sprites on every tick of the timer136fn print_sprite_count(137time: Res<Time>,138mut timer: Local<PrintingTimer>,139sprites: Query<&SpriteMesh>,140) {141timer.tick(time.delta());142143if timer.just_finished() {144info!("Sprites: {}", sprites.iter().count());145}146}147148149