Jump to content
  • Advertisement
Sign in to follow this  
GameObject

Unity My attempts at dealing with memory management of chunk loading - What's a good model approach to do this?

This topic is 1045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my voxel game I am running into an issue in drawing the chunks where there is a lot of memory problems when it comes to building the mesh, so I'm looking for a new design model to base this on. Currently my design model is non-existent and kind of a mishmash/mess.

I'm using Unity 5.x, essentially I've tried this approach so far:

First attempt tried:
Every Chunk GameObject has a meshcreator script on it. However, I've learned this can be a problem when you have 2k+ chunks and leads to wasted memory because that would also be 2k+ meshcreator scripts. That in and of itself isn't really a problem if they aren't doing anything most of the time, but I feel like this leads to less control and more of a mess to have so many scripts like that.

The meshcreator script uses Lists to build Faces, Verticies, UVs, ColliderVerticies, and ColliderFaces. The lists are then converted to an array and applied to the mesh. Presto, the chunk shows up. But the downside with this is that 1) it is using List which in Unity is poorly managed in memory. 2) With each chunk having over 4k of voxels, to be calling List.Add() that many times is very inefficient because you are resizing an array thousands of times and adds garbage collection overhead.

Second attempt tried:
So I went ahead and converted them to arrays instead, predefining a large array size and reusing the array. It has an indexHeight so that every time we add something to the array, the position is increased for the next item to be added. But, as you can guess, when you have 2k+ chunks and each one has several arrays of 65k items, that creates HUGE memory footprint.
 

 private Vector3[] Verticies = new Vector3[65530];
 private int verticiesIndexHeight = 0;
 private void AddVertex(Vector3 vertex)
 {
     Verticies[verticiesIndexHeight] = vertex;
     verticiesIndexHeight += 1;
 }

Third attempt:

Here is where I'm wondering what the best approach might be. Is it feasable to add a mesh creating script to each chunk? Or would it be better to just have one ChunkMeshCreator script that handles all the work? So instead of having thousands of meshcreator scripts on each chunk, instead a single main script renders them? What have other people done? What might be most efficient for Unity?

Edited by GameObject

Share this post


Link to post
Share on other sites
Advertisement


But the downside with this is that 1) it is using List which in Unity is poorly managed in memory

 

This seems like a vague statement. It's been a while since I've used Unity, but is there some reason it is "poorly managed" (whatever that means) in Unity vs "regular" C#?

 

 


Second attempt tried:
So I went ahead and converted them to arrays instead, predefining a large array size and reusing the array.

 

Why didn't you just set the Capacity of List? That way you avoid the resizing issues. Of course, if you know exactly how many you need, then using an array is fine too.

 


But, as you can guess, when you have 2k+ chunks and each one has several arrays of 65k items, that creates HUGE memory footprint.

 

It sounds like these arrays/lists are just used temporarily for generating the mesh, then they go away, correct? Then just re-use one (I guess you can attach it to some "world" game object or something, or (eek) make it static, if you know this will only ever be accessed from one thread).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!