Holes in tiled terrain with matched edges

My terrain is tiled, and each heightmap pixel on one edge is set to the height of the matching pixel on the other edge. However, there are holes in the resultant image where the edges meet. Shouldn’t it be seamless because each edge meets exactly?

	for(int i=0; i<HMRES; i++){
		fheights[i,256] = fheights[i,0];
		fheights[256,i] = fheights[0,i];
	}
	fheights[256,256]=fheights[0,0];
	Terrain.activeTerrain.terrainData.SetHeights(0,0,fheights);
    Terrain.activeTerrain.SetNeighbors(Terrain.activeTerrain,Terrain.activeTerrain,Terrain.activeTerrain,Terrain.activeTerrain);

Original:

Terrain example

Furthermore, edges of the terrain are unusually illuminated (because shadows aren’t computed for the adjacent edge?). Is there any way to render the shadow correctly as well?

After adding SetNeighbors (retextured to show better):

Terrain example 2

You need to use Terrain.SetNeighbours to fill those holes, it sorts out the LOD between two terrains which is the cause of your problem. The exact use of SetNeighbours has already been explained elsewhere on this forum if you get stuck.

http://unity3d.com/support/documentation/ScriptReference/Terrain.SetNeighbors.html