- Home /
Overlapping terrains
Hi,
I am experimenting with having multiple overlapping terrains the idea is to have one representing the ground and another one for the water with some simplistic flow dynamics computed in realtime.
This doesn't have to be optimized, it is just some kind of prototype to experiment with what it can bring in term of gameplay.
The thing is that I don't really get how to access the different height maps from the script. I've seen code snapshots on the internet where people are using Terrain.activeTerrain, but it doesn't let me access the two separate height maps (actually it only give me access to the ground terrain, not the "water").
I am new to Unity but I get the feeling that the engine automatically splits the Terrain in smaller terrains for performances and gives you access to the most relevant "sub-terrain" with Terrain.activeTerrain (I know I might be totally wrong though) but what i need is several overlapping layers...
How can I access two different overlapping heightmaps at the same time from script? Or is it that I should use another kind of primitive, like something that would not be a terrain object but would still follow a heightmap's deformations?
Thanks in advance,
Nicolas
If performance is not a big issue, and you plan on doing it all procedurally anyway, why don't you just cut out the middleman and generate a mesh entirely from scripts? The only things the terrain editor really gives you are nice brush tools and the ability to place detail objects/trees- neither of which you need.
Ok thank you, This what I am trying to do right now. Is it possible to choose the number of subdivisions of the default plane object or do I need to generate the vertices by hand ?
Answer by msknapp · Sep 27, 2011 at 10:11 PM
I don't recommend using a different primitive. Performance will be better if you use Terrain objects.
One weird thing I figured out is that Terrain extends Component, meaning it is not itself a type of game object, but a member of a game object instead. In the Terrain class you can get a reference to the TerrainData it is using with "myTerrain.terrainData". Then you can get a reference to the heightmap with:
// (Please forgive the C#, hope you can translate to javascript if necessary)
Terrain[] myTerrains = Object.FindObjectsOfType(typeof(Terrain));
// through some algorithm (for loop?) find your specific terrain.
// for my example, I just use the first one.
Terrain myTerrain = myTerrains[0];
TerrainData td = myTerrain.terrainData;
// this might be of type float[][] or float[,] I'm not sure and don't feel like
// checking. I'm sure you can figure it out right?
float[][] heightmap = td.GetHeights(0,0,td.heightMapWidth,td.heightMapHeight);
There are also functions to set the heightmap. All the float values will be between 0 and 1, don't set a number outside of 0 and 1, it won't work. The TerrainData class is very difficult to work with, I found out, and pretty much undocumented. Unfortunately I had to go through a grueling process of learning how it worked. Let me know if you have more questions.