-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
2d frustum culling #3944
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
2d frustum culling #3944
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,10 +11,11 @@ use bevy_ecs::{ | |||||
| prelude::*, | ||||||
| system::{lifetimeless::*, SystemParamItem}, | ||||||
| }; | ||||||
| use bevy_math::{const_vec2, Vec2}; | ||||||
| use bevy_math::{const_vec2, Vec2, Vec3}; | ||||||
| use bevy_reflect::Uuid; | ||||||
| use bevy_render::{ | ||||||
| color::Color, | ||||||
| primitives::Aabb, | ||||||
| render_asset::RenderAssets, | ||||||
| render_phase::{ | ||||||
| BatchedPhaseItem, DrawFunctions, EntityRenderCommand, RenderCommand, RenderCommandResult, | ||||||
|
|
@@ -23,7 +24,9 @@ use bevy_render::{ | |||||
| render_resource::{std140::AsStd140, *}, | ||||||
| renderer::{RenderDevice, RenderQueue}, | ||||||
| texture::{BevyDefault, Image}, | ||||||
| view::{Msaa, ViewUniform, ViewUniformOffset, ViewUniforms, Visibility}, | ||||||
| view::{ | ||||||
| ComputedVisibility, Msaa, NoFrustumCulling, ViewUniform, ViewUniformOffset, ViewUniforms, | ||||||
| }, | ||||||
| RenderWorld, | ||||||
| }; | ||||||
| use bevy_transform::components::GlobalTransform; | ||||||
|
|
@@ -178,6 +181,45 @@ impl SpecializedPipeline for SpritePipeline { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| pub fn calculate_bounds( | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs doc strings: it's a public system.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ups...that's an oversight, the system doesn't need to be public.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm...well maybe not exactly an oversight, because all the other functions in that file are |
||||||
| mut commands: Commands, | ||||||
| images: Res<Assets<Image>>, | ||||||
| atlases: Res<Assets<TextureAtlas>>, | ||||||
| sprite_without_aabb: Query< | ||||||
| (Entity, &Sprite, &Handle<Image>), | ||||||
| (Without<Aabb>, Without<NoFrustumCulling>), | ||||||
| >, | ||||||
| atlas_without_aabb: Query< | ||||||
| (Entity, &TextureAtlasSprite, &Handle<TextureAtlas>), | ||||||
| (Without<Aabb>, Without<NoFrustumCulling>), | ||||||
| >, | ||||||
| ) { | ||||||
| for (entity, sprite, texture_handle) in sprite_without_aabb.iter() { | ||||||
| if let Some(image) = images.get(texture_handle) { | ||||||
| let size = sprite.custom_size.unwrap_or_else(|| image.size()); | ||||||
| let aabb = Aabb { | ||||||
| center: Vec3::ZERO, | ||||||
| half_extents: size.extend(0.0) * 0.5, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe faster?
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will try that. Now that you mention this, i noticed that in some other places divisions are used instead of multiplications, but as far as i know or as far as i was taught, multiplications are preferred because they are (in most cases) faster than divisions. Is this something we want to change? Not in this PR, but in a new one. |
||||||
| }; | ||||||
| commands.entity(entity).insert(aabb); | ||||||
| } | ||||||
| } | ||||||
| for (entity, atlas_sprite, atlas_handle) in atlas_without_aabb.iter() { | ||||||
| if let Some(atlas) = atlases.get(atlas_handle) { | ||||||
| if let Some(rect) = atlas.textures.get(atlas_sprite.index) { | ||||||
| let size = atlas_sprite | ||||||
| .custom_size | ||||||
| .unwrap_or_else(|| (rect.min - rect.max).abs()); | ||||||
| let aabb = Aabb { | ||||||
| center: Vec3::ZERO, | ||||||
| half_extents: size.extend(0.0) * 0.5, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| }; | ||||||
| commands.entity(entity).insert(aabb); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[derive(Component, Clone, Copy)] | ||||||
| pub struct ExtractedSprite { | ||||||
| pub transform: GlobalTransform, | ||||||
|
|
@@ -232,18 +274,23 @@ pub fn extract_sprite_events( | |||||
| pub fn extract_sprites( | ||||||
| mut render_world: ResMut<RenderWorld>, | ||||||
| texture_atlases: Res<Assets<TextureAtlas>>, | ||||||
| sprite_query: Query<(&Visibility, &Sprite, &GlobalTransform, &Handle<Image>)>, | ||||||
| sprite_query: Query<( | ||||||
| &ComputedVisibility, | ||||||
| &Sprite, | ||||||
| &GlobalTransform, | ||||||
| &Handle<Image>, | ||||||
| )>, | ||||||
| atlas_query: Query<( | ||||||
| &Visibility, | ||||||
| &ComputedVisibility, | ||||||
| &TextureAtlasSprite, | ||||||
| &GlobalTransform, | ||||||
| &Handle<TextureAtlas>, | ||||||
| )>, | ||||||
| ) { | ||||||
| let mut extracted_sprites = render_world.get_resource_mut::<ExtractedSprites>().unwrap(); | ||||||
| extracted_sprites.sprites.clear(); | ||||||
| for (visibility, sprite, transform, handle) in sprite_query.iter() { | ||||||
| if !visibility.is_visible { | ||||||
| for (computed_visibility, sprite, transform, handle) in sprite_query.iter() { | ||||||
| if !computed_visibility.is_visible { | ||||||
| continue; | ||||||
| } | ||||||
| // PERF: we don't check in this function that the `Image` asset is ready, since it should be in most cases and hashing the handle is expensive | ||||||
|
|
@@ -259,8 +306,8 @@ pub fn extract_sprites( | |||||
| image_handle_id: handle.id, | ||||||
| }); | ||||||
| } | ||||||
| for (visibility, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() { | ||||||
| if !visibility.is_visible { | ||||||
| for (computed_visibility, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() { | ||||||
| if !computed_visibility.is_visible { | ||||||
| continue; | ||||||
| } | ||||||
| if let Some(texture_atlas) = texture_atlases.get(texture_atlas_handle) { | ||||||
|
|
||||||
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.
Batch insertion may be faster. Please try it.