HaLo2FrEeEk

PerlinNoiseGen

Jul 25th, 2021 (edited)
1,333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. $fn=3;
  2.  
  3. randomize = true;
  4. setseed = 123456;
  5. seed = randomize ? floor(rands(0, 1000000, 1)[0]) : setseed;
  6. echo(Seed = seed);
  7.  
  8. {
  9.   // Functions
  10.  
  11.   {
  12.     // Main noise generator
  13.     //
  14.     // b = base random array, don't set this
  15.     // a = recursion array, don't set this!
  16.     // no = number of octaves, you can set this, number of elements is no^2
  17.     // o = current octave iteration, don't set this
  18.     // seed = rands seed, you can set this
  19.     //
  20.     // Returns: Perlin noise array, length(no ^ 2)
  21.     function noise(b=[], a=[], no=4, o=0, seed=0) =
  22.       (o > no)
  23.       ? scalevec(2, a) // Done, return the array
  24.       : (len(b) == 0)
  25.         ? noise(rands(0, 1, no ^ 2, seed), a=[], no=no, o=o, seed=seed)
  26.         : (len(a) == 0)
  27.           ? noise(b=b, a=octave(o=0, b=b), no=no, o=o + 1, seed=seed)
  28.           : noise(b=b, a=addvec(a, octave(o=o, b=b)), no=no, o=o + 1, seed=seed);
  29.   }
  30.   // ^ Main noise generator
  31.  
  32.   {
  33.     // Octave generator
  34.     //
  35.     // o = octave
  36.     // b = base random array
  37.     //
  38.     // Returns: array of values from a single noise octave
  39.     function octave(o=0, b=[]) =
  40.       expand(c=len(b), a=[
  41.         for(i=[0 : len(b) / (2 ^ o) : len(b) - 1]) b[i]
  42.       ]);
  43.   }
  44.   // ^ Octave generator
  45.  
  46.   {
  47.     // Expand octave
  48.     //
  49.     // c = base array length
  50.     // a = octave array
  51.     //
  52.     // Returns: array of length=length(base) with interpolated octave values
  53.     function expand(c=0, a=[]) = [
  54.       for(i=[0 : c - 1])
  55.         (len(a) == 1)
  56.         ? a[0]
  57.         : (len(a) == c)
  58.           ? a[i] * (c / len(a) / c)
  59.           : (interp(v=[
  60.                         a[floor(i / (c / len(a)))],
  61.                         (floor(i / (c / len(a))) + 1 == len(a))
  62.                           ? a[0]
  63.                           : a[floor(i / (c / len(a))) + 1]],
  64.                     s=c / len(a))
  65.                     * (i % (c / len(a)))
  66.                     + a[floor(i / (c / len(a)))])
  67.                   * ((c / len(a)) / c)
  68.       ];
  69.   }
  70.   // ^ Expand octave
  71.  
  72.   {
  73.     // Helper functions
  74.     function interp(v=[], s=0) = (v[1] - v[0]) / s;
  75.    
  76.     function addvec(a=[], b=[]) = [
  77.       for(i=[0 : len(a) - 1])
  78.         a[i] + b[i]
  79.       ];
  80.      
  81.     function scalevec(d=2, v=[]) = [
  82.     for(i=[0 : len(v) - 1])
  83.       v[i] / d
  84.     ];
  85.  
  86.   }
  87.   // ^ Helper functions
  88. }
  89. // ^ Functions
  90.  
  91. // Example:
  92. //
  93. // no = 8 means we will get a noise array with 8 ^ 2 = 64 values (range 0-1)
  94. // seed = seed, this is set up with the variables above, you could set it manually. Defaults to 0 if not set
  95. a = noise(no=8, seed=seed);
  96.  
  97. // This displays the values.
  98. repeat = 1;
  99. for(i=[0 : len(a) - 1], x=[0 : repeat]) {
  100.   translate([i + len(a) * x, 0, 0]) cylinder(a[i] * 100, d=1);
  101. }
Add Comment
Please, Sign In to add comment