Raycast returns Bad Triangle Index after delete triangles in mesh
Hi,
I generate a mesh with many triangles. I want to delete 2 triangles if i click them. It works the first time, but if i try to hit the next triangle.. the raycast triangleindex give me some wrong values.
i select a triangle, detect the triangle to get the quad and i remove it. http://www.terabyte-unlimited.com/help1.png
I remove the triangles from the Mesh an safe ist
public static void RemoveFace(GameObject ga, int tIndex)
{
Mesh tmpMesh = ga.GetComponent<MeshFilter>().mesh;
MeshCollider tmpCol = ga.GetComponent<MeshCollider>();
Vector3[] vertices = tmpMesh.vertices;
Vector2[] uv = tmpMesh.uv;
int[] triangles = tmpMesh.triangles;
Vector3[] nVertices = new Vector3[vertices.Length - 4];
Vector2[] nUV = new Vector2[nVertices.Length];
int[] nTriangles = new int[triangles.Length - 6];
int iC = 0;
int[] TriIndex = new int[6];
/*
TriIndex[0] = triangles[tIndex * 3 + 0];
TriIndex[1] = triangles[tIndex * 3 + 1];
TriIndex[2] = triangles[tIndex * 3 + 2];
if ((float)tIndex / 2.0f - Mathf.Floor((float)tIndex / 2.0f) > 0.0f)
{
TriIndex[3] = triangles[(tIndex - 1) * 3 + 0];
TriIndex[4] = triangles[(tIndex - 1) * 3 + 1];
TriIndex[5] = triangles[(tIndex - 1) * 3 + 2];
}
else
{
TriIndex[3] = triangles[(tIndex + 1) * 3 + 0];
TriIndex[4] = triangles[(tIndex + 1) * 3 + 1];
TriIndex[5] = triangles[(tIndex + 1) * 3 + 2];
}
for (int i = 0; i < vertices.Length; i++)
{
if (!TriIndex.Contains<int>(i))
{
nVertices[iC] = vertices[i];
nUV[iC] = uv[i];
iC++;
}
}
* */
TriIndex[0] = tIndex * 3 + 0;
TriIndex[1] = tIndex * 3 + 1;
TriIndex[2] = tIndex * 3 + 2;
if ((float)tIndex / 2.0f - Mathf.Floor((float)tIndex / 2.0f) > 0.0f)
{
TriIndex[3] = (tIndex - 1) * 3 + 0;
TriIndex[4] = (tIndex - 1) * 3 + 1;
TriIndex[5] = (tIndex - 1) * 3 + 2;
}
else
{
TriIndex[3] = (tIndex + 1) * 3 + 0;
TriIndex[4] = (tIndex + 1) * 3 + 1;
TriIndex[5] = (tIndex + 1) * 3 + 2;
}
iC = 0;
for (int i = 0; i < triangles.Length; i++)
{
if (!TriIndex.Contains<int>(i))
{
nTriangles[iC] = triangles[i];
iC++;
}
}
tmpMesh.Clear();
tmpMesh.vertices = vertices;
tmpMesh.triangles = nTriangles;
tmpMesh.uv = uv;
tmpMesh.Optimize();
tmpMesh.RecalculateNormals();
tmpCol.sharedMesh = tmpMesh;
}
The two triangles are vanish.. all fine.. http://www.terabyte-unlimited.com/help2.png
If i try to raycast the next triangle, then i get some wrong triangleindex From the cast. http://www.terabyte-unlimited.com/help3.png
What can i do to get the right Triangle? Something wrong with the mesh? Or with my Raycast?
Answer by ZeraTFK · Nov 15, 2015 at 12:33 AM
Well i got it! I will share my Function how remove a Quad (two triangles) with his vertices and uv and triangles. Its cleaner than to remove only the triangles!
public static void RemoveFace(GameObject ga, int tIndex)
{
Mesh tmpMesh = ga.GetComponent<MeshFilter>().mesh;
MeshCollider tmpCol = ga.GetComponent<MeshCollider>();
Vector3[] vertices = tmpMesh.vertices;
Vector2[] uv = tmpMesh.uv;
int[] triangles = tmpMesh.triangles;
Vector3[] nVertices = new Vector3[vertices.Length - 4];
Vector2[] nUV = new Vector2[nVertices.Length];
int[] nTriangles = new int[triangles.Length - 6];
int[] sIndex = new int[4];
sIndex[0] = triangles[tIndex * 3 + 0];
sIndex[1] = triangles[tIndex * 3 + 1];
sIndex[2] = triangles[tIndex * 3 + 2];
if ((float)tIndex / 2.0f - Mathf.Floor((float)tIndex / 2.0f) > 0.0f)
sIndex[3] =triangles[(tIndex - 1) * 3 + 0];
else
sIndex[3] =triangles[(tIndex + 1) * 3 + 1];
int iC = 0;
for (int i = 0; i < vertices.Length; i++)
{
if (i != sIndex[0] && i != sIndex[1] && i != sIndex[2] && i != sIndex[3])
{
nVertices[iC] = vertices[i];
nUV[iC] = uv[i];
iC++;
}
}
sIndex = new int[6];
sIndex[0] = tIndex * 3 + 0;
sIndex[1] = tIndex * 3 + 1;
sIndex[2] = tIndex * 3 + 2;
int heighestIndex = tIndex * 3 + 2;
if ((float)tIndex / 2.0f - Mathf.Floor((float)tIndex / 2.0f) > 0.0f)
{
sIndex[3] = (tIndex - 1) * 3 + 0;
sIndex[4] = (tIndex - 1) * 3 + 1;
sIndex[5] = (tIndex - 1) * 3 + 2;
}
else
{
sIndex[3] = (tIndex + 1) * 3 + 0;
sIndex[4] = (tIndex + 1) * 3 + 1;
sIndex[5] = (tIndex + 1) * 3 + 2;
heighestIndex=(tIndex + 1) * 3 + 2;
}
iC = 0;
for (int i = 0; i < triangles.Length; i++)
{
if (i != sIndex[0] && i != sIndex[1] && i != sIndex[2] && i != sIndex[3] && i != sIndex[4] && i != sIndex[5])
{
if (i > heighestIndex)
{
//vertices korrigieren
triangles[i] -= 4;
}
nTriangles[iC] = triangles[i];
iC++;
}
}
tmpMesh.Clear();
tmpMesh.vertices = nVertices;
tmpMesh.triangles = nTriangles;
tmpMesh.uv = nUV;
tmpMesh.Optimize();
tmpMesh.RecalculateNormals();
tmpCol.sharedMesh = null;
tmpCol.sharedMesh = tmpMesh;
}
Your answer
Follow this Question
Related Questions
Procedural Mesh Problems 0 Answers
Creating triangles around a mesh using an array of vertices 1 Answer
Mesh generation triangles issue 0 Answers
delete vertex on spheres 0 Answers
Getting face of mesh when you already have one tri 0 Answers