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. Dismiss Notice

Help with 1D Perlin Noise

Discussion in 'Scripting' started by Warrior1424, Oct 12, 2015.

  1. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    Today I began to delve into the world of Perlin Noise to simulate a wind effect.
    Code (JavaScript):
    1. var turb = Mathf.PerlinNoise(Time.time, 0.0);
    Sometimes, "turb" remains unchanged.
    For example, from 12 to about 14 seconds "turb" remains completely unchanged.
    This happens again at random points for random amounts of time, but always be the same every time I play the scene (ex: will always be unchanged between 12 and 14 seconds).

    What did I do wrong?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    PerlinNoise isn't a random change from value to value.

    Look at the image generated in the documentation:
    http://docs.unity3d.com/ScriptReference/Mathf.PerlinNoise.html



    You can see areas of the same shade over certain areas in some direction. When you sample across that region, you'll get that value.

    PerlinNoise is like a choppy sea. It's got a rythmic rise and fall in its values like waves in a choppy sea. But at times... that sea will have a section of it that shows odd calmness amongst the choppiness. That's what you hit.

    Note, PerlinNoise is deterministic. You'll get the same result for any given x,y coordinate. So every time you play it you'll get the same results if you continue down the x axis (which you are doing since you pass in 0 for y).

    You could get a random path every time by doing this:

    Code (csharp):
    1.  
    2. public class SomeScript : MonoBehaviour
    3. {
    4.  
    5.     private Vector2 _dir;
    6.  
    7.     void Start()
    8.     {
    9.         var a = Random.value * Mathf.PI * 2f;
    10.         _dir = new Vector2(Mathf.Cos(a), Mathf.Sin(a));
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         var v = _dir * Time.time;
    16.         float turb = Mathf.PerlinNoise(v.x, v.y);
    17.     }
    18.  
    19. }
    20.  
    You can get more variance with a random start point.
    And you could also get faster or slower chop by including a scale value.

    Code (csharp):
    1.  
    2. var v = _start + _dir * Time.time * _scale;
    3. float turb = Mathf.PerlinNoise(v.x, v.y);
    4.  
     
    Last edited: Oct 12, 2015
    hippocoder likes this.
  3. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    Or you can sum several perlin noises with different param scale and get more turbulent noise:

    Like:
    Code (csharp):
    1.  
    2. var turb = Mathf.PerlinNoise(Time.time,0.0) + Mathf.PerlinNoise(0.5 * Time.time,0.0) + Mathf.PerlinNoise(0.25 * Time.time,0.0) + ...;
    3.  
    http://paulbourke.net/texture_colour/perlin/

    Then it shouldn't be calm anymore
     
  4. Xonatron

    Xonatron

    Joined:
    Jan 14, 2013
    Posts:
    31
    You should also change the amplitude when you change the frequency, for proper spectral synthesis. The higher the frequency, the lower the amplitude.