Z-Fighting Depth Conflicts: Causes and Solutions
What is Z-Fighting
Z-Fighting (depth conflict) refers to the phenomenon where two or more planes in space are very close to each other, and due to the limited precision of the depth buffer (Z-Buffer), their relative distances cannot be accurately distinguished, causing flickering or texture shimmering when rendering alternately displays different surfaces. It commonly occurs with coplanar geometry (such as decals, overlapping planes).
Cause Analysis
Depth buffers typically store depth values using a non-linear mapping, with higher precision near the near plane and lower precision far away. When the distance between two surfaces is smaller than the minimum distinguishable interval of the depth buffer, the GPU may incorrectly judge which surface is in front, causing the depth test to randomly pass and produce flickering. Additionally, the bit depth of the depth buffer (usually 24-bit or 32-bit) and the ratio of near to far planes also affect precision.
Solutions
Adjusting Geometry
- **Increase Distance**: Separate overlapping surfaces, or slightly offset positions (e.g., offset along normals).
- **Avoid Coplanarity**: Ensure objects do not share the same plane, or use different heights.
Adjusting Depth Buffer Settings
- **Reduce Far/Near Plane Ratio**: Set the far plane closer, or set the near plane farther, to improve overall depth precision.
- **Use Higher Precision Depth Buffer**: Such as 32-bit floating-point depth or Reverse-Z technique. Reverse-Z inverts the depth mapping, giving higher precision at far distances, and is commonly used in modern rendering.
Using Rendering Techniques
- **Depth Bias**: Apply a depth offset to polygons during rendering to make them slightly closer to the camera, typically used for decals, shadows, etc. Set via `Offset` in shaders or API.
- **Polygon Offset**: Similar to depth bias, but adjusted based on slope.
- **Separate Render Queues**: Adjust rendering order to ensure far surfaces are rendered first.
Engine-Specific Methods
- **Unity**: Set Render Queue in the material or use Offset parameters in shaders.
- **Unreal**: Set Depth Bias in the material or use World Position Offset.
Frequently Asked Questions
What is the relationship between Z-Fighting and shadow acne?
Shadow acne is a similar issue in shadow mapping, caused by insufficient depth comparison precision leading to self-shadowing, usually solved with depth bias.
Why is Z-Fighting more likely to occur at far distances?
Because depth buffer precision is non-linear, and far distances have lower precision, two surfaces may be indistinguishable even with a relatively large distance.
How to avoid Z-Fighting between decals and floors?
Use depth bias or adjust render queues so that decals are slightly closer to the camera.
How to deal with Z-Fighting on mobile devices?
Mobile depth buffers are usually 16-bit or 24-bit, with lower precision, so more attention to geometry distances and depth ranges is needed.
What are the benefits of Reverse-Z?
Reverse-Z inverts the floating-point depth mapping, concentrating precision at far distances, suitable for large scenes and reducing far depth conflicts.
FAQ
What is the relationship between Z-Fighting and shadow acne?
Shadow acne is a similar issue in shadow mapping, caused by insufficient depth comparison precision leading to self-shadowing, usually solved with depth bias.
Why is Z-Fighting more likely to occur at far distances?
Because depth buffer precision is non-linear, and far distances have lower precision, two surfaces may be indistinguishable even with a relatively large distance.
How to avoid Z-Fighting between decals and floors?
Use depth bias or adjust render queues so that decals are slightly closer to the camera.
How to deal with Z-Fighting on mobile devices?
Mobile depth buffers are usually 16-bit or 24-bit, with lower precision, so more attention to geometry distances and depth ranges is needed.
What are the benefits of Reverse-Z?
Reverse-Z inverts the floating-point depth mapping, concentrating precision at far distances, suitable for large scenes and reducing far depth conflicts.