Blog Layout

Devlog #3: Better world gen

183:924032007 • Aug 26, 2023

We're gonna need a bigger CPU.

Download


Previously, the world generation system was pretty slowwww. It worked well enough for smaller view distances, but at any reasonable view distance there'd be noticable lag when generating new chunks. The reason for this was due to the way I handled threading. I wasn't quite foolish enough to have chunk generation all be run on the main thread, but my solution was very naive. I had each chunk generator running on its own thread, and when you're trying to generate say 20 chunks as the player travels through the world, on a processor with only 12 processor cores (such as my own), there will obviously be some stuttering. I tried various solutions to this, beginning with C#'s task system. I found that using tasks only slightly alliviated the problem. I also looked into using Unity's jobs system, which I still believe would have been the best option for performance however it also would've required a massive restructure to the game's current systems. In the end, I solved the problem by having a number of threads dynamically created based on the number of available processor cores, where the work of generating chunks would be divided equally among them. In practise, this would mean that for a processor with 4 cores 3 threads would be created (to avoid holding up the main thread) each with its own uinique 'thread ID', and every time a chunk was created (which still has to be on the main thread, since Unity can only instantiate from the main thread) it is assigned an 'owner thread' as illustrated. The thread with the appropriate ID would then generate the mesh for the chunk and send it to the main thread to be applied.

A processor with 4 cores (one for the main thread and three to be used for chunk generation) would generate chunks roughly like this.


I also noticed after finishing these optimisations that there was some remaining stuttering, caused by having to render and generate colliders on the newly spawned chunk meshes. This isn't really something I can do much about, moreso it is a Unity limitation. However, I did manage to alleviate it slightly by pre-caching chunk meshes. Essentially, the meshes for a chunk are generated before the player can see them, for instance you may have a view distance of 100 units however have all chunks generated within 150 units, meaning that as the player travels through the world there is less of an effect of chunks popping into existence right in front of them, since there is effectively a 'safe-zone' which strikes a balance between having the chunks ready (relatively cheap on performance) and rendering allll the chunks (quite expensive!!)


Unfortunately, this all took me a while to figure out. This has been basically the only thing I've been working on for the past 2-3 weeks on the project. This was supposed to be a quick thing!! Regardless, I can now say it is actually in fact in a state I'm happy to continue working with. Unfortunately there's still some minor frame drops (40-50 fps) when generating chunks, but my test scene is an infinite world whereas the actual game will take place in enclosed maps, so I think this will iron out over time, and I don't want to optimise prematurely if that does turn out to be the case.

by 183:924032007 06 Nov, 2023
I still love you, itch.io
22 Oct, 2023
Featuring explosives. 
by 183:924032007 19 Oct, 2023
A lot.
by 183:924032007 19 Oct, 2023
I am speed.
by 183:924032007 06 Oct, 2023
Erectin' a dispenser.
by 183:924032007 03 Oct, 2023
Plus, Unity VS My Soul. 
by 183:924032007 15 Aug, 2023
Ok, it still looks like a Minecraft clone. Trust the process.
by 183:924032007 15 Aug, 2023
That's right, I'm making video games.
by 183:924032007 27 Jun, 2023
We've been here before.
by 183:924032007 21 Jan, 2023
Easy to setup, fun to play, tricky to master.
More posts
Share by: