- Home /
Z Fighting Help - How to get bias for ZTest only, not ZWrite
Hi there,
I discovered that using Unity's Z Bias feature called "Offset" in shaderlab applies to both the ZTest and the ZWrite. Is there a way to apply the bias to one or the other only?
I figured that having a depth bias would help with Z fighting of polygons that occupy nearly the same space. The idea was that in the ZTest phase the pixel would only be drawn if it's depth, plus the bias, was less than what was already there. In which case, it'd draw the pixel and write to the ZBuffer as normal. The expectation was that it'd write the original Z value, without the bias, to the Z buffer:
if ZCurrent + Bias < ZInDepthBuffer
Draw the Pixel.
ZInDepthBuffer = ZCurrent
However, unfortunately, when it wrote to the ZBuffer, it wrote the Z value with the bias added to it:
if ZCurrent + Bias < ZInDepthBuffer
Draw the pixel.
ZInDepthBuffer = ZCurrent + Bias // <-- Problem!!!
Which basically means that the Offset feature is useless in combating Z fighting.
The only alternative is to have two passes, with the first pass writing to the Z buffer only, without any bias. The second pass would not write to the Z buffer, but would perform Z Testing with a bias, drawing the pixel normally if it passed the test.
Of course, the problem here is that this requires the same thing to be drawn twice - that is, twice the geometry is rendered. This is not acceptable in our case due to performance issues.
It also seems extremely ridiculous that we'd need another draw call as a work around for something as simple as how two numbers are added.
In conclusion, is there some magic cheat code keyword or syntax or whatever hack I need to use so that it does not ZWrite with a bias, and only uses the bias for performing the ZTest? If you can find it, you will be the honorary most awesome person of the century!
Thanks,
-Steve