Why does assigning heightmapResolution increase the size of my terrain?

So I’ve been trying to generate a terrain using code and I’ve come across a lot of problems. The most recent one is the fact that when I assign heightmapResolution the size of my terrain increases by a factor of some power of two.

For example, if my heightmapResolution is 513, then my terrain size increases by a factor of 16.
If my heightmapResolution is 257, then my terrain size increases by a factor of 8.

I must stress that the size increase happens only in X and Z axis. Height remains unchanged.

Debug.Log (terrainData.size);
terrainData.heightmapResolution = heightmapResolution;
Debug.Log(terrainData.size);

The change is already visible in the second Debug.Log.

Any ideas as to why?

I figured it out.

I had to set the heightmap resolution before I set the terrain size.
Here is the final code that works as intended.

terrainData.heightmapResolution = heightmapResolution;
terrainData.size = new Vector3(width, height - sandBaseHeight, length);
terrainData.baseMapResolution = baseMapResolution;
terrainData.SetDetailResolution((int)detailResolution.x, (int)detailResolution.y);
terrainData.alphamapResolution = aplhaMapResolution;

Debug.Log ("Terrain size: " + terrainData.size); //Correct terrain size

Hope this helps someone else!

This is a bit simplified and it’s late so I may be wrong, but:

Terrain is built off of a height map. The resolution of this heightmap is the size of this map. Increasing the resolution of the heightmap therefore also increases the size of the terrain, unless you change the…well, the distance in worldspace between heightmap samples, basically. Not sure what variable that is, though.