• 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 /
avatar image
0
Question by Yoirgla · Jan 03, 2015 at 11:01 AM · meshfilter

Rebuild Triangle index of a mesh after deleting triangles ?

Hello everyone. i'm coming to you for help as i'm trying to mess with the meshes. I am trying to delete parts of my meshes at runtime and if possible do it several times in a row. My issue is this : Step 1 : i delete the triangles i'm willing to get rid of. Step 2 : i select another part of my mesh for deletion and i get an out of bound error on my triangle list. Step3 : after a quick debugging i noticed some of my triangles still have index of pre-deletion of the step one. which means trying to access to triangle[20] won't work because the array is now shorter.

question is : what can i do to rebuild the triangle array between each deletion of part of the mesh? here's my code for the braves :p (sorry still a beginner :p)

                         if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit) && Input.GetMouseButton (0)) {
                                 GameObject obj = hit.collider.gameObject;
                                 MeshFilter meshf = (MeshFilter)obj.GetComponent (typeof(MeshFilter));
                                 Mesh mesh2 = meshf.mesh;
                                 Vector3[] vertices = mesh2.vertices;
                                 List<int> triangles = new List<int> (mesh2.triangles);
                                 Vector3 tempnormal = mesh2.normals [hit.triangleIndex];
                                 List<int> indextoremove = new List<int> ();
                                 
                                 //find what's the vertice normal of the hit triangle
                                                             Vector3 targetnormal = mesh2.normals [triangles [hit.triangleIndex * 3]];
                                 //look trough all the normals the same normals as the one we're looking for and record the index in a list.
                                 List<int> verticestodelete = new List<int> ();
                                 for (int i=0; i<mesh2.normals.Length; i++) {
                                         if (mesh2.normals [i] == targetnormal) {
                                                 verticestodelete.Add (i);
                                         }
                                 }
                                 //cycle trough all the triangles to find those with found vertices and delete the ones we do not need
                                 List<int> trianglestodelete = new List<int> ();
                                 for (int i=0; i<mesh2.triangles.Length/3; i++) {
                                         int count = 0;
                                         foreach (int j in verticestodelete) {
                                                 if (mesh2.triangles [i * 3] == j || mesh2.triangles [i * 3 + 1] == j || mesh2.triangles [i * 3 + 2] == j)
                                                         count++;
 
                                         }
                                         if (count >= 3) {
                                                 trianglestodelete.Add (i);
                                         }
                                 }
                                 int deletioncount = 0;
                                 foreach (int i in trianglestodelete) {
                                         //removing triangles and compensating the list switch
                                         triangles.RemoveRange (i * 3 - 3 * deletioncount, 3);
                                         deletioncount++;
                                 }
 
 
                                 mesh2.triangles = triangles.ToArray ();
                                 triggered = true;
     
                         }
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

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How can I modify the mesh in OnPostprocessModel? 2 Answers

'mesh' is not a member of UnityEngine.Component - Explicit Typecasting for Android 1 Answer

How to add a Cube(mesh filter) and MeshRender in my Class? 1 Answer

Merging faces without effecting texture mapping 2 Answers

mesh.colors doesnt seem to work at the moment 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges