1. Frederic De Groef
  2. perlin-noise

Commits

Frederic De Groef  committed e49d2fa

added a bunch of noise-based texture generators

  • Participants
  • Parent commits 4088221
  • Branches default

Comments (0)

Files changed (3)

File src/demo_matplotlib.py

View file
 
 if __name__ == '__main__':
     w, h = 256,256
-    p, n = 1.0, 5
-    values = np.zeros((w, h))
+    n = 8
+    fractal_sum = np.zeros((w, h))
+    turbulence = np.zeros_like(fractal_sum)
+    ridges = np.zeros_like(fractal_sum)
+    noise_values = np.zeros_like(fractal_sum)
+
     for i in range(w-1):
         for j in range(h-1):
             x, y = float(i)/(w), float(j)/(h)
-            values[i, j] = noise.perlin_noise(x, y, p, n) 
-
-
+            #fractal_sum[i, j] = noise.make_fractal_sum_2D(x, y, n)
+            #turbulence[i, j] = noise.make_turbulence_2D(x, y, n)
+            #ridges[i, j] = noise.make_ridgedmf(x, y, n, 5.0)
+            noise_values[i, j] = noise.noise2D(i, j)
+        
+    values = noise_values
     #normalize & show 
     values/=values.max()
     plt.imshow(values, cmap=plt.cm.gray)

File src/noise.py

View file
     gradients2D.append((m.cos(i), m.sin(i)))
 
 
-
 def get_gradient2D(i, j):
     I, J = i & 0xff , j & 0xff
   
     return (t * ( t * ( t * (6*t**2 - 15*t + 10))))
 
 
-def noise(x, y):
+def noise2D(x, y):
     i, j = int(m.floor(x)), int(m.floor(y))
     g00 = get_gradient2D(i,   j)
     g01 = get_gradient2D(i,   j+1)
         freq = 2**i
         ampl = p**i
 
-        total += noise(x * freq, y * freq) * ampl
+        total += noise2D(x * freq, y * freq) * ampl
 
     return total
 
+def make_fractal_sum_2D(x, y, n_octaves, lacunarity=2.0, gain=0.5):
+    sum = 0.0
+    freq, ampl = 1.0, 0.5
+    for i in range(n_octaves):
+        sum += noise2D(x*freq, y*freq) * ampl
+        freq *= lacunarity  # 2.0**i
+        ampl *= gain         # 0.5**i
 
+    return sum
+
+
+def make_turbulence_2D(x, y, n_octaves, lacunarity=2.0, gain=0.5):
+    sum = 0.0
+    freq, ampl = 1.0, 0.5
+    for i in range(n_octaves):
+        sum += abs(noise2D(x*freq, y*freq)) * ampl
+        freq *= lacunarity  # 2.0**i
+        ampl *= gain         # 0.5**
+    return sum
+
+
+
+def make_ridgedmf(x, y, n_octaves,  lacunarity=2.0, gain=0.5, offset=1.0):
+    def ridge(h, offset):
+        h = abs(h)
+        h=offset - h
+        return h
+    sum = 0.0
+    freq, ampl = 1.0, 0.5
+    prev = 1.0
+    for i in range(n_octaves):
+        n = ridge(noise2D(x*freq, y*freq), offset)
+        sum += n*ampl*prev
+        prev = n
+        freq *= lacunarity  # 2.0**i
+        ampl *= gain         # 0.5**
+    return sum
+
+    
 def make_2D_array(w, h):
     array = []
     for i in range(h):
         array.append([0]*w)
     return array
 
-
-def make_perlin_noise(w, h, p, n):
+def make_perlin_noise(w, h, n):
     noise_values = make_2D_array(w, h) 
     for i in range(w-1):
         for j in range(h-1):
-            noise_values[i][j] = perlin_noise(float(i) / (w-1), float(j) / (h-1), p, n)
+            noise_values[i][j] = make_fractal_sum_2D(float(i) / (w-1), float(j) / (h-1), n)
             
     return noise_values
 
 
 
 
+
+
+
+

File src/noise3d.py

View file
+#gradients for 3d noise
+
+p = [51, 3, 190, 211, 218, 30, 27, 64, 65, 158, 42, 99, 78, 48,
+ 238, 245, 228, 243, 38, 98, 2, 115, 226, 210, 36, 87, 9, 101, 23, 63,
+ 40, 154, 34, 181, 20, 168, 104, 167, 144, 165, 28, 68, 19, 247, 14,
+ 171, 44, 80, 102, 222, 159, 135, 49, 84, 17, 95, 209, 117, 136, 235,
+ 237, 197, 206, 143, 217, 59, 246, 239, 5, 160, 43, 103, 176, 35, 45,
+ 72, 113, 26, 193, 172, 227, 57, 131, 153, 70, 123, 139, 156, 255, 75,
+ 12, 204, 97, 61, 1, 105, 91, 121, 108, 180, 134, 189, 169, 50, 185,
+ 71, 194, 253, 79, 252, 41, 215, 219, 93, 145, 127, 232, 90, 7, 73,
+ 83, 230, 201, 170, 60, 138, 100, 150, 220, 21, 147, 141, 16, 25, 110,
+ 195, 216, 24, 109, 250, 89, 88, 236, 233, 116, 202, 112, 130, 85, 52,
+ 107, 6, 163, 129, 125, 119, 157, 221, 186, 140, 114, 122, 207, 229,
+ 22, 18, 198, 39, 56, 86, 111, 46, 62, 8, 191, 224, 69, 249, 15, 67,
+ 132, 234, 173, 182, 124, 240, 251, 58, 244, 241, 96, 148, 199, 74,
+ 212, 94, 133, 126, 178, 254, 242, 231, 174, 76, 0, 55, 183, 29, 146,
+ 77, 208, 188, 213, 164, 161, 53, 200, 33, 203, 162, 11, 166, 179,
+ 187, 196, 248, 151, 92, 177, 32, 81, 82, 31, 155, 120, 10, 4, 223,
+ 184, 175, 37, 205, 225, 66, 128, 214, 13, 142, 118, 54, 137, 152,
+ 192, 47, 106, 149]
+
+
+gradients3D=[(1,1,0),
+    (-1,1,0),
+    (1,-1,0),
+    (-1,-1,0),
+    (1,0,1),
+    (-1,0,1),
+    (1,0,-1),
+    (-1,0,-1),
+    (0,1,1),
+    (0,-1,1),
+    (0,1,-1),
+    (0,-1,-1),
+    (1,1,0),
+    (0,-1,1),
+    (-1,1,0),
+    (0,-1,-1),
+]