Search code examples
c#unity-game-engineterrainheightmap

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 problem. 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.

As I understand it (and I've read this too), the heightmapResolution shouldn't affect the size of the terrain.

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

        /// <summary>
        /// Gets the terrain data either from memory if its loaded, from disk if it isnt, or generates a new one of neither of those are available.
        /// </summary>
        /// <value>The terrain data.</value>
        public static TerrainData terrainData {
            get {
                TerrainData terrainData = new TerrainData();
                if(isTerrainLoaded){
                    //Debug.Log("terrainData is already loaded, returning it!");
                    return(loadedTerrainData);
                }
                else if(Resources.Load("terrainData") != null && !generateNewTerrain){
                    terrainData = Resources.Load("terrainData") as TerrainData;
                    Debug.Log("Read terrainData from disk!");
                }
                else{
                    Debug.Log("No terrainData found on disk, generating a new random one...");

                    terrainData.size = new Vector3(width, height - sandBaseHeight, length);
                    Debug.Log ("Original terrain size: " + terrainData.size);
                    terrainData.heightmapResolution = heightmapResolution;
                    Debug.Log ("Bigger terrain size: " + terrainData.size);
                    terrainData.baseMapResolution = baseMapResolution;

                    terrainData.SetDetailResolution((int)detailResolution.x, (int)detailResolution.y); //(int) because data is stored in a Vector2 which is float
                    terrainData.alphamapResolution = aplhaMapResolution;

                    float[,] heights = new float[terrainData.heightmapWidth,terrainData.heightmapHeight];
                    Vector2 randomLocation; //for perlin noise
                    randomLocation.x = Random.Range(-10000,10000);  //that is hopefully enough
                    randomLocation.y = Random.Range(-10000,10000);

                    for (int x = 0; x < terrainData.heightmapWidth; x++) {
                        for (int z = 0; z < terrainData.heightmapWidth; z++) {
                            heights[x,z] = Mathf.PerlinNoise(randomLocation.x + (float)x/scale, randomLocation.y + (float)z/scale);
                        }
                    }
                    terrainData.SetHeights (0, 0, heights);
                    terrainData.splatPrototypes = splatTextures;

                    AssetDatabase.CreateAsset(terrainData,"Assets/Resources/terrainData.asset");
                }
                loadedTerrainData = terrainData;
                return(terrainData);
            }
        }

So that is the bloc that generates my terrainData. The change in terrain size is already visible in the Debug.Log ("Bigger terrain size: " + terrainData.size); immediately after I assign the heightmapResolution.

Any ideas as to why this happens?


Solution

  • 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 somebody else too!