Importing Terrain Heightmaps in Unity3D

Unity has a good terrain designer with lots of essential options. I wanted to create a forest terrain, so I was searching for the efficient ways of making a terrain and I incidentally found that we can import heightmaps of real locations and use it in a Unity terrain. It is not as efficient out of the box but it is a starting point. Here I will walk through the basic steps that I’ve also taken while learning it.

Choosing Terrain

First of all, you need the heightmap of the real place. I don’t know whether we can use Google Maps to get terrain data but the one site that is recommended in many articles was terrain.party. They use different maps from OpenStreetMap and USGS.

Note: There is an issue with the site currently. You should search for a place’s name when the visible map area is currently near it. If you type and enter Colorado and you were checking near North Korea, the search will return an error saying that the place is not found.

Go to the terrain.party and search the place you want. Let’s say I want to create a Yellow Stone forest area replica. For that, first click on the search button on the top left and type USA.

Capture1

The map will now navigate itself to the country. Now search for Yellow Stone and it will show a square grid area. You can zoom in and out of the map and drag the square to the desired place. You can also specify how large the terrain has to be. There’s a field in the right side where it is mentioned how much area is currently selected. It will be 18km or 10km by default. Let’s just select the smallest area 8km.

Capture

Now click on the Export icon which is a cloud with a down arrow image. Give a name in the dialog box that appears and it will now prepare all the assets for downloading. You will now have a zip file with several types of the same heightmap with different suffixes:

  • Yellow Stone Height Map (ASTER 30m).png
  • Yellow Stone Height Map (Merged).png
  • Yellow Stone Height Map (SRTM3 v4.1).png
  • Yellow Stone Height Map (SRTM30 Plus).png
  • Yellow Stone Height Map (USGS NED 10m).png

It will also have a README.txt file describing what all these files mean. Long story short, what we need is the Merged image.

Generating RAW image

The image we downloaded is obviously a PNG file. But Unity only accepts RAW image files. So we must do a conversion. There are several tools out there to do that and one of the popular ones is using GIMP.

  • Open GIMP and first of all, scale the terrain image with a resolution of a power-of-two plus one. That is, 33×33, 65×65, 129×129 or 513×513.
  • Now click on File → Export As. Select Raw Image Data as the option. Don’t worry about the .data extension now even though Unity seems to accept .raw file. Click Export button.
  • In the next window, select the following option and click Export. That’s it.

Capture

Creating terrain

 

All set and we can start creating the terrain itself! Create a new Terrain gameobject by right clicking anywhere on the Heirarchy window → 3D Object → Terrain. We want to make this terrain look like the place we selected. In order to do that, we need to import our terrain heightmap to this newly created terrain object.

  • Click on the Terrain gameobject
  • Go to terrain Settings in the inspector (the cog icon on the far right)
  • Click on Import Raw button. In the file selection window, change the file type to All Files and select the .raw file that we just exported from GIMP.

Untitled

  • In the next window, don’t make any change now and click Import.

Capture1

There you go! A real-world place right in your Unity scene! If you think the terrain look too ragged, you reduce the Terrain Height property in Settings tab and also use the smoothing tool.

You can do a lot of customization in this terrain like adding grass, trees, fog, water etc and make it alive. Just keep one eye on the FPS and you’re good to go.

Love Peace.

Advertisements