UNPKG

fieldkit

Version:

Basic building blocks for computational design projects. Written in CoffeeScript for browser and server environments.

396 lines (361 loc) 12.1 kB
// Generated by CoffeeScript 1.6.3 /* Base class for all types of Noise generators */ (function() { var FlowNoise, Noise, SimplexNoise, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Noise = (function() { function Noise() {} Noise.prototype.noise = function(x, y) { return 0; }; Noise.prototype.noise2 = function(x, y) { return 0; }; Noise.prototype.noise3 = function(x, y, z) { return 0; }; Noise.prototype.noise4 = function(x, y, z, w) { return 0; }; return Noise; })(); /* Ported from Stefan Gustavson's java implementation http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf Read Stefan's excellent paper for details on how this code works. Sean McCullough banksean@gmail.com Added 4D noise Joshua Koo zz85nus@gmail.com */ SimplexNoise = (function(_super) { var dot; __extends(SimplexNoise, _super); function SimplexNoise(rng) { var i, _i, _j; if (rng == null) { rng = Math; } this.grad3 = [[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]]; this.grad4 = [[0, 1, 1, 1], [0, 1, 1, -1], [0, 1, -1, 1], [0, 1, -1, -1], [0, -1, 1, 1], [0, -1, 1, -1], [0, -1, -1, 1], [0, -1, -1, -1], [1, 0, 1, 1], [1, 0, 1, -1], [1, 0, -1, 1], [1, 0, -1, -1], [-1, 0, 1, 1], [-1, 0, 1, -1], [-1, 0, -1, 1], [-1, 0, -1, -1], [1, 1, 0, 1], [1, 1, 0, -1], [1, -1, 0, 1], [1, -1, 0, -1], [-1, 1, 0, 1], [-1, 1, 0, -1], [-1, -1, 0, 1], [-1, -1, 0, -1], [1, 1, 1, 0], [1, 1, -1, 0], [1, -1, 1, 0], [1, -1, -1, 0], [-1, 1, 1, 0], [-1, 1, -1, 0], [-1, -1, 1, 0], [-1, -1, -1, 0]]; this.p = []; i = 0; for (i = _i = 0; _i <= 256; i = ++_i) { this.p[i] = Math.floor(rng.random() * 256); } this.perm = []; for (i = _j = 0; _j <= 512; i = ++_j) { this.perm[i] = this.p[i & 255]; } this.simplex = [[0, 1, 2, 3], [0, 1, 3, 2], [0, 0, 0, 0], [0, 2, 3, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [1, 2, 3, 0], [0, 2, 1, 3], [0, 0, 0, 0], [0, 3, 1, 2], [0, 3, 2, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [1, 3, 2, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [1, 2, 0, 3], [0, 0, 0, 0], [1, 3, 0, 2], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [2, 3, 0, 1], [2, 3, 1, 0], [1, 0, 2, 3], [1, 0, 3, 2], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [2, 0, 3, 1], [0, 0, 0, 0], [2, 1, 3, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [2, 0, 1, 3], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [3, 0, 1, 2], [3, 0, 2, 1], [0, 0, 0, 0], [3, 1, 2, 0], [2, 1, 0, 3], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [3, 1, 0, 2], [0, 0, 0, 0], [3, 2, 0, 1], [3, 2, 1, 0]]; } dot = function(g, x, y) { return g[0] * x + g[1] * y; }; SimplexNoise.prototype.noise2 = function(xin, yin) { var F2, G2, X0, Y0, gi0, gi1, gi2, i, i1, ii, j, j1, jj, n0, n1, n2, s, t, t0, t1, t2, x0, x1, x2, y0, y1, y2; n0 = 0; n1 = 0; n2 = 0; F2 = 0.5 * (Math.sqrt(3.0) - 1.0); s = (xin + yin) * F2; i = Math.floor(xin + s); j = Math.floor(yin + s); G2 = (3.0 - Math.sqrt(3.0)) / 6.0; t = (i + j) * G2; X0 = i - t; Y0 = j - t; x0 = xin - X0; y0 = yin - Y0; i1 = 0; j1 = 0; if (x0 > y0) { i1 = 1; j1 = 0; } else { i1 = 0; j1 = 1; } x1 = x0 - i1 + G2; y1 = y0 - j1 + G2; x2 = x0 - 1.0 + 2.0 * G2; y2 = y0 - 1.0 + 2.0 * G2; ii = i & 255; jj = j & 255; gi0 = this.perm[ii + this.perm[jj]] % 12; gi1 = this.perm[ii + i1 + this.perm[jj + j1]] % 12; gi2 = this.perm[ii + 1 + this.perm[jj + 1]] % 12; t0 = 0.5 - x0 * x0 - y0 * y0; if (!(t0 < 0)) { t0 *= t0; n0 = t0 * t0 * dot(this.grad3[gi0], x0, y0); } t1 = 0.5 - x1 * x1 - y1 * y1; if (!(t1 < 0)) { t1 *= t1; n1 = t1 * t1 * dot(this.grad3[gi1], x1, y1); } t2 = 0.5 - x2 * x2 - y2 * y2; if (!(t2 < 0)) { t2 *= t2; n2 = t2 * t2 * dot(this.grad3[gi2], x2, y2); } return 70.0 * (n0 + n1 + n2); }; SimplexNoise.prototype.noise3 = function(xin, yin, zin) { var F3, G3, X0, Y0, Z0, gi0, gi1, gi2, gi3, i, i1, i2, ii, j, j1, j2, jj, k, k1, k2, kk, n0, n1, n2, n3, s, t, t0, t1, t2, t3, x0, x1, x2, x3, y0, y1, y2, y3, z0, z1, z2, z3; n0 = 0; n1 = 0; n2 = 0; n3 = 0; F3 = 1.0 / 3.0; s = (xin + yin + zin) * F3; i = Math.floor(xin + s); j = Math.floor(yin + s); k = Math.floor(zin + s); G3 = 1.0 / 6.0; t = (i + j + k) * G3; X0 = i - t; Y0 = j - t; Z0 = k - t; x0 = xin - X0; y0 = yin - Y0; z0 = zin - Z0; i1 = 0; j1 = 0; k1 = 0; i2 = 0; j2 = 0; k2 = 0; if (x0 >= y0) { if (y0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } else if (x0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } else { i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } } else { if (y0 < z0) { i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1; } else if (x0 < z0) { i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1; } else { i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } } x1 = x0 - i1 + G3; y1 = y0 - j1 + G3; z1 = z0 - k1 + G3; x2 = x0 - i2 + 2.0 * G3; y2 = y0 - j2 + 2.0 * G3; z2 = z0 - k2 + 2.0 * G3; x3 = x0 - 1.0 + 3.0 * G3; y3 = y0 - 1.0 + 3.0 * G3; z3 = z0 - 1.0 + 3.0 * G3; ii = i & 255; jj = j & 255; kk = k & 255; gi0 = this.perm[ii + this.perm[jj + this.perm[kk]]] % 12; gi1 = this.perm[ii + i1 + this.perm[jj + j1 + this.perm[kk + k1]]] % 12; gi2 = this.perm[ii + i2 + this.perm[jj + j2 + this.perm[kk + k2]]] % 12; gi3 = this.perm[ii + 1 + this.perm[jj + 1 + this.perm[kk + 1]]] % 12; t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0; if (!(t0 < 0)) { t0 *= t0; n0 = t0 * t0 * dot(this.grad3[gi0], x0, y0, z0); } t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1; if (!(t1 < 0)) { t1 *= t1; n1 = t1 * t1 * dot(this.grad3[gi1], x1, y1, z1); } t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2; if (!(t2 < 0)) { t2 *= t2; n2 = t2 * t2 * dot(this.grad3[gi2], x2, y2, z2); } t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3; if (!(t3 < 0)) { t3 *= t3; n3 = t3 * t3 * dot(this.grad3[gi3], x3, y3, z3); } return 32.0 * (n0 + n1 + n2 + n3); }; SimplexNoise.prototype.noise4 = function(x, y, z, w) { var F4, G4, W0, X0, Y0, Z0, c, c1, c2, c3, c4, c5, c6, gi0, gi1, gi2, gi3, gi4, grad4, i, i1, i2, i3, ii, j, j1, j2, j3, jj, k, k1, k2, k3, kk, l, l1, l2, l3, ll, n0, n1, n2, n3, n4, perm, s, simplex, t, t0, t1, t2, t3, t4, w0, w1, w2, w3, w4, x0, x1, x2, x3, x4, y0, y1, y2, y3, y4, z0, z1, z2, z3, z4; grad4 = this.grad4; simplex = this.simplex; perm = this.perm; F4 = (Math.sqrt(5.0) - 1.0) / 4.0; G4 = (5.0 - Math.sqrt(5.0)) / 20.0; n0 = 0; n1 = 0; n2 = 0; n3 = 0; n4 = 0; s = (x + y + z + w) * F4; i = Math.floor(x + s); j = Math.floor(y + s); k = Math.floor(z + s); l = Math.floor(w + s); t = (i + j + k + l) * G4; X0 = i - t; Y0 = j - t; Z0 = k - t; W0 = l - t; x0 = x - X0; y0 = y - Y0; z0 = z - Z0; w0 = w - W0; c1 = (x0 > y0 ? 32 : 0); c2 = (x0 > z0 ? 16 : 0); c3 = (y0 > z0 ? 8 : 0); c4 = (x0 > w0 ? 4 : 0); c5 = (y0 > w0 ? 2 : 0); c6 = (z0 > w0 ? 1 : 0); c = c1 + c2 + c3 + c4 + c5 + c6; i1 = 0; j1 = 0; k1 = 0; l1 = 0; i2 = 0; j2 = 0; k2 = 0; l2 = 0; i3 = 0; j3 = 0; k3 = 0; l3 = 0; i1 = (simplex[c][0] >= 3 ? 1 : 0); j1 = (simplex[c][1] >= 3 ? 1 : 0); k1 = (simplex[c][2] >= 3 ? 1 : 0); l1 = (simplex[c][3] >= 3 ? 1 : 0); i2 = (simplex[c][0] >= 2 ? 1 : 0); j2 = (simplex[c][1] >= 2 ? 1 : 0); k2 = (simplex[c][2] >= 2 ? 1 : 0); l2 = (simplex[c][3] >= 2 ? 1 : 0); i3 = (simplex[c][0] >= 1 ? 1 : 0); j3 = (simplex[c][1] >= 1 ? 1 : 0); k3 = (simplex[c][2] >= 1 ? 1 : 0); l3 = (simplex[c][3] >= 1 ? 1 : 0); x1 = x0 - i1 + G4; y1 = y0 - j1 + G4; z1 = z0 - k1 + G4; w1 = w0 - l1 + G4; x2 = x0 - i2 + 2.0 * G4; y2 = y0 - j2 + 2.0 * G4; z2 = z0 - k2 + 2.0 * G4; w2 = w0 - l2 + 2.0 * G4; x3 = x0 - i3 + 3.0 * G4; y3 = y0 - j3 + 3.0 * G4; z3 = z0 - k3 + 3.0 * G4; w3 = w0 - l3 + 3.0 * G4; x4 = x0 - 1.0 + 4.0 * G4; y4 = y0 - 1.0 + 4.0 * G4; z4 = z0 - 1.0 + 4.0 * G4; w4 = w0 - 1.0 + 4.0 * G4; ii = i & 255; jj = j & 255; kk = k & 255; ll = l & 255; gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] % 32; gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]] % 32; gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]] % 32; gi3 = perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]] % 32; gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32; t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0; if (!(t0 < 0)) { t0 *= t0; n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0); } t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1; if (!(t1 < 0)) { t1 *= t1; n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1); } t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2; if (!(t2 < 0)) { t2 *= t2; n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2); } t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3; if (!(t3 < 0)) { t3 *= t3; n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3); } t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4; if (!(t4 < 0)) { t4 *= t4; n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4); } return 27.0 * (n0 + n1 + n2 + n3 + n4); }; return SimplexNoise; })(Noise); FlowNoise = (function(_super) { var TWO_PI, n; __extends(FlowNoise, _super); n = 128; TWO_PI = Math.PI * 2; FlowNoise.prototype.basis = []; FlowNoise.prototype.perm = []; function FlowNoise(rng) { var i, theta, _i; if (rng == null) { rng = Math; } for (i = _i = 0; 0 <= n ? _i <= n : _i >= n; i = 0 <= n ? ++_i : --_i) { theta = i * TWO_PI / n; this.basis[i] = [Math.cos(theta, Math.sin(theta))]; this.perm[i] = i; } reinitialize((rng.random() * 1000) | 0); } FlowNoise.prototype.reinitialize = function(seed) { var i, j, _i, _results; _results = []; for (i = _i = 1; 1 <= n ? _i <= n : _i >= n; i = 1 <= n ? ++_i : --_i) { j = (Math.random() * seed) % (i + 1); _results.push(seed += 1); } return _results; }; return FlowNoise; })(Noise); module.exports = { SimplexNoise: SimplexNoise }; }).call(this);