Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - add depth_bias to SpecializedMaterial #4101

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &<Self as RenderAsset>::PreparedAsset) -> f32 {
0.0
}

/// Customizes the default [`RenderPipelineDescriptor`].
#[allow(unused_variables)]
#[inline]
Expand Down Expand Up @@ -125,11 +133,15 @@ impl<M: Material> SpecializedMaterial for M {
<M as Material>::fragment_shader(asset_server)
}

#[allow(unused_variables)]
#[inline]
fn dynamic_uniform_indices(material: &<Self as RenderAsset>::PreparedAsset) -> &[u32] {
<M as Material>::dynamic_uniform_indices(material)
}

#[inline]
fn depth_bias(material: &<Self as RenderAsset>::PreparedAsset) -> f32 {
<M as Material>::depth_bias(material)
}
}

/// Materials are used alongside [`MaterialPlugin`] and [`MaterialMeshBundle`](crate::MaterialMeshBundle)
Expand Down Expand Up @@ -186,6 +198,14 @@ pub trait SpecializedMaterial: Asset + RenderAsset {
fn dynamic_uniform_indices(material: &<Self as RenderAsset>::PreparedAsset) -> &[u32] {
&[]
}

#[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: &<Self as RenderAsset>::PreparedAsset) -> f32 {
0.0
}
}

/// Adds the necessary ECS resources and render logic to enable rendering entities using the given [`SpecializedMaterial`]
Expand Down Expand Up @@ -375,7 +395,8 @@ pub fn queue_material_meshes<M: SpecializedMaterial>(

// 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 {
Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct StandardMaterial {
pub cull_mode: Option<Face>,
pub unlit: bool,
pub alpha_mode: AlphaMode,
pub depth_bias: f32,
}

impl Default for StandardMaterial {
Expand Down Expand Up @@ -88,6 +89,7 @@ impl Default for StandardMaterial {
cull_mode: Some(Face::Back),
unlit: false,
alpha_mode: AlphaMode::Opaque,
depth_bias: 0.0,
}
}
}
Expand Down Expand Up @@ -163,6 +165,7 @@ pub struct GpuStandardMaterial {
pub flags: StandardMaterialFlags,
pub base_color_texture: Option<Handle<Image>>,
pub alpha_mode: AlphaMode,
pub depth_bias: f32,
pub cull_mode: Option<Face>,
}

Expand Down Expand Up @@ -331,6 +334,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,
cull_mode: material.cull_mode,
})
}
Expand Down Expand Up @@ -497,4 +501,9 @@ impl SpecializedMaterial for StandardMaterial {
fn alpha_mode(render_asset: &<Self as RenderAsset>::PreparedAsset) -> AlphaMode {
render_asset.alpha_mode
}

#[inline]
fn depth_bias(material: &<Self as RenderAsset>::PreparedAsset) -> f32 {
material.depth_bias
}
}