Search Unity

terrain setHeights issue

Discussion in 'Scripting' started by guillaumeR, Apr 18, 2016.

  1. guillaumeR

    guillaumeR

    Joined:
    Apr 11, 2015
    Posts:
    12
    Hi everyone,

    I am trying to generate a terrain base on a height map. And I have an issue with the last "column" and last "row" of the map. (see the attached screenshot) some of the heights have a value of 0.

    I think I have offsetting every variable possible, hoping it would solve the problem, but for now nothing works ... Does anybody have an idea about what I am doing wrong ?
    Code (CSharp):
    1.     public void LoadHeightMap(string aFileName, TerrainData aTerrain)
    2.     {
    3.  
    4.         int res = aTerrain.heightmapResolution;
    5.         int h = aTerrain.heightmapResolution-1;
    6.         int w = aTerrain.heightmapResolution-1;
    7.  
    8.         Debug.Log(aTerrain.heightmapResolution + ":::::::::::::::::::::::::::::");
    9.         float[,] data = new float[w, h];
    10.         using (var file = System.IO.File.OpenRead(aFileName))
    11.         using (var reader = new System.IO.BinaryReader(file))
    12.         {
    13.             for (int y = 0; y < h; y++)
    14.             {
    15.                 for (int x = 0; x < h; x++)
    16.                 {
    17.  
    18.                     float v = (float)reader.ReadUInt16() / 0xFFFF;
    19.                     data[y, x] = v;
    20.                     //data[y, x] = (float)x / (float)h;
    21.  
    22.  
    23.  
    24.                 }
    25.                 progress = (float)y / (float)h;
    26.                 EditorUtility.DisplayProgressBar("Loading Height Map", "", progress);
    27.             }
    28.         }
    29.         aTerrain.SetHeights(0, 0, data);
    terrain_problem.PNG
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    My first guess would be because you set w and h to one less than resolution. I suppose that you do this because otherwise you're out of range in your data source. Suggest you reduce the height map resolution to that of your data.
     
    Kiwasi likes this.
  3. guillaumeR

    guillaumeR

    Joined:
    Apr 11, 2015
    Posts:
    12
    I don't know anything anymore ... :confused: I might have to start from scratch, but I am really afraid to hit the same wall ...
    I guess it would be really nice to have builtin function to load Height maps in unity ( i.e the one used in standard terrain script)
     
  4. mdowd

    mdowd

    Joined:
    Jun 14, 2015
    Posts:
    4
    You need to add one more row and column to your height map data, like this:

    float[,] data = new float[w+1, h+1];