Avoiding Z-Fighting with the Depth Offset Factor

Avatar
  • updated
  • Answered

I have multiple opaque polylines that are Flat 2D. I'm not using immediate mode drawing, instead opting to use gameobjects. I'm setting ZOffsetFactor on the lines in code on creation, but there is still Z-Fighting on the lines.

Image 683

If I set the Depth Offset Factor in the inspector, then the lines are ordered as expected. Is there some other call I need to make after setting the ZOffsetFactor via code?

Reporting a bug? please specify Unity version:
2022.1.14f1
Reporting a bug? please specify Shapes version:
4.2.1
Reporting a bug? please specify Render Pipeline:
Built-in render pipeline
Avatar
Brian Agalsoff

Just to add, it seems to be fine for rectangles and discs. The problem seems to be isolated to the polyline as far as I can tell.

Avatar
Freya Holmér creator
  • Answered

I can't seem to reproduce this, it works fine on my end. I tried this on both discs and polylines, works as expected on both:

void Awake() => GetComponent<ShapeRenderer>().ZOffsetFactor = -1;
Avatar
Brian Agalsoff

I can't seem to reproduce it in a fresh project, so it must be something weird on my end. Thanks for trying to reproduce the problem!

Avatar
Brian Agalsoff

One quick follow up question:
Below, the blue line has an offset factor of -1, and the red line has a factor of -2. The red line sits on top as I'd expect.

Image 686

Now if I move the camera to look at these lines directly, the blue line suddenly appears on top. But at any other angle, the red appears on top. Am I misunderstanding how the ZOffsetFactor is supposed to work?

Image 687

Avatar
Freya Holmér creator

this is likely just Unity's sorting being based on camera angle, but it's possible to set Unity to sort based on position instead of relative camera depth. sorting is a pretty deep topic, I recommend reading through Shapes docs on sorting, as well as Unity's documentation on how it sorts transparent objects (component shapes are just mesh renderers)

Avatar
Brian Agalsoff

I'll do that then, thanks for your assistance.