- Home /
Loading terrain files dynamically
I am working on a project involving a large open world. My goal right now is to create a manager that knows which terrain file the player is in, and loads the 8 terrain files around that terrain to place one on each side of the player's current location. The player will be able to roam and not see any terrain updating because the terrains will be larger than the players view distance. I have an idea of the process: create an array of all terrains, determine which terrain the player is on, create another array of all the surrounding terrains plus the current one and load them, unload all terrain files not in the current location array. Actually translating this into c# is a little more difficult. Can anyone point me in the right direction or provide a little bit of insight on this problem?
Answer by getyour411 · Jan 25, 2014 at 03:40 AM
Look at Terrain.SetNeighbors and after changing/setting those, I think it's a good idea to do Terrain.Flush() but that might be myth.
If it helps: http://www.getyour411.com/unoob/terrain1.html
I have the terrains lined up already. I created the heightmap externally, then divided it into chunks for the individual terrains. I need to load and unload these to follow the player's movement so that I only render the terrains that are close to the player.
so push your terrain slices into the array of SetNeightbors
Ok, so how would the system know where the player is?
I think you'll have to manage that, you need to know what terrain slice the player is on in order to know what to render/fill into the top/left/right/bottom slices on SetNeighbor. Presumably your GameStart / Load SavedGame would always know what terrain[] you're on.
If I SetNeighbors isn't the answer you were looking for, post back and I'll change to comment so you can get more input.
What if I just took the players location and put it up against a set of numbers? Say if the player is at (1110, 132, 2140), then he's between 1000-2000 X (the second tile along the x axis) He is at X=2 for tiles. The character is also between 2000 and 3000 Z, so its the third z tile. The player would then be on top of tile (2,3).
Answer by paintbox1 · Jan 27, 2014 at 08:29 PM
I know that is not what you want to hear, but it's what stopped me several times, so before you start to worry about the TileManager, you should ask yourself 2 important questions:
1.Are you really able to load a terrain from a file at runtime? Not just the heightmap, but also splatmaps, alphamaps, treeprototypes and positions, detailprototypes and positions... The terrain doesn't provide any built in serialization. You need to deserialize that yourself. And thats just the Terrain. Your game should contain more than terrain, like npcs, buildings and so on. These also need to be loaded. Hint:I ended up, spending months writing different serializers just to find out, Unity blocks the gamecycle for a few seconds, when its setting heights and alphamaps of a terrain. So the player WILL notice, when your terrain is loading.
2.How do you edit your gamecontent? As mentioned in 1. you may want to place GameObjects in your world or even worse: Edit your Terrain. If you load your terrain into the editor and manipulate it. You dont manipulate its neighbors, which leads to different heights/alphas at the edges.
Well, I make my heightmaps with world machine. I set the tiles by just taking out chunks of the world machine world. I believe most systems would be able to handle JUST the terrain maps and textures, but as you said, I need gameobjects: npc's, items, buildings, lots of other meshes and routines. What do you mean by serialization? Also were you able to work out a dynamic engine to load the terrains in and out? Can it be done in a way that retains the immersion that I'm looking for?
By Serialization I mean loading from and saving to files. You dont have 100x100 Terrains in your Assetdatabase, so you just place them in a specific folder and include their index in the filename by some convention. Then you need to be able to reconstruct complete Terrains from those files. And to be able to edit them, you also need your editor to save the terrains to those files you want to load in game.
At the moment I am able to do just what you are looking for, so it is possible, but as I said, loading terrains with ordinary resolutions just blocks your mainthread for seconds, so I have to use heightmaps of 32x32 and alphamaps of 32x32 at terrainsizes of 1000x1000 to keep the freeze times at milliseconds.
Your answer
Follow this Question
Related Questions
How should you make an MMORPG world? 3 Answers
Tile to Sphere world algorithm 1 Answer
Open World Environment 1 Answer
How can i use Dynamic Terrain Loading? 1 Answer
Seamless Open World Environments 0 Answers