Split Terrain with Height Interpolation

I was able to fix the blocky steps in the new heightmap from the SplitTerrain code by using the interpolated values in the original terrain instead of the integer values stored in the heightmap. It only took a year to figure this out. 😀

// Height
td.heightmapResolution = heightmapResolution;
float[,] newHeights = new float[heightmapResolution, heightmapResolution];
dimRatio1 = (xMax - xMin) / (heightmapResolution - 1);
dimRatio2 = (zMax - zMin) / (heightmapResolution - 1);
for (int i = 0; i < heightmapResolution; i++)
{
    for (int j = 0; j < heightmapResolution; j++)
    {
        // Divide by size.y because height is stored as percentage
        // Note this is [j, i] and not [i, j] (Why?!)
        newHeights[j, i] = origTerrain.SampleHeight(new Vector3(xMin + (i * dimRatio1), 0, zMin + (j * dimRatio2))) / origTerrain.terrainData.size.y;
    }
}
td.SetHeightsDelayLOD(0, 0, newHeights);

New code is at GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *