Terrain Heightmap issues

I’m creating a tiled/chunk based terrain system. Instead of generating multiple terrain heightmaps, and attempting to stitch them together, I’m generating one large heightmap that is broken into pieces (one for each terrain piece).

My problem is that the heightmap isn’t being split into the chunks correctly, I get a result like this:

alt text

It looks like the 5 tiles aren’t getting their chunk of the heightmap correctly. I’ve been working on this for a few hours, and haven’t figured it out, so I’m ready for some help.

Here is the relative function:

void generateTerrainHeights ()
	{
            Vector2 tileSize = new Vector2(3,3);
            int TerrainSize = 5000;

		//Uses the terrain toolkit to generate the perlin heightmap -- WORKS
		if(!gameObject.GetComponent<TerrainToolkit>())
			gameObject.AddComponent<TerrainToolkit>();
		TerrainToolkit tk = gameObject.GetComponent<TerrainToolkit>();

		//Array to hold the generated heightmap
		float[,] heightmap = new float[(int)tileSize.x *TerrainSize , (int)tileSize.y *TerrainSize];
		//Generates the heightmap
		TerrainToolkit.GeneratorProgressDelegate generatorProgressDelegate = new TerrainToolkit.GeneratorProgressDelegate(tk.dummyGeneratorProgress);
		heightmap = tk.generatePerlin(heightmap, new Vector2(tileSize.x *TerrainSize , tileSize.y *TerrainSize), generatorProgressDelegate);

		//Loops over the tiles/chunks of terrain
		for (int x = 0; x < tileSize.x; ++x) {
			for (int y = 0; y < tileSize.y; ++y) {
				//Holds the 5000 by 5000 chunk of heights out of the perlin array
				float[,] localHeights = new float[TerrainSize,TerrainSize];
				//loops over the 5000x5000 section of the perlin heightmap to use for this chunk
				for (int hx = 0; hx < TerrainSize; ++hx) {
					for (int hy = 0; hy < TerrainSize; ++hy) {
						//Sets the chunk height to chunk out of the perlin array
						localHeights[hx, hy] = heightmap[hx*x, hy*y];

					}
				}
				Debug.Log ("tile " + x +"x " + y + " height array size: " + localHeights.Length);
				//Sets the terrain piece to the heights
				TerrainTiles[x,y].GetComponent<Terrain>().terrainData.SetHeights(0,0,localHeights);
			}
		}
	}

I believe the issue is around where it splits the heightmap into the relative chunk for the terrain.

Edit

Updated code:

void generateTerrainHeights ()
     {
             Vector2 tileSize = new Vector2(3,3);
             int TerrainSize = 5000;

         //Uses the terrain toolkit to generate the perlin heightmap -- WORKS
         if(!gameObject.GetComponent<TerrainToolkit>())
             gameObject.AddComponent<TerrainToolkit>();
         TerrainToolkit tk = gameObject.GetComponent<TerrainToolkit>();
 
         //Array to hold the generated heightmap
         float[,] heightmap = new float[(int)tileSize.x *TerrainSize , (int)tileSize.y *TerrainSize];
         //Generates the heightmap
         TerrainToolkit.GeneratorProgressDelegate generatorProgressDelegate = new TerrainToolkit.GeneratorProgressDelegate(tk.dummyGeneratorProgress);
         heightmap = tk.generatePerlin(heightmap, new Vector2(tileSize.x *TerrainSize , tileSize.y *TerrainSize), generatorProgressDelegate);
 
         //Loops over the tiles/chunks of terrain
         for (int x = 0; x < tileSize.x; ++x) {
             for (int y = 0; y < tileSize.y; ++y) {
                 //Holds the 5000 by 5000 chunk of heights out of the perlin array
                 float[,] localHeights = new float[TerrainSize,TerrainSize];
                 //loops over the 5000x5000 section of the perlin heightmap to use for this chunk
                 for (int hx = 0; hx < TerrainSize; ++hx) {
                     for (int hy = 0; hy < TerrainSize; ++hy) {
                         //Sets the chunk height to chunk out of the perlin array
                         localHeights[hx, hy] = heightmap[hx + TerrainSize * x, hy + TerrainSize * y];
 
                     }
                 }
                 Debug.Log ("tile " + x +"x " + y + " height array size: " + localHeights.Length);
                 //Sets the terrain piece to the heights
                 TerrainTiles[x,y].GetComponent<Terrain>().terrainData.SetHeights(0,0,localHeights);
             }
         }
     }

One serious fault. heightmap[hxx,hyy] will not get you local height map, instead you will get values that are multiples of x and y respectively.

For instance, you may want to make a height map for 1-0 terrain, x would be 1, y would be 0.

Now, consider this, as hx and hy are incremented over loop, hxx would be hx while hyy would be 0.