-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
171 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,126 @@ | ||
use bevy_asset::{Assets, Handle}; | ||
use bevy_ecs::{Commands, Entity, Query, Res, With}; | ||
use bevy_math::Vec2; | ||
use bevy_render::{camera::Camera, draw::Frustum}; | ||
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_overlapping(&self, other: Rect) -> bool { | ||
!(self.position.x > other.end().x | ||
|| self.end().x < other.position.x | ||
|| self.position.y > other.end().y | ||
|| self.end().y < other.position.y) | ||
} | ||
|
||
#[inline] | ||
pub fn end(&self) -> Vec2 { | ||
self.position + self.size | ||
} | ||
} | ||
pub fn frustum_culling_sprites( | ||
commands: &mut Commands, | ||
windows: Res<Windows>, | ||
cameras: Query<&Transform, With<Camera>>, | ||
visible: Query<&Frustum, With<Sprite>>, | ||
sprites: Query<(Entity, &Transform, &Sprite)>, | ||
) { | ||
let window = windows.get_primary().unwrap(); | ||
let window_size = Vec2::new(window.width(), window.height()); | ||
|
||
for camera_transform in cameras.iter() { | ||
let camera_position = Vec2::new( | ||
camera_transform.translation.x, | ||
camera_transform.translation.y, | ||
); | ||
|
||
let camera_size = window_size * camera_transform.scale.truncate(); | ||
|
||
let rect = Rect { | ||
position: camera_position - camera_size / Vec2::new(2.0, 2.0), | ||
size: camera_size, | ||
}; | ||
|
||
for (entity, drawable_transform, sprite) in sprites.iter() { | ||
let sprite_rect = Rect { | ||
position: drawable_transform.translation.truncate() | ||
- sprite.size / Vec2::new(2.0, 2.0), | ||
size: sprite.size, | ||
}; | ||
|
||
if rect.is_overlapping(sprite_rect) { | ||
if visible.get(entity).is_err() { | ||
commands.insert_one(entity, Frustum::default()); | ||
} | ||
} else if visible.get(entity).is_ok() { | ||
commands.remove_one::<Frustum>(entity); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn frustum_culling_atlases( | ||
commands: &mut Commands, | ||
windows: Res<Windows>, | ||
textures: Res<Assets<TextureAtlas>>, | ||
cameras: Query<&Transform, With<Camera>>, | ||
visible: Query<&Frustum, 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 camera_transform in cameras.iter() { | ||
let camera_position = Vec2::new( | ||
camera_transform.translation.x, | ||
camera_transform.translation.y, | ||
); | ||
|
||
let camera_size = window_size * camera_transform.scale.truncate(); | ||
|
||
let rect = Rect { | ||
position: camera_position - camera_size / Vec2::new(2.0, 2.0), | ||
size: camera_size, | ||
}; | ||
|
||
for (entity, drawable_transform, sprite, atlas_handle) in sprites.iter() { | ||
let atlas = match textures.get(atlas_handle) { | ||
Some(atlas) => atlas, | ||
None => continue, | ||
}; | ||
|
||
let sprite = match atlas.textures.get(sprite.index as usize) { | ||
Some(sprite) => sprite, | ||
None => continue, | ||
}; | ||
|
||
let size = Vec2::new(sprite.width(), sprite.height()); | ||
|
||
let sprite_rect = Rect { | ||
position: drawable_transform.translation.truncate() - size / Vec2::new(2.0, 2.0), | ||
size, | ||
}; | ||
|
||
if rect.is_overlapping(sprite_rect) { | ||
if visible.get(entity).is_err() { | ||
commands.insert_one(entity, Frustum::default()); | ||
} | ||
} else if visible.get(entity).is_ok() { | ||
commands.remove_one::<Frustum>(entity); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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 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