Skip to content

Commit

Permalink
add transparent depth bias for stability
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Aug 16, 2023
1 parent 3148159 commit 38658f0
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions crates/scene_runner/src/update_world/material.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::{ecs::system::SystemParam, pbr::NotShadowCaster, prelude::*};
use bevy::{ecs::system::SystemParam, pbr::NotShadowCaster, prelude::*, render::primitives::Aabb};

use crate::{renderer_context::RendererSceneContext, ContainerEntity, SceneSets};
use common::util::TryInsertEx;
Expand Down Expand Up @@ -128,7 +128,10 @@ impl Plugin for MaterialDefinitionPlugin {
ComponentPosition::EntityOnly,
);

app.add_systems(Update, update_materials.in_set(SceneSets::PostLoop));
app.add_systems(
Update,
(update_materials, update_bias).in_set(SceneSets::PostLoop),
);
}
}

Expand Down Expand Up @@ -272,3 +275,22 @@ fn update_materials(
materials.get_mut(touch);
}
}

#[allow(clippy::type_complexity)]
fn update_bias(
mut materials: ResMut<Assets<StandardMaterial>>,
query: Query<
(&Aabb, &Handle<StandardMaterial>),
Or<(Changed<Handle<StandardMaterial>>, Changed<Aabb>)>,
>,
) {
for (aabb, h_material) in query.iter() {
if let Some(material) = materials.get_mut(h_material) {
if material.alpha_mode == AlphaMode::Blend {
// add a bias based on the aabb size, to force an explicit transparent order which is
// hopefully correct, but should be better than nothing even if not always perfect
material.depth_bias = aabb.half_extents.length() * 1e-5;
}
}
}
}

0 comments on commit 38658f0

Please sign in to comment.