Dynamically change draw order of procedurally generated planar meshes.

Hi everyone,

I have a draw order / z-fighting problem that I just can’t find a solution for. Based on a xml file I procedurally generate planar meshes that all have the same z-value (areas from an osm file, if anyone is interested).
These planes as they are defined in the xml file can overlap or very often a smaller plane is completely enclosed by a larger one. Due to the same z-value I obviously get z-fighting.

I now want to dynamically change the draw order of the planar meshes via script, so that smaller planes overlapping bigger ones are drawn on top. I tried incrementing material.renderQueue for certain planes, which had no effect, however. Similarly I played around with the Offset in the shaders. Unfortunately you cannot dynamically set this value (see this answer).

With smaller planes drawn on top of bigger ones, which themselves my be overlapped by even smaller ones, this kind of creates a hierarchy. This and the fact that the number of planes isn’t previously known and can be very large, makes me think I cannot offset certain planes in the z-direction, however small the amount is, because the offsetting may accumulate to an amount that a gap could be visible for a perspective camera.

I also cannot use multiple cameras as I would have way to many cameras in the scene.

I’m looking for a solution that considers the following:

  • The z-values of the planar meshes have to stay the same due to the accumulation of z-offsets. Also I assume that if I move my perspective camera around I could have the planes moving relatively to each other i.e. moving different amounts as seen from the camera, due to the difference in z, even if no gap is visible?!
  • A change to a certain planar mesh may only change the object itself, not any other objects that have the same material for example, since the draw order is supposed to be based on the object’s relation to other objects in terms of size, not it’s material.
  • After the generation I export the meshes with this script to an .obj file. The draw order has to be persistent so that the imported .obj file looks exactly the same as the generated scene (another reason why a solution with multiple cameras would not work in my case).

This is what it looks like:
alt text

This is what I want it to look like:
alt text

I really hope there is a reasonably simple solution to that, because I’ve been looking for ages now. Any help is greatly appreciated!

So I worked around the problem wiht the help of the Offset property in custom shaders (see here).

As I mentioned in my quesion, the Offset shader property cannot be set dynamically via script. So the only solution I could come up with, was making several copies of the original shader I was using, each with a different Offset value combination (Offset -1, -1; Offset -2, -2; Offset -3, -3 etc.) and naming them accordingly (Illumin-Diffuse-Offset1, Illumin-Diffuse-Offset2, Illumin-Diffuse-Offset3 etc.). Then I assigned the appropriate shader to the materials at runtime.

In addition there was an issue with saving the meshes to an .obj file, because the information which material was assigned a new shader during runtime is lost. So I had to use a TextAsset to save this information. When the saved .obj file is imported I then read the text file and assign the shaders accordingly.