- Home /
I've stitched my terrains at runtime, but have a wild vertex not playing ball! Any suggestions?
Hello all,
I've managed to devise a way to stitch the edges of my terrain grid at runtime, and after much trial and tribulation I've got it working nicely... except for one vertex, which will not align correctly.
Here's what I mean:
Now, before the terrain is stitched, the following happens:
Grid of terrain objects created and aligned with individual terrain data.
Each terrain object is added to the Tile class, where a grid coordinate is assigned with reference to the Terrain object, example (-1, 0, Terrain).
I then check the left and top neighbours of each, modify the current terrain data, apply it and then move on.
As the image shows, this works as expected except for the error. The code I'm doing this with can be seen below:
Terrain GetTerrainByCoord(float x, float z)
{
foreach (Tile t in gridData)
{
if (t.x == x && t.z == z)
{
return t.t;
}
}
return null;
}
float[] GetCoordByTerrain(Terrain o)
{
foreach (Tile t in gridData)
{
if (t.t.GetInstanceID() == o.GetInstanceID())
{
return new float[]{t.x, t.z};
}
}
return null;
}
IEnumerator StitchTerrainNeighbours()
{
for (int i = 0; i < transform.childCount; i++)
{
Terrain t = transform.GetChild(i).GetComponent<Terrain>();
float[] c = GetCoordByTerrain(t);
float[,] cHeights = t.terrainData.GetHeights(0, 0, t.terrainData.heightmapWidth, t.terrainData.heightmapHeight);
Terrain lNeighbour = GetTerrainByCoord(c[0] - 1, c[1]);
Terrain tNeighbour = GetTerrainByCoord(c[0], c[1] + 1);
if (lNeighbour != null)
{
float[,] lHeights = lNeighbour.terrainData.GetHeights(0, 0, lNeighbour.terrainData.heightmapWidth, lNeighbour.terrainData.heightmapHeight);
for (int y = 0; y < lNeighbour.terrainData.heightmapHeight; y++)
{
cHeights[y, 0] = lHeights[y, lNeighbour.terrainData.heightmapHeight - 1];
}
}
if (tNeighbour != null)
{
float[,] tHeights = tNeighbour.terrainData.GetHeights(0, 0, tNeighbour.terrainData.heightmapWidth, tNeighbour.terrainData.heightmapHeight);
for (int x = 0; x < tNeighbour.terrainData.heightmapWidth; x++)
{
cHeights[tNeighbour.terrainData.heightmapWidth - 1, x] = tHeights[0, x];
}
}
t.terrainData.SetHeights(0, 0, cHeights);
}
yield return null;
}
So, is there something wrong with my stitch function... can it be more efficient and have I missed a coordinate somewhere?
Thanks.
Your answer
Follow this Question
Related Questions
How do I use Terrain.SetNeighbors() to remove level of detail seams between multiple terrains? 5 Answers
stitch spheriphied terrain-like (6) planes (removing gaps from edges) 6 Answers
Custom terrain textures,seeing seams,how do I fix this? 2 Answers
Remove tile seams from single terrain 1 Answer
Yet Another Terrain Seams Question 2 Answers