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;
}
}