bevy/examples/3d
Trashtalk217 56f8e526dd
The Cooler 'Retain Rendering World' (#15320)
- 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>
2024-09-30 18:51:43 +00:00
..
3d_scene.rs Swapping back to using From<Color> for StandardMaterial in examples (#13566) 2024-05-29 13:50:28 +00:00
3d_shapes.rs Disabled usage of the POLYGON_MODE_LINE gpu feature in the examples (#14402) 2024-07-29 23:40:39 +00:00
3d_viewport_to_world.rs Return Results from Camera's world/viewport conversion methods (#14989) 2024-09-03 19:45:15 +00:00
animated_material.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
anisotropy.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
anti_aliasing.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
atmospheric_fog.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
auto_exposure.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
blend_modes.rs Fix common capitalization errors in documentation (#14562) 2024-07-31 21:16:05 +00:00
bloom_3d.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
clearcoat.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
color_grading.rs Fixing text sizes for examples (#15190) 2024-09-16 23:14:37 +00:00
deferred_rendering.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
depth_of_field.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
fog.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
fog_volumes.rs The Cooler 'Retain Rendering World' (#15320) 2024-09-30 18:51:43 +00:00
generate_custom_mesh.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
irradiance_volumes.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
lighting.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
lightmaps.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
lines.rs Highlight dependency on shader files in examples (#13824) 2024-06-12 14:16:01 +00:00
load_gltf.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
load_gltf_extras.rs Fixing text sizes for examples (#15190) 2024-09-16 23:14:37 +00:00
meshlet.rs Meshlet screenspace-derived tangents (#15084) 2024-09-29 18:39:25 +00:00
motion_blur.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
orthographic.rs Split OrthographicProjection::default into 2d & 3d (Adopted) (#15073) 2024-09-09 15:51:28 +00:00
parallax_mapping.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
parenting.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
pbr.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
pcss.rs Implement percentage-closer soft shadows (PCSS). (#13497) 2024-09-18 18:07:17 +00:00
post_processing.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
query_gltf_primitives.rs feat(gltf): add name component to gltf mesh primitive (#13912) 2024-09-30 16:51:52 +00:00
reflection_probes.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
render_to_texture.rs Broaden "Check for bevy_internal imports" CI Task (#15333) 2024-09-20 17:08:37 +00:00
rotate_environment_map.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
scrolling_fog.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
shadow_biases.rs Simplified ui_stack_system (#9889) 2024-09-30 18:43:57 +00:00
shadow_caster_receiver.rs Remove redundant imports (#12817) 2024-04-01 19:59:08 +00:00
skybox.rs Better info message (#15432) 2024-09-26 13:32:33 +00:00
spherical_area_lights.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
split_screen.rs Fix num_cascades in split_screen exmample for WebGL (#14601) 2024-08-04 13:57:22 +00:00
spotlight.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
ssao.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
ssr.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
texture.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
tonemapping.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
transmission.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
transparency_3d.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
two_passes.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
update_gltf_scene.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
vertex_colors.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
visibility_range.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
volumetric_fog.rs Implement volumetric fog support for both point lights and spotlights (#15361) 2024-09-29 21:30:53 +00:00
wireframe.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00