Height maps in Unity

Every week the team comes together for a big meeting and a day of working together. Last Tuesday we've discussed ways to generate terrains quickly and easily, and came up with using heightmaps for this. Heightmaps are greyscale textures with their colors indiciating height. The lighter an area is, the higher it is relative to the darker shades. A height map typically looks like this:

Image result for height maps

As you can see here, this area looks like a coastal area. The black parts are the sea and rivers flowing inland and the white parts are mountain ridges.

Unity can use heighmap data to generate 3D surfaces and terrains. Heightmap data is perceived very similarly to normal map data in Unity, but whereas normal maps do not distort the surface they are on, height maps - when applied to a terrain - do. Here's a great example of a heightmap in action in Unity:


Image result for height maps 3d


As such, height maps can be very easily used to generate a terrain. We saw this as an opportunity to prototype and "sketch" a terrain that's immediately on the right scale and has the right feel. When we've found a heightmap we like, the data can be loaded into ZBrush and manipulated to our liking.

Map data can be easily found and downloaded from the website "terrain.party". When navigating to this website, you'll be spawned on a map of the world. You can move around freely and use your grid marker to select an area you want to generate a heightmap of.

I've found that using the biggest area setting on the marker works best (this will capture multiple heights such as hills and mountains instead of maybe just one, and will create a more interesting and full terrain), mostly on mountainous areas.




I would suggest checking out the Andes (Argentina), the Alps (Switzerland), the Himalayas (Nepal), Haggar Mountains (Algeria) and Tian Shan (China). These areas range from flat highlands to some of the highest, steepest peaks in the world. Have fun and grab what piques your interest!

After you've found a nice area, generate your heightmap and open the zip. All you'll be needing is the "(merged)" file. You can throw the rest away. Resize the file to 1024x1024 and save as .png.

Navigate to the following website: "http://wiki.unity3d.com/index.php?title=HeightmapFromTexture" and copy the entire script. Create a new script within Unity and name it "HeightmapFromTexture". Paste the entire script from the site into your script editor.

When saved, you'll find a new menu in the top bar in Unity saying "Terrain". When clicked on it, you'll see the option "Heightmap from Texture" appear. Import your heightmap into the project and on its Import Settings change it's Texture Type to "Advanced". Enable Read/Write. Apply changes.




Create a Terrain. Scale it to a factor of 6 (600, 6000) so the heightmap won't get scaled and distorted. I.e. you can use a 600 unit length and width and use a 60 unit height. You can play with the height all you want but remember, you probably don't want everything squashed into a cube, so keep the height smaller than the width and length of the terrain.




 Now for the generation, all you need to do is select your heightmap texture and go to the newly added Terrain option and select Heightmap from Texture. The script will recognize your terrain and use it. Your terrain is done! Now you can go ahead and edit the heightmap as much as you need.




An important thing to notice however, is that a heightmap cannot generate overhanging scructures. To really get a nice terrain going, we're going to edit its model. This generation in Unity was handy for finding out quickly what a terrain is going to look like in 3D and testing out some handmade heightmaps. But for our project, we're gonna need more. We're gonna modify our generated terrain to suit our needs in ZBrush! I'll be going over this in the next post.

By the way, I've used the following tutorial to write this. If you can read faster than someone speaking in a video, feel free to use (like I will use this for myself haha).
https://www.youtube.com/watch?v=-vyNbalvXR4