add standard material depth bias to pipeline (#7847)
# Objective the current depth bias only adjusts ordering, so it doesn't work for opaque meshes vs alpha-blend meshes, and it doesn't help when two meshes are infinitesimally offset from one another. ## Solution pass the material's depth bias into the pipeline depth stencil `constant` field.
This commit is contained in:
parent
465fff2b01
commit
6beca13553
@ -126,7 +126,8 @@ pub trait Material: AsBindGroup + Send + Sync + Clone + TypeUuid + Sized + 'stat
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
/// Add a bias to the view depth of the mesh which can be used to force a specific render order
|
/// 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 {
|
fn depth_bias(&self) -> f32 {
|
||||||
0.0
|
0.0
|
||||||
}
|
}
|
||||||
@ -514,6 +515,7 @@ pub struct MaterialProperties {
|
|||||||
pub alpha_mode: AlphaMode,
|
pub alpha_mode: AlphaMode,
|
||||||
/// Add a bias to the view depth of the mesh which can be used to force a specific render order
|
/// 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 equal depth, to avoid z-fighting.
|
||||||
|
/// The bias is in depth-texture units so large values may be needed to overcome small depth differences.
|
||||||
pub depth_bias: f32,
|
pub depth_bias: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,19 +220,14 @@ pub struct StandardMaterial {
|
|||||||
/// See [`AlphaMode`] for details. Defaults to [`AlphaMode::Opaque`].
|
/// See [`AlphaMode`] for details. Defaults to [`AlphaMode::Opaque`].
|
||||||
pub alpha_mode: AlphaMode,
|
pub alpha_mode: AlphaMode,
|
||||||
|
|
||||||
/// Re-arrange render ordering.
|
/// Adjust rendered depth.
|
||||||
///
|
///
|
||||||
/// A material with a positive depth bias will render closer to the
|
/// A material with a positive depth bias will render closer to the
|
||||||
/// camera while negative values cause the material to render behind
|
/// camera while negative values cause the material to render behind
|
||||||
/// other objects. This is independent of the viewport.
|
/// other objects. This is independent of the viewport.
|
||||||
///
|
///
|
||||||
/// `depth_bias` only affects render ordering. This means that for opaque materials,
|
/// `depth_bias` affects render ordering and depth write operations
|
||||||
/// `depth_bias` will only have any effect if two materials are overlapping,
|
/// using the `wgpu::DepthBiasState::Constant` field.
|
||||||
/// 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.
|
|
||||||
///
|
///
|
||||||
/// [z-fighting]: https://en.wikipedia.org/wiki/Z-fighting
|
/// [z-fighting]: https://en.wikipedia.org/wiki/Z-fighting
|
||||||
pub depth_bias: f32,
|
pub depth_bias: f32,
|
||||||
@ -420,6 +415,7 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
|
|||||||
pub struct StandardMaterialKey {
|
pub struct StandardMaterialKey {
|
||||||
normal_map: bool,
|
normal_map: bool,
|
||||||
cull_mode: Option<Face>,
|
cull_mode: Option<Face>,
|
||||||
|
depth_bias: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&StandardMaterial> for StandardMaterialKey {
|
impl From<&StandardMaterial> for StandardMaterialKey {
|
||||||
@ -427,6 +423,7 @@ impl From<&StandardMaterial> for StandardMaterialKey {
|
|||||||
StandardMaterialKey {
|
StandardMaterialKey {
|
||||||
normal_map: material.normal_map_texture.is_some(),
|
normal_map: material.normal_map_texture.is_some(),
|
||||||
cull_mode: material.cull_mode,
|
cull_mode: material.cull_mode,
|
||||||
|
depth_bias: material.depth_bias as i32,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -449,6 +446,9 @@ impl Material for StandardMaterial {
|
|||||||
if let Some(label) = &mut descriptor.label {
|
if let Some(label) = &mut descriptor.label {
|
||||||
*label = format!("pbr_{}", *label).into();
|
*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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user