• 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 multinfs · Oct 21, 2015 at 02:04 PM · performanceproceduralworld

Procedural chunk based world performance troubles

Ok so I've worked with multiple infinite procedural worlds and tried to work out how the generation works and trying to optimize my code to get the most out of my world as possible. Almost every time I've had performance issues and problems with keeping a steady framerate due to bad handling of the workload( and maybe some unefficient code ).

Now my question is about how you can implement a chunk-based system which generates the geometry for each chunk of the world over a period of multiple frames, effectively spreading out the heavy load which comes with creating meshes.

AFAIK Unity and threading is a bad combination and thus the only way of spreading out a task over multiple frames is to use Coroutines. These do help alot but I haven't heard of many using them for this purpose, which makes me wonder if there are more efficient ways of doing it.

Also, looping through all chunks in the world to see if any needs updating is quite inefficient but I cannot come up with a good solution for doing it since making a coroutine check for updates every frame is not the way to go.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Oct 21, 2015 at 02:57 PM

I strongly recommend to use threads in this case. However you have to keep in mind what things you can do in a thread and what not. The generation of the vertex and triangle index as well as uv and normal arrays can be done inside a thread. Once the data for a whole chunk is "ready" you will store that data (packaged in a class) inside a synchronised queue. Somewhere in Update or in a coroutine you will poll that queue for new items and simply grab one and create the mesh out of the data.

Keep in mind when you use MeshColliders the generation of those is horrible slow and can't be sped up. If that's your bottleneck you either have to use smaller chunks for the collider or use a different system for collisions. For a block-like voxel grid it's usually better to generate single cube colliders for the visible voxels. If you use an object pool for the colliders the overhead should be minimal and you can spread out the enabling / repositioning in a coroutine.

I've even heard of approaches where they only generate colliders where any physics object is near. So chunks far away won't have any colliders at all.

I've posted a simple example of how to implement a threaded job. However that was for a single job. When having tons of those jobs you might want to use a threadpool and a job queue instead. However that queue should be able to reorder it's items based on priority. If you move fast across your world the priorities of those jobs will change.

Also use object pooling where possible to avoid unnecessary garbage generation.

Here's a general information post about threads and coroutines, just in case.

Comment
Add comment · Show 1 · 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 multinfs · Oct 21, 2015 at 04:39 PM 0
Share

Oh, this was informative :). When I've read a bit about ThreadPools it seems that I want to go with creating 4 dedicated threads to do the job of calculating mesh data and let chunks queue up for those threads. I know threadpools might be a better option but I just cannot wrap my head around how they work. I might look into them in the future but afaik using these 4 threads will already do alot good for performance.

About the colliders, yea I'm using box colliders on the "touchable" cubes but they still need to be generated when there aren't enough cached on the chunk already, which creates some additional generation time when building my chunks.

Thanks for your help 'cause it does help alot!

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

29 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

Related Questions

how to make an horizontal mesh? c# 0 Answers

How can I set World Builder to generate right to the edge? 0 Answers

Procedural texturing of low poly plane 0 Answers

Performance issue Raycast each frame 1 Answer

Which is the best option (Procedurally world generation save) 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