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 Legnar · Apr 22, 2013 at 12:05 AM · cubemeshesworldperfomance

Generating Meshes Is Slow

Finally mananged to have 1 gameobject that created several 1x1x1 cube meshes and stiches them together. When the program runs it creates 10x10x10 cube, but it takes about 10 seconds to load and uses 300mb memory. what is wrong!?

NewMesh Class:

 public class NewMesh : MonoBehaviour {
 
 void Start()
 {        
    
         
 }
         
 public static Mesh ExtendMesh(Mesh first,Mesh second,int counter)
 {
     
     int [] trianglesooo = second.triangles;
         
     for (int i = 0; i < trianglesooo.Length;i++)
     {
             
         trianglesooo[i] +=8*counter;
             
     }
         
         Vector3[] Lvertices = new Vector3[first.vertices.Length + second.vertices.Length];
         int[] Ltriangles = new int[first.triangles.Length + trianglesooo.Length];    
         
         first.vertices.CopyTo(Lvertices , 0);
         second.vertices.CopyTo(Lvertices,first.vertices.Length);
         
         first.triangles.CopyTo(Ltriangles , 0);
         trianglesooo.CopyTo(Ltriangles,first.triangles.Length);
         
         second.vertices = Lvertices;
         second.triangles = Ltriangles;
  
         
         return second;           
 }
     
 
     
  
 public static Mesh createNewMesh(Vector3 position)
 {
     Mesh returnMesh = new Mesh();
     returnMesh.name = "Chunk" + position;
  
     Vector3[] vertices = new Vector3[8] {new Vector3(Mathf.Floor(position.x) , position.y, Mathf.Floor(position.z ) ), 
                                          new Vector3((Mathf.Floor(position.x) + 1) , position.y, Mathf.Floor(position.z  )  ),
                                          new Vector3(Mathf.Floor(position.x)  , position.y, (Mathf.Floor(position.z  ) + 1)  ), 
                                          new Vector3((Mathf.Floor(position.x) + 1)  , position.y, (Mathf.Floor(position.z  ) + 1)  ), 
                                          new Vector3((Mathf.Floor(position.x) + 1)  , position.y+1, (Mathf.Floor(position.z  ) + 1)  ),
                                          new Vector3((Mathf.Floor(position.x) + 0)  , position.y+1, (Mathf.Floor(position.z  ) + 1)  ),
                                          new Vector3((Mathf.Floor(position.x) + 0)  , position.y+1, (Mathf.Floor(position.z-1  ) + 1)  ),
                                          new Vector3((Mathf.Floor(position.x) + 1)  , position.y+1, (Mathf.Floor(position.z-1  ) + 1)  )};
  
    int[] triangles = new int[39] {0,2,1,  1,2,3,  2,3,4,  4,5,2,  5,0,2,  5,6,0,  6,7,0,  0,7,1,  3,4,1,  1,7,4,  6,4,7,  4,6,5,  1,4,3};
    returnMesh.vertices = vertices;
    returnMesh.triangles = triangles;
      
    return returnMesh;
 }
     
 }


Class attached to gameobject:

 using UnityEngine;
 using System.Collections;
 
 public class Chunk : MonoBehaviour 
 {
 
     int counter;
     void Start () 
     {    
         
         this.gameObject.renderer.material.color = Color.green;
         GetComponent<MeshFilter>().mesh = NewMesh.createNewMesh(new Vector3(0,0,0));
         counter+=1;
         
         Mesh secondMesh = new Mesh();
         for(int x = 0; x< 10; x++)
         {    
             for(int y = 0; y< 10; y++)
             {
                 for(int z = 0; z< 10; z++)
                 {
                     
                 GetComponent<MeshFilter>().mesh = NewMesh.ExtendMesh(GetComponent<MeshFilter>().mesh,NewMesh.createNewMesh(new Vector3(x,y,z)),counter); //Adds second mesh to mainmesh.
                 counter+=1;
                             
                 }
                 
             }
             
         }
             
     }
     
     void Update () 
     {
     
 
     
     }
     
     
 }

alt text

fdfdfdfdfdfdf.jpg (159.9 kB)
Comment
Add comment · Show 1
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 Fattie · Apr 22, 2013 at 07:54 AM 0
Share

this is a rare "sophisticated duplicate" question :)

http://answers.unity3d.com/questions/315059/how-to-improve-performance-while-generating-extrud.html

http://answers.unity3d.com/questions/417483/is-there-a-way-to-assign-meshverticesi-directly.html

http://answers.unity3d.com/questions/321428/adding-mesh-collider-in-run-time-slow.html

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by whydoidoit · Apr 22, 2013 at 06:29 AM

Well firstly don't do this:

   first.vertices.Length

Because each time you do that you make a copy of the vertices array, same for triangles etc. That's why there is a vertexCount property. So in the ExtendMesh you are getting multiple copies of the vertex array - each time allocating memory to contain them.

Consider the result of mesh.vertices etc to be an array that is yours to do with as you see fit, but call it only when you need it and keep the result so you don't keep creating new copies.

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

A node in a childnode? 1 Answer

cube based enging 1 Answer

Random cube length on instantiate... 1 Answer

Creating a World? 2 Answers

Transform.Translate() coordinates confusion 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