
- Adopted from #14449 - Still fixes #12144. ## Migration Guide The retained render world is a complex change: migrating might take one of a few different forms depending on the patterns you're using. For every example, we specify in which world the code is run. Most of the changes affect render world code, so for the average Bevy user who's using Bevy's high-level rendering APIs, these changes are unlikely to affect your code. ### Spawning entities in the render world Previously, if you spawned an entity with `world.spawn(...)`, `commands.spawn(...)` or some other method in the rendering world, it would be despawned at the end of each frame. In 0.15, this is no longer the case and so your old code could leak entities. This can be mitigated by either re-architecting your code to no longer continuously spawn entities (like you're used to in the main world), or by adding the `bevy_render::world_sync::TemporaryRenderEntity` component to the entity you're spawning. Entities tagged with `TemporaryRenderEntity` will be removed at the end of each frame (like before). ### Extract components with `ExtractComponentPlugin` ``` // main world app.add_plugins(ExtractComponentPlugin::<ComponentToExtract>::default()); ``` `ExtractComponentPlugin` has been changed to only work with synced entities. Entities are automatically synced if `ComponentToExtract` is added to them. However, entities are not "unsynced" if any given `ComponentToExtract` is removed, because an entity may have multiple components to extract. This would cause the other components to no longer get extracted because the entity is not synced. So be careful when only removing extracted components from entities in the render world, because it might leave an entity behind in the render world. The solution here is to avoid only removing extracted components and instead despawn the entire entity. ### Manual extraction using `Extract<Query<(Entity, ...)>>` ```rust // in render world, inspired by bevy_pbr/src/cluster/mod.rs pub fn extract_clusters( mut commands: Commands, views: Extract<Query<(Entity, &Clusters, &Camera)>>, ) { for (entity, clusters, camera) in &views { // some code commands.get_or_spawn(entity).insert(...); } } ``` One of the primary consequences of the retained rendering world is that there's no longer a one-to-one mapping from entity IDs in the main world to entity IDs in the render world. Unlike in Bevy 0.14, Entity 42 in the main world doesn't necessarily map to entity 42 in the render world. Previous code which called `get_or_spawn(main_world_entity)` in the render world (`Extract<Query<(Entity, ...)>>` returns main world entities). Instead, you should use `&RenderEntity` and `render_entity.id()` to get the correct entity in the render world. Note that this entity does need to be synced first in order to have a `RenderEntity`. When performing manual abstraction, this won't happen automatically (like with `ExtractComponentPlugin`) so add a `SyncToRenderWorld` marker component to the entities you want to extract. This results in the following code: ```rust // in render world, inspired by bevy_pbr/src/cluster/mod.rs pub fn extract_clusters( mut commands: Commands, views: Extract<Query<(&RenderEntity, &Clusters, &Camera)>>, ) { for (render_entity, clusters, camera) in &views { // some code commands.get_or_spawn(render_entity.id()).insert(...); } } // in main world, when spawning world.spawn(Clusters::default(), Camera::default(), SyncToRenderWorld) ``` ### Looking up `Entity` ids in the render world As previously stated, there's now no correspondence between main world and render world `Entity` identifiers. Querying for `Entity` in the render world will return the `Entity` id in the render world: query for `MainEntity` (and use its `id()` method) to get the corresponding entity in the main world. This is also a good way to tell the difference between synced and unsynced entities in the render world, because unsynced entities won't have a `MainEntity` component. --------- Co-authored-by: re0312 <re0312@outlook.com> Co-authored-by: re0312 <45868716+re0312@users.noreply.github.com> Co-authored-by: Periwink <charlesbour@gmail.com> Co-authored-by: Anselmo Sampietro <ans.samp@gmail.com> Co-authored-by: Emerson Coskey <56370779+ecoskey@users.noreply.github.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Christian Hughes <9044780+ItsDoot@users.noreply.github.com>
153 lines
5.4 KiB
Rust
153 lines
5.4 KiB
Rust
use crate::{
|
|
CascadeShadowConfig, Cascades, DirectionalLight, Material, PointLight, SpotLight,
|
|
StandardMaterial,
|
|
};
|
|
use bevy_asset::Handle;
|
|
use bevy_derive::{Deref, DerefMut};
|
|
use bevy_ecs::{
|
|
bundle::Bundle,
|
|
component::Component,
|
|
entity::{Entity, EntityHashMap},
|
|
reflect::ReflectComponent,
|
|
};
|
|
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
|
use bevy_render::{
|
|
mesh::Mesh,
|
|
primitives::{CascadesFrusta, CubemapFrusta, Frustum},
|
|
view::{InheritedVisibility, ViewVisibility, Visibility},
|
|
world_sync::SyncToRenderWorld,
|
|
};
|
|
use bevy_transform::components::{GlobalTransform, Transform};
|
|
|
|
/// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`].
|
|
pub type PbrBundle = MaterialMeshBundle<StandardMaterial>;
|
|
|
|
/// A component bundle for entities with a [`Mesh`] and a [`Material`].
|
|
#[derive(Bundle, Clone)]
|
|
pub struct MaterialMeshBundle<M: Material> {
|
|
pub mesh: Handle<Mesh>,
|
|
pub material: Handle<M>,
|
|
pub transform: Transform,
|
|
pub global_transform: GlobalTransform,
|
|
/// User indication of whether an entity is visible
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
}
|
|
|
|
impl<M: Material> Default for MaterialMeshBundle<M> {
|
|
fn default() -> Self {
|
|
Self {
|
|
mesh: Default::default(),
|
|
material: Default::default(),
|
|
transform: Default::default(),
|
|
global_transform: Default::default(),
|
|
visibility: Default::default(),
|
|
inherited_visibility: Default::default(),
|
|
view_visibility: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Collection of mesh entities visible for 3D lighting.
|
|
///
|
|
/// This component contains all mesh entities visible from the current light view.
|
|
/// The collection is updated automatically by [`crate::SimulationLightSystems`].
|
|
#[derive(Component, Clone, Debug, Default, Reflect, Deref, DerefMut)]
|
|
#[reflect(Component, Debug, Default)]
|
|
pub struct VisibleMeshEntities {
|
|
#[reflect(ignore)]
|
|
pub entities: Vec<Entity>,
|
|
}
|
|
|
|
#[derive(Component, Clone, Debug, Default, Reflect)]
|
|
#[reflect(Component, Debug, Default)]
|
|
pub struct CubemapVisibleEntities {
|
|
#[reflect(ignore)]
|
|
data: [VisibleMeshEntities; 6],
|
|
}
|
|
|
|
impl CubemapVisibleEntities {
|
|
pub fn get(&self, i: usize) -> &VisibleMeshEntities {
|
|
&self.data[i]
|
|
}
|
|
|
|
pub fn get_mut(&mut self, i: usize) -> &mut VisibleMeshEntities {
|
|
&mut self.data[i]
|
|
}
|
|
|
|
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &VisibleMeshEntities> {
|
|
self.data.iter()
|
|
}
|
|
|
|
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut VisibleMeshEntities> {
|
|
self.data.iter_mut()
|
|
}
|
|
}
|
|
|
|
#[derive(Component, Clone, Debug, Default, Reflect)]
|
|
#[reflect(Component)]
|
|
pub struct CascadesVisibleEntities {
|
|
/// Map of view entity to the visible entities for each cascade frustum.
|
|
#[reflect(ignore)]
|
|
pub entities: EntityHashMap<Vec<VisibleMeshEntities>>,
|
|
}
|
|
|
|
/// A component bundle for [`PointLight`] entities.
|
|
#[derive(Debug, Bundle, Default, Clone)]
|
|
pub struct PointLightBundle {
|
|
pub point_light: PointLight,
|
|
pub cubemap_visible_entities: CubemapVisibleEntities,
|
|
pub cubemap_frusta: CubemapFrusta,
|
|
pub transform: Transform,
|
|
pub global_transform: GlobalTransform,
|
|
/// Enables or disables the light
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Marker component that indicates that its entity needs to be synchronized to the render world
|
|
pub sync: SyncToRenderWorld,
|
|
}
|
|
|
|
/// A component bundle for spot light entities
|
|
#[derive(Debug, Bundle, Default, Clone)]
|
|
pub struct SpotLightBundle {
|
|
pub spot_light: SpotLight,
|
|
pub visible_entities: VisibleMeshEntities,
|
|
pub frustum: Frustum,
|
|
pub transform: Transform,
|
|
pub global_transform: GlobalTransform,
|
|
/// Enables or disables the light
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Marker component that indicates that its entity needs to be synchronized to the render world
|
|
pub sync: SyncToRenderWorld,
|
|
}
|
|
|
|
/// A component bundle for [`DirectionalLight`] entities.
|
|
#[derive(Debug, Bundle, Default, Clone)]
|
|
pub struct DirectionalLightBundle {
|
|
pub directional_light: DirectionalLight,
|
|
pub frusta: CascadesFrusta,
|
|
pub cascades: Cascades,
|
|
pub cascade_shadow_config: CascadeShadowConfig,
|
|
pub visible_entities: CascadesVisibleEntities,
|
|
pub transform: Transform,
|
|
pub global_transform: GlobalTransform,
|
|
/// Enables or disables the light
|
|
pub visibility: Visibility,
|
|
/// Inherited visibility of an entity.
|
|
pub inherited_visibility: InheritedVisibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub view_visibility: ViewVisibility,
|
|
/// Marker component that indicates that its entity needs to be synchronized to the render world
|
|
pub sync: SyncToRenderWorld,
|
|
}
|