From b1db977176ea9eb2b78c1868274f20e827ff4f59 Mon Sep 17 00:00:00 2001 From: Marco Buono Date: Wed, 19 Apr 2023 02:08:50 -0300 Subject: [PATCH] Use depth prepass data to reject values that are in front of the current fragment --- crates/bevy_pbr/src/prepass/mod.rs | 14 ++++++++------ crates/bevy_pbr/src/render/pbr_lighting.wgsl | 9 +++++++-- examples/3d/transmission.rs | 3 ++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/crates/bevy_pbr/src/prepass/mod.rs b/crates/bevy_pbr/src/prepass/mod.rs index 0659f20a32..958c253cc8 100644 --- a/crates/bevy_pbr/src/prepass/mod.rs +++ b/crates/bevy_pbr/src/prepass/mod.rs @@ -988,12 +988,14 @@ pub fn queue_prepass_material_meshes( rangefinder.distance(&mesh_uniform.transform) + material.properties.depth_bias; match alpha_mode { AlphaMode::Opaque => { - opaque_phase.add(Opaque3dPrepass { - entity: *visible_entity, - draw_function: opaque_draw_prepass, - pipeline_id, - distance, - }); + if !material.properties.is_transmissive { + opaque_phase.add(Opaque3dPrepass { + entity: *visible_entity, + draw_function: opaque_draw_prepass, + pipeline_id, + distance, + }); + } } AlphaMode::Mask(_) => { alpha_mask_phase.add(AlphaMask3dPrepass { diff --git a/crates/bevy_pbr/src/render/pbr_lighting.wgsl b/crates/bevy_pbr/src/render/pbr_lighting.wgsl index d7ec5a3bdc..7af2fef4ec 100644 --- a/crates/bevy_pbr/src/render/pbr_lighting.wgsl +++ b/crates/bevy_pbr/src/render/pbr_lighting.wgsl @@ -336,13 +336,18 @@ fn fetch_transmissive_background(offset_position: vec2, frag_coord: vec3(f32(i) * 4773.0, f32(i) * 1472.0)); let dither_offset = (blur_intensity * 7.0) * (blur_intensity * 7.0) * (30.0 * dither.yz + 0.03 * normalize(dither).xy) * vec2(1.0, -aspect); + let offset_position_with_dither = offset_position + dither_offset; + + // Use depth prepass data to reject values that are in front of the current fragment + if (prepass_depth(vec4(offset_position_with_dither * view.viewport.zw, 0.0, 0.0), 0u) > frag_coord.z) { + continue; + } // Sample the view transmission texture at the offset position + dither offset, to get the background color - // TODO: Use depth prepass data to reject values that are in front of the current fragment result += textureSample( view_transmission_texture, view_transmission_sampler, - offset_position + dither_offset, + offset_position_with_dither, ); } diff --git a/examples/3d/transmission.rs b/examples/3d/transmission.rs index 286d9be2d0..0ba8af8db4 100644 --- a/examples/3d/transmission.rs +++ b/examples/3d/transmission.rs @@ -17,7 +17,7 @@ use std::f32::consts::PI; use bevy::{ - core_pipeline::{bloom::BloomSettings, tonemapping::Tonemapping}, + core_pipeline::{bloom::BloomSettings, prepass::DepthPrepass, tonemapping::Tonemapping}, pbr::{NotShadowCaster, NotTransmittedShadowReceiver, PointLightShadowMap}, prelude::*, render::view::ColorGrading, @@ -343,6 +343,7 @@ fn setup( tonemapping: Tonemapping::TonyMcMapface, ..default() }, + DepthPrepass, EnvironmentMapLight { diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"), specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),