Skip to content

Commit 67aa295

Browse files
Extract component derive (#7399)
# Objective In simple cases we might want to derive the `ExtractComponent` trait. This adds symmetry to the existing `ExtractResource` derive. ## Solution Add an implementation of `#[derive(ExtractComponent)]`. The implementation is adapted from the existing `ExtractResource` derive macro. Additionally, there is an attribute called `extract_component_filter`. This allows specifying a query filter type used when extracting. If not specified, no filter (equal to `()`) is used. So: ```rust #[derive(Component, Clone, ExtractComponent)] #[extract_component_filter(With<Fuel>)] pub struct Car { pub wheels: usize, } ``` would expand to (a bit cleaned up here): ```rust impl ExtractComponent for Car { type Query = &'static Self; type Filter = With<Fuel>; type Out = Self; fn extract_component(item: QueryItem<'_, Self::Query>) -> Option<Self::Out> { Some(item.clone()) } } ``` --- ## Changelog - Added the ability to `#[derive(ExtractComponent)]` with an optional filter.
1 parent c9a53bf commit 67aa295

File tree

9 files changed

+102
-70
lines changed

9 files changed

+102
-70
lines changed

crates/bevy_core_pipeline/src/core_2d/camera_2d.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{clear_color::ClearColorConfig, tonemapping::Tonemapping};
2-
use bevy_ecs::{prelude::*, query::QueryItem};
2+
use bevy_ecs::prelude::*;
33
use bevy_reflect::Reflect;
44
use bevy_render::{
55
camera::{Camera, CameraProjection, CameraRenderGraph, OrthographicProjection},
@@ -9,22 +9,13 @@ use bevy_render::{
99
};
1010
use bevy_transform::prelude::{GlobalTransform, Transform};
1111

12-
#[derive(Component, Default, Reflect, Clone)]
12+
#[derive(Component, Default, Reflect, Clone, ExtractComponent)]
13+
#[extract_component_filter(With<Camera>)]
1314
#[reflect(Component)]
1415
pub struct Camera2d {
1516
pub clear_color: ClearColorConfig,
1617
}
1718

18-
impl ExtractComponent for Camera2d {
19-
type Query = &'static Self;
20-
type Filter = With<Camera>;
21-
type Out = Self;
22-
23-
fn extract_component(item: QueryItem<'_, Self::Query>) -> Option<Self> {
24-
Some(item.clone())
25-
}
26-
}
27-
2819
#[derive(Bundle)]
2920
pub struct Camera2dBundle {
3021
pub camera: Camera,

crates/bevy_core_pipeline/src/core_3d/camera_3d.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{clear_color::ClearColorConfig, tonemapping::Tonemapping};
2-
use bevy_ecs::{prelude::*, query::QueryItem};
2+
use bevy_ecs::prelude::*;
33
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
44
use bevy_render::{
55
camera::{Camera, CameraRenderGraph, Projection},
@@ -12,7 +12,8 @@ use bevy_transform::prelude::{GlobalTransform, Transform};
1212
use serde::{Deserialize, Serialize};
1313

1414
/// Configuration for the "main 3d render graph".
15-
#[derive(Component, Reflect, Clone, Default)]
15+
#[derive(Component, Reflect, Clone, Default, ExtractComponent)]
16+
#[extract_component_filter(With<Camera>)]
1617
#[reflect(Component)]
1718
pub struct Camera3d {
1819
/// The clear color operation to perform for the main 3d pass.
@@ -47,16 +48,6 @@ impl From<Camera3dDepthLoadOp> for LoadOp<f32> {
4748
}
4849
}
4950

50-
impl ExtractComponent for Camera3d {
51-
type Query = &'static Self;
52-
type Filter = With<Camera>;
53-
type Out = Self;
54-
55-
fn extract_component(item: QueryItem<'_, Self::Query>) -> Option<Self> {
56-
Some(item.clone())
57-
}
58-
}
59-
6051
#[derive(Bundle)]
6152
pub struct Camera3dBundle {
6253
pub camera: Camera,

crates/bevy_core_pipeline/src/fxaa/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{core_2d, core_3d, fullscreen_vertex_shader::fullscreen_shader_vertex
22
use bevy_app::prelude::*;
33
use bevy_asset::{load_internal_asset, HandleUntyped};
44
use bevy_derive::Deref;
5-
use bevy_ecs::{prelude::*, query::QueryItem};
5+
use bevy_ecs::prelude::*;
66
use bevy_reflect::TypeUuid;
77
use bevy_render::{
88
extract_component::{ExtractComponent, ExtractComponentPlugin},
@@ -40,7 +40,8 @@ impl Sensitivity {
4040
}
4141
}
4242

43-
#[derive(Component, Clone)]
43+
#[derive(Component, Clone, ExtractComponent)]
44+
#[extract_component_filter(With<Camera>)]
4445
pub struct Fxaa {
4546
/// Enable render passes for FXAA.
4647
pub enabled: bool,
@@ -67,16 +68,6 @@ impl Default for Fxaa {
6768
}
6869
}
6970

70-
impl ExtractComponent for Fxaa {
71-
type Query = &'static Self;
72-
type Filter = With<Camera>;
73-
type Out = Self;
74-
75-
fn extract_component(item: QueryItem<Self::Query>) -> Option<Self> {
76-
Some(item.clone())
77-
}
78-
}
79-
8071
const FXAA_SHADER_HANDLE: HandleUntyped =
8172
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 4182761465141723543);
8273

crates/bevy_core_pipeline/src/tonemapping/mod.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::fullscreen_vertex_shader::fullscreen_shader_vertex_state;
22
use bevy_app::prelude::*;
33
use bevy_asset::{load_internal_asset, HandleUntyped};
44
use bevy_ecs::prelude::*;
5-
use bevy_ecs::query::QueryItem;
65
use bevy_reflect::{Reflect, TypeUuid};
76
use bevy_render::camera::Camera;
87
use bevy_render::extract_component::{ExtractComponent, ExtractComponentPlugin};
@@ -145,7 +144,8 @@ pub fn queue_view_tonemapping_pipelines(
145144
}
146145
}
147146

148-
#[derive(Component, Clone, Reflect, Default)]
147+
#[derive(Component, Clone, Reflect, Default, ExtractComponent)]
148+
#[extract_component_filter(With<Camera>)]
149149
#[reflect(Component)]
150150
pub enum Tonemapping {
151151
#[default]
@@ -160,13 +160,3 @@ impl Tonemapping {
160160
matches!(self, Tonemapping::Enabled { .. })
161161
}
162162
}
163-
164-
impl ExtractComponent for Tonemapping {
165-
type Query = &'static Self;
166-
type Filter = With<Camera>;
167-
type Out = Self;
168-
169-
fn extract_component(item: QueryItem<Self::Query>) -> Option<Self::Out> {
170-
Some(item.clone())
171-
}
172-
}

crates/bevy_pbr/src/wireframe.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy_core_pipeline::core_3d::Opaque3d;
66
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
77
use bevy_reflect::std_traits::ReflectDefault;
88
use bevy_reflect::{Reflect, TypeUuid};
9-
use bevy_render::Extract;
9+
use bevy_render::extract_component::{ExtractComponent, ExtractComponentPlugin};
1010
use bevy_render::{
1111
extract_resource::{ExtractResource, ExtractResourcePlugin},
1212
mesh::{Mesh, MeshVertexBufferLayout},
@@ -39,27 +39,21 @@ impl Plugin for WireframePlugin {
3939
app.register_type::<Wireframe>()
4040
.register_type::<WireframeConfig>()
4141
.init_resource::<WireframeConfig>()
42-
.add_plugin(ExtractResourcePlugin::<WireframeConfig>::default());
42+
.add_plugin(ExtractResourcePlugin::<WireframeConfig>::default())
43+
.add_plugin(ExtractComponentPlugin::<Wireframe>::default());
4344

4445
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
4546
render_app
4647
.add_render_command::<Opaque3d, DrawWireframes>()
4748
.init_resource::<WireframePipeline>()
4849
.init_resource::<SpecializedMeshPipelines<WireframePipeline>>()
49-
.add_system_to_stage(RenderStage::Extract, extract_wireframes)
5050
.add_system_to_stage(RenderStage::Queue, queue_wireframes);
5151
}
5252
}
5353
}
5454

55-
fn extract_wireframes(mut commands: Commands, query: Extract<Query<Entity, With<Wireframe>>>) {
56-
for entity in &query {
57-
commands.get_or_spawn(entity).insert(Wireframe);
58-
}
59-
}
60-
6155
/// Controls whether an entity should rendered in wireframe-mode if the [`WireframePlugin`] is enabled
62-
#[derive(Component, Debug, Clone, Default, Reflect)]
56+
#[derive(Component, Debug, Clone, Default, ExtractComponent, Reflect)]
6357
#[reflect(Component, Default)]
6458
pub struct Wireframe;
6559

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use proc_macro::TokenStream;
2+
use quote::quote;
3+
use syn::{parse_macro_input, parse_quote, DeriveInput, Path};
4+
5+
pub fn derive_extract_component(input: TokenStream) -> TokenStream {
6+
let mut ast = parse_macro_input!(input as DeriveInput);
7+
let bevy_render_path: Path = crate::bevy_render_path();
8+
let bevy_ecs_path: Path = bevy_macro_utils::BevyManifest::default()
9+
.maybe_get_path("bevy_ecs")
10+
.expect("bevy_ecs should be found in manifest");
11+
12+
ast.generics
13+
.make_where_clause()
14+
.predicates
15+
.push(parse_quote! { Self: Clone });
16+
17+
let struct_name = &ast.ident;
18+
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
19+
20+
let filter = if let Some(attr) = ast
21+
.attrs
22+
.iter()
23+
.find(|a| a.path.is_ident("extract_component_filter"))
24+
{
25+
let filter = match attr.parse_args::<syn::Type>() {
26+
Ok(filter) => filter,
27+
Err(e) => return e.to_compile_error().into(),
28+
};
29+
30+
quote! {
31+
#filter
32+
}
33+
} else {
34+
quote! {
35+
()
36+
}
37+
};
38+
39+
TokenStream::from(quote! {
40+
impl #impl_generics #bevy_render_path::extract_component::ExtractComponent for #struct_name #type_generics #where_clause {
41+
type Query = &'static Self;
42+
43+
type Filter = #filter;
44+
type Out = Self;
45+
46+
fn extract_component(item: #bevy_ecs_path::query::QueryItem<'_, Self::Query>) -> Option<Self::Out> {
47+
Some(item.clone())
48+
}
49+
}
50+
})
51+
}

crates/bevy_render/macros/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod as_bind_group;
2+
mod extract_component;
23
mod extract_resource;
34

45
use bevy_macro_utils::BevyManifest;
@@ -17,6 +18,37 @@ pub fn derive_extract_resource(input: TokenStream) -> TokenStream {
1718
extract_resource::derive_extract_resource(input)
1819
}
1920

21+
/// Implements `ExtractComponent` trait for a component.
22+
/// The component must implement [`Clone`].
23+
/// The component will be extracted into the render world via cloning.
24+
/// Note that this only enables extraction of the component, it does not execute the extraction.
25+
/// See `ExtractComponentPlugin` to actually perform the extraction.
26+
///
27+
/// If you only want to extract a component conditionally, you may use the `extract_component_filter` attribute.
28+
///
29+
/// # Example
30+
///
31+
/// ```no_compile
32+
/// use bevy_ecs::component::Component;
33+
/// use bevy_render_macros::ExtractComponent;
34+
///
35+
/// #[derive(Component, Clone, ExtractComponent)]
36+
/// #[extract_component_filter(With<Camera>)]
37+
/// pub struct Foo {
38+
/// pub should_foo: bool,
39+
/// }
40+
///
41+
/// // Without a filter (unconditional).
42+
/// #[derive(Component, Clone, ExtractComponent)]
43+
/// pub struct Bar {
44+
/// pub should_bar: bool,
45+
/// }
46+
/// ```
47+
#[proc_macro_derive(ExtractComponent, attributes(extract_component_filter))]
48+
pub fn derive_extract_component(input: TokenStream) -> TokenStream {
49+
extract_component::derive_extract_component(input)
50+
}
51+
2052
#[proc_macro_derive(
2153
AsBindGroup,
2254
attributes(uniform, texture, sampler, bind_group_data, storage)

crates/bevy_render/src/extract_component.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use bevy_ecs::{
1414
};
1515
use std::{marker::PhantomData, ops::Deref};
1616

17+
pub use bevy_render_macros::ExtractComponent;
18+
1719
/// Stores the index of a uniform inside of [`ComponentUniforms`].
1820
#[derive(Component)]
1921
pub struct DynamicUniformIndex<C: Component> {

crates/bevy_ui/src/camera_config.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use bevy_ecs::component::Component;
44
use bevy_ecs::prelude::With;
5-
use bevy_ecs::query::QueryItem;
65
use bevy_render::camera::Camera;
76
use bevy_render::extract_component::ExtractComponent;
87

@@ -11,7 +10,8 @@ use bevy_render::extract_component::ExtractComponent;
1110
/// When a [`Camera`] doesn't have the [`UiCameraConfig`] component,
1211
/// it will display the UI by default.
1312
///
14-
#[derive(Component, Clone)]
13+
#[derive(Component, Clone, ExtractComponent)]
14+
#[extract_component_filter(With<Camera>)]
1515
pub struct UiCameraConfig {
1616
/// Whether to output UI to this camera view.
1717
///
@@ -25,13 +25,3 @@ impl Default for UiCameraConfig {
2525
Self { show_ui: true }
2626
}
2727
}
28-
29-
impl ExtractComponent for UiCameraConfig {
30-
type Query = &'static Self;
31-
type Filter = With<Camera>;
32-
type Out = Self;
33-
34-
fn extract_component(item: QueryItem<'_, Self::Query>) -> Option<Self> {
35-
Some(item.clone())
36-
}
37-
}

0 commit comments

Comments
 (0)