- Home /
How do I render triangles in a mesh in depth order?
I'm using Unity to create a 2D game and I'm having a problem with meshes. It seems that triangles in the same mesh are drawn in creation order instead of depth order.
I created a simple example:
public class MeshTest : MonoBehaviour
{
Mesh mesh;
void Start ()
{
mesh = GetComponent<MeshFilter> ().mesh;
BuildMesh ();
}
List<Vector3> newVertices = new List<Vector3>();
List<int> newTriangles = new List<int>();
List<Color> newColor = new List<Color>();
void BuildMesh()
{
Vector3 pos = Vector3.zero;
// Create three quads in the mesh
GenerateQuad (Vector3.zero, Color.red);
GenerateQuad (new Vector3(1f, 1f, 1f), Color.yellow);
GenerateQuad (new Vector3(0.5f, 0.5f, 0.5f), Color.green);
// The green square has a z coordinate in-between the other two squares
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.colors = newColor.ToArray ();
mesh.Optimize ();
mesh.RecalculateNormals ();
newVertices.Clear ();
newTriangles.Clear ();
newColor.Clear ();
}
void GenerateQuad(Vector3 topLeft, Color color)
{
int triangleIndex = newVertices.Count;
float x = topLeft.x;
float y = topLeft.y;
float z = topLeft.z;
newVertices.Add( new Vector3 (x, y, z) );
newVertices.Add( new Vector3 (x + 1f, y, z));
newVertices.Add( new Vector3 (x, y - 1f, z));
newVertices.Add( new Vector3 (x + 1f, y - 1f, z));
newTriangles.Add(triangleIndex+0);
newTriangles.Add(triangleIndex+1);
newTriangles.Add(triangleIndex+2);
newTriangles.Add(triangleIndex+1);
newTriangles.Add(triangleIndex+2);
newTriangles.Add(triangleIndex+3);
newColor.Add (color);
newColor.Add (color);
newColor.Add (color);
newColor.Add (color);
}
}
When I run a scene with just this game object in I see this:
The green square is on top of the other two when it should be in between them.
Any ideas what I'm doing wrong? Is this the normal behaviour?
Thanks
Answer by Eric5h5 · Nov 27, 2014 at 02:59 AM
You need to use a shader that writes to the depth buffer. (Also, unrelated, your bottom-right triangles in each quad have the winding order reversed.)
$$anonymous$$any thanks. I know very little about shaders. I solved the problem by adding this: ZWrite On
to my shader.
Thanks for pointing out the triangles, should it be:
newTriangles.Add(triangleIndex+0); newTriangles.Add(triangleIndex+1); newTriangles.Add(triangleIndex+2); newTriangles.Add(triangleIndex+2); newTriangles.Add(triangleIndex+1); newTriangles.Add(triangleIndex+3);
? (also what could go wrong by having it the wrong way around?)
Yes, 2, 1, 3 would work; also 1, 3, 2. The surface normal was pointing the opposite way otherwise, which can be seen with a shader that culls backfaces. If you never used the mesh for collisions and never culled backfaces, it wouldn't really matter, but I think it's best to be consistent just in case.
Your answer
Follow this Question
Related Questions
3d collider mesh to 2d collider mesh 0 Answers
Any way to access SpriteRenderer mesh UV2? 0 Answers
I want do my own mesh "quad", but not square, I want a 2D polygon to apply in a parallax scroll 1 Answer
Having trouble implementing edge collapse operation with half-edge data structure 0 Answers
UI Button order 1 Answer