wde_renderer/sync/mod.rs
1//! The sync module provides tools to synchronize entities, components and resources from the main world to the render world. This is useful for plugins that need to extract data from the main world and use it in the render world.
2//!
3//! It is built on [`bevy::extract`], specialized for the [`RenderApp`]: the traits take [`RenderApp`] as their label, and the
4//! plugins and entity components are aliased (e.g. [`RenderEntity`] is `SubEntity<RenderApp>`).
5//!
6//!
7//! # Extracting resources
8//! ## Manual extraction
9//! If you have a plugin that has resources that need to be extracted, you can implement the [`ExtractResource`] trait for those resources and add the corresponding [`ExtractResourcePlugin`] to your plugin. For example, if you have a plugin with a resource `MyResource`, you can do:
10//! ```
11//! app
12//! .insert_resource(MyResource { /* ... */ })
13//! .add_plugins(ExtractResourcePlugin::<MyResource>::default());
14//! ```
15//! This will automatically extract `MyResource` from the main world to the render world, and update it in the render world whenever it changes in the main world, without having to write the extraction system manually. The associated [`ExtractResource`] trait implementation will look like this:
16//! ```
17//! impl ExtractResource<RenderApp> for MyResource {
18//! type Source = Self;
19//!
20//! fn extract_resource(source: &Self::Source) -> Self {
21//! source.clone()
22//! }
23//! }
24//! ```
25//!
26//! ## ExtractResource macro
27//! If the extraction logic is simple, (i.e. when it only consists of cloning the resource), the trait implementation can be automatically derived using the [`ExtractResource`] derive macro. For example,
28//! ```
29//! #[derive(Resource, Clone, ExtractResource)]
30//! #[extract_app(RenderApp)]
31//! struct MyResource {
32//! value: i32,
33//! }
34//! ```
35//!
36//!
37//! # Syncing entities and components
38//! Entities from the main world can be synchronized to the render world by adding the [`SyncToRenderWorld`] component to them.
39//!
40//! ## Manual synchronization vs automatic synchronization
41//! One way to do this is to add the component manually to the entities that need to be synchronized. For example:
42//! ```
43//! commands.spawn((MyComponent { /* ... */ }, SyncToRenderWorld::default()));
44//! ```
45//! Another way to synchronize entities is to use the [`SyncComponentPlugin`] for a component that is on those entities. For example, if you have a component `MyComponent` that is on the entities you want to synchronize, you can do:
46//! ```
47//! app.add_plugins(SyncComponentPlugin::<MyComponent>::default());
48//! ```
49//! This will automatically add the [`SyncToRenderWorld`] component to any entity that has `MyComponent`, which will trigger the synchronization of that entity to the render world.
50//!
51//! ## Synchronized entities
52//! After an entity is synchronized, it will have the following components in order to maintain the link between the main world and the render world:
53//! - In the main world, a [`RenderEntity`] component that stores the corresponding entity in the render world.
54//! - In the render world, a [`MainEntity`] component that stores the corresponding entity in the main world.
55//!
56//! You can access these components just like the [`Entity`](bevy::prelude::Entity) component (without &).
57//! For example, you can then create `extract` systems that query for every entity with `MyComponent` that changed in the main world (supposing you used [`SyncComponentPlugin`] for `MyComponent`), and then extract these changes to their corresponding render world entities:
58//! ```rust
59//! fn extract_my_component(
60//! query: Extract<Query<(RenderEntity, &MyComponent), Changed<MyComponent>>>,
61//! mut commands: Commands
62//! ) {
63//! for (render_entity, my_component) in query.iter() {
64//! commands
65//! .entity(render_entity)
66//! .insert(MyComponent { /* ... */ });
67//! }
68//! }
69//! ```
70//! Note that one doesn't need to create this entity in the render world manually, as it is automatically managed by the synchronization system.
71//!
72//! ## Extracting components
73//! To simplify the process of extracting components, like the `extract_my_component` system in the example above, you can implement the [`ExtractComponent`] trait for a component and add the corresponding [`ExtractComponentPlugin`] to your plugin.
74//!
75//! For example, to implement the `extract_my_component` system above, first add the plugin:
76//! ```
77//! app.add_plugins(ExtractComponentPlugin::<MyComponent>::default());
78//! ```
79//! This will automatically add the [`SyncComponentPlugin`] for `MyComponent`.
80//! Then, you can implement the [`ExtractComponent`] and [`SyncComponent`] traits for `MyComponent`:
81//! ```
82//! impl SyncComponent<RenderApp> for MyComponent {
83//! type Target = Self;
84//! }
85//! impl ExtractComponent<RenderApp> for MyComponent {
86//! type QueryData = &'static Self;
87//! type QueryFilter = Changed<Self>;
88//! type Out = Self;
89//!
90//! fn extract_component(item: QueryItem<'_, '_, Self::QueryData>) -> Option<Self::Out> {
91//! Some(item.clone())
92//! // Note: If return None, remove the component from the entity in the render world
93//! }
94//! }
95//! ```
96//! This will automatically extract `MyComponent` from the main world to the render world for every entity that has `MyComponent` and for which `MyComponent` changed in the main world, without having to write the `extract_my_component` system manually.
97//!
98//! ## ExtractComponent macro
99//! When the extraction logic is simple, (i.e. when it only consists of cloning the component), the trait implementation of [`ExtractComponent`] and [`SyncComponent`] can be automatically derived using the [`ExtractComponent`] derive macro. For example,
100//! ```
101//! #[derive(Component, Clone, ExtractComponent)]
102//! #[extract_app(RenderApp)]
103//! #[extract_component_filter(Changed<Self>)]
104//! struct MyComponent {
105//! value: i32,
106//! }
107//! ```
108
109use bevy::extract;
110
111use crate::core::RenderApp;
112
113pub use bevy::ecs::query::QueryItem;
114pub use extract::{
115 Extract, ExtractSchedule, MainEntity, MainWorld, extract_component::ExtractComponent,
116 extract_resource::ExtractResource, sync_component::SyncComponent
117};
118
119/// Main world component pointing to the synchronized entity of the render world.
120pub type RenderEntity = extract::SubEntity<RenderApp>;
121/// Marker component that synchronizes its entity to the render world.
122pub type SyncToRenderWorld = extract::SyncToSubWorld<RenderApp>;
123/// Adds the [`SyncToRenderWorld`] marker to the entities holding a [`SyncComponent`].
124pub type SyncComponentPlugin<C, F = ()> =
125 extract::sync_component::SyncComponentPlugin<C, RenderApp, F>;
126/// Extracts an [`ExtractComponent`] to the render world entities.
127pub type ExtractComponentPlugin<C, F = ()> =
128 extract::extract_component::ExtractComponentPlugin<C, RenderApp, F>;
129/// Extracts an [`ExtractResource`] to the render world.
130pub type ExtractResourcePlugin<R, F = ()> =
131 extract::extract_resource::ExtractResourcePlugin<R, RenderApp, F>;