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

3D Perlin Noise in progress (terrain generation, etc.)

Discussion in 'Made With Unity' started by •BTM, Mar 26, 2011.

  1. •BTM

    •BTM

    Joined:
    Jun 6, 2010
    Posts:
    108
    [26/03/11] : V. 0.01

    View attachment $TerrainGen3.js

    Small update; Faster, removed the yields in the script as well, although it stops unity while the script is thinking. For a terrain using default settings (calculating 131,072 points), it should be created in under 2 minutes; still very slow, between 1000 1200 points per second.

    I found some extra time these past couple of days, and decided I'd play with Unity for a bit. I figured that I'd come up with a procedural terrain system allowing overhangs, tunnels, etc.
    I started last night, and got some of it working this morning. I began by looking up perlin noise, and making my own implementation of 3d perlin noise (or something similar) using the now-accessible multi-dimensional builtin arrays.

    I have it working, but it is incredibly slow, as it always seems to be with codes in the first little while after writing them. Also, I'm making the terrain out of cubes, which means that it resembles minecraft a lot (hopefully I can change this by using something similar to marching cubes, and then smoothing).

    It does work very similarly to minecraft, in that they both use 3d perlin noise. I also decided to generate terrain in blocks, because it seems to be the only feasible method to generate terrain in real time and save it (I'm still not there yet).

    Well, I'm posting it in this early stage because I figure it could be a useful start for others who might know a lot more about this type of thing than me. I'm also not sure how much time I'll have in the next little while to improve it.

    More notes can be found in the script itself.

    $Picture 4.png
    Some blocky terrain generated by the script (cubes are prefabs)

    $Picture 2.png
    Overdraw view of perlin noise (not terrain, see notes in script)

    Settings:

    Terrain Blocks : The amount of blocks in the x y directions the script creates. (4 would be a 4x4 grid of terrain blocks)

    Cube : A cube prefab goes here.

    Detail Size : The distance between integral areas, or at least that's how I like to describe it. Basically, the size of the noise. (since it's not limited to a terrain size)

    Persistence : How noisy the terrain will be. (persistence in Perlin noise)

    Octaves :Amount of iterations it goes through, in which size and persistence change each iteration. (octaves in Perlin noise)

    Terrain Size : The length and width of a terrain block; the total size of the terrain is (Terrain Size)^2*(Terrain Blocks)^2.

    Terrain Height : The maximum height of the terrain (average height is half of this)

    Seeded : The random terrain seed. The same seed will always generate the same terrain.
     
    Last edited: Mar 27, 2011
  2. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,663
    My first though when i opened this thread was: Minecraft :D
    Nice work!
     
  3. cupsster

    cupsster

    Joined:
    Apr 14, 2009
    Posts:
    363
    If you want your noise function be quicker google little about perlin noise.. There are newer implementations that were developed by Perlin himself that solve some problems with speed. Can't remember how it's named but utilizes different approach in computation and it's faster and more lightweight.
     
  4. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Nice Work...

    Shhh! Don't let notch know your making a unity version of M I N E C R A F T!
     
  5. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Extremely Interested, been mucking around with similar myself recently.
     
  6. Quickfingers

    Quickfingers

    Joined:
    Aug 29, 2009
    Posts:
    268
  7. cupsster

    cupsster

    Joined:
    Apr 14, 2009
    Posts:
    363
    that's the one I was unable to remember... :D thanx quickfingers
     
  8. DavidDebnar

    DavidDebnar

    Joined:
    Mar 19, 2011
    Posts:
    115
    Dude.. I was just checking out your fracture script and then I found this link in your signature. Your awesome :). And this script is in JavaScript...I have to thank you :).
     
  9. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    There are some common misconceptions about Perlin noise:

    Perlin noise, does not have octaves. It gives you just one single octave.

    A very common use of Perlin noise is to sample perlin noise at multiple frequencies (octaves), and add the results together at different scales. This is called fbm or Fractional Brownian motion. When you do this, instead giant smooth hills, you get giant hills with smaller features. This is a very common way to create realistic heightmaps.

    Perlin noise is a gradient based noise. What you are doing is called a value based noise, which has nothing to do with Perlin. The difference is: gradient noises calculate a gradient at each discrete point, values at the discrete points are always 0, and values between discrete points are obtained by projecting the gradients. On value noises, you calculate a value at each discrete point and interpolate the values for points between them.

    What you have implemented is a fbm of value based noises. Very similar to what is described here:
    http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

    The author of that has the exact same confusion. He calls his fbm of value based noises "Perlin noise", so my guess is that is what you based your code on.

    If you are after speed, I recommend you implement simplex noise, as described here:
    http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf

    That article also has a good explanation of what perlin noise really is.

    It is much harder to understand, but it just works faster and has higher quality. Also, allows you to calculate the derivative, which means that in your heightmap, you can calculate whether you are on a slope or not and how steep it is. You can then create an fbm (multiple octaves) based on simplex noise for interesting heightmaps, or even caves.

    If you wish to get some numbers: I calculate 7 million blocks calling 3d simplex noise 3 times and 2d simplex noise 4 times in 4.2 seconds . In order to create this:


    This video is outdated, as my terrain is much better nowadays, but it gives you an idea.
     
  10. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    Another good article that hopefully clarifies things is:

    http://developer.amd.com/assets/Tatarchuk-Noise(GDC07-D3D_Day).pdf

    Page 16 shows you a simple value noise
    Page 17 shows you an interpolated value noise. This is essentially what you have in your function "InterpolatedNoise"
    Page 23 shows a gradient noise
    Page 24 and 25 shows a gradient noise with interpolation
    Page 36 shows simplex noise
    Page 42 shows fBM. This is equivalent to what you implemented.

    Here is an implementation of simplex noise with derivative, that you can easily translate to C#:
    http://mines.lumpylumpy.com/Electro...pp/Graphics/Bitmap/Textures/Noise/Simplex.php
     
  11. Daxiongmao87

    Daxiongmao87

    Joined:
    Jan 7, 2010
    Posts:
    148
  12. DavidDebnar

    DavidDebnar

    Joined:
    Mar 19, 2011
    Posts:
    115
    I like the article you posted.. I will look at it further a bit later but I like the cave generating method. Can't believe that I haven't came up with it sooner :eek: . I will try to rewrite your code so it doesn't instantiate cubes but that it makes the mesh procedurally instead if I have time...I so hate time :).
     
  13. Daxiongmao87

    Daxiongmao87

    Joined:
    Jan 7, 2010
    Posts:
    148
    Glad it gave you some insight. It is not my post, I just happened to stumble upon it a while back and bookmarked it :)
     
  14. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
  15. windexglow

    windexglow

    Joined:
    Jun 18, 2010
    Posts:
    378
  16. _zeta

    _zeta

    Joined:
    Dec 24, 2011
    Posts:
    99
    i am wondering is it still alive cause none posted in long time and
    also what about Procedural part of process? is it possible to make it?
     
  17. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    949
    Marching cubes! DO IT! DO IT NAO!
     
  18. Vino178

    Vino178

    Joined:
    Jul 17, 2010
    Posts:
    18
    Sorry for bringing this back up after three weeks, But I was wondering if I could get some tips or suggestions on having this generate in a hollow manner.

    As in only the sides and the top of the terrain are created and none of the cubes on the inside are instantiated.