pub struct SyncWorldPlugin;
Expand description
A plugin that synchronizes entities with SyncToRenderWorld
between the main world and the render world.
All entities with the SyncToRenderWorld
component are kept in sync. It
is automatically added as a required component by ExtractComponentPlugin
and SyncComponentPlugin
, so it doesn’t need to be added manually when
spawning or as a required component when either of these plugins are used.
§Implementation
Bevy’s renderer is architected independently from the main app.
It operates in its own separate ECS World
, so the renderer logic can run in parallel with the main world logic.
This is called “Pipelined Rendering”, see PipelinedRenderingPlugin
for more information.
SyncWorldPlugin
is the first thing that runs every frame and it maintains an entity-to-entity mapping
between the main world and the render world.
It does so by spawning and despawning entities in the render world, to match spawned and despawned entities in the main world.
The link between synced entities is maintained by the RenderEntity
and MainEntity
components.
The RenderEntity
contains the corresponding render world entity of a main world entity, while MainEntity
contains
the corresponding main world entity of a render world entity.
For convenience, QueryData
implementations are provided for both components:
adding MainEntity
to a query (without a &
) will return the corresponding main world Entity
,
and adding RenderEntity
will return the corresponding render world Entity
.
If you have access to the component itself, the underlying entities can be accessed by calling .id()
.
Synchronization is necessary preparation for extraction (ExtractSchedule
), which copies over component data from the main
to the render world for these entities.
|--------------------------------------------------------------------|
| | | Main world update |
| sync | extract |---------------------------------------------------|
| | | Render world update |
|--------------------------------------------------------------------|
An example for synchronized main entities 1v1 and 18v1
|---------------------------Main World------------------------------|
| Entity | Component |
|-------------------------------------------------------------------|
| ID: 1v1 | PointLight | RenderEntity(ID: 3V1) | SyncToRenderWorld |
| ID: 18v1 | PointLight | RenderEntity(ID: 5V1) | SyncToRenderWorld |
|-------------------------------------------------------------------|
|----------Render World-----------|
| Entity | Component |
|---------------------------------|
| ID: 3v1 | MainEntity(ID: 1V1) |
| ID: 5v1 | MainEntity(ID: 18V1) |
|---------------------------------|
Note that this effectively establishes a link between the main world entity and the render world entity.
Not every entity needs to be synchronized, however; only entities with the SyncToRenderWorld
component are synced.
Adding SyncToRenderWorld
to a main world component will establish such a link.
Once a synchronized main entity is despawned, its corresponding render entity will be automatically
despawned in the next sync
.
The sync step does not copy any of component data between worlds, since its often not necessary to transfer over all
the components of a main world entity.
The render world probably cares about a Position
component, but not a Velocity
component.
The extraction happens in its own step, independently from, and after synchronization.
Moreover, SyncWorldPlugin
only synchronizes entities. RenderAsset
s like meshes and textures are handled
differently.
Trait Implementations§
Source§impl Default for SyncWorldPlugin
impl Default for SyncWorldPlugin
Source§fn default() -> SyncWorldPlugin
fn default() -> SyncWorldPlugin
Source§impl Plugin for SyncWorldPlugin
impl Plugin for SyncWorldPlugin
Source§fn ready(&self, _app: &App) -> bool
fn ready(&self, _app: &App) -> bool
finish
should be called.Source§fn finish(&self, _app: &mut App)
fn finish(&self, _app: &mut App)
App
, once all plugins registered are ready. This can
be useful for plugins that depends on another plugin asynchronous setup, like the renderer.Source§fn cleanup(&self, _app: &mut App)
fn cleanup(&self, _app: &mut App)
Auto Trait Implementations§
impl Freeze for SyncWorldPlugin
impl RefUnwindSafe for SyncWorldPlugin
impl Send for SyncWorldPlugin
impl Sync for SyncWorldPlugin
impl Unpin for SyncWorldPlugin
impl UnwindSafe for SyncWorldPlugin
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self
using default()
.
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more