bevy/crates
Sou1gh0st 3a478ad5c1
Fix lightmaps break when deferred rendering is enabled (#14599)
# Objective
- Fixes https://github.com/bevyengine/bevy/issues/13552

## Solution
- Thanks for the guidance from @DGriffin91, the current solution is to
transmit the light_map through the emissive channel to avoid increasing
the bandwidth of deferred shading.
- <del>Store lightmap sample result into G-Buffer and pass them into the
`Deferred Lighting Pipeline`, therefore we can get the correct indirect
lighting via the `apply_pbr_lighting` function.</del>
- <del>The original G-Buffer lacks storage for lightmap data, therefore
a new buffer is added. We can only use Rgba16Uint here due to the
32-byte limit on the render targets.</del>

## Testing
- Need to test all the examples that contains a prepass, with both the
forward and deferred rendering mode.
- I have tested the ones below.
- `lightmaps` (adjust the code based on the issue and check the
rendering result)
    - `transmission` (it contains a prepass)
    - `ssr` (it also uses the G-Bufffer)
    - `meshlet` (forward and deferred)
    - `pbr`

## Showcase
By updating the `lightmaps` example to use deferred rendering, this pull
request enables correct rendering result of the Cornell Box.

```
diff --git a/examples/3d/lightmaps.rs b/examples/3d/lightmaps.rs
index 564a3162b..11a748fba 100644
--- a/examples/3d/lightmaps.rs
+++ b/examples/3d/lightmaps.rs
@@ -1,12 +1,14 @@
 //! Rendering a scene with baked lightmaps.
 
-use bevy::pbr::Lightmap;
+use bevy::core_pipeline::prepass::DeferredPrepass;
+use bevy::pbr::{DefaultOpaqueRendererMethod, Lightmap};
 use bevy::prelude::*;
 
 fn main() {
     App::new()
         .add_plugins(DefaultPlugins)
         .insert_resource(AmbientLight::NONE)
+        .insert_resource(DefaultOpaqueRendererMethod::deferred())
         .add_systems(Startup, setup)
         .add_systems(Update, add_lightmaps_to_meshes)
         .run();
@@ -19,10 +21,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
         ..default()
     });
 
-    commands.spawn(Camera3dBundle {
-        transform: Transform::from_xyz(-278.0, 273.0, 800.0),
-        ..default()
-    });
+    commands
+        .spawn(Camera3dBundle {
+            transform: Transform::from_xyz(-278.0, 273.0, 800.0),
+            ..default()
+        })
+        .insert(DeferredPrepass);
 }
 
 fn add_lightmaps_to_meshes(
```

<img width="1280" alt="image"
src="https://github.com/user-attachments/assets/17fd3367-61cc-4c23-b956-e7cfc751af3c">

## Emissive Issue
**The emissive light object appears incorrectly rendered because the
alpha channel of emission is set to 1 in deferred rendering and 0 in
forward rendering, leading to different emissive light result. Could
this be a bug?**

```wgsl
// pbr_deferred_functions.wgsl - pbr_input_from_deferred_gbuffer
let emissive = rgb9e5::rgb9e5_to_vec3_(gbuffer.g);
if ((pbr.material.flags & STANDARD_MATERIAL_FLAGS_UNLIT_BIT) != 0u) {
    pbr.material.base_color = vec4(emissive, 1.0);
    pbr.material.emissive = vec4(vec3(0.0), 1.0);
} else {
    pbr.material.base_color = vec4(pow(base_rough.rgb, vec3(2.2)), 1.0);
    pbr.material.emissive = vec4(emissive, 1.0);
}

// pbr_functions.wgsl - apply_pbr_lighting
emissive_light = emissive_light * mix(1.0, view_bindings::view.exposure, emissive.a);
```

---------

Co-authored-by: JMS55 <47158642+JMS55@users.noreply.github.com>
2024-10-18 23:18:11 +00:00
..
bevy_a11y Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_animation aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
bevy_app Migrate from Query::single and friends to Single (#15872) 2024-10-13 20:32:06 +00:00
bevy_asset [Adopted] Add a method for asynchronously waiting for an asset to load (#15913) 2024-10-15 02:50:33 +00:00
bevy_audio Fix audio not playing (#15638) 2024-10-04 01:07:09 +00:00
bevy_color Fix bevy_color not compiling standalone. (#15938) 2024-10-15 23:52:45 +00:00
bevy_core Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_core_pipeline Implement WorldQuery for MainWorld and RenderWorld components (#15745) 2024-10-13 20:58:46 +00:00
bevy_derive move ANDROID_APP to bevy_window (#15585) 2024-10-02 03:01:06 +00:00
bevy_dev_tools Merge Style properties into Node. Use ComputedNode for computed properties. (#15975) 2024-10-18 22:25:33 +00:00
bevy_diagnostic aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
bevy_dylib Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_ecs Merge Style properties into Node. Use ComputedNode for computed properties. (#15975) 2024-10-18 22:25:33 +00:00
bevy_encase_derive Update `glam to 0.29, encase` to 0.10. (#15249) 2024-09-23 19:44:02 +00:00
bevy_gilrs Remove thiserror from bevy_gilrs (#15773) 2024-10-09 14:21:25 +00:00
bevy_gizmos Fix gizmos (#15836) 2024-10-10 22:04:04 +00:00
bevy_gltf Improve API for scaling orthographic cameras (#15969) 2024-10-17 17:50:06 +00:00
bevy_hierarchy Migrate from Query::single and friends to Single (#15872) 2024-10-13 20:32:06 +00:00
bevy_image Remove AVIF feature (#15973) 2024-10-17 19:47:28 +00:00
bevy_input Remove thiserror from bevy_input (#15770) 2024-10-09 14:23:01 +00:00
bevy_internal Remove AVIF feature (#15973) 2024-10-17 19:47:28 +00:00
bevy_log Use oslog for ios (#13364) 2024-10-11 08:58:14 +00:00
bevy_macro_utils Modify derive_label to support no_std environments (#15465) 2024-09-27 20:23:26 +00:00
bevy_math Remove write access to ConvexPolygon.vertices (#15965) 2024-10-16 22:21:01 +00:00
bevy_mesh Remove write access to ConvexPolygon.vertices (#15965) 2024-10-16 22:21:01 +00:00
bevy_mikktspace Add no_std support to bevy_mikktspace (#15528) 2024-09-30 18:17:03 +00:00
bevy_pbr Fix lightmaps break when deferred rendering is enabled (#14599) 2024-10-18 23:18:11 +00:00
bevy_picking Add read-only access to PointerInteraction (#15964) 2024-10-16 21:21:19 +00:00
bevy_ptr Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_reflect bevy_reflect: get_represented_kind_info APIs for reflected kinds (#14380) 2024-10-15 02:08:31 +00:00
bevy_remote Remove a dbg! statement left over from debugging (#15867) 2024-10-12 09:07:01 +00:00
bevy_render Improve API for scaling orthographic cameras (#15969) 2024-10-17 17:50:06 +00:00
bevy_scene Rename App/World::observe to add_observer, EntityWorldMut::observe_entity to observe. (#15754) 2024-10-09 15:39:29 +00:00
bevy_sprite Revert default mesh materials (#15930) 2024-10-15 19:47:40 +00:00
bevy_state Add World::get_resource_or_init as an alternative to World::get_resource_or_insert_with (#15758) 2024-10-09 20:56:26 +00:00
bevy_tasks bump async-channel to 2.3.0 (#15497) 2024-09-28 19:21:59 +00:00
bevy_text Merge Style properties into Node. Use ComputedNode for computed properties. (#15975) 2024-10-18 22:25:33 +00:00
bevy_time aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
bevy_transform Remove thiserror from bevy_transform (#15761) 2024-10-09 14:27:30 +00:00
bevy_ui Merge Style properties into Node. Use ComputedNode for computed properties. (#15975) 2024-10-18 22:25:33 +00:00
bevy_utils Fix detailed_trace module scope (#15912) 2024-10-15 02:48:36 +00:00
bevy_window Add window drag move and drag resize without decoration example. (#15814) 2024-10-15 23:38:35 +00:00
bevy_winit Add window drag move and drag resize without decoration example. (#15814) 2024-10-15 23:38:35 +00:00