From 361c79239d6bc415f322177d0fe9855e928ae5eb Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Fri, 4 Mar 2022 12:02:29 +0000 Subject: [PATCH 1/4] add depth_bias to SpecializedMaterial --- crates/bevy_pbr/src/material.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 537026524b51d..a80d363d507e0 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -186,6 +186,12 @@ pub trait SpecializedMaterial: Asset + RenderAsset { fn dynamic_uniform_indices(material: &::PreparedAsset) -> &[u32] { &[] } + + #[allow(unused_variables)] + #[inline] + fn depth_bias(material: &::PreparedAsset) -> f32 { + 0.0 + } } /// Adds the necessary ECS resources and render logic to enable rendering entities using the given [`SpecializedMaterial`] @@ -375,7 +381,8 @@ pub fn queue_material_meshes( // NOTE: row 2 of the inverse view matrix dotted with column 3 of the model matrix // gives the z component of translation of the mesh in view space - let mesh_z = inverse_view_row_2.dot(mesh_uniform.transform.col(3)); + let bias = M::depth_bias(material); + let mesh_z = inverse_view_row_2.dot(mesh_uniform.transform.col(3)) + bias; match alpha_mode { AlphaMode::Opaque => { opaque_phase.add(Opaque3d { From ff4136498e533c9efa33c0384c3a81a4a281d282 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Sat, 5 Mar 2022 23:44:16 +0000 Subject: [PATCH 2/4] add doc comment --- crates/bevy_pbr/src/material.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index a80d363d507e0..bbbb9261a835e 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -189,6 +189,8 @@ pub trait SpecializedMaterial: Asset + RenderAsset { #[allow(unused_variables)] #[inline] + /// Add a bias to the view depth of the mesh which can be used to force a specific render order + /// for meshes with equal depth, to avoid z-fighting. fn depth_bias(material: &::PreparedAsset) -> f32 { 0.0 } From 530b9906d343167edd8a27425fe13f80d22f4b73 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 29 Mar 2022 11:28:02 +0100 Subject: [PATCH 3/4] add bias to StandardMaterial & Material --- crates/bevy_pbr/src/material.rs | 14 +++++++++++++- crates/bevy_pbr/src/pbr_material.rs | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index bbbb9261a835e..14630cfd4614b 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -74,6 +74,14 @@ pub trait Material: Asset + RenderAsset { &[] } + #[allow(unused_variables)] + #[inline] + /// Add a bias to the view depth of the mesh which can be used to force a specific render order + /// for meshes with equal depth, to avoid z-fighting. + fn depth_bias(material: &::PreparedAsset) -> f32 { + 0.0 + } + /// Customizes the default [`RenderPipelineDescriptor`]. #[allow(unused_variables)] #[inline] @@ -125,11 +133,15 @@ impl SpecializedMaterial for M { ::fragment_shader(asset_server) } - #[allow(unused_variables)] #[inline] fn dynamic_uniform_indices(material: &::PreparedAsset) -> &[u32] { ::dynamic_uniform_indices(material) } + + #[inline] + fn depth_bias(material: &::PreparedAsset) -> f32 { + ::depth_bias(material) + } } /// Materials are used alongside [`MaterialPlugin`] and [`MaterialMeshBundle`](crate::MaterialMeshBundle) diff --git a/crates/bevy_pbr/src/pbr_material.rs b/crates/bevy_pbr/src/pbr_material.rs index eaf45397a78a4..907f628a0baf7 100644 --- a/crates/bevy_pbr/src/pbr_material.rs +++ b/crates/bevy_pbr/src/pbr_material.rs @@ -51,6 +51,7 @@ pub struct StandardMaterial { pub double_sided: bool, pub unlit: bool, pub alpha_mode: AlphaMode, + pub depth_bias: f32, } impl Default for StandardMaterial { @@ -79,6 +80,7 @@ impl Default for StandardMaterial { double_sided: false, unlit: false, alpha_mode: AlphaMode::Opaque, + depth_bias: 0.0, } } } @@ -154,6 +156,7 @@ pub struct GpuStandardMaterial { pub flags: StandardMaterialFlags, pub base_color_texture: Option>, pub alpha_mode: AlphaMode, + pub depth_bias: f32, } impl RenderAsset for StandardMaterial { @@ -321,6 +324,7 @@ impl RenderAsset for StandardMaterial { has_normal_map, base_color_texture: material.base_color_texture, alpha_mode: material.alpha_mode, + depth_bias: material.depth_bias, }) } } @@ -483,4 +487,9 @@ impl SpecializedMaterial for StandardMaterial { fn alpha_mode(render_asset: &::PreparedAsset) -> AlphaMode { render_asset.alpha_mode } + + #[inline] + fn depth_bias(material: &::PreparedAsset) -> f32 { + material.depth_bias + } } From c0f96a740b6a761ca240ca40cfbd6f9c9fab3cc2 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 29 Mar 2022 12:36:11 +0100 Subject: [PATCH 4/4] format --- crates/bevy_pbr/src/material.rs | 2 +- crates/bevy_pbr/src/pbr_material.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 14630cfd4614b..f96df74313834 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -141,7 +141,7 @@ impl SpecializedMaterial for M { #[inline] fn depth_bias(material: &::PreparedAsset) -> f32 { ::depth_bias(material) - } + } } /// Materials are used alongside [`MaterialPlugin`] and [`MaterialMeshBundle`](crate::MaterialMeshBundle) diff --git a/crates/bevy_pbr/src/pbr_material.rs b/crates/bevy_pbr/src/pbr_material.rs index 32d50b93189ed..b1e90f8665a0a 100644 --- a/crates/bevy_pbr/src/pbr_material.rs +++ b/crates/bevy_pbr/src/pbr_material.rs @@ -505,5 +505,5 @@ impl SpecializedMaterial for StandardMaterial { #[inline] fn depth_bias(material: &::PreparedAsset) -> f32 { material.depth_bias - } + } }