From 45967b03b56d1a278c6f7ca020f393dc10c199dd Mon Sep 17 00:00:00 2001 From: vero Date: Mon, 29 Jan 2024 08:39:49 -0800 Subject: [PATCH] Fix specular envmap in deferred (#11534) # Objective - Fixes #11414 ## Solution - Add specular occlusion to g-buffer so PbrInput can be properly reconstructed for shading with a non-zero value allowing the spec envmap to be seen ![image](https://github.com/bevyengine/bevy/assets/11307157/84aa8312-7c06-4dc7-92da-5d94b54b133d) --------- Co-authored-by: JMS55 <47158642+JMS55@users.noreply.github.com> --- crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl | 5 ++--- crates/bevy_pbr/src/render/pbr_types.wgsl | 7 +++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl b/crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl index 495ee6b734..d8d923ede8 100644 --- a/crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl +++ b/crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl @@ -1,7 +1,7 @@ #define_import_path bevy_pbr::pbr_deferred_functions #import bevy_pbr::{ - pbr_types::{PbrInput, standard_material_new, STANDARD_MATERIAL_FLAGS_UNLIT_BIT}, + pbr_types::{PbrInput, pbr_input_new, STANDARD_MATERIAL_FLAGS_UNLIT_BIT}, pbr_deferred_types as deferred_types, pbr_functions, rgb9e5, @@ -58,8 +58,7 @@ fn deferred_gbuffer_from_pbr_input(in: PbrInput) -> vec4 { // Creates a PbrInput from the deferred gbuffer. fn pbr_input_from_deferred_gbuffer(frag_coord: vec4, gbuffer: vec4) -> PbrInput { - var pbr: PbrInput; - pbr.material = standard_material_new(); + var pbr = pbr_input_new(); let flags = deferred_types::unpack_flags(gbuffer.a); let deferred_flags = deferred_types::mesh_material_flags_from_deferred_flags(flags); diff --git a/crates/bevy_pbr/src/render/pbr_types.wgsl b/crates/bevy_pbr/src/render/pbr_types.wgsl index cd7170275b..72a70e45c8 100644 --- a/crates/bevy_pbr/src/render/pbr_types.wgsl +++ b/crates/bevy_pbr/src/render/pbr_types.wgsl @@ -24,7 +24,7 @@ struct StandardMaterial { }; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -// NOTE: if these flags are updated or changed. Be sure to also update +// NOTE: if these flags are updated or changed. Be sure to also update // deferred_flags_from_mesh_material_flags and mesh_material_flags_from_deferred_flags // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! const STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT: u32 = 1u; @@ -74,13 +74,15 @@ fn standard_material_new() -> StandardMaterial { material.max_parallax_layer_count = 16.0; material.max_relief_mapping_search_steps = 5u; material.deferred_lighting_pass_id = 1u; - + return material; } struct PbrInput { material: StandardMaterial, + // Note: this gets monochromized upon deferred PbrInput reconstruction. diffuse_occlusion: vec3, + // Note: this is 1.0 (entirely unoccluded) when SSAO is off. specular_occlusion: f32, frag_coord: vec4, world_position: vec4, @@ -103,6 +105,7 @@ fn pbr_input_new() -> PbrInput { pbr_input.material = standard_material_new(); pbr_input.diffuse_occlusion = vec3(1.0); + // If SSAO is enabled, then this gets overwritten with proper specular occlusion. If its not, then we get specular environment map unoccluded (we have no data with which to occlude it with). pbr_input.specular_occlusion = 1.0; pbr_input.frag_coord = vec4(0.0, 0.0, 0.0, 1.0);