//! Renders a lot of sprites to allow performance testing. //! See //! //! It sets up many 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. //! //! Add the `--colored` arg to run with color tinted sprites. This will cause the sprites to be rendered //! in multiple batches, reducing performance but useful for testing. use bevy::{ diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, window::PresentMode, }; use rand::Rng; const CAMERA_SPEED: f32 = 1000.0; const COLORS: [Color; 3] = [Color::BLUE, Color::WHITE, Color::RED]; #[derive(Resource)] struct ColorTint(bool); fn main() { App::new() .insert_resource(WindowDescriptor { present_mode: PresentMode::AutoNoVsync, ..default() }) .insert_resource(ColorTint( std::env::args().nth(1).unwrap_or_default() == "--colored", )) // 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(print_sprite_count) .add_system(move_camera.after(print_sprite_count)) .run(); } fn setup(mut commands: Commands, assets: Res, color_tint: Res) { warn!(include_str!("warning_string.txt")); 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 sprite_handle = assets.load("branding/icon.png"); // Spawns the camera commands.spawn(( Camera2dBundle::default(), Transform::from_xyz(0.0, 0.0, 1000.0), )); // Builds and spawns the sprites let mut sprites = vec![]; 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); sprites.push(SpriteBundle { texture: sprite_handle.clone(), transform: Transform { translation, rotation, scale, }, sprite: Sprite { custom_size: Some(tile_size), color: if color_tint.0 { COLORS[rng.gen_range(0..3)] } else { Color::WHITE }, ..default() }, ..default() }); } } commands.spawn_batch(sprites); } // System for rotating and translating the camera fn move_camera(time: Res