Terrain Tile Seams and Terrain.SetNeighbor


Via a Unity Editor script, I am loading a GIS elevation GRID via a header txt file and binary float file. I am reading the large data set, choosing the nearest power of 2 and filling a corner with the real data. Then I break the large data set in to 513x513 terrain chunks and create the Terrains and gameObjects.

I am getting LOD seams between the TerrainChunks but only after starting the simulation, just after creating the tiles in the editor and before pressing play I can see that the terrain.setNeighbor() is working correctly. But pressing play and going into game mode changes that and the seams can be seen, after stoping the game simulation the terrain chunks continue to not respect the neightbors LOD.

Relevant Code

for( int y = 0; y < numberOfChuncks; y++ )

for( int x = 0; x < numberOfChuncks; x++ )
Terrain left = null;
Terrain top = null;
Terrain right = null;
Terrain bottom = null;

// Terrain Tile layout

// (x,y)

// (0,2)(1,2)(2,2)

// (0,1)(1,1)(2,1)

// (0,0)(1,0)(2,0)


if( y == numberOfChuncks - 1 )
top = null;

top = terrainObject[x,y+1].GetComponent();


if( y == 0 )
bottom = null;

bottom = terrainObject[x,y-1].GetComponent();


if( x == 0 )
left = null;

left = terrainObject[x-1,y].GetComponent();


if( x == numberOfChuncks - 1 )
right = null;

right = terrainObject[x+1,y].GetComponent();


terrainObject[x,y].GetComponent().SetNeighbors( left, top, right, bottom );
terrainObject[x,y].GetComponent().heightmapPixelError = 2;


Do I need to update Terrain.setNeighbors per frame?
Is the fact that this is a Editor Scrip restricting the neighbors to only the editor window?


You certainly don’t need to call it every frame, but i’m not sure if the neighbor information is stored in the terrain asset. You might need to call setNeighbors in Start()


I just had a look into the Terrain script and, as i guessed, the neighbor-variables aren’t serialized:

    private Terrain m_LeftNeighbor;
    private Terrain m_RightNeighbor;
    private Terrain m_BottomNeighbor;
    private Terrain m_TopNeighbor;

So you need to setup the neighbors at runtime. One way is to store the terrain list in a custom script along with the terrains and have it’s Start method setting the neighbors.