After getting one terrain to generate, my next goal was to get multiple mesh terrains to generate that the player can travel between allowing the terrain to be much larger to explore and not take up as many resources to run. One method to do this is through chunk generation.
Chunk generation is storing sections of a large level. When a player travels around a terrain, only the current chunk and occasionally neighbouring chunks are instantiated within a world at the time and as the player explores closer to the end of a chunk, the furthest away chunks would de-spawn (but stored again) and new chunks in front of the player will be instantiated. Because only sections of the level are active at a time, memory and processing power are saved for other tasks.
Through experimenting with the values within the Unity editor, I found that each mesh would need to be offset by 5 from the current one to be a continuation, and I was already aware of having to instantiate each mesh 100 in the X or Z axis to be connected. Now what was left was the actual chunk spawning.
Despite knowing how chunk generation works, I couldn't find any research on how to make chunk generation in Unity, specifically for a mesh terrain instead of a Minecraft clone. Having a talk with my lecturer about what I plan to focus on that week, Will was able to help break down the task to make it easier to understand and gave the hint at what I should be researching by using the word "pooling" instead of chunk loading. With this small change in wording, I was able to find research on what pooling is in Unity.
My main focus was to get multiple meshes to generate at once, with the method I used is creating a prefab that has a mesh renderer and a mesh collider that can be instantiated when called, this allowed me to run my functions on start multiple times the within a for loop until I have the desired number of terrains generated.
Talking to a coursemate I learnt how to allow a mesh collider to update in real-time to be in the shape of the mesh created and to test I added a purple bean with a rigid body to represent a player to test the colliders work with success.
One thing I noticed with adding the collider and multiple meshes is that the framerate recorded in the stats section goes from around 800-900 FPS to 6FPS when altering the current Perlin noise offset, this shouldn't be a problem in the future as once the mesh has been generated it won't need to be altered again but does outline how long it could take to generate when first starting the game. A potential fix for this is to incorporate multi-threading where all generation is running separately from the rest of the code and can be worked on simultaneously.
My next task is to get each mesh to be stored and only instantiated when set conditions have been met (in this case, where the player is on the current mesh), with my focus on researching the Unity Pooling system.
Comments