Z-fighting

On this page 7

Z-fighting is a rendering artifact in which two or more nearly coplanar surfaces compete for the same pixels, so the visible surface flickers or forms striped, noisy patterns as the camera moves. In Real-time 3D graphics it arises because the depth buffer stores depth with limited precision, and when two fragments land in the same quantized depth value, the depth test cannot decide reliably which one is in front.

The artifact is common in games, WebGL scenes, architectural visualization and virtual production, wherever large scenes, decals or overlapping geometry meet a fixed-size depth buffer. It is rarely a bug in the GPU; it is almost always the result of scene construction or camera setup, which is why the fixes live in modeling practice and projection settings rather than in hardware.

Why the depth test fails on coplanar surfaces

Z-fighting occurs when the depth test compares two values that should differ but round to the same, or nearly the same, stored number. During rasterization, every fragment receives an interpolated depth, and the Z-buffer keeps the closest one. If two triangles lie in the same plane or within a tiny distance of each other, rounding errors in interpolation make the winner change from pixel to pixel and from frame to frame.

The pattern is not random noise. Each triangle is interpolated with slightly different edge equations, so the error forms bands, stripes or moiré patterns that crawl across the surface when the view changes. The effect is strongest at grazing angles and far from the camera, where many world units collapse into one depth step.

Three ingredients are therefore always present: overlapping geometry, finite depth precision and a projection that distributes that precision unevenly. Removing any one of them removes the artifact.

How projection and buffer format distribute depth precision

A standard perspective projection stores a value proportional to 1/z rather than z, which concentrates precision near the near clipping plane and leaves very little for distant objects. The ratio between the far and near planes matters far more than the absolute distances: moving the near plane from 0.01 to 0.1 units improves distant precision roughly tenfold, while moving the far plane outward has a comparatively small effect.

The following table summarizes the depth formats in common use and how they behave.

FormatStoragePrecision behaviorTypical use
16-bit integerFixed pointVery coarse at distanceMobile, legacy, shadow maps
24-bit integerFixed point, often with 8-bit stencilAdequate for modest far/near ratiosDefault in many WebGL contexts
32-bit floatIEEE 754 single precisionPoor with standard mappingDesktop APIs, compute passes
32-bit float, reversed ZFloat with near mapped to 1Nearly uniform across the rangeModern engines with large scenes
Common depth buffer formats and their precision

Reversed Z maps the near plane to 1.0 and the far plane to 0.0. Because floating-point numbers are densest near zero, this mapping cancels much of the hyperbolic distribution and gives close to uniform relative precision. Combined with an infinite far plane, it is the most effective single change an engine can make. Another option, the logarithmic depth buffer, writes a log-scaled depth from the fragment shader, at the cost of disabling early depth testing on many GPUs.

Typical causes in production scenes

Most Z-fighting cases trace back to a small number of recurring modeling and setup errors. The table below links each symptom to its usual cause and a reliable fix.

SymptomLikely causeRecommended fix
Flicker where a decal meets a wallDecal mesh placed exactly on the surfaceUse polygon offset or a deferred decal
Stripes on distant terrainNear plane set too close to the cameraRaise the near plane or use reversed Z
Two floor textures alternateDuplicated faces from a merge or importDelete overlapping faces in the model
Shimmer on coplanar roads and groundRoad meshes sharing the terrain heightOffset geometry or render in a fixed order
Noise only on a large world mapSingle-precision positions far from originUse camera-relative rendering
Symptoms, causes and fixes

Imported assets are a frequent source. Duplicated faces from boolean operations, CAD exports or merged layers stay invisible in a modeling application with a different renderer and only appear once the asset reaches the engine. A clean-up pass that removes coincident geometry prevents a whole class of reports.

Techniques for preventing Z-fighting

The right technique depends on whether the overlap is intentional, as with decals and outlines, or accidental, as with duplicated faces. Intentional overlaps need a controlled bias; accidental ones should be removed at the source.

TechniqueHow it worksMain trade-off
Polygon offsetAdds a slope-scaled bias to fragment depthToo much bias makes surfaces float
Tighter near and far planesReduces the far/near ratioNear objects may be clipped
Reversed Z bufferSpreads float precision evenlyRequires API and projection changes
Logarithmic depthWrites log-scaled depth per fragmentCan disable early depth rejection
Geometric separationMoves one surface a small distance awayGap may show at close range
Stencil or draw orderResolves layers without comparing depthAdds passes and sorting logic
Prevention techniques compared

In WebGL and OpenGL, polygon offset is set with a factor and a units value; in Direct3D and Vulkan the equivalent is depth bias in the rasterizer state. A good starting point is a small negative factor for the overlay, tuned visually, because the effective bias depends on the depth format and surface slope. Frameworks such as Three.js and Babylon.js expose both polygon offset and logarithmic depth as material or engine options.

For very large worlds, depth precision is only part of the problem. Vertex positions stored in 32-bit floats lose sub-centimeter accuracy thousands of kilometers from the origin, so engines use floating origin or camera-relative transforms in the vertex shader to keep values small.

Z-fighting is often confused with other depth-related problems that look similar but have different causes and fixes. Shadow acne in shadow mapping is a self-shadowing version of the same precision issue, solved with a shadow bias; peter-panning is the opposite failure, caused by too much bias.

  • A higher bit depth alone does not cure it: a 32-bit float buffer with standard mapping can perform worse than a 24-bit integer buffer.
  • Pushing the far plane is rarely the culprit: the near plane controls most of the precision loss.
  • Backface culling does not help when both surfaces face the camera, which is the usual case.
  • Exactly coplanar faces fight even with perfect precision, because their depths are mathematically equal and the result depends on rounding.

A plain-language summary

A 3D program decides which object is in front by measuring each object’s distance from the camera. It stores those distances with a limited number of steps, like a ruler with marks only every few centimeters. When two surfaces sit almost exactly on top of each other, both fall on the same mark, and the computer keeps changing its mind about which one to show.

The result is a flickering, striped surface that seems to fight with itself. The cure is usually simple: do not stack surfaces in the same place, or give the computer a finer ruler by adjusting the camera settings.

An example from practice

A team building a browser-based city model in WebGL places road meshes directly on top of a terrain mesh at the same height. The camera uses a near plane of 0.01 meters and a far plane of 20,000 meters, a ratio of two million. From street level the roads look fine, but in a bird’s-eye view at 2 kilometers the asphalt and grass flicker in diagonal stripes.

The developers raise the near plane to 0.5 meters, switch the renderer to a logarithmic depth buffer for the overview mode, and lift the road meshes by 3 centimeters with a small polygon offset on the road material. The flicker disappears at every zoom level, and the frame rate stays unchanged because the scene already uses a depth prepass only for buildings. The outcome is a stable map at all distances with no visible gap between road and ground.