Public Group

# Questions concerning terrain heightmaps

This topic is 472 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm curious as to how one calculates position coordinates for the terrain; specifically the height coordinates. My understanding of Terrain generation with height maps is iterating through the heightmap and retrieving pixel data. The pixel data, depending on its greyscale color (white or black), will determine its height. However, I'm unsure of the practical implementation of this, specifically the equation used to convert pixel data to new height coordinates.

In addition to the Y coordinates, how do I determine the values in the X and Z plane. Do I just pick a range of values (ex. [-30, 30]) in X and Z?

Lastly, I'm sure indices will have to be used in order to make an efficient program. My prior experience with Index is manually entering values for simple geometric shapes (Cubes and triangles). This seems more involved than before. I was wondering if there exists an algorithm that handles indices when dealing with terrain.

##### Share on other sites

There may be different approaches due to the project/environment you are using. Making an open world game with a fixed world cell size and some meta data based on the surrounding cells may lead to a different approach than using a terrain in Unity.

Normally you would sample the images X/Y coords accordingly to your X/Z resolution of the terrain you are drawing. In Unity you could set the sample rate for example to create more accurate or rough terrains that leads to the need of passing a greater or smaller heightmap image (in resolution) where the same is true for the Y coords of your terrain. Unity sets a fixed maximum height at which a color value of White will be 100% of that height.

The approach may as I already wrote, differ from e.g. an open world game where the maximum height may lead to surrounding cells (like in a mountain environment) but setting the height value in Unity to 300 max will a half gray pixel (128; RGB) become 150 units in your terrain.

Note that the X/Z coords mapping is uneffected by translation so pixel (0; 0) will always lead to the top left corner of your terrain cell

##### Share on other sites

So a pixel sample, (x,y), will yield a number, h, the maximum of which is determined by the pixels size in bytes or depth, call it, D. Take h and convert it to a percent of D. Apply that percent to your maximum height world value. The result would be the height at vertex( x,y, h). You can also scale (x,y) to a maximum world value just like height.

As far as the indicies go, you should first try using a triangle list.  For each pixel, create a quad ( two triangles forming a square). In a triangle-list, each quad needs six indexes, generate them for each quad or pixel. You would have a counter, indexCount, loop through your terrain x-y plane, create a quad, and for each vertex apply indexCount and increment it.

Edited by h8CplusplusGuru

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 10
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633745
• Total Posts
3013667
×