Skip to content

Commit 839d2f8

Browse files
authored
Approximate indirect specular occlusion (#11152)
# Objective - The current PBR renderer over-brightens indirect specular reflections, which tends to cause objects to appear to glow, because specular occlusion is not accounted for. ## Solution - Attenuate indirect specular term with an approximation for specular occlusion, using [[Lagarde et al., 2014] (pg. 76)](https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf). | Before | After | Animation | | --- | --- | --- | | <img width="1840" alt="before bike" src="https://github.com/bevyengine/bevy/assets/2632925/b6e10d15-a998-4a94-875a-1c2b1e98348a"> | <img width="1840" alt="after bike" src="https://github.com/bevyengine/bevy/assets/2632925/53b1479c-b1e4-427f-b140-53df26ca7193"> | ![ezgif-1-fbcbaf272b](https://github.com/bevyengine/bevy/assets/2632925/c2dece1c-eb3d-4e05-92a2-46cf83052c7c) | | <img width="1840" alt="classroom before" src="https://github.com/bevyengine/bevy/assets/2632925/b16c0e74-741e-4f40-a7df-8863eaa62596"> | <img width="1840" alt="classroom after" src="https://github.com/bevyengine/bevy/assets/2632925/26f9e971-0c63-4ee9-9544-964e5703d65e"> | ![ezgif-1-0f390edd06](https://github.com/bevyengine/bevy/assets/2632925/d8894e52-380f-4528-aa0d-1ca249108178) | --- ## Changelog - Ambient occlusion now applies to indirect specular reflections to approximate how objects occlude specular light. ## Migration Guide - Renamed `PbrInput::occlusion` to `diffuse_occlusion`, and added `specular_occlusion`.
1 parent 4695b82 commit 839d2f8

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

crates/bevy_pbr/src/deferred/deferred_lighting.wgsl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
pbr_functions,
55
pbr_deferred_functions::pbr_input_from_deferred_gbuffer,
66
pbr_deferred_types::unpack_unorm3x4_plus_unorm_20_,
7+
lighting,
78
mesh_view_bindings::deferred_prepass_texture,
89
}
910

