# Mesh rendered wierdley without depth buffer

I’m trying to achieve an optical illusion.
In order to do so I cut my mesh into two pieces. One of them should be rendered in front of the other even though it’s behind it. This is my setup:

that’s how it looks like in the end:

Now my question is: How to remove the wrongly rendered part at the left edge?
Btw: It happens not only there, but on the other site too.
Thank you.

Well, since you disabled the depth test, the triangles are rendered in the order they are specified in the Mesh. You problem is that the lower left part of the mesh is most likely more at the beginning of the mesh triangle list while that part that “looks through” is more to the end of your mesh triangle list.

If you disable the depthbuffer you actually could have used a single mesh. You just have to make sure you ordered the triangles the way you need them. If that extra part would be added to the first mesh at the end of the triangle list it would appear above the other.

• either use the depth buffer, render the first object, clear the depth buffer and then render the second mesh.
• If you don’t use the depth buffer you should reorder your triangles of your mesh so they are drawn in the right order.

edit
Here’s a script that you should simply add to your mesh and it should reorder the triangles from right to left.

``````// ReorderMeshTriangles.cs
using UnityEngine;
using System.Collections.Generic;

public class ReorderMeshTriangles : MonoBehaviour
{
private struct Triangle
{
public int I1;
public int I2;
public int I3;
public Vector3 center;
public Triangle(int aI1, int aI2, int aI3, Vector3[] verts)
{
I1 = aI1;
I2 = aI2;
I3 = aI3;
center = (verts[I1] + verts[I2] + verts[I3]) / 3;
}
}

void Start()
{
var mf = GetComponent<MeshFilter>();
var m = mf.mesh;
var verts = m.vertices;
var indices = m.triangles;
var tris = new List<Triangle>(indices.Length / 3);
for(int i = 0; i < indices.Length; i+=3)
{
tris.Add(new Triangle(indices*, indices[i + 1], indices[i + 2], verts));*
``````

}

tris.Sort((a,b)=>a.center.x.CompareTo(b.center.x));

for (int i = 0; i < tris.Count; i++)
{
indices[i * 3 ] = tris*.I1;*
indices[i * 3 + 1] = tris*.I2;*
indices[i * 3 + 2] = tris*.I3;*
}
m.triangles = indices;
}
}
When you run your game it should look right.
Note: I haven’t tested it so i’m not sure if the sorting direction is correct. If it’s not you just want to swap “a” and “b” inside the sorting function:
tris.Sort((a,b)=>b.center.x.CompareTo(a.center.x));
*/|\ /|*
| |
// here and here
Of course if “right” isn’t the x axis you have to change the “x” as well to whatever you need.