Saving a randomly generated terrain

Hi!

I have a terrain generation script working, and it generates the terrain quite fast. Now I have a problem with saving it and no, I don’t want to use PlayerPrefs for saving, I have been using Serializing it to binary, but I tried to serialize one 1000 * 1000 piece of terrain (heightmap size 1025) terrain and the terrain can be a whole lot bigger than that and it took about 4 MB. I cannot save just the seed as I am most likely going to edit the Terrain generation. What would be the best way of doing it?

One solution would be to store the seed + all edits made to the terrain. To load it, just generate the seed and apply the edits back on. Another alternative is to use the seed generated terrain as a “base”, and use sparse data trees to include the “diff” from the seed version. My idea is that you should use the seed generated result as a base for compression and apply “patches” to that base result to get the most current version.

To actually save it, just dump it to a binary file if on stand alone, or you could try to save it to a ftp/sql database. Or be clever with web browser communication if you have a more ideal storage place.

Heightmaps are actually 16-bit integers. Get/SetHeights uses floats for, I suppose, convenience; I guess it’s easier to work with values from 0…1 instead of 0…65535. So you can convert the floats to shorts before saving, which will cut the size in half, so a 1025x1025 terrain would take 2MB rather than 4MB. After that, the only thing you can really do is use some sort of compression. Unity doesn’t have anything built-in for that, but there are some C# libraries for zip compression which you can try.