Programatically importing terrain heights vs Unity Import heightmap

Hi guys, I’m having a bit of an issue here and have spent days trying different things but nothing seems to work. I have done extensive goggling and nothing has come up. Maybe somebody has run into this problem and wants to share some light on this matter? Thanks in advance for your help.


Please note that the second terrain is exactly like the first with the exception of the first row and column.

For testing purposes, I tried to set the first row and column values before SetHeights to their neighbor value which is correct but the result is exactly the same.

I have tried the script posted at http://wiki.unity3d.com/index.php?title=TerrainImporter and the result is the same as my code.

private void GetHeights(){
	heights = new float[65536];
	for(int i=0; i<heights.Length; i++)
		heights[i] = i / 65536.0f;
}

...	Relevant Code
if(heights == null)
	GetHeights();

using (System.IO.FileStream f = System.IO.File.OpenRead(heighmapFile)) {
	using (System.IO.BinaryReader br = new System.IO.BinaryReader(f)) {
		res = Mathf.CeilToInt (Mathf.Sqrt (f.Length / 2));
		tdata.heightmapResolution = res + 1;
		tdata.size = new Vector3 (res, res / 3, res);
		tdata.baseMapResolution = res;
		tdata.SetDetailResolution (res, 8);
		float[,] heightData = new float[res,res];
		for (int x=0; x<res; x++) {
			for (int y=0; y<res; y++) {
				heightData [x, y] = heights[br.ReadUInt16()];
			}
		}
		tdata.SetHeights (0, 0, heightData);
	}
}

Problem Fixed… If you happen to come across this thread, then the issue is that if you’re importing a square power of two map, unity requires the heightmap resolution to be scale by 1. then you end up with an empty row and column that doesn’t have data represented in your heightmap. I don’t really know if the following code is what the unity heightmap importer does but at least it did fix the weird cliff at the last row and column

float[,] heightData = new float[tdata.heightmapResolution,tdata.heightmapResolution];
	for (x=0; x<res; x++) {
		for (y=0; y<res; y++) {
			heightData [x, y] = heights[br.ReadUInt16()];
		}
	}
	for(x=res; x<res+1;x++){
		for (y=0; y<res; y++) 
			heightData [x, y] = heightData [x-1, y];
	}
	for(x=0; x<res;x++){
		for (y=res; y<res+1; y++) 
			heightData [x, y] = heightData [x, y-1];
	}
	tdata.SetHeights (0, 0, heightData);
1 Like