How do Heightmap and Alphamap from Terrain match together?

Hello all,

this is my first question here because I don’t know how to continue in the moment…
Maybe this is a rudimentary question but I have hang on that popint now for nearly a week.

I have a terrain: HeightmapSize 513, AlphamapSize:512

So I assume the first point (x) on terrain has first point (x) in Alphamap.
So are seccond points. But what color do I need from texturemap for x=513 in heightmap?

I’m trying to manipulate textures during runtime and it works perfeclty except at the cutting edge of terrains where I put 2 terrains as neightbours besides.

When I start manipulating the texture I’m fetching e.g. from x=0/z=0 size of 10 data:

terrainData.getHeights(0,0,10+1,10+1);
terrainData.getAlphaMap(0,0,10+1,10+1);

So now all is fine. But reaching end of terrain I do following:

terrainData.getHeights(502,502,10+1,10+1);
terrainData.getAlphaMaps(502,502,10,10);

Because the alphamap is less as big as heightmap I have one point in heigths that does not have a matching point in alphamap.

Am I missunderstanding something? Has had anyone else a problem like this?

Regards,
Marc

Maybe I have a hint on how it could work, but i will have to test:

Is it possible that Unity stores not the textures for a vertice but for a tile (e.g. x=0 to x=1 and z=0 to z=1)? Getting the textures from a tile of the terrain could be solved by mid values in the following way:

heightmap x=0 => take alphas for x=0 (First tile, so now prev value)
heightmap x=1 => take alphas mid values of x=0 AND x=1
heightmap x=2 => take alphas mid values of x=1 AND x=2

heightmap x=126 => take alphas mid values of x=125 AND x=126
heightmap x=127 (last tile) => take alphas values from x=127

or graphically:

A->E are terrain tiles, 0->5 are x coordinates:

0  1   2   3   4   5   <= x-values
 A | B | C | D | E     <= tiles

x=0: Colors from A
x=1: Mid for Colors from A/B
x=2: Mid for Colors from B/C
...
x=4: Mid for Colors from D/E
x=5: Colors for E

This would match the structure

Hello guys,

I tested the idea of kingmac2104 and I can confirm his theory.

I tested it like that:

  1. Take the terrain data and set one solid texture only in point 0/0 to 1.0f.

  2. Create a cube of size 1/1/1 and place it on terrain at 0.5/0.5

  3. start the game and you will see that the color of this texture is not completly hidden beneith the cube.

I doublechecked it with size 2/2 and again: same effect that texture on terrain is bigger than the cube.

So alphamap 0/0 means for the tile of size 1/1 reaching on terrain from 0/0 to 1/1.

Hope this answers helps to solve this question.

HeightMap is an array of vertexes.
AlphaMap is an array of faces.

Therefore, HeightMap will always be sized +1 larger than the AlphaMap.

Think about a single square. If we create an array of the vertexes in the square, we need a 2x2 array. If we are going to set the height of each of the corners, we loop through all 4 indexes in that array.
If we want to set the texture of a square, there is only 1 face, so we only need an array of 1x1.

It holds true for grids made of squares too. Grab some graph paper and draw it out.

Also, Unity Terrain objects will splat-map textures. That means that each face has a texture that blurs with the neighbor faces. This will make the texture look slightly larger/smaller than the actual size of the face.


I know it’s an old post. Google still leads here, so maybe this will help someone.

Hi, I did some intensive testing on this in Unity 2020.3 and I found the information given here to be incorrect.

This post is quite old but it is still the first result in google for the question so updating it will hopefully be helpful to any futur people trying to work with unity terrains.

Each value of the AlphaMap DO NOT correpond to a face, contrary to what would make sense.
Best way to demonstrate this is to see that on the terrain border the value of AlphaMap apply on the edge.

This means that HeightMap values and AlphaMap values behave the same, if they had the same size, each AlphaMap value would fall on an exact HeightMap edge/value. HOWEVER, AlphaMap is 1 smaller, so the values dont align.

You can see it clearly is you enable the shaded wireframe view in editor.

To illustrate, here is how AlphaMap and HeightMap values should align if AlphaMap where faces:

alpha :    a     a     a     a     a     a     a
height: h     h     h     h     h     h     h     h

Here is how values effectively missalign:

alpha : a      a      a      a      a      a      a
height: h     h     h     h     h     h     h     h

So no, it is not easy to tell what AlphaMap value correspond to what HeightMap value, simply because they dont align.

This also means for anyone trying to achieve seemless terrain transition across multiple terrain objects that the first value of a row/column must be the same as the last value of the corresponding row/column in the neighboring terrain for both HeighMap and AlphaMap.,