What would cause this "cliff face" on a programmatically create Terrain Tile?

I think a picture is worth a thousand words:

I have a heightmap array of the usual 0-1 noises. The array size is 1024x1024.
I made my terrain be 1024 in size, and - using the following code - load the noise array into a new terrain.
And I get a ‘right edge’ and ‘top edge’ “wall” as the mesh goes from [height] to zero.

I am an utter noob when it comes to, well, everything … but especially terrain coding. I’m slowly and yet surely trying out values, seeing what they do etc, which is why there are some pointless things in here or commented out code.

That said, I am unclear if a noise array needs to be 1 larger than a terrain tile, or visa-versa, or if there is a bad value for resolution or something in here.

So … for a terrain tile 1024x1024 … what size noise array should I have? I assumed 1024x1024.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapTile_CreateTerrain : MonoBehaviour
{

    /// <summary>
    /// Creates a terrain from heights.
    /// </summary>
    /// <param name="heightPercents">Terrain height percentages ranging from 0 to 1.</param>
    /// <param name="maxHeight">The maximum height of the terrain, corresponding to a height percentage of 1.</param>
    /// <returns>A terrain GameObject.</returns>
    //public static GameObject CreateTerrain(float[,] heightPercents, float maxHeight, float heightSampleDistance, SplatPrototype[] splatPrototypes, float[,,] alphaMap, Vector3 position)
    public static GameObject CreateTerrain(Vector3 pos, float[,] heightMap, float maxHeight)
    {
        //var terrainData = CreateTerrainData(heightPercents, maxHeight, heightSampleDistance, splatPrototypes, alphaMap);
        var terrainData = CreateTerrainData(heightMap, maxHeight);

        return CreateTerrainFromTerrainData(pos, terrainData);
    }


    /// <summary>
    /// Creates terrain data from heights.
    /// </summary>
    /// <param name="heightPercents">Terrain height percentages ranging from 0 to 1.</param>
    /// <param name="maxHeight">The maximum height of the terrain, corresponding to a height percentage of 1.</param>
    /// <returns>A TerrainData instance.</returns>
    public static TerrainData CreateTerrainData(float[,] heightMap, float maxHeight)
    {
        // Create the TerrainData.
        var terrainData = new TerrainData();
        terrainData.heightmapResolution = heightMap.GetLength(0)+1;
        var terrainWidth = heightMap.GetLength(0);
        terrainData.size = new Vector3(terrainWidth, maxHeight, terrainWidth);
        terrainData.SetHeights(0, 0, heightMap);

        return terrainData;
    }


    public static GameObject CreateTerrainFromTerrainData(Vector3 position, TerrainData terrainData)
    {
        // Create the terrain game object.
        GameObject terrainObject = new GameObject("terrain"+ Random.Range(-10.0f, 10.0f));
        var terrain = terrainObject.AddComponent<Terrain>();

        terrain.terrainData = terrainData;
        terrainObject.AddComponent<TerrainCollider>().terrainData = terrainData;
        terrain.groupingID = 1;
        terrain.allowAutoConnect = true;
        terrain.drawHeightmap = true;
        terrain.heightmapPixelError = 5;
        terrain.basemapDistance = 1025;
        terrain.terrainData.SetDetailResolution(1025, 32);
        //terrain.shadowCastingMode = "TwoSided";
        terrain.drawTreesAndFoliage = true;
       
        terrainObject.transform.position = position;

        return terrainObject;
    }
}

I’m 80% sure it’s the heighmap array. It’s not documented (from what I can see), but the array needs to be 1 larger than the terrain. Someone on SO mentioned “When importing raw heightmaps, they are required to have dimensions that satisfy 2^n + 1” … so I am guessing that my “1024” is not going to cut it, and the terrain tile is “filling in the blank” … and defaulting that to zero height.

Going back into the code to adjust the source heightmap array.

1 Like