diff --git a/Cargo.toml b/Cargo.toml index ee901649c3d94..3353a5a09e8bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,6 +94,10 @@ path = "examples/hello_world.rs" name = "contributors" path = "examples/2d/contributors.rs" +[[example]] +name = "many_sprites" +path = "examples/2d/many_sprites.rs" + [[example]] name = "sprite" path = "examples/2d/sprite.rs" diff --git a/examples/2d/many_sprites.rs b/examples/2d/many_sprites.rs new file mode 100644 index 0000000000000..ef0a82571ea3a --- /dev/null +++ b/examples/2d/many_sprites.rs @@ -0,0 +1,95 @@ +use bevy::{ + diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + math::Quat, + prelude::*, +}; + +use rand::Rng; + +const CAMERA_SPEED: f32 = 1000.0; + +pub struct PrintTimer(Timer); +pub struct Position(Transform); + +///This example is for performance testing purposes. +///See https://github.com/bevyengine/bevy/pull/1492 +fn main() { + App::build() + .add_plugin(LogDiagnosticsPlugin::default()) + .add_plugin(FrameTimeDiagnosticsPlugin::default()) + .insert_resource(bevy::log::LogSettings { + level: bevy::log::Level::DEBUG, + ..Default::default() + }) + .add_plugins(DefaultPlugins) + .add_startup_system(setup.system()) + .add_system(tick.system().label("Tick")) + .add_system(move_camera.system().after("Tick")) + .run() +} + +fn setup( + mut commands: Commands, + assets: Res, + mut materials: 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 sprite_handle = materials.add(assets.load("branding/icon.png").into()); + + commands + .spawn() + .insert_bundle(OrthographicCameraBundle::new_2d()) + .insert(PrintTimer(Timer::from_seconds(1.0, true))) + .insert(Position(Transform::from_translation(Vec3::new( + 0.0, 0.0, 1000.0, + )))); + + 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); + + commands.spawn().insert_bundle(SpriteBundle { + material: sprite_handle.clone(), + transform: Transform { + translation, + rotation, + scale, + }, + sprite: Sprite::new(tile_size), + ..Default::default() + }); + } + } +} + +fn move_camera(time: Res