Example for 2D Frustum Culling (#1503)

This adds a new project for showing off Frustum Culling.
(Master runs this at sub 1 FPS while with the frustum culling it runs at 144 FPS on my system)

Short clip of the project running:
https://streamable.com/vvzh2u
This commit is contained in:
Aaron Winter 2021-03-25 01:46:22 +00:00
parent b65ec82d46
commit 621cba4864
3 changed files with 100 additions and 0 deletions

View file

@ -94,6 +94,10 @@ path = "examples/hello_world.rs"
name = "contributors" name = "contributors"
path = "examples/2d/contributors.rs" path = "examples/2d/contributors.rs"
[[example]]
name = "many_sprites"
path = "examples/2d/many_sprites.rs"
[[example]] [[example]]
name = "sprite" name = "sprite"
path = "examples/2d/sprite.rs" path = "examples/2d/sprite.rs"

View file

@ -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<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
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::<f32>());
let rotation = Quat::from_rotation_z(rng.gen::<f32>());
let scale = Vec3::splat(rng.gen::<f32>() * 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<Time>, mut query: Query<(&mut Transform, &mut Position)>) {
for (mut transform, mut position) in query.iter_mut() {
position
.0
.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
position.0 =
position.0 * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
transform.translation = position.0.translation;
transform.rotation *= Quat::from_rotation_z(time.delta_seconds() / 2.0);
}
}
fn tick(time: Res<Time>, sprites: Query<&Sprite>, mut query: Query<&mut PrintTimer>) {
for mut timer in query.iter_mut() {
timer.0.tick(time.delta());
if timer.0.just_finished() {
println!("Sprites: {}", sprites.iter().count(),);
}
}
}

View file

@ -72,6 +72,7 @@ Example | Main | Description
Example | Main | Description Example | Main | Description
--- | --- | --- --- | --- | ---
`contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball! `contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball!
`many_sprites` | [`2d/many_sprites.rs`](./2d/many_sprites.rs) | Displays many sprites in a grid arragement! Used for performance testing.
`sprite` | [`2d/sprite.rs`](./2d/sprite.rs) | Renders a sprite `sprite` | [`2d/sprite.rs`](./2d/sprite.rs) | Renders a sprite
`sprite_sheet` | [`2d/sprite_sheet.rs`](./2d/sprite_sheet.rs) | Renders an animated sprite `sprite_sheet` | [`2d/sprite_sheet.rs`](./2d/sprite_sheet.rs) | Renders an animated sprite
`text2d` | [`2d/text2d.rs`](./2d/text2d.rs) | Generates text in 2d `text2d` | [`2d/text2d.rs`](./2d/text2d.rs) | Generates text in 2d