Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Please Help! Inconsistent Heightmap behaviour when mapping image

Discussion in 'Scripting' started by toxictoddler, Jun 18, 2020.

  1. toxictoddler

    toxictoddler

    Joined:
    Feb 1, 2017
    Posts:
    3
    I am trying to load real world terrain data onto unity terrains and I am encountering recurring set backs. I tracked it down to the images not being loaded as I would expect.
    To show the issue I use a real image and a simple test image and apply them via the same simple test function.



    Code (cssharp):
    1.  
    2. float[,] GetHeightmapFromTexture(TerrainData m_terrainData, Texture2D tex)
    3. {
    4.     var resolution = m_terrainData.heightmapResolution;
    5.     float[,] heights = new float[resolution,
    6.         resolution];
    7.    
    8.     for (int i = 0; i < resolution; i++)
    9.     {
    10.         for (int j = 0; j < resolution; j++)
    11.         {
    12.             var pixel = tex.GetPixel(i,j);
    13.            
    14.             heights[i,j] = pixel.grayscale;
    15.         }
    16.     }
    17.  


    The simple test image turns out as I would expect it to, but the complex terrain scan is rotated by 90° clockwise and flipped along the x axis. I have no idea why.

    The worldmap was loaded from the internet as RGB32 png. Heights decoded from it and stored. Then a new texture2d was written with the greyscale heightmap information.
    The testimage is a simple texture2D black with pixels x&y <=100 in white.

    Please help. I am frustrated with this.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    You might have to rotate the data by 90 degrees when you copy it. Unfortunately there is no standard way images or anything stores 2D grid data.

    What I like to do is make a test image that has:

    Code (csharp):
    1.  A B
    2.  C D
    In a grid, black and white in your case, and read that. Then modify your pixel copy code until it reads correctly.

    Easiest way to remap is to have two additional coordinate variables between the get pixel and set pixel:

    Code (csharp):
    1. int i2 = i;
    2. int j2 = j;
    And then do things to those variables, swap them, invert them, etc., and use them for where you write the data to the heights array.
     
  3. toxictoddler

    toxictoddler

    Joined:
    Feb 1, 2017
    Posts:
    3
    What I do not understand is the following: I have written this texture2D myself from an array of integers, so it should store the data the same way the testimage does. It displays properly rotated in unity aswell. Only when I read it it gets weird.
    If it were only that one image it would not be a problem but unfortunately I need to atleast understand what is going on to adjust the program around it.