• 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
1
Question by ludokun · Jul 13, 2016 at 09:27 AM · mesh

How to create a mesh in a background process

Hello everyone,

I'm trying to create a mesh from a text file at running.

My script is based on this url : http://www.kamend.com/2014/05/rendering-a-point-cloud-inside-unity/ I'm using the same shader and I change the "CreateMesh" function in order to be able to show more than 60 000 points.

My problem is that creating a mesh with a lot of points takes a lot of time and I didn't succeed to move the CreateMesh method into an IEnumerator. (I have to show an object with more than 4 000 000 points or several object of 800 000 points each)

The way I created the IEnumerator was like this:

 IEnumerator createMesh(){
       yield return null;
       <create the mesh>
 }


This way the IEnumerator is meaningless. (and IEnumerator are run in the main thread)

Could you please tell me how can I create my mesh in a background process (for not blocking the Augmented Reality part and other stuff)? (The best would be to be able to show the 3d model progressively) The target platforms are iOS and Android.

PS: I know there is an asset which manage more than 20 million points (https://www.assetstore.unity3d.com/jp/#!/content/16019) but my company is not interested in buying this asset for several reason (not about money)

I really thank you in advance for your help

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

2 Replies

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

Answer by Bunny83 · Jul 13, 2016 at 10:31 AM

I see several problems here:

  • First of all a mesh in Unity can only have 65k vertices since the used index buffer is a 16bit buffer so you can't address more vertices. That means you have to split your mesh into seperate meshes. 4 000 000 vertices would mean you need 60+ seperate meshes.

  • You can't use the Mesh class from a different thread than the main thread. You can prepare the vertex / index arrays in a background thread, but the actual mesh creation / assignment of the arrays has to be done on the main thread.

There are ways to workaround those limitations, but they require you to write native code plugins which manually create VBOs (OpenGL) or VertexBuffers(D3D) in order to use 32 bit index buffers. This is not that trivial. That's why such ready-to-use solutions usually aren't free.

Comment
Add comment · Show 3 · 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 ludokun · Jul 13, 2016 at 01:26 PM 0
Share

Thank you for your reply.

You're right, a mesh can only have 65k vertices, that's why I've improved the code from the url on my post in order to create automatically enough GameObject with $$anonymous$$eshFilter and $$anonymous$$eshRenderer for displaying my full object.

Concerning the second point, that's ok if I can't show my model progressively, but at which point of the creation process, I have to run it on the main thread? when running the mesh.SetIndices() method?

avatar image Bunny83 ludokun · Jul 13, 2016 at 04:09 PM 0
Share

Everything that involves the $$anonymous$$esh class directly has to be done on the main thread. So when you call the setter for the vertex positions / colors / uvs / normals / ... as well as setting the indices. Those are all methods that run engine core functions which can only be used from the main thread.

So what you can do in a background thread is the allocation of your arrays and the actual generation of the mesh data. However the assignment to the $$anonymous$$esh class has to be done on the main thread.

avatar image ludokun Bunny83 · Jul 14, 2016 at 06:14 AM 0
Share

Thank you for your reply.

I've succeeded to create a thread for retrieving all vertices, colors and indices and set them on the main thread (not glad the way I manage it though...)

Everything works the way I expected :) thank you very much

avatar image
0

Answer by LiamMitchell · Dec 17, 2019 at 06:02 AM

You can use UInt32 for the index.

         if (Vertices.Length >= ushort.MaxValue)
         {
             mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
         }
         else if (Vertices.Length >= uint.MaxValue)
         {
             Debug.Log("Too many vertices for Unitys limit per mesh. Suggest to chunk it up into multiple meshes.");
         }

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Terrain vegetation on custom modelled mesh 1 Answer

Save to Prefab or Mesh? 0 Answers

Can terrain detail meshes have basic shading applied? 1 Answer

How do I render a collider? 4 Answers

Mouse click a collision mesh 3 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