Godot Version
4.3
Question
Hey all, I am working on a 3D project and I am having a lot of issues with Z fighting. I would love to get your input on best ways to fix these issues.
My project is essentially implementing catlikecoding’s Unity hex grid tutorials in Godot. His original hex grid tutorials can be found at the following link: Unity Hex Map Tutorials
Overall it’s going really well, and I am enjoying learning Godot as I do this project.
But the z fighting… it is a constant issue…
Here is what I am currently experiencing:
I have a hex grid that I procedurally generate. For the purposes of this post, I have made my hex grid completely flat - so there are no variations in elevation at all. It is a single mesh. This screenshot visualizes what the hex grid mesh looks like:
Each hex in the mesh is about 10 meters in diameter. My camera is currently hovering about 150 meters above the hex grid mesh, and is pointed directly at the mesh. I am using the “default” values for the FOV, Near, and Far parameters for the camera, which are respectively: 75 degrees, 0.05 meters, and 4000 meters.
Now, I am currently implementing some functionality that allows the user to “select” a hex. When the user selects a hex, that hex will get “highlighted”. Basically, this just means I will draw a dark hexagonal outline around the hex.
For the purposes of this post, I have added that dark hexagonal outline to every hex on the hex grid. While the hex grid itself is a single mesh, I also have another object that represents each hex. This other object is placed at the appropriate position for each hex and contains objects such as a position label and the aforementioned hex outline. I have also “raised” each of these hex objects above the hex grid mesh by about 0.1 meters to try and get them to show up.
This is what shows up:
So, there is some obvious “z fighting” happening here, since I am not able to see a dark outline around each hex.
So I decided to incrementally raise the height of each of my hex objects above the hex grid mesh until ALL of the hex outlines were visible. So eventually I got this:
I had to raise each of the objects to a height of about 24 meters until each of the hex outlines was completely visible rather than the hex mesh hiding it.
Now, of course I don’t want my hexes looking like this, so I then decided to try setting the “sorting offset” of the hex outline. First, I set the “sorting offset” to a value of 30. My assumption was that would be enough, since previously each hex outline had been fully visible when I had raised them to a height of 24 meters above the hex grid mesh. But alas, a sorting offset of 30 was not enough. While most of them are visible, you can see that some still are not:
So then I tried incrementally increasing the “sorting offset” of the hex outline. Finally, at a sorting offset of around 100, I am able to reliably see every hex outline without any of them awkwardly disappearing at times:
Anyway, this (and other z-fighting issues that I have been experiencing) has been really frustrating.
Should I just leave the sorting offset at 100? That seems like a rather high number.
Is there a deterministic and reliable way to make sure that these hex outlines actually appear on top of each hex rather than hiding behind the hex mesh?