I want to create a chunk-based heightmap terrain in Unity.
I've already set up a basic multifractal simplex noise and am working on it that I can generate height fields and meshes at runtime … but only one at a time.
I'm trying to figure out how best to store and access each block. I have already worked out some methods to convert global positions into chunk coordinates and chunk-local barycentric triangular coordinates so that I can interpolate heights. I've used this to create a way to beam a ray from the camera space to space over the mesh and traverse the intersections in the x / z plane until the intersection is found in elevation – position yourself on the terrain without having to install collision networks for each chunk, and use Unity's Raycasting. Everything works fine.
I'm just a bit clueless about how to implement the main infinite grid memory from which I generate chunk meshes.
This is planned to serve a game with an isometric perspective that reveals the world, while units investigate the fog of war over time … with the ability to move the camera through the uncovered terrain relatively quickly There may be limited possibilities to unload previously generated chunks. It's going to be an ever-growing dataset (so I'll probably have to look for ways to minimize memory).
I think I can either:
- For the entire site, create a two-dimensional array of float values (or perhaps short values of 64 KB in height) and dynamically resize the array as the X / Z boundaries move backward. Generate only the terrain mesh in chunks to keep any mesh chunk below the Unity 64k vertex boundary.
- Create a grid manager class with a kind of dynamic data structure that stores and instantiates / references map chunk objects, each containing its own (res * res) -large elevation map array.
- Something else?
I'm assuming Option 2 has the most mileage, but I'm not sure what kind of dynamic data structure to use. I would guess a kind of key / object pair structure in which the key is created from the block coordinates.
Suppose I want to access altitude values quickly and consistently across chunk boundaries. I think I need a way to access chunks. That's almost as fast as looking up arrays for indices and the complexity of O (1).
Or maybe a way to cache recently accessed chunks or chunks near the camera? Or pointer to her or something?
As you can probably tell, I'm pretty new to Spielev, but I have a VFX background, so I'm familiar with 3D geometry math and basic scripts. However, when it comes to designing / structuring / implementing a game, I'm an absolute novice, so references to relevant tutorials and the like are welcome.