Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix frustum culling w/ sprite transform scaling #2753

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ name = "hello_world"
path = "examples/hello_world.rs"

# 2D Rendering
[[example]]
name = "frustum"
path = "examples/2d/frustum.rs"

[[example]]
name = "contributors"
path = "examples/2d/contributors.rs"
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_sprite/src/frustum_culling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ pub fn sprite_frustum_culling_system(
if let Ok(camera_transform) = camera_transforms.get(active_camera_entity) {
let camera_size = window_size * camera_transform.scale.truncate();

let rect = Rect {
let camera_rect = Rect {
position: camera_transform.translation.truncate(),
size: camera_size,
};

for (entity, drawable_transform, sprite) in sprites.iter() {
let sprite_rect = Rect {
position: drawable_transform.translation.truncate(),
size: sprite.size,
size: sprite.size * drawable_transform.scale.truncate(),
};

if rect.is_intersecting(sprite_rect) {
if camera_rect.is_intersecting(sprite_rect) {
if culled_sprites.get(entity).is_ok() {
commands.entity(entity).remove::<OutsideFrustum>();
}
Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn atlas_frustum_culling_system(
if let Ok(camera_transform) = camera_transforms.get(active_camera_entity) {
let camera_size = window_size * camera_transform.scale.truncate();

let rect = Rect {
let camera_rect = Rect {
position: camera_transform.translation.truncate(),
size: camera_size,
};
Expand All @@ -102,10 +102,10 @@ pub fn atlas_frustum_culling_system(

let sprite_rect = Rect {
position: drawable_transform.translation.truncate(),
size,
size: size * drawable_transform.scale.truncate(),
};

if rect.is_intersecting(sprite_rect) {
if camera_rect.is_intersecting(sprite_rect) {
if culled_sprites.get(entity).is_ok() {
commands.entity(entity).remove::<OutsideFrustum>();
}
Expand Down
96 changes: 96 additions & 0 deletions examples/2d/frustum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use bevy::render::camera::OrthographicProjection;
use bevy::{prelude::*, render::draw::OutsideFrustum, sprite::SpriteSettings};

struct Bar;
struct PrintTimer(Timer);

fn setup(mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());

let mut transform = Transform::from_xyz(-400.0, 0.0, 0.0);
transform.scale = Vec3::new(1.0, 20.0, 1.0);

commands
.spawn_bundle(SpriteBundle {
material: materials.add(Color::rgb(0.5, 0.5, 1.0).into()),
transform,
sprite: Sprite::new(Vec2::new(30.0, 30.0)),
..Default::default()
})
.insert(Bar);
}

fn rotate(mut query: Query<&mut Transform, With<Bar>>, time: Res<Time>) {
let speed = 5f32;
for mut t in query.iter_mut() {
t.rotation *= Quat::from_rotation_z(time.delta_seconds() * speed);
}
}

fn travel_camera(
keys: Res<Input<KeyCode>>,
mut query: Query<&mut Transform, With<OrthographicProjection>>,
time: Res<Time>,
) {
let speed = 2f32;
for mut t in query.iter_mut() {
if keys.pressed(KeyCode::S) {
t.scale += time.delta_seconds() * speed;
}
if keys.pressed(KeyCode::W) {
t.scale -= time.delta_seconds() * speed;
}
}
}

fn travel(keys: Res<Input<KeyCode>>, mut query: Query<&mut Transform, With<Bar>>, time: Res<Time>) {
let speed = 500f32;
for mut t in query.iter_mut() {
if keys.pressed(KeyCode::Right) {
t.translation.x += time.delta_seconds() * speed;
}
if keys.pressed(KeyCode::Left) {
t.translation.x -= time.delta_seconds() * speed;
}
if keys.pressed(KeyCode::Up) {
t.translation.y += time.delta_seconds() * speed;
}
if keys.pressed(KeyCode::Down) {
t.translation.y -= time.delta_seconds() * speed;
}
}
}

fn info(
time: Res<Time>,
mut timer: ResMut<PrintTimer>,
query: Query<&Bar, Without<OutsideFrustum>>,
) {
let mut count = 0;
for _ in query.iter() {
count += 1;
}
if timer.0.tick(time.delta()).just_finished() {
info!("{} sprites on screen", count);
}
}

fn startup() {
info!("use the arrow keys to move the bar, 'W' & 'S' to scale the camera");
}

fn main() {
App::new()
.insert_resource(SpriteSettings {
frustum_culling_enabled: true,
})
.insert_resource(PrintTimer(Timer::from_seconds(1.0, true)))
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_startup_system(startup.system())
.add_system(rotate.system())
.add_system(travel.system())
.add_system(travel_camera.system())
.add_system(info.system())
.run();
}
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Example | File | Description
Example | File | Description
--- | --- | ---
`contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball!
`frustum` | [`2d/frustum.rs`](./2d/frustum.rs) | Tests the frustum culling system (only draws sprites on-screen)
`many_sprites` | [`2d/many_sprites.rs`](./2d/many_sprites.rs) | Displays many sprites in a grid arragement! Used for performance testing.
`mesh` | [`2d/mesh.rs`](./2d/mesh.rs) | Renders a custom mesh
`sprite` | [`2d/sprite.rs`](./2d/sprite.rs) | Renders a sprite
Expand Down