• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ZeraTFK · Nov 14, 2015 at 10:03 PM · raycastverticesdeletetriangles

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.

  1. 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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Procedural Mesh Problems 0 Answers

Getting face of mesh when you already have one tri 0 Answers

How can I identify the location of mesh triangles at run time? (Procedural Generation) 0 Answers

Object Conformity 0 Answers

Triangles offset from vertices 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges