Say I have about 100 boxes, each made up of six individual planes. Each plane is a separate GameObject, with 4 verts, naturally.
This boxes are all drawn up against each other in various ways. Therefore, many of the planes are smack dab up against another plane. Essentially, the two planes will be “z-fighting”.
Finally, since they are right up against each other, if one plane is smaller than the other, and inside it’s bounds, or if each are the exact same size and their verts in the same position, one/both (respectively) do not need to be drawn, as they will never be seen.
I’ll admit, I thought a little on how to calculate this for each plane, and just boggled, I couldn’t think of how to do it at all. Usually I can come up with at least some sort of ridiculously complex and silly solution- but nothing
The time taken to finish is not a major issue. It could take an hour, that’s fine. The important thing is hiding those planes, every durn one of 'em!
Can’t you store all of your game objects in an array, and then just compare their transform.position? seems to me that if they have the same transform.position, then they must be z-fighting
Well, let’s look at the data… A plane is a flat rectangle with negligible thickness and an adjustable width and height. The ones in Unity are 1x1 by default, however, so you can just go with that.
Step #1: Given that I know the direction I’m facing and I know my forward vector, is any other plane within one unit of me to my .right or .up? (You can use Vector3.Dot to determine this)
Step #2: Which direction am I facing? If I’m facing the same direction as the close plane or an inverse (rotated 180) direction, I might be z-fighting. Even if I’m not, the above check will let me know I’m intersecting at least.
Step #3: If both of the above indicate z-fighting, plane.renderer.enabled = false; for both planes.
A rough description of a solution, definitely not the most optimized, but it may work for you. You’ll need to do an N! number of comparisons, so it won’t be cheap, but you’ll likely only need to do it once.
Note: In order to adjust for special shapes, you’ll likely end up having to do a more complex check than the “1-away” check.
Thanks Gargerath, I had been thinking something a little along these lines myself- to just brute-force it and walk through each and every plane and it’s verts checking various things…always hoping there might be a slightly faster method though! Since the verts can be moved however folks want to…and the plane’s actual transform could be anywhere…the calculations get ugly
Just out of curiosity: if sides of two box are right against each other, those sides will not actually be visible? Am I missing something? Do you have a screenshot of the problem?
The other thing to consider is to just separate the two boxes ever so slightly so as to remove any potential Z- fighting. It would cheaper to render a box object than 6 separate planes I’d think. Unless you need the planes for another purpose.