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 standard material depth bias to pipeline #7847

Closed
wants to merge 2 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
4 changes: 3 additions & 1 deletion crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ pub trait Material: AsBindGroup + Send + Sync + Clone + TypeUuid + Sized + 'stat

#[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.
/// for meshes with similar depth, to avoid z-fighting.
/// The bias is in depth-texture units so large values may be needed to overcome small depth differences.
fn depth_bias(&self) -> f32 {
0.0
}
Expand Down Expand Up @@ -507,6 +508,7 @@ pub struct MaterialProperties {
pub alpha_mode: AlphaMode,
/// 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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// for meshes with equal depth, to avoid z-fighting.
/// for meshes with similar depth, to avoid z-fighting.

nit: change this line to match the change in documentation for fn depth_bias(&self)

/// The bias is in depth-texture units so large values may be needed to overcome small depth differences.
pub depth_bias: f32,
}

Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,14 @@ pub struct StandardMaterial {
/// See [`AlphaMode`] for details. Defaults to [`AlphaMode::Opaque`].
pub alpha_mode: AlphaMode,

/// Re-arrange render ordering.
/// Adjust rendered depth.
///
/// A material with a positive depth bias will render closer to the
/// camera while negative values cause the material to render behind
/// other objects. This is independent of the viewport.
///
/// `depth_bias` only affects render ordering. This means that for opaque materials,
/// `depth_bias` will only have any effect if two materials are overlapping,
/// which only serves as a [z-fighting] resolver.
///
/// `depth_bias` can however reorder [`AlphaMode::Blend`] materials.
/// This is useful if your transparent materials are not rendering
/// in the expected order.
/// `depth_bias` affects render ordering and depth write operations
/// using the `wgpu::DepthBiasState::Constant` field.
///
/// [z-fighting]: https://en.wikipedia.org/wiki/Z-fighting
pub depth_bias: f32,
Expand Down Expand Up @@ -420,13 +415,15 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
pub struct StandardMaterialKey {
normal_map: bool,
cull_mode: Option<Face>,
depth_bias: i32,
}

impl From<&StandardMaterial> for StandardMaterialKey {
fn from(material: &StandardMaterial) -> Self {
StandardMaterialKey {
normal_map: material.normal_map_texture.is_some(),
cull_mode: material.cull_mode,
depth_bias: material.depth_bias as i32,
}
}
}
Expand All @@ -449,6 +446,9 @@ impl Material for StandardMaterial {
if let Some(label) = &mut descriptor.label {
*label = format!("pbr_{}", *label).into();
}
if let Some(depth_stencil) = descriptor.depth_stencil.as_mut() {
depth_stencil.bias.constant = key.bind_group_data.depth_bias;
}
Ok(())
}

Expand Down