@@ -64,7 +65,15 @@ fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> {
6465
#ifdef SCREEN_SPACE_AMBIENT_OCCLUSION
6566
let ssao = textureLoad(screen_space_ambient_occlusion_texture, vec2<i32>(in.position.xy), 0i).r;
6667
let ssao_multibounce = gtao_multibounce(ssao, pbr_input.material.base_color.rgb);
67-
pbr_input.occlusion = min(pbr_input.occlusion, ssao_multibounce);
68+
pbr_input.diffuse_occlusion = min(pbr_input.diffuse_occlusion, ssao_multibounce);
69+
70+
// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
71+
let NdotV = max(dot(pbr_input.N, pbr_input.V), 0.0001);
72+
var perceptual_roughness: f32 = pbr_input.material.perceptual_roughness;
73+
let roughness = lighting::perceptualRoughnessToRoughness(perceptual_roughness);
74+
// Use SSAO to estimate the specular occlusion.
75+
// Lagarde and Rousiers 2014, "Moving Frostbite to Physically Based Rendering"
76+
pbr_input.specular_occlusion = saturate(pow(NdotV + ssao, exp2(-16.0 * roughness - 1.0)) - 1.0 + ssao);
6877
#endif // SCREEN_SPACE_AMBIENT_OCCLUSION
6978

7079
output_color = pbr_functions::apply_pbr_lighting(pbr_input);

crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ fn deferred_gbuffer_from_pbr_input(in: PbrInput) -> vec4<u32> {
2222
// Real time occlusion is applied in the deferred lighting pass.
2323
// Deriving luminance via Rec. 709. coefficients
2424
// https://en.wikipedia.org/wiki/Rec._709
25-
let occlusion = dot(in.occlusion, vec3<f32>(0.2126, 0.7152, 0.0722));
25+
let diffuse_occlusion = dot(in.diffuse_occlusion, vec3<f32>(0.2126, 0.7152, 0.0722));
2626
#ifdef WEBGL2 // More crunched for webgl so we can also fit depth.
2727
var props = deferred_types::pack_unorm3x4_plus_unorm_20_(vec4(
2828
in.material.reflectance,
2929
in.material.metallic,
30-
occlusion,
30+
diffuse_occlusion,
3131
in.frag_coord.z));
3232
#else
3333
var props = deferred_types::pack_unorm4x8_(vec4(
3434
in.material.reflectance, // could be fewer bits
3535
in.material.metallic, // could be fewer bits
36-
occlusion, // is this worth including?
36+
diffuse_occlusion, // is this worth including?
3737
0.0)); // spare
3838
#endif // WEBGL2
3939
let flags = deferred_types::deferred_flags_from_mesh_material_flags(in.flags, in.material.flags);
@@ -85,7 +85,7 @@ fn pbr_input_from_deferred_gbuffer(frag_coord: vec4<f32>, gbuffer: vec4<u32>) ->
8585
pbr.material.reflectance = props.r;
8686
#endif // WEBGL2
8787
pbr.material.metallic = props.g;
88-
pbr.occlusion = vec3(props.b);
88+
pbr.diffuse_occlusion = vec3(props.b);
8989
let octahedral_normal = deferred_types::unpack_24bit_normal(gbuffer.a);
9090
let N = octahedral_decode(octahedral_normal);
9191

crates/bevy_pbr/src/render/pbr_fragment.wgsl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
pbr_bindings,
66
pbr_types,
77
prepass_utils,
8+
lighting,
89
mesh_bindings::mesh,
910
mesh_view_bindings::view,
1011
parallax_mapping::parallaxed_uv,
@@ -68,6 +69,9 @@ fn pbr_input_from_standard_material(
6869
pbr_input.material.base_color *= pbr_bindings::material.base_color;
6970
pbr_input.material.deferred_lighting_pass_id = pbr_bindings::material.deferred_lighting_pass_id;
7071

72+
// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
73+
let NdotV = max(dot(pbr_input.N, pbr_input.V), 0.0001);
74+
7175
#ifdef VERTEX_UVS
7276
var uv = in.uv;
7377

@@ -120,6 +124,7 @@ fn pbr_input_from_standard_material(
120124
// metallic and perceptual roughness
121125
var metallic: f32 = pbr_bindings::material.metallic;
122126
var perceptual_roughness: f32 = pbr_bindings::material.perceptual_roughness;
127+
let roughness = lighting::perceptualRoughnessToRoughness(perceptual_roughness);
123128
#ifdef VERTEX_UVS
124129
if ((pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT) != 0u) {
125130
let metallic_roughness = textureSampleBias(pbr_bindings::metallic_roughness_texture, pbr_bindings::metallic_roughness_sampler, uv, view.mip_bias);
@@ -159,20 +164,23 @@ fn pbr_input_from_standard_material(
159164
#endif
160165
pbr_input.material.diffuse_transmission = diffuse_transmission;
161166

162-
// occlusion
163-
// TODO: Split into diffuse/specular occlusion?
164-
var occlusion: vec3<f32> = vec3(1.0);
167+
var diffuse_occlusion: vec3<f32> = vec3(1.0);
168+
var specular_occlusion: f32 = 1.0;
165169
#ifdef VERTEX_UVS
166170
if ((pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT) != 0u) {
167-
occlusion = vec3(textureSampleBias(pbr_bindings::occlusion_texture, pbr_bindings::occlusion_sampler, uv, view.mip_bias).r);
171+
diffuse_occlusion = vec3(textureSampleBias(pbr_bindings::occlusion_texture, pbr_bindings::occlusion_sampler, uv, view.mip_bias).r);
168172
}
169173
#endif
170174
#ifdef SCREEN_SPACE_AMBIENT_OCCLUSION
171175
let ssao = textureLoad(screen_space_ambient_occlusion_texture, vec2<i32>(in.position.xy), 0i).r;
172176
let ssao_multibounce = gtao_multibounce(ssao, pbr_input.material.base_color.rgb);
173-
occlusion = min(occlusion, ssao_multibounce);
177+
diffuse_occlusion = min(diffuse_occlusion, ssao_multibounce);
178+
// Use SSAO to estimate the specular occlusion.
179+
// Lagarde and Rousiers 2014, "Moving Frostbite to Physically Based Rendering"
180+
specular_occlusion = saturate(pow(NdotV + ssao, exp2(-16.0 * roughness - 1.0)) - 1.0 + ssao);
174181
#endif
175-
pbr_input.occlusion = occlusion;
182+
pbr_input.diffuse_occlusion = diffuse_occlusion;
183+
pbr_input.specular_occlusion = specular_occlusion;
176184

177185
// N (normal vector)
178186
#ifndef LOAD_PREPASS_NORMALS

crates/bevy_pbr/src/render/pbr_functions.wgsl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ fn apply_pbr_lighting(
164164

165165
let specular_transmissive_color = specular_transmission * in.material.base_color.rgb;
166166

167-
let occlusion = in.occlusion;
167+
let diffuse_occlusion = in.diffuse_occlusion;
168+
let specular_occlusion = in.specular_occlusion;
168169

169170
// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
170171
let NdotV = max(dot(in.N, in.V), 0.0001);
@@ -306,7 +307,7 @@ fn apply_pbr_lighting(
306307
}
307308

308309
// Ambient light (indirect)
309-
var indirect_light = ambient::ambient_light(in.world_position, in.N, in.V, NdotV, diffuse_color, F0, perceptual_roughness, occlusion);
310+
var indirect_light = ambient::ambient_light(in.world_position, in.N, in.V, NdotV, diffuse_color, F0, perceptual_roughness, diffuse_occlusion);
310311

311312
if diffuse_transmission > 0.0 {
312313
// NOTE: We use the diffuse transmissive color, the second Lambertian lobe's calculated
@@ -316,14 +317,14 @@ fn apply_pbr_lighting(
316317
// perceptual_roughness = 1.0;
317318
// NdotV = 1.0;
318319
// F0 = vec3<f32>(0.0)
319-
// occlusion = vec3<f32>(1.0)
320+
// diffuse_occlusion = vec3<f32>(1.0)
320321
transmitted_light += ambient::ambient_light(diffuse_transmissive_lobe_world_position, -in.N, -in.V, 1.0, diffuse_transmissive_color, vec3<f32>(0.0), 1.0, vec3<f32>(1.0));
321322
}
322323

323324
// Environment map light (indirect)
324325
#ifdef ENVIRONMENT_MAP
325326
let environment_light = environment_map::environment_map_light(perceptual_roughness, roughness, diffuse_color, NdotV, f_ab, in.N, R, F0);
326-
indirect_light += (environment_light.diffuse * occlusion) + environment_light.specular;
327+
indirect_light += (environment_light.diffuse * diffuse_occlusion) + (environment_light.specular * specular_occlusion);
327328

328329
// we'll use the specular component of the transmitted environment
329330
// light in the call to `specular_transmissive_light()` below
@@ -338,7 +339,7 @@ fn apply_pbr_lighting(
338339
// NdotV = 1.0;
339340
// R = T // see definition below
340341
// F0 = vec3<f32>(1.0)
341-
// occlusion = 1.0
342+
// diffuse_occlusion = 1.0
342343
//
343344
// (This one is slightly different from the other light types above, because the environment
344345
// map light returns both diffuse and specular components separately, and we want to use both)

crates/bevy_pbr/src/render/pbr_types.wgsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ fn standard_material_new() -> StandardMaterial {
8080

8181
struct PbrInput {
8282
material: StandardMaterial,
83-
occlusion: vec3<f32>,
83+
diffuse_occlusion: vec3<f32>,
84+
specular_occlusion: f32,
8485
frag_coord: vec4<f32>,
8586
world_position: vec4<f32>,
8687
// Normalized world normal used for shadow mapping as normal-mapping is not used for shadow
@@ -101,7 +102,8 @@ fn pbr_input_new() -> PbrInput {
101102
var pbr_input: PbrInput;
102103

103104
pbr_input.material = standard_material_new();
104-
pbr_input.occlusion = vec3<f32>(1.0);
105+
pbr_input.diffuse_occlusion = vec3<f32>(1.0);
106+
pbr_input.specular_occlusion = 1.0;
105107

106108
pbr_input.frag_coord = vec4<f32>(0.0, 0.0, 0.0, 1.0);
107109
pbr_input.world_position = vec4<f32>(0.0, 0.0, 0.0, 1.0);

0 commit comments

Comments
 (0)