Archivi categoria: Senza categoria

How to generate a Unity 3D Terrain from height map image

Hi all,

In this tutorial I’m trying to explaing how to create an Unity 3D terrain, using a simple Height Map Image.

Height Map Image.

What is this height map image? Wikipedia says:

“In computer graphics, a heightmap or heightfield is a raster image used to store values, such as surface elevation data, for display in 3D computer graphics. A heightmap can be used in bump mapping to calculate where this 3D data would create shadow in a material, in displacement mapping to displace the actual geometric position of points over the textured surface, or for terrain where the heightmap is converted into a 3D mesh.”

http://en.wikipedia.org/wiki/Heightmap

For my tutorial I searched on Internet an height map image of Australia and I found this image:

Height-map-image-of-Australia
very nice image of Australia

In this image you can see this: darker areas correspond to areas of lower heights, and the  lighter areas correpond to higher zones.
So, when I see a white zone, there there is a mountain. Black zones are plain or sea.

Processing the image

For using the image in Unity 3D some simple image processing work has to be done. So, open the image in a Photo Processing software ( I used Photoshop TM ).

03-image-in-photoshop

And convert the image in grayscale – mode, better to reduce the size to 512×512 px.
Pay attention to use always 2 power sizes for this images, because Unity 3D optimize them very efficently with this sizes.

The next step is to save the image in a raw format. (Photoshop raw format *.RAW ). I named my image australia-gray.raw
04-save-photoshop-raw

Importing the image in Unity 3D

Now it’s time to play with Unity 3D and import the image that we’ve done.

Open Unity in a new scene, and create a new Terrain.
For Unity 5 the Terrain object creation from menu is here:

Game Object -> 3D Object -> Terrain

and a new terrain will be created:

01-Unity-NewTerrain

If you select the terrain GameObject from the Hierarchy, the inspector will show many options that you can play with.

For our Pourpose is important to select the “gear icon” in the inspector that opens many parameters.

05-ingranaggio-inspector

In this tab there are the setting for importing the raw image of Australia height map.
Press the button Import Raw.. and select the saved raw image

06-Import-Raw

07-import-raw-heightmap

A new import setting windows opens after you have selected the file, I modified the sizes of importing accordling with the image dimensions.

08-import-settings

Change Width and Height to 512×512 and also terrain size to 512 by 512.

Now you will have the new imported terrain:

09-adjust-terrain-height-1

There are some problems with the height due to the setting of “Terrain Height” in the inspector. Change the value to a better number, for exanple 60 or 50, and the terrain will display better:

10-adjust-terrain-height-2

Here we have the Australia with mountains in our Terrain Object!!! Hurraay!!

Then I played a little with some textures of grass, rocks and sea and I colored the map:

11-adding-texture

WOW! better.

You can download my unity 3D project from here, for Unity 3D v.5. and above versions.

Bye