Devlog #3: Better World Gen
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.
Files
Get Build Fight Game
Build Fight Game
Status | In development |
Author | peterkcodes |
Genre | Shooter |
More posts
- V0.11.1 ChangelogDec 08, 2023
- V0.11.0 ChangelogDec 05, 2023
- 0.10.1 ChangelogDec 04, 2023
- 0.10.0 ChangelogDec 04, 2023
- Version 0.9.0Nov 29, 2023
- Version 0.8.0Nov 21, 2023
- Version 0.7.0 ChangelogNov 21, 2023
- Devlog #9: New Player & New PlacesNov 06, 2023
- V0.6.0 ChangelogNov 06, 2023
- v0.5.0 Changelog (+ how to play)Nov 01, 2023
Leave a comment
Log in with itch.io to leave a comment.