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 Sca · Oct 06, 2013 at 06:23 PM · procedural meshinstantiation

Procedurally generated mesh performance

Hello. I am generating a hexagon mesh procedurally, and putting it in a game object, like so:

 GameObject hex = new GameObject ();
         hex.AddComponent<MeshRenderer> ();
         hex.GetComponent<MeshRenderer> ().castShadows = false;
         hex.GetComponent<MeshRenderer> ().receiveShadows = false;
         
         hex.AddComponent<MeshFilter> ();
         hex.GetComponent<MeshFilter> ().mesh = GenerateHexagonMesh ();
         //hex.renderer.sharedMaterial = mat;
         hex.isStatic = true;

I am instantiating the object about 400 times, without any performance issues. However, if I set a material to the object (tested several), the performance drops to the point that Unity just stops responding. If I assign a material, only 10 or so objects can be instantiated before the performance gets too bad.

Any tips to what might be going wrong here? Thanks

edit: this is how i generate the mesh:

 private Mesh GenerateHexagonalMesh (float hexRadius)
     {
         Mesh mesh = new Mesh ();
  
         Vector3 vertPos = new Vector3 (0, 0, hexRadius);
         Vector3[] vertices = new Vector3[7];
      
         Quaternion rot = Quaternion.Euler (0, 60, 0); //360/6 = 60
             
         vertices [0] = new Vector3 (0, 0, 0);
         for (int i=0; i< 6; i++) {
             
             vertPos = rot * vertPos;
             vertices [i + 1] = vertPos;
         }
  
     
         mesh.vertices = vertices;
  
         int[] tri = new int[]{
             0, 1, 2,
             0, 2, 3,
             0, 3, 4,
             0, 4, 5,
             0, 5, 6,
             0,6,1};
  
         mesh.triangles = tri;
  
         Vector3[] normals = new Vector3[7];
  
         normals [0] = Vector3.up;
         normals [1] = Vector3.up;
         normals [2] = Vector3.up;
         normals [3] = Vector3.up;
         normals [4] = Vector3.up;
         normals [5] = Vector3.up;
         normals [6] = Vector3.up;
  
         mesh.normals = normals;
         //mesh.RecalculateNormals ();
         mesh.RecalculateBounds ();
         mesh.Optimize ();
         
         
         return mesh;
     }
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 robertbu · Oct 07, 2013 at 12:04 AM 1
Share

How many triangles are then in the mesh generated by 'GenerateHexagon$$anonymous$$esh'?

avatar image whydoidoit · Oct 07, 2013 at 12:07 AM 0
Share

I think we need to see the code for GenerateHexagon$$anonymous$$esh... Any chance it's just making a more complicated mesh each time and not reseting something for instance?

avatar image Sca · Oct 07, 2013 at 09:21 AM 0
Share

I edited my post with the mesh generation code

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ArkaneX · Oct 07, 2013 at 11:18 AM

It's just a guess, but you're probably using material with a shader expecting a texture, and your hex mesh doesn't have UVs defined. I tested your code using material with Diffuse shader, and there was a big performance hit indeed.

When I set mesh UVs (just after setting mesh.vertices):

 var uvs = new Vector2[7];
 mesh.uv = uvs;

everything worked ok, and generating 1000 hexes with material didn't result in any performance loss.

Of course if you plan to use texture, then you have to provide proper UVs, not just initialize the array.

Comment
Add comment · Show 2 · 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 Sca · Oct 07, 2013 at 12:24 PM 0
Share

I actually had a hunch today that uv mapping might be the problem, and you confirmed it. I had no idea though that i could just assign them like that and it wouldn't be a problem.

Thank you so much! :)

avatar image ArkaneX · Oct 07, 2013 at 12:34 PM 0
Share

The above lines work, because Vector2 is value type defaulting to Vector2.zero. So all vertices are UV mapped to the same (0,0) texture coordinate.

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

17 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

Related Questions

Instantiation happens three times, but called only once 1 Answer

Is it necessary to assign the result of Instantiate() to a variable and is there a need to always typecast? 1 Answer

instantiate object and let instantiated object know what it got instantiated from 2 Answers

IgnorePhysics on BoxCollider at GameObject instantiation 0 Answers

scripts gets disabled when the prefab is instantiated 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