Changing draw order of meshes in Graphics.DrawMesh()

Hi! I’m working on a game where we need to be able to draw things with 3D lines, and I’ve come across a road block. I’m using the Graphics.DrawMesh() function to draw each line to the screen (each line is its own procedurally generated mesh). However, I can’t figure out what the best way is to modify the order in which they draw.

Here’s the Draw method for a drawing:

public void Draw()
{
    for (int i = 0; i < _lineMeshes.Count; i++)
    {
        Graphics.DrawMesh(_lineMeshes<em>, transform.localToWorldMatrix, _lineMaterials*, 0);*</em>

}
}
My first instinct was to reverse the order of the For loop, but that didn’t change anything. Changing the Z position of the meshes didn’t change anything, either. The issue is, every mesh that is created later is drawn later to the screen. Here’s an image showing the problem:
[24373-lines.png|24373]_
_
“1” was the first mesh created, “2” second, and so on. I’d like this order to be reversed._
The shader on the lines is as follows (not sure if it helps, but maybe it’s part of the problem):
Shader “Custom/LineShader”
_
{_
Properties
_
{_
Color (“Main Color”, Color) = (0,0,0,1)
_
}*

SubShader {

Pass
* {*
* Blend SrcAlpha OneMinusSrcAlpha*
ZWrite Off
* Color [Color]
Lighting Off
Cull Off
_
}*

Pass
* {*
* Blend SrcAlpha OneMinusSrcAlpha*
ZWrite Off
* Color [Color]
Lighting Off
Cull Off
_
}*

}

FallBack “VertexLit”
}
Any help is appreciated! Thanks in advance
_*

The shader helps. You turned ZWrite off in the shader. If you leave z write on and change the Z value of the mesh (assuming you are looking down the Z axis), that will allow the Z value to control the order. I’m guessing DrawMesh() probably just queues the meshes up into a buffer and is not guaranteed to keep the order that you call DrawMesh in, which is unfortunate.

EDIT: Hmmm…I should clarify a couple of things. Z Write has Zero to do with the Z axis (can I call you Zorro just for fun?). Z Write is the Z Depth in the final view-projection matrix, i.e. after after everything has been transformed into the screen space, it’s the Z Depth into the screen. So you could draw your numbers along the Y axis, along the X axis, and then the Y and X values would control the depth. Just wanted to clarify.

Another thought came to me. You could use the Sorting Layers and Sorting Order to order the draw calls. You can assume the default sorting layer and would only need to set the sorting order for each mesh, but I don’t know the API for that off hand, and it’s not listed in the Unity reference.

The other thing I thought of is that you may want the numbers to draw on top of everything else in the world, hence is why you set the Z write off. In that case, if you can’t control the order in which the numbers are drawn using sorting layers, you must leave ZWrite on, and you need to render the numbers on a separate layer and with a separate camera, similar to how we would do UI. The second camera would clear the depth buffer then draw the meshes in that layer. There are several unity answers on how to render UI to a second camera if you’re interested.