From f7e0d99e74bcb4f09e9ca08e79a421458c67b598 Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Thu, 23 Jun 2022 17:44:04 -0700 Subject: [PATCH 1/7] Simplisitic stress test based on many_sprites and sprite_sheet examples --- Cargo.toml | 4 + .../stress_tests/many_animated_sprites.rs | 127 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 examples/stress_tests/many_animated_sprites.rs diff --git a/Cargo.toml b/Cargo.toml index 62be72f770747..4874f9e5e83fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -558,6 +558,10 @@ path = "examples/shader/compute_shader_game_of_life.rs" name = "bevymark" path = "examples/stress_tests/bevymark.rs" +[[example]] +name = "many_animated_sprites" +path = "examples/stress_tests/many_animated_sprites.rs" + [[example]] name = "many_cubes" path = "examples/stress_tests/many_cubes.rs" diff --git a/examples/stress_tests/many_animated_sprites.rs b/examples/stress_tests/many_animated_sprites.rs new file mode 100644 index 0000000000000..01b37300a3c49 --- /dev/null +++ b/examples/stress_tests/many_animated_sprites.rs @@ -0,0 +1,127 @@ +//! Renders a lot of animated sprites to allow performance testing. +//! +//! It sets up many animated sprites in different sizes and rotations, and at different scales in the world, +//! and moves the camera over them to see how well frustum culling works. +//! +//! To measure performance realistically, be sure to run this in release mode. +//! `cargo run --example many_animated_sprites --release` + + +use std::time::Duration; + +use bevy::{ + diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + math::Quat, + prelude::*, + render::camera::Camera, +}; + +use rand::Rng; + +const CAMERA_SPEED: f32 = 1000.0; + +fn main() { + App::new() + // Since this is also used as a benchmark, we want it to display performance data. + .add_plugin(LogDiagnosticsPlugin::default()) + .add_plugin(FrameTimeDiagnosticsPlugin::default()) + .add_plugins(DefaultPlugins) + .add_startup_system(setup) + .add_system(animate_sprite) + .add_system(print_sprite_count) + .add_system(move_camera.after(print_sprite_count)) + .run(); +} + +fn setup(mut commands: Commands, assets: Res, mut texture_atlases: ResMut>) { + let mut rng = rand::thread_rng(); + + let tile_size = Vec2::splat(64.0); + let map_size = Vec2::splat(320.0); + + let half_x = (map_size.x / 2.0) as i32; + let half_y = (map_size.y / 2.0) as i32; + + let texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png"); + let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1); + let texture_atlas_handle = texture_atlases.add(texture_atlas); + + // Spawns the camera + commands + .spawn() + .insert_bundle(Camera2dBundle::default()) + .insert(Transform::from_xyz(0.0, 0.0, 1000.0)); + + // Builds and spawns the sprites + for y in -half_y..half_y { + for x in -half_x..half_x { + let position = Vec2::new(x as f32, y as f32); + let translation = (position * tile_size).extend(rng.gen::()); + let rotation = Quat::from_rotation_z(rng.gen::()); + let scale = Vec3::splat(rng.gen::() * 2.0); + let mut timer = Timer::from_seconds(0.1, true); + timer.set_elapsed(Duration::from_secs_f32(rng.gen::())); + + commands.spawn_bundle(SpriteSheetBundle { + texture_atlas: texture_atlas_handle.clone(), + transform: Transform { + translation, + rotation, + scale, + }, + sprite: TextureAtlasSprite { + custom_size: Some(tile_size), + ..default() + }, + ..default() + }).insert(AnimationTimer(timer)); + } + } +} + +// System for rotating and translating the camera +fn move_camera(time: Res