-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Merged by Bors] - Frustum Culling (for Sprites) #1492
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5394f13
frustum cullign for sprites
Byteron 876a1a6
Switch to OutsideFrustum component
cart 42a7022
account for nasty insert error
cart 9e4598f
Update OutsideFrustum docs
cart dcbdccc
Only use active cameras when culling. Add SpriteSettings
cart dbb8cd1
fmt
cart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use bevy_asset::{Assets, Handle}; | ||
use bevy_ecs::prelude::{Commands, Entity, Query, Res, With}; | ||
use bevy_math::Vec2; | ||
use bevy_render::{ | ||
camera::{ActiveCameras, Camera}, | ||
draw::OutsideFrustum, | ||
}; | ||
use bevy_transform::components::Transform; | ||
use bevy_window::Windows; | ||
|
||
use crate::{Sprite, TextureAtlas, TextureAtlasSprite}; | ||
|
||
struct Rect { | ||
position: Vec2, | ||
size: Vec2, | ||
} | ||
|
||
impl Rect { | ||
#[inline] | ||
pub fn is_intersecting(&self, other: Rect) -> bool { | ||
self.position.distance(other.position) < (self.get_radius() + other.get_radius()) | ||
} | ||
|
||
#[inline] | ||
pub fn get_radius(&self) -> f32 { | ||
let half_size = self.size / Vec2::splat(2.0); | ||
(half_size.x.powf(2.0) + half_size.y.powf(2.0)).sqrt() | ||
} | ||
} | ||
|
||
pub fn sprite_frustum_culling_system( | ||
mut commands: Commands, | ||
windows: Res<Windows>, | ||
active_cameras: Res<ActiveCameras>, | ||
camera_transforms: Query<&Transform, With<Camera>>, | ||
culled_sprites: Query<&OutsideFrustum, With<Sprite>>, | ||
sprites: Query<(Entity, &Transform, &Sprite)>, | ||
) { | ||
let window_size = if let Some(window) = windows.get_primary() { | ||
Vec2::new(window.width(), window.height()) | ||
} else { | ||
return; | ||
}; | ||
|
||
for active_camera_entity in active_cameras.iter().filter_map(|a| a.entity) { | ||
if let Ok(camera_transform) = camera_transforms.get(active_camera_entity) { | ||
let camera_size = window_size * camera_transform.scale.truncate(); | ||
|
||
let 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, | ||
}; | ||
|
||
if rect.is_intersecting(sprite_rect) { | ||
if culled_sprites.get(entity).is_ok() { | ||
commands.entity(entity).remove::<OutsideFrustum>(); | ||
} | ||
} else if culled_sprites.get(entity).is_err() { | ||
commands.entity(entity).insert(OutsideFrustum); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn atlas_frustum_culling_system( | ||
mut commands: Commands, | ||
windows: Res<Windows>, | ||
active_cameras: Res<ActiveCameras>, | ||
textures: Res<Assets<TextureAtlas>>, | ||
camera_transforms: Query<&Transform, With<Camera>>, | ||
culled_sprites: Query<&OutsideFrustum, With<TextureAtlasSprite>>, | ||
sprites: Query<( | ||
Entity, | ||
&Transform, | ||
&TextureAtlasSprite, | ||
&Handle<TextureAtlas>, | ||
)>, | ||
) { | ||
let window = windows.get_primary().unwrap(); | ||
let window_size = Vec2::new(window.width(), window.height()); | ||
|
||
for active_camera_entity in active_cameras.iter().filter_map(|a| a.entity) { | ||
if let Ok(camera_transform) = camera_transforms.get(active_camera_entity) { | ||
let camera_size = window_size * camera_transform.scale.truncate(); | ||
|
||
let rect = Rect { | ||
position: camera_transform.translation.truncate(), | ||
size: camera_size, | ||
}; | ||
|
||
for (entity, drawable_transform, sprite, atlas_handle) in sprites.iter() { | ||
if let Some(atlas) = textures.get(atlas_handle) { | ||
if let Some(sprite) = atlas.textures.get(sprite.index as usize) { | ||
let size = Vec2::new(sprite.width(), sprite.height()); | ||
|
||
let sprite_rect = Rect { | ||
position: drawable_transform.translation.truncate(), | ||
size, | ||
}; | ||
|
||
if rect.is_intersecting(sprite_rect) { | ||
if culled_sprites.get(entity).is_ok() { | ||
commands.entity(entity).remove::<OutsideFrustum>(); | ||
} | ||
} else if culled_sprites.get(entity).is_err() { | ||
commands.entity(entity).insert(OutsideFrustum); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double negatives 🤯
(nothing we can do here, but always causes me to pause for a moment when reading logic like this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I like to avoid double negatives in apis whenever possible. But yeah the tradeoff here for the inverse is too much :)