[READ-ONLY] Mirror of https://github.com/flo-bit/every-noise. javascript noise class with lots of features flo-bit.github.io/every-noise/
javascript noise procedural-generation
0

Configure Feed

Select the types of activity you want to include in your feed.

first commit

flo-bit (Dec 29, 2022, 10:12 PM +0100) ea6da72b

+1353
+1
.gitignore
··· 1 + /.DS_Store
+1
.prettierrc
··· 1 + {}
+44
README.md
··· 1 + ## Noise 2 + 3 + 3D every(where) noise javscript class for all your noise needs. 4 + 5 + - seeded 6 + - scaling, pow 7 + - fBM 8 + - erosion-like fBM 9 + - domain warping 10 + - ridges, billows 11 + - stepped noise 12 + - combined noise 13 + - tileable noise 14 + 15 + ### demos 16 + 17 + - [x] simple p5 1D noise 18 + - [x] simple p5 2D noise 19 + 20 + - [x] tileable 1D noise 21 + - [x] tileable 2D noise (very notable artifacts) 22 + 23 + - [x] fbm 1D noise 24 + - [x] fbm 2D noise 25 + 26 + - [ ] 3D noise on sphere 27 + 28 + - [ ] seeded noise 29 + - [ ] erosion 2D noise 30 + - [ ] erosion on sphere 31 + 32 + - [ ] 2D stepped noise 33 + 34 + - [ ] pixi noise flow field 35 + 36 + - [ ] performance test 37 + 38 + ### Get noise 39 + 40 + noise.get(x, y, z); 41 + 42 + OR 43 + 44 + noise.get(vector);
+48
demos/p5-fbm-1D-noise.html
··· 1 + <!DOCTYPE html> 2 + <html> 3 + <head> 4 + <script src="../noise.js"></script> 5 + <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.min.js"></script> 6 + 7 + <meta charset="utf-8" /> 8 + </head> 9 + <style> 10 + html, 11 + body { 12 + width: 100%; 13 + height: 100%; 14 + margin: 0; 15 + padding: 0; 16 + } 17 + </style> 18 + <body style="background: black"> 19 + <script> 20 + var nois; 21 + 22 + var counter = 0; 23 + function setup() { 24 + createCanvas(windowWidth - 2, windowHeight - 2); 25 + stroke(255); 26 + 27 + nois = new Noise({ scl: 0.005, octaves: 5 }); 28 + } 29 + 30 + function draw() { 31 + background(0); 32 + let stepSize = 0.5; 33 + let lastY; 34 + counter += deltaTime / 10; 35 + for (let x = 0; x < width; x += stepSize) { 36 + let v = nois.get(x + counter); 37 + let y = map(v, -1, 1, 0, height); 38 + if (lastY) line(x, lastY, x + stepSize, y); 39 + lastY = y; 40 + } 41 + } 42 + 43 + function windowResized() { 44 + resizeCanvas(windowWidth - 2, windowHeight - 2); 45 + } 46 + </script> 47 + </body> 48 + </html>
+50
demos/p5-fbm-2D-noise.html
··· 1 + <!DOCTYPE html> 2 + <html> 3 + <head> 4 + <script src="../noise.js"></script> 5 + <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.min.js"></script> 6 + 7 + <meta charset="utf-8" /> 8 + </head> 9 + <style> 10 + html, 11 + body { 12 + width: 100%; 13 + height: 100%; 14 + margin: 0; 15 + padding: 0; 16 + } 17 + </style> 18 + <body style="background: black"> 19 + <script> 20 + var nois; 21 + 22 + var counter = 0; 23 + function setup() { 24 + createCanvas(windowWidth - 2, windowHeight - 4); 25 + noStroke(); 26 + 27 + nois = new Noise({ scl: 0.002, octaves: 4 }); 28 + } 29 + 30 + function draw() { 31 + background(0); 32 + let cellSize = 8; 33 + counter += deltaTime / 20; 34 + 35 + for (let x = 0; x < width; x += cellSize) { 36 + for (let y = 0; y < height; y += cellSize) { 37 + let v = nois.get(x, y, counter); 38 + let r = map(v, -1, 1, 0, 255); 39 + fill(r); 40 + rect(x, y, cellSize, cellSize); 41 + } 42 + } 43 + } 44 + 45 + function windowResized() { 46 + resizeCanvas(windowWidth - 2, windowHeight - 4); 47 + } 48 + </script> 49 + </body> 50 + </html>
+48
demos/p5-simple-1D-noise.html
··· 1 + <!DOCTYPE html> 2 + <html> 3 + <head> 4 + <script src="../noise.js"></script> 5 + <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.min.js"></script> 6 + 7 + <meta charset="utf-8" /> 8 + </head> 9 + <style> 10 + html, 11 + body { 12 + width: 100%; 13 + height: 100%; 14 + margin: 0; 15 + padding: 0; 16 + } 17 + </style> 18 + <body style="background: black"> 19 + <script> 20 + var nois; 21 + 22 + var counter = 0; 23 + function setup() { 24 + createCanvas(windowWidth - 2, windowHeight - 2); 25 + stroke(255); 26 + 27 + nois = new Noise({ scl: 0.005 }); 28 + } 29 + 30 + function draw() { 31 + background(0); 32 + let stepSize = 0.5; 33 + let lastY; 34 + counter += deltaTime / 10; 35 + for (let x = 0; x < width; x += stepSize) { 36 + let v = nois.get(x + counter); 37 + let y = map(v, -1, 1, 0, height); 38 + if (lastY) line(x, lastY, x + stepSize, y); 39 + lastY = y; 40 + } 41 + } 42 + 43 + function windowResized() { 44 + resizeCanvas(windowWidth - 2, windowHeight - 2); 45 + } 46 + </script> 47 + </body> 48 + </html>
+1161
noise.js
··· 1 + /* 2 + * 3 + * vector class 4 + * 5 + */ 6 + class Vector { 7 + constructor(x, y, z) { 8 + this.x = x == undefined ? 0 : x; 9 + this.y = y == undefined ? 0 : y; 10 + this.z = z == undefined ? 0 : z; 11 + 12 + this.isVector = true; 13 + this.is2D = z == undefined; 14 + } 15 + copy(vec) { 16 + this.x = vec.x; 17 + this.y = vec.y; 18 + this.z = vec.z; 19 + return this; 20 + } 21 + clone() { 22 + return new Vector(this.x, this.y, this.z); 23 + } 24 + set(x, y, z) { 25 + if (typeof x != "number" && x.x != undefined) { 26 + return this.set(x.x, x.y, x.z); 27 + } 28 + this.x = x == undefined ? 0 : x; 29 + this.y = y == undefined ? 0 : y; 30 + this.z = z == undefined ? 0 : z; 31 + return this; 32 + } 33 + add(x, y, z) { 34 + if (typeof x != "number" && x.x != undefined) { 35 + return this.add(x.x, x.y, x.z); 36 + } 37 + this.x += x == undefined ? 0 : x; 38 + this.y += y == undefined ? 0 : y; 39 + this.z += z == undefined ? 0 : z; 40 + return this; 41 + } 42 + sub(x, y, z) { 43 + if (typeof x != "number" && x.x != undefined) { 44 + return this.sub(x.x, x.y, x.z); 45 + } 46 + this.x -= x == undefined ? 0 : x; 47 + this.y -= y == undefined ? 0 : y; 48 + this.z -= z == undefined ? 0 : z; 49 + return this; 50 + } 51 + mult(x, y, z) { 52 + if (typeof x != "number" && x.x != undefined) { 53 + return this.mult(x.x, x.y, x.z); 54 + } 55 + this.x *= x == undefined ? 0 : x; 56 + this.y *= y == undefined ? (x == undefined ? 0 : x) : y; 57 + this.z *= z == undefined ? (x == undefined ? 0 : x) : z; 58 + return this; 59 + } 60 + div(x, y, z) { 61 + if (typeof x != "number" && x.x != undefined) { 62 + return this.div(m.x, x.y, x.z); 63 + } 64 + this.x /= x == undefined ? 0 : x; 65 + this.y /= y == undefined ? (x == undefined ? 0 : x) : y; 66 + this.z /= z == undefined ? (x == undefined ? 0 : x) : z; 67 + return this; 68 + } 69 + dot(vec) { 70 + return this.x * vec.x + this.y * vec.y + this.z * vec.z; 71 + } 72 + cross(vec) { 73 + return new Vector( 74 + this.y * vec.z - this.z * vec.y, 75 + this.z * vec.x - this.x * vec.z, 76 + this.x * vec.y - this.y * vec.x 77 + ); 78 + } 79 + 80 + distXYZ(bx, by, bz) { 81 + let dx = this.x - bx; 82 + let dy = (this.y || 0) - (by || 0); 83 + let dz = (this.z || 0) - (bz || 0); 84 + return Math.sqrt(dx * dx + dy * dy + dz * dz); 85 + } 86 + 87 + dist(vec) { 88 + return this.distXYZ(vec.x, vec.y, vec.z); 89 + } 90 + distance(vec) { 91 + return this.dist(vec); 92 + } 93 + distanceTo(vec) { 94 + return this.dist(vec); 95 + } 96 + length() { 97 + return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); 98 + } 99 + 100 + setLength(l) { 101 + this.mult(l / this.length()); 102 + return this; 103 + } 104 + limit(l) { 105 + if (this.length() > l) this.setLength(l); 106 + return this; 107 + } 108 + 109 + // 2d only 110 + heading() { 111 + return Math.atan2(this.x, this.y); 112 + } 113 + // 2d only 114 + rotate(a) { 115 + let ca = Math.cos(a); 116 + let sa = Math.sin(a); 117 + this.set(ca * this.x - sa * this.y, sa * this.x + ca * this.y); 118 + return this; 119 + } 120 + 121 + angleBetween(vec) { 122 + let d = this.dot(vec); 123 + let l = this.length() * vec.length(); 124 + return Math.acos(d / l); 125 + } 126 + angleTo(vec) { 127 + return this.angleBetween(vec); 128 + } 129 + equals(vec) { 130 + return this.x == vec.x && this.y == vec.y && this.z == vec.z; 131 + } 132 + 133 + normalize() { 134 + this.setLength(1); 135 + return this; 136 + } 137 + mag() { 138 + return this.length(); 139 + } 140 + setMag(m) { 141 + this.setLength(m); 142 + return this; 143 + } 144 + manhattanLength() { 145 + return this.x + this.y + this.z; 146 + } 147 + lerp(vec, a) { 148 + let dx = vec.x - this.x; 149 + let dy = vec.y - this.y; 150 + let dz = vec.z - this.z; 151 + this.add(dx * a, dy * a, dz * a); 152 + return this; 153 + } 154 + 155 + // 2d vector from angle and optional length (default length 1) 156 + static fromAngle2D(a, l) { 157 + let v = new Vector(Math.cos(a), Math.sin(a)); 158 + if (l) v.setLength(l); 159 + return v; 160 + } 161 + 162 + // random 2d vector with length between 0 and 1 163 + // or set length 164 + static random2D(l) { 165 + let v = new Vector(Math.random() * 2 - 1, Math.random() * 2 - 1); 166 + while (v.length() > 1) { 167 + v.set(Math.random() * 2 - 1, Math.random() * 2 - 1); 168 + } 169 + if (l) v.setLength(l); 170 + return v; 171 + } 172 + 173 + // random 3d vector with length between 0 and 1 174 + // or set length 175 + static random3D(l) { 176 + let v = new Vector( 177 + Math.random() * 2 - 1, 178 + Math.random() * 2 - 1, 179 + Math.random() * 2 - 1 180 + ); 181 + while (v.length() > 1) { 182 + v.set( 183 + Math.random() * 2 - 1, 184 + Math.random() * 2 - 1, 185 + Math.random() * 2 - 1 186 + ); 187 + } 188 + if (l) v.setLength(l); 189 + return v; 190 + } 191 + 192 + static breakIntoParts(a, b, parts) { 193 + if (a == undefined || b == undefined || !a.isVector || !b.isVector) return; 194 + 195 + parts = Math.floor(parts || 2); 196 + let arr = [a.clone()]; 197 + for (let i = 1; i < parts; i++) { 198 + arr.push(a.clone().lerp(b, i / parts)); 199 + } 200 + arr.push(b.clone()); 201 + return arr; 202 + } 203 + } 204 + 205 + /* 206 + * 207 + * simplex-noise.js from https://github.com/jwagner/simplex-noise.js 208 + * 209 + */ 210 + !(function () { 211 + "use strict"; 212 + var r = 0.5 * (Math.sqrt(3) - 1), 213 + e = (3 - Math.sqrt(3)) / 6, 214 + t = 1 / 6, 215 + a = (Math.sqrt(5) - 1) / 4, 216 + o = (5 - Math.sqrt(5)) / 20; 217 + function i(r) { 218 + var e; 219 + (e = 220 + "function" == typeof r 221 + ? r 222 + : r 223 + ? (function () { 224 + var r = 0, 225 + e = 0, 226 + t = 0, 227 + a = 1, 228 + o = 229 + ((i = 4022871197), 230 + function (r) { 231 + r = r.toString(); 232 + for (var e = 0; e < r.length; e++) { 233 + var t = 0.02519603282416938 * (i += r.charCodeAt(e)); 234 + (t -= i = t >>> 0), 235 + (i = (t *= i) >>> 0), 236 + (i += 4294967296 * (t -= i)); 237 + } 238 + return 2.3283064365386963e-10 * (i >>> 0); 239 + }); 240 + var i; 241 + (r = o(" ")), (e = o(" ")), (t = o(" ")); 242 + for (var n = 0; n < arguments.length; n++) 243 + (r -= o(arguments[n])) < 0 && (r += 1), 244 + (e -= o(arguments[n])) < 0 && (e += 1), 245 + (t -= o(arguments[n])) < 0 && (t += 1); 246 + return ( 247 + (o = null), 248 + function () { 249 + var o = 2091639 * r + 2.3283064365386963e-10 * a; 250 + return (r = e), (e = t), (t = o - (a = 0 | o)); 251 + } 252 + ); 253 + })(r) 254 + : Math.random), 255 + (this.p = n(e)), 256 + (this.perm = new Uint8Array(512)), 257 + (this.permMod12 = new Uint8Array(512)); 258 + for (var t = 0; t < 512; t++) 259 + (this.perm[t] = this.p[255 & t]), (this.permMod12[t] = this.perm[t] % 12); 260 + } 261 + function n(r) { 262 + var e, 263 + t = new Uint8Array(256); 264 + for (e = 0; e < 256; e++) t[e] = e; 265 + for (e = 0; e < 255; e++) { 266 + var a = e + ~~(r() * (256 - e)), 267 + o = t[e]; 268 + (t[e] = t[a]), (t[a] = o); 269 + } 270 + return t; 271 + } 272 + (i.prototype = { 273 + grad3: new Float32Array([ 274 + 1, 1, 0, -1, 1, 0, 1, -1, 0, -1, -1, 0, 1, 0, 1, -1, 0, 1, 1, 0, -1, -1, 275 + 0, -1, 0, 1, 1, 0, -1, 1, 0, 1, -1, 0, -1, -1, 276 + ]), 277 + grad4: new Float32Array([ 278 + 0, 1, 1, 1, 0, 1, 1, -1, 0, 1, -1, 1, 0, 1, -1, -1, 0, -1, 1, 1, 0, -1, 1, 279 + -1, 0, -1, -1, 1, 0, -1, -1, -1, 1, 0, 1, 1, 1, 0, 1, -1, 1, 0, -1, 1, 1, 280 + 0, -1, -1, -1, 0, 1, 1, -1, 0, 1, -1, -1, 0, -1, 1, -1, 0, -1, -1, 1, 1, 281 + 0, 1, 1, 1, 0, -1, 1, -1, 0, 1, 1, -1, 0, -1, -1, 1, 0, 1, -1, 1, 0, -1, 282 + -1, -1, 0, 1, -1, -1, 0, -1, 1, 1, 1, 0, 1, 1, -1, 0, 1, -1, 1, 0, 1, -1, 283 + -1, 0, -1, 1, 1, 0, -1, 1, -1, 0, -1, -1, 1, 0, -1, -1, -1, 0, 284 + ]), 285 + noise2D: function (t, a) { 286 + var o, 287 + i, 288 + n = this.permMod12, 289 + f = this.perm, 290 + s = this.grad3, 291 + v = 0, 292 + h = 0, 293 + l = 0, 294 + u = (t + a) * r, 295 + d = Math.floor(t + u), 296 + p = Math.floor(a + u), 297 + M = (d + p) * e, 298 + m = t - (d - M), 299 + c = a - (p - M); 300 + m > c ? ((o = 1), (i = 0)) : ((o = 0), (i = 1)); 301 + var y = m - o + e, 302 + w = c - i + e, 303 + g = m - 1 + 2 * e, 304 + A = c - 1 + 2 * e, 305 + x = 255 & d, 306 + q = 255 & p, 307 + D = 0.5 - m * m - c * c; 308 + if (D >= 0) { 309 + var S = 3 * n[x + f[q]]; 310 + v = (D *= D) * D * (s[S] * m + s[S + 1] * c); 311 + } 312 + var U = 0.5 - y * y - w * w; 313 + if (U >= 0) { 314 + var b = 3 * n[x + o + f[q + i]]; 315 + h = (U *= U) * U * (s[b] * y + s[b + 1] * w); 316 + } 317 + var F = 0.5 - g * g - A * A; 318 + if (F >= 0) { 319 + var N = 3 * n[x + 1 + f[q + 1]]; 320 + l = (F *= F) * F * (s[N] * g + s[N + 1] * A); 321 + } 322 + return 70 * (v + h + l); 323 + }, 324 + noise3D: function (r, e, a) { 325 + var o, 326 + i, 327 + n, 328 + f, 329 + s, 330 + v, 331 + h, 332 + l, 333 + u, 334 + d, 335 + p = this.permMod12, 336 + M = this.perm, 337 + m = this.grad3, 338 + c = (r + e + a) * (1 / 3), 339 + y = Math.floor(r + c), 340 + w = Math.floor(e + c), 341 + g = Math.floor(a + c), 342 + A = (y + w + g) * t, 343 + x = r - (y - A), 344 + q = e - (w - A), 345 + D = a - (g - A); 346 + x >= q 347 + ? q >= D 348 + ? ((s = 1), (v = 0), (h = 0), (l = 1), (u = 1), (d = 0)) 349 + : x >= D 350 + ? ((s = 1), (v = 0), (h = 0), (l = 1), (u = 0), (d = 1)) 351 + : ((s = 0), (v = 0), (h = 1), (l = 1), (u = 0), (d = 1)) 352 + : q < D 353 + ? ((s = 0), (v = 0), (h = 1), (l = 0), (u = 1), (d = 1)) 354 + : x < D 355 + ? ((s = 0), (v = 1), (h = 0), (l = 0), (u = 1), (d = 1)) 356 + : ((s = 0), (v = 1), (h = 0), (l = 1), (u = 1), (d = 0)); 357 + var S = x - s + t, 358 + U = q - v + t, 359 + b = D - h + t, 360 + F = x - l + 2 * t, 361 + N = q - u + 2 * t, 362 + C = D - d + 2 * t, 363 + P = x - 1 + 0.5, 364 + T = q - 1 + 0.5, 365 + _ = D - 1 + 0.5, 366 + j = 255 & y, 367 + k = 255 & w, 368 + z = 255 & g, 369 + B = 0.6 - x * x - q * q - D * D; 370 + if (B < 0) o = 0; 371 + else { 372 + var E = 3 * p[j + M[k + M[z]]]; 373 + o = (B *= B) * B * (m[E] * x + m[E + 1] * q + m[E + 2] * D); 374 + } 375 + var G = 0.6 - S * S - U * U - b * b; 376 + if (G < 0) i = 0; 377 + else { 378 + var H = 3 * p[j + s + M[k + v + M[z + h]]]; 379 + i = (G *= G) * G * (m[H] * S + m[H + 1] * U + m[H + 2] * b); 380 + } 381 + var I = 0.6 - F * F - N * N - C * C; 382 + if (I < 0) n = 0; 383 + else { 384 + var J = 3 * p[j + l + M[k + u + M[z + d]]]; 385 + n = (I *= I) * I * (m[J] * F + m[J + 1] * N + m[J + 2] * C); 386 + } 387 + var K = 0.6 - P * P - T * T - _ * _; 388 + if (K < 0) f = 0; 389 + else { 390 + var L = 3 * p[j + 1 + M[k + 1 + M[z + 1]]]; 391 + f = (K *= K) * K * (m[L] * P + m[L + 1] * T + m[L + 2] * _); 392 + } 393 + return 32 * (o + i + n + f); 394 + }, 395 + noise4D: function (r, e, t, i) { 396 + var n, 397 + f, 398 + s, 399 + v, 400 + h, 401 + l, 402 + u, 403 + d, 404 + p, 405 + M, 406 + m, 407 + c, 408 + y, 409 + w, 410 + g, 411 + A, 412 + x, 413 + q = this.perm, 414 + D = this.grad4, 415 + S = (r + e + t + i) * a, 416 + U = Math.floor(r + S), 417 + b = Math.floor(e + S), 418 + F = Math.floor(t + S), 419 + N = Math.floor(i + S), 420 + C = (U + b + F + N) * o, 421 + P = r - (U - C), 422 + T = e - (b - C), 423 + _ = t - (F - C), 424 + j = i - (N - C), 425 + k = 0, 426 + z = 0, 427 + B = 0, 428 + E = 0; 429 + P > T ? k++ : z++, 430 + P > _ ? k++ : B++, 431 + P > j ? k++ : E++, 432 + T > _ ? z++ : B++, 433 + T > j ? z++ : E++, 434 + _ > j ? B++ : E++; 435 + var G = P - (l = k >= 3 ? 1 : 0) + o, 436 + H = T - (u = z >= 3 ? 1 : 0) + o, 437 + I = _ - (d = B >= 3 ? 1 : 0) + o, 438 + J = j - (p = E >= 3 ? 1 : 0) + o, 439 + K = P - (M = k >= 2 ? 1 : 0) + 2 * o, 440 + L = T - (m = z >= 2 ? 1 : 0) + 2 * o, 441 + O = _ - (c = B >= 2 ? 1 : 0) + 2 * o, 442 + Q = j - (y = E >= 2 ? 1 : 0) + 2 * o, 443 + R = P - (w = k >= 1 ? 1 : 0) + 3 * o, 444 + V = T - (g = z >= 1 ? 1 : 0) + 3 * o, 445 + W = _ - (A = B >= 1 ? 1 : 0) + 3 * o, 446 + X = j - (x = E >= 1 ? 1 : 0) + 3 * o, 447 + Y = P - 1 + 4 * o, 448 + Z = T - 1 + 4 * o, 449 + $ = _ - 1 + 4 * o, 450 + rr = j - 1 + 4 * o, 451 + er = 255 & U, 452 + tr = 255 & b, 453 + ar = 255 & F, 454 + or = 255 & N, 455 + ir = 0.6 - P * P - T * T - _ * _ - j * j; 456 + if (ir < 0) n = 0; 457 + else { 458 + var nr = (q[er + q[tr + q[ar + q[or]]]] % 32) * 4; 459 + n = 460 + (ir *= ir) * 461 + ir * 462 + (D[nr] * P + D[nr + 1] * T + D[nr + 2] * _ + D[nr + 3] * j); 463 + } 464 + var fr = 0.6 - G * G - H * H - I * I - J * J; 465 + if (fr < 0) f = 0; 466 + else { 467 + var sr = (q[er + l + q[tr + u + q[ar + d + q[or + p]]]] % 32) * 4; 468 + f = 469 + (fr *= fr) * 470 + fr * 471 + (D[sr] * G + D[sr + 1] * H + D[sr + 2] * I + D[sr + 3] * J); 472 + } 473 + var vr = 0.6 - K * K - L * L - O * O - Q * Q; 474 + if (vr < 0) s = 0; 475 + else { 476 + var hr = (q[er + M + q[tr + m + q[ar + c + q[or + y]]]] % 32) * 4; 477 + s = 478 + (vr *= vr) * 479 + vr * 480 + (D[hr] * K + D[hr + 1] * L + D[hr + 2] * O + D[hr + 3] * Q); 481 + } 482 + var lr = 0.6 - R * R - V * V - W * W - X * X; 483 + if (lr < 0) v = 0; 484 + else { 485 + var ur = (q[er + w + q[tr + g + q[ar + A + q[or + x]]]] % 32) * 4; 486 + v = 487 + (lr *= lr) * 488 + lr * 489 + (D[ur] * R + D[ur + 1] * V + D[ur + 2] * W + D[ur + 3] * X); 490 + } 491 + var dr = 0.6 - Y * Y - Z * Z - $ * $ - rr * rr; 492 + if (dr < 0) h = 0; 493 + else { 494 + var pr = (q[er + 1 + q[tr + 1 + q[ar + 1 + q[or + 1]]]] % 32) * 4; 495 + h = 496 + (dr *= dr) * 497 + dr * 498 + (D[pr] * Y + D[pr + 1] * Z + D[pr + 2] * $ + D[pr + 3] * rr); 499 + } 500 + return 27 * (n + f + s + v + h); 501 + }, 502 + }), 503 + (i._buildPermutationTable = n), 504 + "undefined" != typeof define && 505 + define.amd && 506 + define(function () { 507 + return i; 508 + }), 509 + "undefined" != typeof exports 510 + ? (exports.SimplexNoise = i) 511 + : "undefined" != typeof window && (window.SimplexNoise = i), 512 + "undefined" != typeof module && (module.exports = i); 513 + })(); 514 + 515 + function firstDefined(...arr) { 516 + for (let i = 0; i < arr.length; i++) { 517 + if (arr[i] !== undefined) return arr[i]; 518 + } 519 + } 520 + 521 + class Noise { 522 + static defaultUp = new Vector(0, 1, 0); 523 + 524 + /** 525 + * create new Noise object 526 + * 527 + * @param {object} opts 528 + * 529 + * @property {number} seed - seed for the noise 530 + * 531 + * @property {number} min - minimun value of noise 532 + * @property {number} max - maximum value of noise 533 + * 534 + * @property {number} scale - scale of the noise 535 + * @property {number} power - power of the noise (1 = linear, 2 = quadratic, etc) 536 + * @property {Vector} shift - move noise in 3d space 537 + * 538 + * @property {number} octaves - number of layers for fbm noise 539 + * @property {number} gain - how much to multiply amplitude per layer 540 + * @property {number} lacunarity - how much to multiply scale per layer 541 + * @property {array} amps - array of amplitudes for each layer 542 + * @property {number} erosion - how much previous layers influence amplitude of later layers 543 + * @property {number} sharpness - billowed or rigded noise (0 = normal, 1 = billowed, -1 = ridged) 544 + * @property {number} steps - will turn noise into steps (integer, number of steps) 545 + * 546 + * @property {number} warp - how much to warp the noise 547 + * @property {number} warpNoise - noise to warp the noise with 548 + * 549 + * @property {boolean} invert - invert the noise 550 + * @property {boolean} abs - absolute value of the noise 551 + * @property {boolean} clamp - clamp the noise between min and max 552 + * @property {boolean} tileX - tile the noise in x direction 553 + * @property {boolean} tileY - tile the noise in y direction 554 + * @property {boolean} tile - tile the noise in all directions (will override tileX and tileY) 555 + * 556 + * @constructor 557 + */ 558 + constructor(opts) { 559 + opts = opts || {}; 560 + 561 + this.pos = new Vector(0, 0, 0); 562 + 563 + this.scale = firstDefined(opts.scale, opts.scl, 1); 564 + this.power = firstDefined(opts.power, opts.pow, 1); 565 + 566 + this.shift = firstDefined( 567 + opts.shift, 568 + new Vector(opts.x || 0.357, opts.y || 0.579, opts.z || 0.739) 569 + ); 570 + 571 + // fbm stuff 572 + // how many layers 573 + this.octaves = firstDefined(opts.octaves, opts.oct, 0); 574 + // how much to multiply amplitude per layer 575 + this.gain = firstDefined(opts.gain, opts.persistence, opts.per, 0.5); 576 + // how much to multiply scale per layer 577 + this.lacunarity = firstDefined(opts.lacunarity, opts.lac, 2); 578 + 579 + // how much previous layers influence amplitude of later layers 580 + this.erosion = firstDefined(opts.erosion, opts.ero, 0); 581 + // how much to move x, y, z to calculate derivative 582 + // (x2 - x1) / delta, (y2 - y1) / delta, (z2 - z1) / delta 583 + this.delta = firstDefined(opts.delta, opts.del, 0.0001 * this.scale); 584 + 585 + // amp is also only used for fbm 586 + this.amp = firstDefined(opts.amplitude, opts.amp); 587 + 588 + this.sharpness = firstDefined(opts.sharpness, opts.sharp, 0); 589 + 590 + this.steps = opts.steps; 591 + 592 + this.min = firstDefined(opts.min, -1); 593 + this.max = firstDefined(opts.max, 1); 594 + 595 + if (this.octaves > 0 || opts.layers != undefined) { 596 + this.layers = []; 597 + for ( 598 + let i = 0; 599 + i <= this.octaves || (opts.layers && i < opts.layers.length); 600 + i++ 601 + ) { 602 + let settings = 603 + opts.layers != undefined && opts.layers.length > i 604 + ? opts.layers[i] 605 + : {}; 606 + if (settings.seed == undefined) settings.seed = this.seed * 3 + i * 5; 607 + 608 + let n = settings; 609 + if (opts.all) { 610 + for (let k of Object.keys(opts.all)) { 611 + n[k] = opts.all[k]; 612 + } 613 + } 614 + if (n.isNoise != true) { 615 + n = new Noise(settings); 616 + } 617 + this.layers.push(n); 618 + } 619 + } else { 620 + this.simplex = new SimplexNoise(this.seed); 621 + } 622 + 623 + if (opts.combine != undefined) { 624 + this.combine = opts.combine; 625 + } 626 + 627 + this.mod = opts.mod; 628 + 629 + if (opts.warp != undefined) { 630 + this.warp = opts.warp; 631 + this.checkValue("warp"); 632 + if (opts.warpNoise) { 633 + this.warpNoise = opts.warpNoise; 634 + if (this.warpNoise.isNoise != true) 635 + this.warpNoise = new Noise(opts.warpNoise); 636 + } 637 + } 638 + if (opts.warp2 != undefined) { 639 + this.warp2 = opts.warp2; 640 + this.checkValue("warp2"); 641 + 642 + if (opts.warpNoise2) { 643 + this.warpNoise2 = opts.warpNoise2; 644 + if (this.warpNoise2.isNoise != true) 645 + this.warpNoise2 = new Noise(opts.warpNoise2); 646 + } 647 + } 648 + 649 + this.defaultUp = opts.defaultUp; 650 + 651 + if (opts.amps) { 652 + this.multiplyAmps(opts.amps); 653 + } 654 + 655 + // use central position when calculating derivative 656 + this.central = opts.central; 657 + 658 + this.tileX = opts.tileX; 659 + this.tileY = opts.tileY; 660 + 661 + if (opts.tile) { 662 + this.tileX = true; 663 + this.tileY = true; 664 + } 665 + 666 + this.isNoise = true; 667 + 668 + this.seed = opts.seed; 669 + this.derivative = undefined; 670 + } 671 + 672 + get scale() { 673 + if (this.pos != undefined) 674 + return this.getPropertyAtPosition("_scale", this.pos); 675 + return this._scale; 676 + } 677 + set scale(scale) { 678 + this._scale = scale; 679 + this.checkValue("_scale"); 680 + } 681 + 682 + get power() { 683 + if (this.pos != undefined) 684 + return this.getPropertyAtPosition("_power", this.pos); 685 + return this._power; 686 + } 687 + set power(power) { 688 + this._power = power; 689 + this.checkValue("_power"); 690 + } 691 + 692 + get octaves() { 693 + if (this.pos != undefined) 694 + return this.getPropertyAtPosition("_octaves", this.pos); 695 + return this._octaves; 696 + } 697 + set octaves(octaves) { 698 + this._octaves = octaves; 699 + this.checkValue("_octaves"); 700 + } 701 + get gain() { 702 + if (this.pos != undefined) 703 + return this.getPropertyAtPosition("_gain", this.pos); 704 + return this._gain; 705 + } 706 + set gain(gain) { 707 + this._gain = gain; 708 + this.checkValue("_gain"); 709 + } 710 + get lacunarity() { 711 + if (this.pos != undefined) 712 + return this.getPropertyAtPosition("_lacunarity", this.pos); 713 + return this._lacunarity; 714 + } 715 + set lacunarity(lacunarity) { 716 + this._lacunarity = lacunarity; 717 + this.checkValue("_lacunarity"); 718 + } 719 + get erosion() { 720 + if (this.pos != undefined) 721 + return this.getPropertyAtPosition("_erosion", this.pos); 722 + return this._erosion; 723 + } 724 + set erosion(erosion) { 725 + this._erosion = erosion; 726 + this.checkValue("_erosion"); 727 + } 728 + get amp() { 729 + if (this.pos != undefined) 730 + return this.getPropertyAtPosition("_amp", this.pos); 731 + return this._amp; 732 + } 733 + set amp(amp) { 734 + this._amp = amp; 735 + this.checkValue("_amp"); 736 + } 737 + get combine() { 738 + if (this.pos != undefined) 739 + return this.getPropertyAtPosition("_combine", this.pos); 740 + return this._combine; 741 + } 742 + set combine(combine) { 743 + this._combine = combine; 744 + this.checkValue("_combine"); 745 + } 746 + get sharpness() { 747 + if (this.pos != undefined) 748 + return this.getPropertyAtPosition("_sharpness", this.pos); 749 + return this._sharpness; 750 + } 751 + set sharpness(sharpness) { 752 + this._sharpness = sharpness; 753 + this.checkValue("_sharpness"); 754 + } 755 + get warp() { 756 + if (this.pos != undefined) 757 + return this.getPropertyAtPosition("_warp", this.pos); 758 + return this._warp; 759 + } 760 + set warp(warp) { 761 + this._warp = warp; 762 + this.checkValue("_warp"); 763 + } 764 + get warp2() { 765 + if (this.pos != undefined) 766 + return this.getPropertyAtPosition("_warp2", this.pos); 767 + return this._warp2; 768 + } 769 + set warp2(warp2) { 770 + this._warp2 = warp2; 771 + this.checkValue("_warp2"); 772 + } 773 + get steps() { 774 + if (this.pos != undefined) 775 + return this.getPropertyAtPosition("_steps", this.pos); 776 + return this._steps; 777 + } 778 + set steps(steps) { 779 + this._steps = steps; 780 + this.checkValue("_steps"); 781 + } 782 + get min() { 783 + if (this.pos != undefined) 784 + return this.getPropertyAtPosition("_min", this.pos); 785 + return this._min; 786 + } 787 + set min(min) { 788 + this._min = min; 789 + this.checkValue("_min"); 790 + } 791 + get max() { 792 + if (this.pos != undefined) 793 + return this.getPropertyAtPosition("_max", this.pos); 794 + return this._max; 795 + } 796 + set max(max) { 797 + this._max = max; 798 + this.checkValue("_max"); 799 + } 800 + 801 + set x(x) { 802 + this.pos.x = x; 803 + } 804 + get x() { 805 + return this.pos.x; 806 + } 807 + set y(y) { 808 + this.pos.y = y; 809 + } 810 + get y() { 811 + return this.pos.y; 812 + } 813 + set z(z) { 814 + this.pos.z = z; 815 + } 816 + get z() { 817 + return this.pos.z; 818 + } 819 + 820 + // checks if this[key] is an object or a number and 821 + // if it is an object but .isNoise != true 822 + // turns that into a new Noise object 823 + checkValue(key) { 824 + let v = this[key]; 825 + if (v != undefined && typeof v != "number" && !v.isNoise) 826 + this[key] = new Noise(v); 827 + } 828 + 829 + get seed() { 830 + return this._seed; 831 + } 832 + set seed(seed) { 833 + this.setSeed(seed); 834 + } 835 + 836 + setSeed(seed) { 837 + this._seed = seed || Math.random() * 100000; 838 + if (this.simplex) this.simplex = new SimplexNoise(this._seed); 839 + if (this.layers) { 840 + let i = 13; 841 + for (let l of this.layers) { 842 + l.setSeed(this._seed * 3 + i++ * 7); 843 + } 844 + } 845 + 846 + for (let k of Object.keys(this)) { 847 + if (this[k] != undefined && this[k].isNoise) { 848 + this[k].setSeed(this._seed * 17 + 513); 849 + } 850 + } 851 + 852 + return seed; 853 + } 854 + 855 + shift(dX, dY, dZ) { 856 + this.shift.x += dX || 0; 857 + this.shift.y += dY || 0; 858 + this.shift.z += dZ || 0; 859 + } 860 + 861 + multiplyAmps(arr) { 862 + if (this.layers == undefined) return; 863 + 864 + for (let i = 0; i < this.layers.length && i < arr.length; i++) { 865 + this.layers[i].amp *= arr[i]; 866 + } 867 + } 868 + 869 + getFBM(x, y, z, noErosion) { 870 + let scale = this.scale; 871 + 872 + // if no layers exit early 873 + if (this.layers == undefined) { 874 + // if object has simplex noise return result of that 875 + if (this.simplex != undefined) 876 + return this.simplex.noise3D(x * scale, y * scale, z * scale); 877 + // no data 878 + return 0; 879 + } 880 + // for calculating angle between derivative and tangent 881 + // when erosion > 0 882 + let up = 883 + this.defaultUp != undefined ? this.defaultUp(x, y, z) : Noise.defaultUp; 884 + 885 + let maxAmp = 1; 886 + let amp = 1, 887 + freq = scale; 888 + 889 + let lac = this.lacunarity; 890 + let gain = this.gain; 891 + 892 + // reuse vector 893 + this.sum = this.sum || new Vector(); 894 + this.sum.set(0, 0, 0); 895 + 896 + let n = 0; 897 + let erosion = noErosion ? 0 : this.erosion; 898 + let octaves = this.octaves; 899 + for (let i = 0; i <= octaves && i < this.layers.length; i++) { 900 + let l = this.layers[i]; 901 + let layerAmp = l.amp || 1; 902 + let val = l.get(x * freq, y * freq, z * freq, up) * amp * layerAmp; 903 + if (erosion > 0) { 904 + let d = l.getDerivative(x * freq, y * freq, z * freq); 905 + d.setLength(amp * layerAmp); 906 + 907 + this.sum.add(d); 908 + // calculate normalized angle between sum of derivatives and tangent, should be between 0 and 1 909 + let mult = Math.abs(1 - up.angleTo(this.sum) / Math.PI); 910 + 911 + n += val * (mult * erosion + 1 - erosion); 912 + } else { 913 + n += val; 914 + } 915 + amp *= gain; 916 + freq *= lac; 917 + maxAmp += amp * layerAmp; 918 + } 919 + return n / maxAmp; 920 + } 921 + 922 + warpPosition(x, y, z) { 923 + let warp = this.warp; 924 + if (warp) { 925 + if (this.warpNoise) { 926 + this.warpNoise.pos.copy(this.pos); 927 + let scl = this.warpNoise.scale; 928 + x += 929 + this.warpNoise.get( 930 + x - 74.98 * scl, 931 + y + 49.33 * scl, 932 + z + 11.11 * scl 933 + ) * warp; 934 + y += 935 + this.warpNoise.get( 936 + x + 13.23 * scl, 937 + y + 56.79 * scl, 938 + z + 93.31 * scl 939 + ) * warp; 940 + z += 941 + this.warpNoise.get( 942 + x + 11.47 * scl, 943 + y + 17.98 * scl, 944 + z + 23.56 * scl 945 + ) * warp; 946 + } else { 947 + let scl = this.scale; 948 + x += 949 + this.getFBM(x - 74.98 * scl, y + 41.33 * scl, z + 18.1 * scl, true) * 950 + warp; 951 + y += 952 + this.getFBM(x + 1.23 * scl, y + 5.79 * scl, z + 9.31 * scl, true) * 953 + warp; 954 + z += 955 + this.getFBM(x + 11.47 * scl, y + 17.98 * scl, z + 23.56 * scl, true) * 956 + warp; 957 + } 958 + } 959 + 960 + let warp2 = this.warp2; 961 + if (warp2) { 962 + if (this.warpNoise2) { 963 + this.warpNoise2.pos.copy(this.pos); 964 + let scl = this.warpNoise2.scale; 965 + x += 966 + this.warpNoise2.get(x + 1.23 * scl, y + 5.79 * scl, z + 9.31 * scl) * 967 + warp2; 968 + y += 969 + this.warpNoise2.get( 970 + x + 11.47 * scl, 971 + y + 17.98 * scl, 972 + z + 23.56 * scl 973 + ) * warp2; 974 + z += 975 + this.warpNoise2.get( 976 + x - 71.98 * scl, 977 + y + 43.33 * scl, 978 + z + 93.1 * scl 979 + ) * warp2; 980 + } else { 981 + let scl = this.scale; 982 + x += 983 + this.getFBM(x + 11.47 * scl, y + 17.98 * scl, z + 23.56 * scl, true) * 984 + warp2; 985 + y += 986 + this.getFBM(x - 73.98 * scl, y + 44.33 * scl, z + 15.13 * scl, true) * 987 + warp2; 988 + z += 989 + this.getFBM(x + 11.23 * scl, y + 53.79 * scl, z + 96.31 * scl, true) * 990 + warp2; 991 + } 992 + } 993 + 994 + this.pos.set(x, y, z); 995 + } 996 + 997 + tilePosition(x, y, z) { 998 + let newX = 0, 999 + newY = 0, 1000 + newZ = 0; 1001 + if (this.tileX) { 1002 + newX = Math.sin(x * Math.PI * 2); 1003 + newY = Math.cos(x * Math.PI * 2); 1004 + } 1005 + if (this.tileY) { 1006 + newY += Math.sin(y * Math.PI * 2); 1007 + newZ = Math.cos(y * Math.PI * 2); 1008 + } 1009 + this.pos.set( 1010 + (this.tileX ? 0 : x) + newX, 1011 + (this.tileY ? 0 : y) + newY, 1012 + z + newZ 1013 + ); 1014 + } 1015 + 1016 + // main method, returns value between -1 and +1 1017 + getNoise(x, y, z) { 1018 + x = x || 0; 1019 + y = y || 0; 1020 + z = z || 0; 1021 + this.pos.set(x + this.shift.x, y + this.shift.y, z + this.shift.z); 1022 + 1023 + this.tilePosition(this.x, this.y, this.z); 1024 + this.warpPosition(this.x, this.y, this.z); 1025 + 1026 + let norm = this.getFBM(this.x, this.y, this.z); 1027 + 1028 + if (this.clamp) { 1029 + norm = Math.min(norm, 1); 1030 + norm = Math.max(norm, -1); 1031 + } 1032 + 1033 + if (this.sharpness != undefined && this.sharpness != 0) { 1034 + let sharp = this.sharpness; 1035 + 1036 + let billow = (Math.abs(norm) - 0.5) * 2; 1037 + let ridged = (0.5 - Math.abs(norm)) * 2; 1038 + 1039 + norm = MathUtils.lerp(norm, billow, Math.max(0, sharp)); 1040 + norm = MathUtils.lerp(norm, ridged, Math.abs(Math.min(0, sharp))); 1041 + } 1042 + 1043 + // modify with function 1044 + if (this.mod) { 1045 + norm = this.mod(norm, this.x, this.y, this.z, this, up); 1046 + } 1047 + 1048 + let power = this.power; 1049 + if (power && power != 1) { 1050 + // convert to [0 - 1], apply power and back to [-1, 1] 1051 + norm = (Math.pow((norm + 1) * 0.5, power) - 0.5) * 2; 1052 + } 1053 + 1054 + //combine with other noise: 1055 + if (this.combine) { 1056 + norm *= this.combine; 1057 + } 1058 + 1059 + // turn into steps 1060 + // (e.g. 2 steps => only 0 or 1, 3 steps => 0, 0.5 and 1) 1061 + let steps = Math.round(this.steps); 1062 + if (steps != undefined && steps > 1) { 1063 + let s = (Math.floor((norm + 1) * steps * 0.5) / (steps - 1) - 0.5) * 2; 1064 + return s; 1065 + } 1066 + 1067 + return norm; 1068 + } 1069 + 1070 + getDerivative(x, y, z, n) { 1071 + // left side or central difference 1072 + // very expensive (four/six noise calls), should be changed to analytical derivatives 1073 + // see https://iquilezles.org/www/articles/morenoise/morenoise.htm 1074 + 1075 + n = n || this.get(x, y, z); 1076 + let mov = this.delta; 1077 + 1078 + let dx = 1079 + (this.central ? this.get(x - mov, y, z) : n) - this.get(x + mov, y, z); 1080 + let dy = 1081 + (this.central ? this.get(x, y - mov, z) : n) - this.get(x, y + mov, z); 1082 + let dz = 1083 + (this.central ? this.get(x, y, z - mov) : n) - this.get(x, y, z + mov); 1084 + 1085 + if (this.derivative == undefined) this.derivative = new Vector(); 1086 + this.derivative.set(dx, dy, dz); 1087 + this.derivative.normalize(); 1088 + return this.derivative; 1089 + } 1090 + 1091 + getPropertyAtPosition(key, vec) { 1092 + let v = this[key]; 1093 + if (typeof v == "number") return v; 1094 + if (v != undefined && v.isNoise) return v.get(vec.x, vec.y, vec.z); 1095 + } 1096 + 1097 + // converts from -1, 1 to min, max 1098 + normToMinMax(norm) { 1099 + return (norm + 1) * 0.5 * (this.max - this.min) + this.min; 1100 + } 1101 + // converts from min, max to -1, 1 1102 + minMaxToNorm(minMax) { 1103 + return ((minMax - this.min) / (this.max - this.min) - 0.5) * 2; 1104 + } 1105 + 1106 + /** 1107 + * same as .getNorm but can only be called with x, y, z 1108 + * 1109 + * @param {number} x 1110 + * @param {number} y 1111 + * @param {number} z 1112 + * @returns {number} 1113 + */ 1114 + getNormXYZ(x, y, z) { 1115 + return this.getNoise(x, y, z); 1116 + } 1117 + /** 1118 + * function to get normalized noise value (between -1 and 1) 1119 + * can be called either with x, y, z or with a vector 1120 + * 1121 + * @param {number, object} vecOrX 1122 + * @param {number} y 1123 + * @param {number} z 1124 + * @returns {number} 1125 + */ 1126 + getNorm(vecOrX, y, z) { 1127 + if (typeof vecOrX == "number") { 1128 + return this.getNormXYZ(vecOrX, y, z); 1129 + } 1130 + return this.getNormXYZ(vecOrX.x, vecOrX.y, vecOrX.z); 1131 + } 1132 + 1133 + /** 1134 + * same as .get but can only be called with x, y, z 1135 + * 1136 + * @param {number} x 1137 + * @param {number} y 1138 + * @param {number} z 1139 + * @returns {number} 1140 + */ 1141 + getXYZ(x, y, z) { 1142 + return this.normToMinMax(this.getNormXYZ(x, y, z)); 1143 + } 1144 + 1145 + /** 1146 + * main function to get noise value 1147 + * will return value between min and max 1148 + * call either with x, y, z or with a vector 1149 + * 1150 + * @param {number, object} vecOrX 1151 + * @param {number} y 1152 + * @param {number} z 1153 + * @returns {number} 1154 + */ 1155 + get(vecOrX, y, z) { 1156 + if (typeof vecOrX == "number") { 1157 + return this.getXYZ(vecOrX, y, z); 1158 + } 1159 + return this.getXYZ(vecOrX.x, vecOrX.y, vecOrX.z); 1160 + } 1161 + }