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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Convert noise texture to terrain heightmap (solved)

Discussion in 'Scripting' started by Qualdan, Feb 13, 2023.

  1. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    I've been trying to convert a simple noise texture into terrain heightmap, but so far no luck.

    I've tried googling and searching this forum. I've found multiple tutorials for perlin noise based terrains and even some that show that noise as a texture, but none that convert an actual texture into a heightmap.

    I made an attempt with GetPixel but it's too imprecise for this as it creates a Minecraft looking terrain.

    Code (CSharp):
    1. void GenerateHeights(TerrainData data) {
    2.     float[,] heights = new float[data.heightmapResolution, data.heightmapResolution];
    3.     for (int x = 0; x < data.heightmapResolution; x++) {
    4.         for (int y = 0; y < data.heightmapResolution; y++) {
    5.             heights[x, y] = _noiseTexture.GetPixel(x, y).grayscale;
    6.         }
    7.     }
    8.     data.SetHeights(0, 0, heights);
    9. }
    Anyone have ideas?

    GeneratedTerrain.png NoiseTexture.png
     
  2. LethalGenes

    LethalGenes

    Joined:
    Jan 31, 2023
    Posts:
    69
    Good morning from England,

    my suggestion would be that you haven’t put much effort into distinguishing the height.
    You should be grabbing a height value from possibly the alpha or combined colour values ( the float of (R+G+B+A) / 4) and then setting the height suitably. Take maximum height for example; multiplied by that value. That value should be between 0 and 1.

    Let me know if that makes sense to you.
     
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    This is completely useless on a noisemap, as R = G = B (= A). So is
    .grayscale
    . Simply use 1 of the channels.

    You'll want to interpolate between the values (adding more vertices) or lower the extremes (min & max-yPosition)
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    If your terrain is very large or has a very low heightmap resolution, you'll wind up with obvious steps in the terrain.

    Note that using GetPixel isn't the problem here: you literally don't have enough heightmap resolution to make the terrain look smooth! By default, you get a 513x513 heightmap -- so if the terrain is 1000 by 1000 units in size, moving two meters only moves you one pixel over in the heightmap.

    As a demonstration, here's a single spot with a very high height:

    upload_2023-2-13_11-21-47.png

    Here's the result of trying to use the smooth tool. It's very blocky.

    upload_2023-2-13_11-21-11.png

    Same smoothing, but after cranking up the resolution to 2049x2049:

    upload_2023-2-13_11-22-1.png
     
  5. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    Thanks but like that other guy said, I get pretty much the same result with
    .greyscale
    (which is in itself an overkill and simply one channel is enough).
     
  6. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    Thanks for the answer but I'm not entirely sure I understand what you're going for with these. I know what interpolating is, but how would I "add more vertices"? And what are you referring to with "lower the extremes (min & max-yPosition)"?
     
  7. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    Thanks but I'm not sure that's the solution (unless I'm doing something wrong). I increased the heightmap to 2049 and noise map to 2048 but the end result still looked the same.

    GeneratedTerrain2.png
     
  8. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Add more vertices to your mesh and/or increase the resolution of your heightmap.
    And by extremes I mean the distance between the lowest & highest point.
    Your heightmap is 0-1, and you're multiplying these values with some sort of height-factor, no?
     
  9. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Oh, right, I forgot a very important thing -- your noise texture is probably stored with 8-bit R/G/B/A channels, right?

    That means you only get 256 possible heights!

    You'd need to use a format that gives you more detail, like one of the 16-bit ones. I'm not sure on the specifics, though.

    Alternatively, you could use the Mathematics package to generate the noise. cnoise or snoise would be appropriate -- I forget if they give you values in [-1, 1] or [0, 1], though...
     
  10. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    I tried increasing the resolution of the heightmap but as you can see from my other response, the result was pretty much identical. At this stage I'm just trying to get the pixel data, which comes out between 0-1. I'm not altering it anyway (yet).
     
  11. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    Changing the noise texture into 16 bits didn't seem to have an effect.

    Also, I could get noise from the Unity perlin function if I wanted to. The idea is to convert a previously made noise texture into heightmap, which seems to be somewhat difficult to achieve.
     
  12. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Is the texture an image you've imported into your project, rather than something you're generating on the fly? If so, changing the format wouldn't help -- you won't be squeezing any more detail out of the original image.
     
  13. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    It's a separate, imported image and for this test I specifically created a 16-bit image in Photoshop with Render/Clouds to simulate the noise.
     
  14. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    And you did set the texture format to R16, right? (In the import-settings for the texture in Unity)
     
  15. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    That I didn't do. Now it works. Thank you all for help.

    To recap: the problem was that the noise texture didn't have enough detail: it needed to be 16-bit image with R16 bit selected in Unity import settings.
     
    Last edited: Feb 13, 2023
    chemicalcrux and SF_FrankvHoof like this.
  16. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Bit of a tangent here, but wanted to share a few heightmaps and hopefully give some ideas for generating interesting terrains. Screenshots are for the Photoshop files and the resulting maps in Unity. 5 files allowed max, so missing one screenshot. Also no RAW files allowed, so you'll have to make 'em ;)

    The circular pyramid one was just a radial gradient and Posterize to reduce it to solid bands. These translate to the different levels in the map. The second one used Invert on half of that image. Super easy to create, and lots of variations possible.

    The cube one was generated with a small image file (40x40, equaling the number of cubes you want). Add Filter--Noise--Gaussian, Monochromatic, and set the level of general brightness you want for the squares. Then upscale to your full texture resolution using Nearest Neighbor, so the edges stay sharp.

    Hope that's not hijacking the thread too much and that it gives some ideas!
     

    Attached Files:

  17. Qualdan

    Qualdan

    Joined:
    Aug 5, 2011
    Posts:
    27
    I don't mind as the original issue got resolved already. Thank you for these.
     
    seejayjames likes this.
  18. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    You can always link to e.g. Imgur, or a file-upload site if you're hitting limitations here ;)
     
    seejayjames and Qualdan like this.
  19. damir2076

    damir2076

    Joined:
    Feb 13, 2018
    Posts:
    10
    Thank you for tip :)

    It took me hours to understand why the terrain is cubed like in minecraft after import.