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

Can anyone please explain me perlin noise

Discussion in 'Scripting' started by Yash987654321, Apr 17, 2016.

  1. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    I have been banging my head for 4 days but I can't seem to understand it. The magician named Google has failed to provide links that I can understand (Or my brain has failed to compile it). I already have a crappy terrain class (Or a plane mesh generator to be right :p). I tried many ways to generate heights and hills and valleys but nothing giving acceptable effect. So can anyone please explain it as you would explain it to a kid who is very bad at maths. Thanks :D
     
    BrandyStarbrite likes this.
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    are you having issues specifically with perlin noise, or are you more interested in terrain generation?

    I've always from sebastian's tutorials well explained
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  4. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    I have a slow internet so I'll try to see that video but it would be great if you can provide a text link. BTW I am really interested in terrain generation but learning Perlin noise too won't harm ;)
     
  5. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    Can you explain scale factor please?
     
  6. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    Edit: whoops, not intended as an explanation of scale. I just wrote all this, before I saw the other replies

    The rough idea behind Perlin noise (https://en.wikipedia.org/wiki/Perlin_noise.) is to get a random signal that has a predictable "scale" of the features. That way you would be able to control whether the noise is supposed to have distances like mountains or distances like small rubble.

    Let's say you want to make some noise for mountains. A simple thing to try would be to make a 2D grid, where the distances between points on the grid are about mountain-sized. At every point on the grid you then generate a random height. And in between you interpolate between the grid points.
    This would look kind of boring, since the highest and lowest points would always be exactly at the grid points.

    Perlin noise is a little smarter, since what it does is pick a random direction at each grid point. The direction points to the direction, that grid point thinks the top of the local mountain should be. And when that is combined with the opinion of the neighboring grid points, a more interesting pattern appears.

    However, the grid is still somewhat visible in Perlin noise, which is why some people use https://en.wikipedia.org/wiki/Simplex_noise or https://en.wikipedia.org/wiki/OpenSimplex_noise

    To get Perlin noise to look better, you usually have to add several layers of it on top of itself. So maybe you start with something at distance 1 km per grid point to get some basic mountains, then some at say 700 m distance to subdivide that a bit, then some at 300m, some at say, 17m, and some at 0.5 meters.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I use Perlin something like this.

    Code (CSharp):
    1. heights [x,y] = maxHeight * Mathf.PerlinNoise ((x + seed[0]) * scale, (y + seed[0]) * scale)
    2.           + 0.5f * maxHeight * Mathf.PerlinNoise ((x + seed[1]) * 0.5f * scale, (y + seed[1]) * 0.5f * scale)
    3.           ...
    Each layer of Perlin noise gets smaller in magnitude and scale. Experiment with different values for scal and magnitude until you get it right.

    It's worth noting a couple of points
    • Perlin noise is obviously recognisable in terrain shapes
    • Perlin noise is symmetrical around zero on both axes. So make sure your seeds are high enough to avoid crossing zero. It makes for a very distinct ugly seam in the terrain.
     
    INDAX likes this.
  8. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    Unfortunately I am having no luck. Can't provide a pic right now but It is all generating the same mount all again and again. Do a different note, I am trying to generate a terrain for an RTS game (terrain like Rise of nations) so correct me if I an using the wrong algorithm.
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    It will always be the same every time, that's one of the main useful aspects of perlin noise
    You don't want the world to change every time you load a saved game
     
    Kiwasi likes this.
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's what the seeds were for in my equation. Why not share code?
     
  11. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    sorry but i meant it generate just same hill again and again (On different positions on terrain). I am sure I am doing something wrong. Will post the script when I'll use my PC