- Home /
Heightmap to Array[256] and other way around?
My apologies if this question is illogical, I'm relatively new to heightmaps and am trying to figure out some of the ways I can work with them.
If I understand it correctly, a heightmap is a visual representation of 256 values (more if colors are used), divided evenly as a raster/grid across the image.
Is it possible to convert a heightmap to these values, so I can store them in an array of 256 values? And what about the other way, if I have an array of 256 values, can I transform this into a heightmap? This way I could manually or dynamically create specific heightmaps.
If this is not possible in Unity/C#, other methods are welcome as well :)
Answer by Bunny83 · Nov 02, 2017 at 10:18 AM
It is true that a heightmap is a grid of height values. It doesn't necessarily be 256 values. It could be a 16 bit number as well so you have 65536 steps. However this only applies to how the terrain might be stored in raw format on disk. Unity represents the heightmap with float numbers.
I also don't understand what would be the point of an array with 256 values. A heightmap is a 2 dimensional array and each array element holds the height of this point on the grid. You can get all height values in a 2d array by using GetHeights on a TerrainData object. Likewise you can use SetHeights to replace the height values of the terrain with the ones in your 2d array.
If you want to get / set all height values for a terrain the size of the 2d array depends on the size of your "grid". All information relevant to terrains is basically in the TerrainData object. If you have a terrain with a width / height or 512x512 you get a 2 dimensional array that is 512x512 in size (so it contains 262144 float values)
Dealing with heightmaps through code manually is quite complicated to get it right. They are ment to be created either with the terrain tools inside Unity or to import a heightmap from a raw heightmap file.
Thank you, this helps a lot. SetHeights and GetHeights seems to be what I was looking for.