diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 2b1fd83cabe7d..b0d20dde6e25a 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -204,7 +204,7 @@ impl Default for MaterialPlugin { impl Plugin for MaterialPlugin { fn build(&self, app: &mut App) { app.add_asset::() - .add_plugin(ExtractComponentPlugin::>::default()) + .add_plugin(ExtractComponentPlugin::>::extract_visible()) .add_plugin(RenderAssetPlugin::::default()); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { render_app diff --git a/crates/bevy_render/src/render_component.rs b/crates/bevy_render/src/render_component.rs index 8d15f2476d6ae..7cd3158cb94d7 100644 --- a/crates/bevy_render/src/render_component.rs +++ b/crates/bevy_render/src/render_component.rs @@ -1,6 +1,7 @@ use crate::{ render_resource::{std140::AsStd140, DynamicUniformVec}, renderer::{RenderDevice, RenderQueue}, + view::ComputedVisibility, RenderApp, RenderStage, }; use bevy_app::{App, Plugin}; @@ -131,18 +132,38 @@ fn prepare_uniform_components( /// /// Therefore it sets up the [`RenderStage::Extract`](crate::RenderStage::Extract) step /// for the specified [`ExtractComponent`]. -pub struct ExtractComponentPlugin(PhantomData (C, F)>); +pub struct ExtractComponentPlugin { + only_extract_visible: bool, + marker: PhantomData (C, F)>, +} impl Default for ExtractComponentPlugin { fn default() -> Self { - Self(PhantomData) + Self { + only_extract_visible: false, + marker: PhantomData, + } + } +} + +impl ExtractComponentPlugin { + pub fn extract_visible() -> Self { + Self { + only_extract_visible: true, + marker: PhantomData, + } } } impl Plugin for ExtractComponentPlugin { fn build(&self, app: &mut App) { if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { - render_app.add_system_to_stage(RenderStage::Extract, extract_components::); + if self.only_extract_visible { + render_app + .add_system_to_stage(RenderStage::Extract, extract_visible_components::); + } else { + render_app.add_system_to_stage(RenderStage::Extract, extract_components::); + } } } } @@ -170,3 +191,19 @@ fn extract_components( *previous_len = values.len(); commands.insert_or_spawn_batch(values); } + +/// This system extracts all visible components of the corresponding [`ExtractComponent`] type. +fn extract_visible_components( + mut commands: Commands, + mut previous_len: Local, + mut query: StaticSystemParam, C::Query), C::Filter>>, +) { + let mut values = Vec::with_capacity(*previous_len); + for (entity, computed_visibility, query_item) in query.iter_mut() { + if computed_visibility.is_visible { + values.push((entity, (C::extract_component(query_item),))); + } + } + *previous_len = values.len(); + commands.insert_or_spawn_batch(values); +} diff --git a/crates/bevy_sprite/src/mesh2d/material.rs b/crates/bevy_sprite/src/mesh2d/material.rs index f088e761a054c..77cb3c4ebe92f 100644 --- a/crates/bevy_sprite/src/mesh2d/material.rs +++ b/crates/bevy_sprite/src/mesh2d/material.rs @@ -194,7 +194,7 @@ impl Default for Material2dPlugin { impl Plugin for Material2dPlugin { fn build(&self, app: &mut App) { app.add_asset::() - .add_plugin(ExtractComponentPlugin::>::default()) + .add_plugin(ExtractComponentPlugin::>::extract_visible()) .add_plugin(RenderAssetPlugin::::default()); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { render_app