Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

when I import raw in terrain, it likes stairs, not smooth.

Discussion in 'General Discussion' started by yumyrq11920, Jun 16, 2017.

  1. yumyrq11920

    yumyrq11920

    Joined:
    Jun 16, 2017
    Posts:
    6
    when I import raw in unity terrain, it is like the picture1

    http://www.jianshu.com/p/1d0fe567c9c8

    the format of picture is like picture2 I really don't know why

    please help me thanks!!
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    11,708
    For smoother terrain you need to use 16bit grayscale images. See:
    https://docs.unity3d.com/Manual/terrain-Height.html
     
  3. yumyrq11920

    yumyrq11920

    Joined:
    Jun 16, 2017
    Posts:
    6

    first, thank you very much, but it didn't work, because I have already been bit16

    to confirm for you, I import raw into unity terrain, and it shows like picture3, the picture3's url is http://www.jianshu.com/p/1d0fe567c9c8

    because my computer is Mac pro, so the byte order I choose mac,

    finally it is like picture1
     

    Attached Files:

  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    11,708
    If you first paint terrain in 8 bit grayscale mode (8 位) and then switch to 16 bit grayscale, the terrain will retain "ladder-like structure" when you import it into unity, even though it is 16 bit. That's because when you convert color from 8 bit to 16 bit, conversion does not automatically create smooth gradient.

    Basically... in 8bit mode you have value of 0..255 per channel. And in 16 bit mode you have value of 0..65535 per channel. When you convert from 8bit grayscale to 16 bit grayscale, values 0, 1, 2, (8 bit) turn into 0, 256, 512... (16 bit). Which creates ladder-like steps.

    To avoid the problem you'll need to paint the terrain in grayscale mode since the beginning.

    If you're trying to convert existing 8bit grayscale terrain into 16bit grayscale and can't just paint it over again... you could try blurring it slightly with a low radius gaussian blur after you've converted it into 16big grayscale color. This will smooth the steps somewhat (but may destroy terrain details).

    i'm afraid I can't give advice specific to mac pro, because I use different OS.
     
    frosted likes this.
  5. yumyrq11920

    yumyrq11920

    Joined:
    Jun 16, 2017
    Posts:
    6

    thank you so much for helping me.

    the file I upload is the original file data screenshot, including file size and format

    As you can see, it is tif format and 33M, is it 8bit, so no matter how I operate, it is still ladder-like?
     

    Attached Files:

  6. Farelle

    Farelle

    Joined:
    Feb 20, 2015
    Posts:
    504
    If you used any image editing program like photoshop or similar and used layers, you need to merge all layers(flatten in photoshop) before you export/save it as raw file. I experienced some issues with that before, that some data from the layers seems to still get saved somehow.
    (you don't need to save the tif with merged layers, you can revert the merging after saving it as raw)
    If that's not your problem, it would be helpful to tell more about how you created the map, which programs you used and how you saved it as raw file etc.
     
  7. yumyrq11920

    yumyrq11920

    Joined:
    Jun 16, 2017
    Posts:
    6
    thank you very much

    I want to ask that when unity import raw, is it read the int16 format, rather than float format?

    if it read float format, should it won't like this stairs?

    and how to read float format?
     
  8. yumyrq11920

    yumyrq11920

    Joined:
    Jun 16, 2017
    Posts:
    6

    thank you very much

    I want to ask that when unity import raw, is it read the int16 format, rather than float format?

    if it read float format, should it won't like this stairs?

    and how to read float format?
     
  9. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    11,708
    I'm uncertain, but 16bit grayscale usually refers to 16 bit int, because float takes 32bit to store.

    To read terrain floats by yourself you may have to implement your own reader. Apparently terrain data can be modified using GetHeights/SetHeights methods on TerrainData object, as specified here:
    https://docs.unity3d.com/ScriptReference/TerrainData.SetHeights.html
     
  10. MichaelEGA

    MichaelEGA

    Joined:
    Oct 11, 2019
    Posts:
    11
    Last edited: Aug 7, 2022 at 1:37 AM
  11. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    11,708
  12. MichaelEGA

    MichaelEGA

    Joined:
    Oct 11, 2019
    Posts:
    11
    Hey thanks for the reply, that would be a problem, yes, but I'm not using 8 bit images, I'm using 16bit grayscale images.

    Setting the image to "RGB 16bit" and reading the "Grayscale" using unity's terrain tools... causes stepping... in 2021.2.13f1... I presume this is some kind of bug.



    Setting the image to "R 16 bit" and reading of the "R" value instead of the "Grayscale" using the custom script below... gives the correct result



    Code (CSharp):
    1. public class TerrainGenerator : MonoBehaviour
    2. {
    3.     public Texture2D heightMap;
    4.     public float depth;
    5.     public float width;
    6.     public float length;
    7.     private bool terrainGenerating;
    8.  
    9.     public void Update()
    10.     {
    11.         if (ControllerInput.NKey() & terrainGenerating == false)
    12.         {
    13.             StartCoroutine(GenerateTerrain());
    14.         }
    15.     }
    16.  
    17.     public IEnumerator GenerateTerrain()
    18.     {
    19.  
    20.         terrainGenerating = true;
    21.  
    22.         Terrain terrain = GetComponent<Terrain>();
    23.         TerrainData tData = terrain.terrainData;
    24.  
    25.         terrain.Flush();
    26.  
    27.         tData.heightmapResolution = heightMap.width;
    28.  
    29.         tData.size = new Vector3(heightMap.width, depth, heightMap.height);
    30.  
    31.         float[,] heights = new float[heightMap.width, heightMap.height];
    32.  
    33.         for (int y = 0; y < heightMap.width; y++)
    34.         {
    35.             for (int x = 0; x < heightMap.width; x++)
    36.             {
    37.                 heights[x, y] = heightMap.GetPixel(x, y).r;
    38.             }
    39.         }
    40.    
    41.         tData.SetHeights(0, 0, heights);
    42.  
    43.         tData.size = new Vector3(width, depth, length);
    44.  
    45.         terrainGenerating = false;
    46.  
    47.         yield return null;
    48.     }
    49.  
    50. }
     
unityunity