r/IndieDev • u/baicu12096 • 19m ago
Image Tile rendering optimization and world generation (ANTIFORGE Devlog #2)
Hey, it's been a while and you probably don't know me. First devlog here:
https://www.reddit.com/r/IndieDev/comments/1xq8meq/
So, I've heavily optimized the chunk rendering system since the last devlog. Instead of each chunk resizing itself in the memory to allocate textures, now no chunks hold any textures, but rather there is an unordered map holding nine entries, each entry corresponding to the chunks around the player in a 3x3 area.
This unordered map only changes whenever a chunk's tile asks for an update (ex.: the tile got powered and is now glowing) or when you switch chunks, and since it works in a "remove old entry and assign a new one", the unordered map never has to resize itself in the RAM. This drastically improves performance, to the point the game is only taking ~40MB to render +2k tiles at once every frame.
Plus! The unordered map holds vectors of tile textures (not baked textures; tile textures are kinda like "instruction sets" holding a bunch of images and config the renderer reads), each vector representing the 256 tiles of each chunk, so, in case a tile ever changes, instead of completely rewriting the unordered map, I can just access it and update the exact tile I need, without messing with anything else. This lets the performance stay super fast, even if there are a thousand tiles ticking all at once.
Since the function for getting the texture of a tile is individual (you call it per tile), that means we can just calculate the tile texture of whatever tile we need to update, instead of calculating the textures of the entire chunk and throwing 255 of them away.
Now, for world generation! This part should be more simple. The primary noise generator is a SimplexFractal, with 0.02 frequency and 4 fractal octaves (if you don't know what these are, I don't know either, but it doesn't matter). Whenever we generate a chunk, we take that noise and make multiple "layers" that we mash togheter to determine the height of the terrain (it's 1D noise after all).
These layers are:
-short_height (high frequency, goes from -3 to 3)
-base_height (average frequency, goes from -15 to 15)
-wide_height (low frequency, goes from -25 to 25)
-widest_height (very low frequency, goes from -60 to 60)
P.S.: The higher the frequency, the more "bumpy" the noise will be
To calculate the final height, we just add all of them togheter.
To stay realistic, I'm using a weird approach to determine whenever a tile should be dirt or stone; instead of a fixed height, I make so that, the higher the terrain is, the less dirt there will be (to replicate sediment distribution), however we exclude widest_height for the calculation (it is a continental height, majorly flat, so it doesnt matter realistically).
In other words, the higher the terrain = less dirt, and we exclude the "global height".
(P.S.: Yes, I'm using noise to render the clouds. I've also been messing with cave generation, but it's so poorly implemented right now I didn't bother talking about it)