Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 slythethieve · Sep 10, 2019 at 09:13 PM · scripting problemarrayoptimizationtrianglesvertice manipulation

Deleting square in Mesh really slow. Need help optimizing code.

Hi everybody, After following the great tutorial of holistic3d on how to delete a square on a mesh at runtime, I decided to add this functionality to my mesh. At the moment I don't really need this, what I need is an highlight of the square, but whatever. While her script works just fine on small meshes like planes or cubes, on mine it takes about 2 seconds to delete a square. I think it has to do either on number of vertices (around 700k) or on how the code was written with multiple array assignments and modifications. This is the code, can anybody help me in coming up with a more optimized solution that will run faster? Maybe parallel for loops? Also could you guys help me out in modifying this, so the square I hit instead of being deleted, I can put a texture on it. Thank you.

 public class cutHole : MonoBehaviour {
 
 
     private int triangleStripDown;
     private int triangleStripUp;
 
     private int[] triangles;
     private Vector3[] vertices;
     private int[] oldTriangles;
     private int[] newTriangles;
     private Mesh mesh;
 
     // Use this for initialization
     void Start () {
         mesh = transform.GetComponent<MeshFilter>().mesh;
         triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
         vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
         oldTriangles = triangles;
         newTriangles = new int[triangles.Length - 6];
 
     }
 
     void highlightSquare (int index1, int index2)
     {
 
     }
     
 
     void deleteSquare(int index1, int index2)
     {
         Destroy(this.gameObject.GetComponent<MeshCollider>());
         //Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
         //int[] oldTriangles = triangles;
         //int[] newTriangles = new int[triangles.Length-6];
 
         int i = 0;
         int j = 0;
         while (j < triangles.Length)
         {
             if(j != index1*3 && j != index2*3)
             {
                 newTriangles[i++] = oldTriangles[j++];
                 newTriangles[i++] = oldTriangles[j++];
                 newTriangles[i++] = oldTriangles[j++];
             }
             else
             {
                 
                 j += 3;
             }
         }
         triangles = newTriangles;
         oldTriangles = triangles;
         transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
         this.gameObject.AddComponent<MeshCollider>();
 
     }
 
     int findVertex(Vector3 v)
     {
         //vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
         for(int i = 0; i < vertices.Length; i++)
         {
             if(vertices[i] == v)
                 return i;
         }
         Debug.Log("Not found");
         return -1;
     }
 
     int findTriangle(Vector3 v1, Vector3 v2, int notTriIndex)
     {
         //triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
         //vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
         int i = 0;
         int j = 0;
         int found = 0;
         while (j < triangles.Length)
         {
             if(j/3 != notTriIndex)
             {
                 if(vertices[triangles[j]] == v1 && (vertices[triangles[j+1]] == v2 || vertices[triangles[j+2]] == v2))
                     return j/3;
                 else if(vertices[triangles[j]] == v2 && (vertices[triangles[j+1]] == v1 || vertices[triangles[j+2]] == v1))
                     return j/3;
                 else if(vertices[triangles[j+1]] == v2 && (vertices[triangles[j]] == v1 || vertices[triangles[j+2]] == v1))
                     return j/3;
                 else if(vertices[triangles[j+1]] == v1 && (vertices[triangles[j]] == v2 || vertices[triangles[j+2]] == v2))
                     return j/3;
             }
 
             j+=3;
         }
 
         return -1;
     }
 
     void deleteTri(int index)
     {
         Destroy(this.gameObject.GetComponent<MeshCollider>());
         Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
         int[] oldTriangles = triangles;
         int[] newTriangles = new int[triangles.Length-3];
 
         int i = 0;
         int j = 0;
         while (j < mesh.triangles.Length)
         {
             if(j != index*3)
             {
                 newTriangles[i++] = oldTriangles[j++];
                 newTriangles[i++] = oldTriangles[j++];
                 newTriangles[i++] = oldTriangles[j++];
             }
             else
             {
                 
                 j += 3;
             }
         }
         transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
         triangles = newTriangles;
         this.gameObject.AddComponent<MeshCollider>();
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if(Physics.Raycast(ray, out hit, 1000.0f))
             {
                 int hitTri = hit.triangleIndex;
                 
 
 
                 //get neighbour
                 //triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
                 //vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
                 Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
                 Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
                 Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
 
                 float edge1 = Vector3.Distance(p0,p1);
                 float edge2 = Vector3.Distance(p0,p2);
                 float edge3 = Vector3.Distance(p1,p2);
 
                 Vector3 shared1;
                 Vector3 shared2;
                 if(edge1 > edge2 && edge1 > edge3)
                 {
                     shared1 = p0;
                     shared2 = p1;
                 }
                 else if(edge2 > edge1 && edge2 > edge3)
                 {
                     shared1 = p0;
                     shared2 = p2;
                 }
                 else
                 {
                     shared1 = p1;
                     shared2 = p2;
                 }
 
                 int v1 = findVertex(shared1);
                 int v2 = findVertex(shared2);
 
                 deleteSquare(hitTri,findTriangle(vertices[v1], vertices[v2], hitTri));
             }
         }
 
     }
 }




I modified it a bit because the original version was freezing Unity.

Edit: I think I know what might be the problem, It could be reassigning the mesh collider that is causing the problem.

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.

Follow this Question

Answers Answers and Comments

209 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning Triangles from String Array issue 0 Answers

IndexOutOfRange Issue 1 Answer

How Do I Not Render Objects That My Player Doesn't See 1 Answer

Physics2D.OverlapCircleAll and method call cause a huge drop in animation frames 1 Answer

Simple optimization question on array vs hardcoding. 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