Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Wdarkfenix · Mar 19, 2013 at 03:01 PM · meshproceduralfreezegeneration

Why Unity freezes while creating Meshes procedurally?

I'm creating a mesh procedurally with around 10000 vertices, the problem is that when I start the mesh generation it freezes for a few moments, I used the time method to get the number of frames that past but it appears that not a single one past, then the mesh is finished, but I dont want that the program freeze each time that I generate a new mesh, 'cause I'm doing it constantly.

The arrays fills automatically before creating the mesh, but just filling them without sending them to the mesh there's no lag, so I don't think the problem is there.

Also, if ,after generating the mesh, a method move all the vertices around there's no lag, so the manipulation of the mesh appears to be ok too, anyone has a clue of why is this happening?.

Here's the codo so you can take a look:

     public void Build(    GameObject GObject,
                         List<Vector3>    chunkPosition,
                         List<Vector3>    chunkRotation,
                         List<int>        chunkSides,
                         List<int>        chunkSpace){
         float time1,time2,timeResult;
         time1 = Time.time;            
         int    arraySize      = 0,
             index          =    0,
             sidesToRender = 0,
             tama          =    chunkBlockID.Count;
         
         MeshFilter    meshFilter    = GObject.GetComponent<MeshFilter>();
         Mesh         mesh        = meshFilter.mesh;
 
         mesh.Clear();
         
     //Each position in each list represents information of a cube that occupies that place
     //chunkSpace represent the number of triangles of the cube that are to be rendered

         for (int i = 0; i < tama; i++)
             arraySize += chunkSpace[i];
         
         Vector3[] vertices = new Vector3[arraySize];
         Vector2[] uv       = new Vector2[arraySize];
         int[] triangles    = new int[arraySize];
         Vector3 Temp       = new Vector3(0,0,0);
 
         ////ChunkSides contains six glags, one per side, so this cycle decides the orientation of each triangle
         for (int i = 0; i < tama; i++){//Todos los posibles bloques

             sidesToRender = chunkSides[i];
             for(int j = 0,selector  = 1,Kstart  = 0,Kend  = 6;
                     j < 6;
                     j++  ,selector *= 2,Kstart += 6,Kend += 6){
                 //If the flag of a side is on, then add the respective rendering parts
                 if((sidesToRender&selector) != 0)
                     for(int k = Kstart; k < Kend; k++, index++){
                         vertices[index] = Shapes.CubeVx[k]+Temp;
                         triangles[index]= index;
                         uv[index]        = Shapes.CubeUV[k];
                     }
             }
         }
         mesh.vertices     = vertices;
         mesh.triangles    = triangles;
 
         mesh.uv    = uv;
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         mesh.Optimize();
             time2 = Time.time;
             timeResult = time2-time1;
             print(timeResult);
             //Get the total time elapsed (it always gives me 0)
     }
Comment
Add comment · Show 3
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
avatar image Bunny83 · Mar 19, 2013 at 03:28 PM 1
Share

Do you use a $$anonymous$$eshCollider? If so, that's your problem ;) Updating a $$anonymous$$eshCollider is very (very) slow. This was an old webplayer i created. The cube is made up of 6 independent meshes each 128x128 vertices (as far as i remember). The recreation is very fast, but i don't use a $$anonymous$$eshCollider. If i use one a single update might take up to 10 sec.

avatar image whydoidoit · Mar 19, 2013 at 04:18 PM 0
Share

@Bunny83 - interesting about $$anonymous$$eshColliders - I kind of never use them for other reasons - but now I've got a new shiny reason!!

avatar image Wdarkfenix · Mar 19, 2013 at 05:34 PM 0
Share

That really helped, I went down from 25 miliseconds, to 8, in average!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Mar 19, 2013 at 03:04 PM

Firstly no frames will pass if you are taking a long time to do something, it will be a synchronous operation.

If you want to know how long individual parts take you could use a StopWatch to time them.

Without seeing your code it's hard to tell why its taking some time, but of course creating a mesh will potentially need to do lots of things with the GPU and allocate a significant number of objects. One solution would be to prepare a collection of meshes and then manipulate them as that seems to be a fast enough operation for you.

Comment
Add comment · Show 4 · 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
avatar image Wdarkfenix · Mar 19, 2013 at 03:14 PM 0
Share

I just finished editing the question

avatar image whydoidoit · Mar 19, 2013 at 03:17 PM 0
Share

Well it would certainly be worth working out which parts of that caused the delay using a StopWatch as I suggested. You can't rely on the Time functions as they are part of the game loop.

avatar image Wdarkfenix · Mar 19, 2013 at 05:44 PM 0
Share

I used stop watch, the major problem was the collider.

avatar image whydoidoit · Mar 19, 2013 at 05:47 PM 0
Share

Good to know!

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

12 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

Related Questions

Flat shading procedural generated mesh? 1 Answer

Help in understanding Mesh Generation 1 Answer

generate procedural floating island 1 Answer

Generated mesh loses lighting at certain angles 2 Answers

Procedural Cylinder Generation 2 Answers


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