|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +# The noise2() was ported from https://github.com/zbenjamin/vec_noise |
| 4 | + |
| 5 | +# https://github.com/zbenjamin/vec_noise/blob/master/_noise.h#L13 |
| 6 | +GRAD3 = np.array([ |
| 7 | + [1,1,0], [-1,1,0], [1,-1,0], [-1,-1,0], |
| 8 | + [1,0,1], [-1,0,1], [1,0,-1], [-1,0,-1], |
| 9 | + [0,1,1], [0,-1,1], [0,1,-1], [0,-1,-1], |
| 10 | + [1,0,-1], [-1,0,-1], [0,-1,1], [0,1,1] |
| 11 | +]) |
| 12 | + |
| 13 | +# https://github.com/zbenjamin/vec_noise/blob/master/_noise.h#L31 |
| 14 | +PERM = np.array([ |
| 15 | + 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, |
| 16 | + 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, |
| 17 | + 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, |
| 18 | + 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, |
| 19 | + 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, |
| 20 | + 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, |
| 21 | + 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, |
| 22 | + 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, |
| 23 | + 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, |
| 24 | + 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, |
| 25 | + 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, |
| 26 | + 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, |
| 27 | + 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, |
| 28 | + 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, |
| 29 | + 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, |
| 30 | + 141, 128, 195, 78, 66, 215, 61, 156, 180 |
| 31 | +], dtype=np.int32) |
| 32 | +PERM = np.concatenate((PERM, PERM)) |
| 33 | + |
| 34 | +# 2D simplex skew factors |
| 35 | +F2 = 0.5 * (np.sqrt(3.0) - 1.0) |
| 36 | +G2 = (3.0 - np.sqrt(3.0)) / 6.0 |
| 37 | + |
| 38 | +# https://github.com/zbenjamin/vec_noise/blob/master/_simplex.c#L46 |
| 39 | +def snoise2(x, y): |
| 40 | + """Generate 2D simplex noise for given coordinates.""" |
| 41 | + s = (x + y) * F2 |
| 42 | + i = np.floor(x + s).astype(int) |
| 43 | + j = np.floor(y + s).astype(int) |
| 44 | + t = (i + j) * G2 |
| 45 | + |
| 46 | + x0 = x - (i - t) |
| 47 | + y0 = y - (j - t) |
| 48 | + |
| 49 | + # Determine which simplex we're in |
| 50 | + i1 = (x0 > y0).astype(int) |
| 51 | + j1 = 1 - i1 |
| 52 | + |
| 53 | + x1 = x0 - i1 + G2 |
| 54 | + y1 = y0 - j1 + G2 |
| 55 | + x2 = x0 - 1 + 2 * G2 |
| 56 | + y2 = y0 - 1 + 2 * G2 |
| 57 | + |
| 58 | + # Hash coordinates of the three simplex corners |
| 59 | + ii = i & 255 |
| 60 | + jj = j & 255 |
| 61 | + gi0 = PERM[ii + PERM[jj]] % 12 |
| 62 | + gi1 = PERM[ii + i1 + PERM[jj + j1]] % 12 |
| 63 | + gi2 = PERM[ii + 1 + PERM[jj + 1]] % 12 |
| 64 | + |
| 65 | + # Calculate contribution from three corners |
| 66 | + t0 = 0.5 - x0**2 - y0**2 |
| 67 | + t1 = 0.5 - x1**2 - y1**2 |
| 68 | + t2 = 0.5 - x2**2 - y2**2 |
| 69 | + |
| 70 | + mask0 = (t0 >= 0).astype(float) |
| 71 | + mask1 = (t1 >= 0).astype(float) |
| 72 | + mask2 = (t2 >= 0).astype(float) |
| 73 | + |
| 74 | + n0 = mask0 * t0**4 * (GRAD3[gi0, 0] * x0 + GRAD3[gi0, 1] * y0) |
| 75 | + n1 = mask1 * t1**4 * (GRAD3[gi1, 0] * x1 + GRAD3[gi1, 1] * y1) |
| 76 | + n2 = mask2 * t2**4 * (GRAD3[gi2, 0] * x2 + GRAD3[gi2, 1] * y2) |
| 77 | + |
| 78 | + # Sum up and scale the result |
| 79 | + return 70 * (n0 + n1 + n2) |
0 commit comments