terrain height manipulation

Hi,

I am trying to convert the shape of a mesh to the terrain object. First I tried exporting a greyscale map from my modelling program but found that the surface gets distorted to much. Using the smooth height tool results in loss of detail.

So I thought of manipulating the terrain height directly in unity using terrain.SetHeights and Physics.Raycast on the modeled surface.

The terrain and the modeled surface on top of eachother

for (var i : float = 0; i < terrain.heightmapHeight; i++) {
	for (var j : float = 0; j < terrain.heightmapWidth; j++) {
//for (i = 0; i < terrain.heightmapHeight; i++) {
//	for (j = 0; j < terrain.heightmapWidth; j++) {		
		var pos : Vector3 = Vector3(0,0,0);
		pos.x = (j / terrain.heightmapWidth) * terrain.size.x;
		pos.z = (i / terrain.heightmapHeight) * terrain.size.z;
				
		h = GetRaycast(pos);
		tData[i,j] = h;
				
		str += pos.x + " - " + pos.z + " - " + j + " - " + i + " - " + h + " - " + tData[i,j] + "\n";
	}
}
		 
terrain.SetHeights(0, 0, tData);	
Write (str);

The result of this code

The log file does show accurate information about all the scanned coordinates, the height of the modeled terrain at each position and the height stored in tData. Yet the height of most of the terrain is set to the maximum Terrain height and some parts it is set to 0.

Any ideas what goes wrong here?

Thanks,
Raoul

I don’t know for sure, but I’m guessing that terrain heights are an array of floats between 0 and 1, and the vertical size of the terrain is specified by TerrainData.size.y. Your code puts the raycast height directly into the array, and I think it’s getting clamped to 1 as that’s the maximum value allowed. I presume you need to scale your raycast height by the terrain height before putting it into the array.

Many thanks Neil, works well now!

Hi,
I’ve your same problem, could you please post the script to download?
Thanks in advance.

You just have to divide h by your terrain height as suggested by NCarter.