Skip to content

Commit a9e70c6

Browse files
committed
added example project for showing off frustum culling.
1 parent 47004df commit a9e70c6

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ path = "examples/hello_world.rs"
9494
name = "contributors"
9595
path = "examples/2d/contributors.rs"
9696

97+
[[example]]
98+
name = "many_sprites"
99+
path = "examples/2d/many_sprites.rs"
100+
97101
[[example]]
98102
name = "sprite"
99103
path = "examples/2d/sprite.rs"

examples/2d/many_sprites.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use bevy::{
2+
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
3+
math::Quat,
4+
prelude::*,
5+
};
6+
7+
use rand::Rng;
8+
9+
const CAMERA_SPEED: f32 = 1000.0;
10+
11+
pub struct PrintTimer(Timer);
12+
pub struct Position(Transform);
13+
14+
///This example is for performance testing purposes.
15+
///See https://github.com/bevyengine/bevy/pull/1492
16+
fn main() {
17+
App::build()
18+
.add_plugin(LogDiagnosticsPlugin::default())
19+
.add_plugin(FrameTimeDiagnosticsPlugin::default())
20+
.insert_resource(bevy::log::LogSettings {
21+
level: bevy::log::Level::DEBUG,
22+
..Default::default()
23+
})
24+
.add_plugins(DefaultPlugins)
25+
.add_startup_system(setup.system())
26+
.add_system(tick.system().label("Tick"))
27+
.add_system(move_camera.system().after("Tick"))
28+
.run()
29+
}
30+
31+
fn setup(
32+
mut commands: Commands,
33+
assets: Res<AssetServer>,
34+
mut materials: ResMut<Assets<ColorMaterial>>,
35+
) {
36+
let mut rng = rand::thread_rng();
37+
38+
let tile_size = Vec2::splat(64.0);
39+
let map_size = Vec2::splat(320.0);
40+
41+
let half_x = (map_size.x / 2.0) as i32;
42+
let half_y = (map_size.y / 2.0) as i32;
43+
44+
let sprite_handle = materials.add(assets.load("branding/icon.png").into());
45+
46+
commands
47+
.spawn()
48+
.insert_bundle(OrthographicCameraBundle::new_2d())
49+
.insert(PrintTimer(Timer::from_seconds(1.0, true)))
50+
.insert(Position(Transform::from_translation(Vec3::new(
51+
0.0, 0.0, 1000.0,
52+
))));
53+
54+
for y in -half_y..half_y {
55+
for x in -half_x..half_x {
56+
let position = Vec2::new(x as f32, y as f32);
57+
let translation = (position * tile_size).extend(rng.gen::<f32>());
58+
let rotation = Quat::from_rotation_z(rng.gen::<f32>());
59+
let scale = Vec3::splat(rng.gen::<f32>() * 2.0);
60+
61+
commands.spawn().insert_bundle(SpriteBundle {
62+
material: sprite_handle.clone(),
63+
transform: Transform {
64+
translation,
65+
rotation,
66+
scale,
67+
},
68+
sprite: Sprite::new(tile_size),
69+
..Default::default()
70+
});
71+
}
72+
}
73+
}
74+
75+
fn move_camera(time: Res<Time>, mut query: Query<(&mut Transform, &mut Position)>) {
76+
for (mut transform, mut position) in query.iter_mut() {
77+
position
78+
.0
79+
.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
80+
position.0 =
81+
position.0 * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
82+
transform.translation = position.0.translation;
83+
transform.rotation *= Quat::from_rotation_z(time.delta_seconds() / 2.0);
84+
}
85+
}
86+
87+
fn tick(time: Res<Time>, sprites: Query<&Sprite>, mut query: Query<&mut PrintTimer>) {
88+
for mut timer in query.iter_mut() {
89+
timer.0.tick(time.delta());
90+
91+
if timer.0.just_finished() {
92+
println!("Sprites: {}", sprites.iter().count(),);
93+
}
94+
}
95+
}

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Example | Main | Description
7272
Example | Main | Description
7373
--- | --- | ---
7474
`contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball!
75+
`many_sprites` | [`2d/many_sprites.rs`](./2d/many_sprites.rs) | Displays many sprites in a grid arragement! Used for performance testing.
7576
`sprite` | [`2d/sprite.rs`](./2d/sprite.rs) | Renders a sprite
7677
`sprite_sheet` | [`2d/sprite_sheet.rs`](./2d/sprite_sheet.rs) | Renders an animated sprite
7778
`text2d` | [`2d/text2d.rs`](./2d/text2d.rs) | Generates text in 2d

0 commit comments

Comments
 (0)