- Home /
Question by
Icni · Oct 26, 2021 at 09:27 PM ·
terrainprocedural generationterraindataterrain seamless
Terrain Stitching
Hello,
I'm relatively new to procedural terrain generation, but right now I am trying to create a procedurally-generated world using multiple terrains, by using SetHeights
to map an array of floats onto the terrains. However, the terrains don't connect at the seams even though I tried to write a stitching algorithm (below). I would really appreciate some help as to what I'm doing wrong here. Thanks!
void SetNoiseTexture(float[,] noise)
{
Terrain[,] terrains = new Terrain[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
var chunk = GetChunk(noise, x * _resolution, y * _resolution, _resolution);
if (x > 0)
{
StitchLeft(chunk, GetChunk(noise, (x - 1) * _resolution, y * _resolution, _resolution), _resolution);
}
if (y > 0)
{
StitchBottom(chunk, GetChunk(noise, x * _resolution, (y - 1) * _resolution, _resolution), _resolution);
}
var t = Instantiate(terrain).GetComponent<Terrain>();
terrains[x, y] = t;
t.terrainData = Instantiate(t.terrainData);
t.gameObject.GetComponent<TerrainCollider>().terrainData = t.terrainData;
t.transform.position = new Vector3((x - width/2) * t.terrainData.size.x, 0, (y - height/2) * t.terrainData.size.z);
t.terrainData.SetHeights(0, 0, chunk);
}
}
}
float[,] GetChunk(float[,] array, int x, int y, int length)
{
float[,] ret = new float[length, length];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
ret[i, j] = array[i + x, j + y];
}
}
return ret;
}
void StitchLeft(float[,] chunk, float[,] neighbor, int resolution)
{
for (int i = 0; i < resolution; i++)
{
chunk[0, i] = neighbor[resolution - 1, i];
}
}
void StitchBottom(float[,] chunk, float[,] neighbor, int resolution)
{
for (int i = 0; i < resolution; i++)
{
chunk[i, 0] = neighbor[i, resolution - 1];
}
}
screen-shot-2021-10-26-at-45922-pm.png
(269.6 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613044641im_/https://answers.unity.com/themes/thub/images/avi.jpg)