├── .gitignore
├── .npmignore
├── .travis.yml
├── LICENSE
├── README.md
├── demo
├── assets
│ ├── logo.png
│ └── logo.svg
├── demo.js
├── index.html
└── style.css
├── dist
├── tumult.min.js
└── tumult.min.map
├── images
├── abs.png
├── invert.png
├── octaves.png
├── simplex.png
└── sin.png
├── index.js
├── lib
├── index.js
├── perlin
│ ├── perlin1.js
│ ├── perlin2.js
│ ├── perlin3.js
│ ├── perlin4.js
│ └── perlinN.js
├── simplex
│ ├── simplex1.js
│ └── simplex2.js
└── util
│ ├── 1d.js
│ ├── 2d.js
│ ├── 3d.js
│ ├── 4d.js
│ ├── math.js
│ ├── nd.js
│ └── noise.js
├── package-lock.json
├── package.json
└── test
└── test.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | .nyc_output/
3 | .cache/
4 | pages/
5 | *.swo
6 | *.swp
7 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | demo/
2 | images/
3 | test/
4 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | script:
3 | - npm run test
4 | - npm run pages
5 | after_script:
6 | - npm run coverage
7 | cache:
8 | directories:
9 | - ~/.npm
10 | notifications:
11 | email: false
12 | node_js:
13 | - '10'
14 | - '8'
15 | after_success:
16 | - npm run travis-deploy-once "npm run semantic-release"
17 | branches:
18 | except:
19 | - /^v\d+\.\d+\.\d+$/
20 | deploy:
21 | provider: pages
22 | skip_cleanup: true
23 | github_token: $GH_TOKEN
24 | local_dir: pages
25 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Philip Scott
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # tumult
2 |
3 | [](https://badge.fury.io/js/tumult)
4 | [](https://travis-ci.com/ScottyFillups/tumult)
5 | [](https://coveralls.io/github/ScottyFillups/tumult?branch=master)
6 | [](https://packagephobia.now.sh/result?p=tumult)
7 |
8 | 
9 | 
10 | 
11 | 
12 | 
13 |
14 | Yet another **Javascript noise library**. Demonstrations [here](http://philipjscott.github.io/tumult). Currently supports Perlin noise for any arbitrary dimension and Simplex[1-2]. Eventually might support:
15 | * Simplex[3-4]
16 |
17 | ## Installation
18 |
19 | ```sh
20 | npm install tumult --save
21 | ```
22 |
23 | The built files are also available on `unpkg` and `jsdelivr`:
24 |
25 |
26 | ```html
27 |
28 |
29 | ```
30 |
31 | ## Usage
32 |
33 | ```js
34 | const tumult = require('tumult')
35 |
36 | const simplex2 = new tumult.Simplex2('some_seed')
37 |
38 | for (let x = 0; x < 10; x++) {
39 | for (let y = 0; y < 10; y++) {
40 | console.log(simplex2.gen(x / 64, y / 64))
41 | }
42 | }
43 | ```
44 |
45 | ## API
46 |
47 | ### tumult
48 |
49 | Object that stores noise constructors. Below is the full list of constructors:
50 |
51 | * **tumult.Simplex1**
52 | * **tumult.Simplex2**
53 | * **tumult.Perlin1**
54 | * **tumult.Perlin2**
55 | * **tumult.Perlin3**
56 | * **tumult.Perlin4**
57 | * **tumult.PerlinN**
58 |
59 | Every constructor has the following signature:
60 |
61 | #### tumult.NoiseConstructor([seed])
62 |
63 | Returns a `noise` object.
64 |
65 | #### seed
66 |
67 | Type: `String | Number`
68 |
69 | Seed to use for shuffling the permutation look-up table. If no value is passed, `Math.random()` will be used as a seed.
70 |
71 | ### noise
72 |
73 | Noise object returned from invoke a noise constructor; all noise objects have the same API:
74 |
75 | #### noise.seed([seed])
76 |
77 | Re-seeds the permutation look-up table. If a number is passed, it will be converted to a string which will seed the generator. If no string is passed, `.seed()` defaults to using `Math.random()`
78 |
79 | #### noise.gen(x, y, z...)
80 |
81 | Generates a noise value given the appropriate dimensions (eg. a simplex2 generator should take two arguments, a perlin3 generator should take three arguments, etc.)
82 |
83 | #### noise.octavate(octaves, x, y, z...)
84 |
85 | Applies [fractal Brownian motion](https://thebookofshaders.com/13/), summing iterations of the noise (# of iterations = `octaves`). With each successive iteration, the frequency is doubled and the weight is halved.
86 |
87 | Note that the generator created by `tumult.PerlinN` is variadic, meaning you can get Nth dimensional perlin noise by passing N arguments. Note that the gradient lookup table for `perlinN` isn't optimised, so calling `perlinN(x, y)` will likely produce less "attractive" noise than `perlin2(x, y)`.
88 |
89 | For quickly displaying heightmaps, I highly recommend using [terrapaint](https://www.npmjs.com/package/terrapaint).
90 |
91 | ~~**noise.transform(fn)**~~
92 |
93 | Deprecated
94 |
95 |
96 | **Consider wrapping your function instead:**
97 |
98 | ```js
99 | const tumult = require('tumult')
100 |
101 | const simplex2 = new tumult.Simplex2()
102 | const transform = (x, y) => Math.sin(1 / simplex2(x, y))
103 | ```
104 |
105 | Takes in a function which will its `this` bound to noiseGenerator object, meaning you can call `gen` and `octavate` using `this.gen`, etc. This function should take in the dimensions as parameters, and return a value. `.transform` will return the new transformed noise function. For example, suppose you want a function which will return `sin(1/noise(x/32,y/32))`, you can do the following:
106 |
107 | ```js
108 | const tumult = require('tumult')
109 |
110 | const simplex2 = new tumult.Simplex2('seed')
111 | const noise = simplex2.transform(function (x, y) {
112 | return Math.sin(1 / this.gen(x/32, y/32))
113 | })
114 |
115 | for (let i = 0; i < 100; i++) {
116 | for (let j = 0; j < 100; j++) {
117 | console.log(noise(i, j))
118 | }
119 | }
120 | ```
121 |
122 | TL;DR, `noise.transform` is essentially a helper function that lets you wrap the noise function with your own function.
123 |
124 | ## Note on testing
125 |
126 | Currently the tests only verify trivial test requirements (eg. presence of methods, checking if output is within expected [-1, 1] bound); a better way to test this library would be to utilize OpenCV to verify the noise produced is correct, outlined here: https://stackoverflow.com/questions/32023240/how-to-write-unit-tests-for-a-perlin-noise-library
127 |
128 | Unfortunately I'm lacking the bandwidth to implement this, but pull requests are welcome!
129 |
130 | ## Acknowledgements
131 |
132 | Perlin noise was invented in 1985 by Ken Perlin.
133 |
--------------------------------------------------------------------------------
/demo/assets/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipjscott/tumult/31d03d7f722c05319d3d56e20b095c60abad9a99/demo/assets/logo.png
--------------------------------------------------------------------------------
/demo/assets/logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
193 |
--------------------------------------------------------------------------------
/demo/demo.js:
--------------------------------------------------------------------------------
1 | /* global alert */
2 |
3 | 'use strict'
4 |
5 | import terrapaint from 'terrapaint'
6 | import tumult from '../'
7 |
8 | const { Simplex2 } = tumult
9 | const seed = Math.random()
10 | const simplex = new Simplex2(seed)
11 |
12 | ;(function () {
13 | const map = terrapaint.map(simplex.gen, {
14 | offset: true,
15 | period: 64,
16 | thisArg: simplex
17 | })
18 | map.draw('#noise-canvas')
19 | })()
20 |
21 | $('#gen-noise').addEventListener('click', function () {
22 | const evalStr = $('#eval-str').value
23 | const fnBody = `
24 | var n = this.gen.bind(this)
25 | var f = this.octavate.bind(this)
26 | var sin = Math.sin
27 | var cos = Math.cos
28 | var pow = Math.pow
29 | var pi = Math.PI
30 | var abs = Math.abs
31 | var e = Math.E
32 | return ${evalStr}
33 | `
34 |
35 | let transformFn
36 |
37 | try {
38 | // eslint-disable-next-line no-new-func
39 | transformFn = (new Function('x', 'y', fnBody)).bind(simplex)
40 | } catch (e) {
41 | alert(`
42 | Something is wrong with the syntax of your function.
43 | Please ensure all the parentheses are closed and that you're
44 | using the correct functions and variable names.
45 | `)
46 |
47 | return
48 | }
49 |
50 | const transformedNoise = simplex.transform(function (x, y) {
51 | try {
52 | return transformFn(x, y)
53 | } catch (e) {
54 | alert(`
55 | Your function created a run-time error. Please ensure
56 | the period of the noise function is greater than one
57 | (ie. divide x and y by a value, like 4 or 16, before
58 | passing it to n()).
59 | `)
60 | throw new Error('Runtime error')
61 | }
62 | })
63 | const map = terrapaint.map(transformedNoise, {
64 | offset: true,
65 | period: 1,
66 | thisArg: simplex
67 | })
68 | map.draw('#noise-canvas')
69 | })
70 |
71 | function $ (selector) {
72 | return document.querySelector(selector)
73 | }
74 |
--------------------------------------------------------------------------------
/demo/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Tumult
5 |
6 |
7 |
8 |
9 |
10 |
11 |
Tumult
12 |
A Javascript noise library
13 |
Philip Scott | ScottyFillups
14 |
15 |
19 |
20 | Tumult is a noise library, supporting simplex[1-2],
21 | perlin[1-4], and perlinN (perlin noise in any arbitrary
22 | dimension). It provides the following methods:
23 |
24 |
25 |
26 | .gen(x, y...): Generates a noise value
27 |
28 |
29 | .octavate(octaves, x, y...): Applies
30 | fractal Brownian motion,
31 | summing iterations of the noise. With each
32 | successive iteration, the frequency is doubled
33 | and the weight is halved.
34 |
35 |
36 | .transform(fn): Returns a new transformed noise
37 | function given the transformation function. The passed
38 | transformation function is bound to the 'this' object
39 | of the noise generator, therefore you can call
40 | 'this.octavate' and 'this.gen' to generate the noise.
41 |
42 |
43 |
44 | You can try the transform function by entering your own
45 | transformation function. Below are some functions and variables
46 | you can use:
47 |
48 |
49 |
n(x, y) - The noise function. In this case, it's Simplex2
50 |
f(octaves, x, y) - The octavate, or 'F'ractal function.
51 |
sin(x)
52 |
cos(x)
53 |
abs(x)
54 |
pow(radix, exponent)
55 |
pi
56 |
e
57 |
58 |
59 | Alternatively, you can use vanilla Javascript code (eg. Math.abs() instead of abs(), this.gen() instead of n(), etc.).
60 |
61 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/demo/style.css:
--------------------------------------------------------------------------------
1 | @import url('https://fonts.googleapis.com/css?family=Slabo+27px|Source+Sans+Pro');
2 |
3 | html, body {
4 | padding: 0;
5 | margin: 0;
6 | }
7 | main {
8 | text-align: center;
9 | color: #222;
10 | max-width: 600px;
11 | font-family: 'Slabo 27px', serif;
12 | padding: 20px;
13 | margin: 20px auto 0 auto;
14 | }
15 | ul {
16 | text-align: left;
17 | margin-left: 10px;
18 | }
19 |
20 | p, ul, li {
21 | line-height: 170%;
22 | }
23 | h1, h2, h3 {
24 | font-family: 'Source Sans Pro', sans-serif;
25 | }
26 | input[type='text'] {
27 | border: 0;
28 | border-bottom: 1px solid #222;
29 | font-family: 'Source Sans Pro', serif;
30 | text-align: center;
31 | color: #222;
32 | font-size: 1em;
33 | }
34 | button {
35 | color: #222;
36 | font-family: 'Source Sans Pro', sans-serif;
37 | font-size: 1em;
38 | border: 1px solid #222;
39 | padding: 5px 20px;
40 | text-transform: uppercase;
41 | background-color: transparent;
42 | border-radius: 10px;
43 | cursor: pointer;
44 | transition: color 0.3s, background-color 0.3s, border 0.3s;
45 | }
46 | button:hover, button:active {
47 | background-color: #222;
48 | border: 1px solid transparent;
49 | color: #fff;
50 | }
51 |
52 | h1 { font-size: 3em }
53 | h3 { font-size: 1em }
54 | #lib-name { margin: 0 0 10px 0 }
55 | #desc { margin: 0 0 10px 0 }
56 | #author { margin: 0 0 40px 0 }
57 | #noise-canvas { margin-bottom: 10px }
58 | #transform-demo { font-family: 'Source Sans Pro', sans-serif }
59 | #gen-noise { margin: 15px 0 20px 0 }
60 |
61 | @media only screen and (min-width: 0) {
62 | main { font-size: 100% }
63 | #noise-canvas {
64 | width: 128px;
65 | height: 128px;
66 | }
67 | #logo { margin-bottom: 10px }
68 | }
69 | @media only screen and (min-width: 340px) {
70 | main { font-size: 120% }
71 | #noise-canvas {
72 | width: 256px;
73 | height: 256px;
74 | }
75 | #logo { margin-bottom: 0 }
76 | }
77 |
78 |
--------------------------------------------------------------------------------
/dist/tumult.min.js:
--------------------------------------------------------------------------------
1 | parcelRequire=function(e,r,n,t){var i="function"==typeof parcelRequire&&parcelRequire,o="function"==typeof require&&require;function u(n,t){if(!r[n]){if(!e[n]){var f="function"==typeof parcelRequire&&parcelRequire;if(!t&&f)return f(n,!0);if(i)return i(n,!0);if(o&&"string"==typeof n)return o(n);var c=new Error("Cannot find module '"+n+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[n][1][r]||r},p.cache={};var l=r[n]=new u.Module(n);e[n][0].call(l.exports,p,l,l.exports,this)}return r[n].exports;function p(e){return u(p.resolve(e))}}u.isParcelRequire=!0,u.Module=function(e){this.id=e,this.bundle=u,this.exports={}},u.modules=e,u.cache=r,u.parent=i,u.register=function(r,n){e[r]=[function(e,r){r.exports=n},{}]};for(var f=0;f0){var u=n.indexOf(this);~u?n.splice(u+1):n.push(this),~u?r.splice(u,1/0,t):r.push(t),~n.indexOf(l)&&(l=e.call(this,t,l))}else n.push(l);return null==i?l:i.call(this,t,l)}}exports=module.exports=i,exports.getSerialize=e;
3 | },{}],"KFk+":[function(require,module,exports) {
4 | "use strict";var n=require("json-stringify-safe"),r=function(){var n=4022871197;return function(r){if(r){r=r.toString();for(var t=0;t>>0,n=(e*=n)>>>0,n+=4294967296*(e-=n)}return 2.3283064365386963e-10*(n>>>0)}n=4022871197}},t=function(t){return function(){var e,o,i=48,u=1,a=i,f=new Array(i),c=0,l=new r;for(e=0;e=i&&(a=0);var n=1768863*f[a]+2.3283064365386963e-10*u;return f[a]=n-(u=0|n)},h=function(n){return Math.floor(n*(g()+1.1102230246251565e-16*(2097152*g()|0)))};h.string=function(n){var r,t="";for(r=0;rc?1:0,p=i>c?0:1,a=i-f+y,b=c-p+y,m=i-1+2*y,O=c-1+2*y;return 70*(h(i,c)*l(this.p,r,o).dot(i,c)+h(a,b)*l(this.p,r+f,o+p).dot(a,b)+h(m,O)*l(this.p,r+1,o+1).dot(m,O))}}]),n}();module.exports=m;
17 | },{"../util/noise":"WRFH","../util/2d":"VHE+","../util/math":"CFdN"}],"nJiC":[function(require,module,exports) {
18 | "use strict";function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){for(var r=0;r=1?1:-1,u[n]=new a(r)}}function f(e,n){if(1===n.length)return l(e[0],e[1],i(n[0]));var r=e.slice(0,Math.floor(e.length/2)),t=e.slice(Math.ceil(e.length/2));return l(f(r,n.slice(0,n.length-1)),f(t,n.slice(0,n.length-1)),i(n[n.length-1]))}function h(e,n,r,t){var l=[];0===u.length&&c(n);for(var i=0;i<2<>=1;l[i]=u[o(e,a)%u.length].dot(f)}return l}module.exports={lerpN:f,getNs:h};
31 | },{"./math":"CFdN"}],"wUKa":[function(require,module,exports) {
32 | "use strict";function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var n=0;n 0) {\n var thisPos = stack.indexOf(this)\n ~thisPos ? stack.splice(thisPos + 1) : stack.push(this)\n ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)\n if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value)\n }\n else stack.push(value)\n\n return replacer == null ? value : replacer.call(this, key, value)\n }\n}\n","/*\n * random-seed\n * https://github.com/skratchdot/random-seed\n *\n * This code was originally written by Steve Gibson and can be found here:\n *\n * https://www.grc.com/otg/uheprng.htm\n *\n * It was slightly modified for use in node, to pass jshint, and a few additional\n * helper functions were added.\n *\n * Copyright (c) 2013 skratchdot\n * Dual Licensed under the MIT license and the original GRC copyright/license\n * included below.\n */\n/*\t============================================================================\n\t\t\t\t\t\t\t\t\tGibson Research Corporation\n\t\t\t\tUHEPRNG - Ultra High Entropy Pseudo-Random Number Generator\n\t============================================================================\n\tLICENSE AND COPYRIGHT: THIS CODE IS HEREBY RELEASED INTO THE PUBLIC DOMAIN\n\tGibson Research Corporation releases and disclaims ALL RIGHTS AND TITLE IN\n\tTHIS CODE OR ANY DERIVATIVES. Anyone may be freely use it for any purpose.\n\t============================================================================\n\tThis is GRC's cryptographically strong PRNG (pseudo-random number generator)\n\tfor JavaScript. It is driven by 1536 bits of entropy, stored in an array of\n\t48, 32-bit JavaScript variables. Since many applications of this generator,\n\tincluding ours with the \"Off The Grid\" Latin Square generator, may require\n\tthe deteriministic re-generation of a sequence of PRNs, this PRNG's initial\n\tentropic state can be read and written as a static whole, and incrementally\n\tevolved by pouring new source entropy into the generator's internal state.\n\t----------------------------------------------------------------------------\n\tENDLESS THANKS are due Johannes Baagoe for his careful development of highly\n\trobust JavaScript implementations of JS PRNGs. This work was based upon his\n\tJavaScript \"Alea\" PRNG which is based upon the extremely robust Multiply-\n\tWith-Carry (MWC) PRNG invented by George Marsaglia. MWC Algorithm References:\n\thttp://www.GRC.com/otg/Marsaglia_PRNGs.pdf\n\thttp://www.GRC.com/otg/Marsaglia_MWC_Generators.pdf\n\t----------------------------------------------------------------------------\n\tThe quality of this algorithm's pseudo-random numbers have been verified by\n\tmultiple independent researchers. It handily passes the fermilab.ch tests as\n\twell as the \"diehard\" and \"dieharder\" test suites. For individuals wishing\n\tto further verify the quality of this algorithm's pseudo-random numbers, a\n\t256-megabyte file of this algorithm's output may be downloaded from GRC.com,\n\tand a Microsoft Windows scripting host (WSH) version of this algorithm may be\n\tdownloaded and run from the Windows command prompt to generate unique files\n\tof any size:\n\tThe Fermilab \"ENT\" tests: http://fourmilab.ch/random/\n\tThe 256-megabyte sample PRN file at GRC: https://www.GRC.com/otg/uheprng.bin\n\tThe Windows scripting host version: https://www.GRC.com/otg/wsh-uheprng.js\n\t----------------------------------------------------------------------------\n\tQualifying MWC multipliers are: 187884, 686118, 898134, 1104375, 1250205,\n\t1460910 and 1768863. (We use the largest one that's < 2^21)\n\t============================================================================ */\n'use strict';\nvar stringify = require('json-stringify-safe');\n\n/*\t============================================================================\nThis is based upon Johannes Baagoe's carefully designed and efficient hash\nfunction for use with JavaScript. It has a proven \"avalanche\" effect such\nthat every bit of the input affects every bit of the output 50% of the time,\nwhich is good.\tSee: http://baagoe.com/en/RandomMusings/hash/avalanche.xhtml\n============================================================================\n*/\nvar Mash = function () {\n\tvar n = 0xefc8249d;\n\tvar mash = function (data) {\n\t\tif (data) {\n\t\t\tdata = data.toString();\n\t\t\tfor (var i = 0; i < data.length; i++) {\n\t\t\t\tn += data.charCodeAt(i);\n\t\t\t\tvar h = 0.02519603282416938 * n;\n\t\t\t\tn = h >>> 0;\n\t\t\t\th -= n;\n\t\t\t\th *= n;\n\t\t\t\tn = h >>> 0;\n\t\t\t\th -= n;\n\t\t\t\tn += h * 0x100000000; // 2^32\n\t\t\t}\n\t\t\treturn (n >>> 0) * 2.3283064365386963e-10; // 2^-32\n\t\t} else {\n\t\t\tn = 0xefc8249d;\n\t\t}\n\t};\n\treturn mash;\n};\n\nvar uheprng = function (seed) {\n\treturn (function () {\n\t\tvar o = 48; // set the 'order' number of ENTROPY-holding 32-bit values\n\t\tvar c = 1; // init the 'carry' used by the multiply-with-carry (MWC) algorithm\n\t\tvar p = o; // init the 'phase' (max-1) of the intermediate variable pointer\n\t\tvar s = new Array(o); // declare our intermediate variables array\n\t\tvar i; // general purpose local\n\t\tvar j; // general purpose local\n\t\tvar k = 0; // general purpose local\n\n\t\t// when our \"uheprng\" is initially invoked our PRNG state is initialized from the\n\t\t// browser's own local PRNG. This is okay since although its generator might not\n\t\t// be wonderful, it's useful for establishing large startup entropy for our usage.\n\t\tvar mash = new Mash(); // get a pointer to our high-performance \"Mash\" hash\n\n\t\t// fill the array with initial mash hash values\n\t\tfor (i = 0; i < o; i++) {\n\t\t\ts[i] = mash(Math.random());\n\t\t}\n\n\t\t// this PRIVATE (internal access only) function is the heart of the multiply-with-carry\n\t\t// (MWC) PRNG algorithm. When called it returns a pseudo-random number in the form of a\n\t\t// 32-bit JavaScript fraction (0.0 to <1.0) it is a PRIVATE function used by the default\n\t\t// [0-1] return function, and by the random 'string(n)' function which returns 'n'\n\t\t// characters from 33 to 126.\n\t\tvar rawprng = function () {\n\t\t\tif (++p >= o) {\n\t\t\t\tp = 0;\n\t\t\t}\n\t\t\tvar t = 1768863 * s[p] + c * 2.3283064365386963e-10; // 2^-32\n\t\t\treturn s[p] = t - (c = t | 0);\n\t\t};\n\n\t\t// this EXPORTED function is the default function returned by this library.\n\t\t// The values returned are integers in the range from 0 to range-1. We first\n\t\t// obtain two 32-bit fractions (from rawprng) to synthesize a single high\n\t\t// resolution 53-bit prng (0 to <1), then we multiply this by the caller's\n\t\t// \"range\" param and take the \"floor\" to return a equally probable integer.\n\t\tvar random = function (range) {\n\t\t\treturn Math.floor(range * (rawprng() + (rawprng() * 0x200000 | 0) * 1.1102230246251565e-16)); // 2^-53\n\t\t};\n\n\t\t// this EXPORTED function 'string(n)' returns a pseudo-random string of\n\t\t// 'n' printable characters ranging from chr(33) to chr(126) inclusive.\n\t\trandom.string = function (count) {\n\t\t\tvar i;\n\t\t\tvar s = '';\n\t\t\tfor (i = 0; i < count; i++) {\n\t\t\t\ts += String.fromCharCode(33 + random(94));\n\t\t\t}\n\t\t\treturn s;\n\t\t};\n\n\t\t// this PRIVATE \"hash\" function is used to evolve the generator's internal\n\t\t// entropy state. It is also called by the EXPORTED addEntropy() function\n\t\t// which is used to pour entropy into the PRNG.\n\t\tvar hash = function () {\n\t\t\tvar args = Array.prototype.slice.call(arguments);\n\t\t\tfor (i = 0; i < args.length; i++) {\n\t\t\t\tfor (j = 0; j < o; j++) {\n\t\t\t\t\ts[j] -= mash(args[i]);\n\t\t\t\t\tif (s[j] < 0) {\n\t\t\t\t\t\ts[j] += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\t// this EXPORTED \"clean string\" function removes leading and trailing spaces and non-printing\n\t\t// control characters, including any embedded carriage-return (CR) and line-feed (LF) characters,\n\t\t// from any string it is handed. this is also used by the 'hashstring' function (below) to help\n\t\t// users always obtain the same EFFECTIVE uheprng seeding key.\n\t\trandom.cleanString = function (inStr) {\n\t\t\tinStr = inStr.replace(/(^\\s*)|(\\s*$)/gi, ''); // remove any/all leading spaces\n\t\t\tinStr = inStr.replace(/[\\x00-\\x1F]/gi, ''); // remove any/all control characters\n\t\t\tinStr = inStr.replace(/\\n /, '\\n'); // remove any/all trailing spaces\n\t\t\treturn inStr; // return the cleaned up result\n\t\t};\n\n\t\t// this EXPORTED \"hash string\" function hashes the provided character string after first removing\n\t\t// any leading or trailing spaces and ignoring any embedded carriage returns (CR) or Line Feeds (LF)\n\t\trandom.hashString = function (inStr) {\n\t\t\tinStr = random.cleanString(inStr);\n\t\t\tmash(inStr); // use the string to evolve the 'mash' state\n\t\t\tfor (i = 0; i < inStr.length; i++) { // scan through the characters in our string\n\t\t\t\tk = inStr.charCodeAt(i); // get the character code at the location\n\t\t\t\tfor (j = 0; j < o; j++) { //\t\"mash\" it into the UHEPRNG state\n\t\t\t\t\ts[j] -= mash(k);\n\t\t\t\t\tif (s[j] < 0) {\n\t\t\t\t\t\ts[j] += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\t// this EXPORTED function allows you to seed the random generator.\n\t\trandom.seed = function (seed) {\n\t\t\tif (typeof seed === 'undefined' || seed === null) {\n\t\t\t\tseed = Math.random();\n\t\t\t}\n\t\t\tif (typeof seed !== 'string') {\n\t\t\t\tseed = stringify(seed, function (key, value) {\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\treturn (value).toString();\n\t\t\t\t\t}\n\t\t\t\t\treturn value;\n\t\t\t\t});\n\t\t\t}\n\t\t\trandom.initState();\n\t\t\trandom.hashString(seed);\n\t\t};\n\n\t\t// this handy exported function is used to add entropy to our uheprng at any time\n\t\trandom.addEntropy = function ( /* accept zero or more arguments */ ) {\n\t\t\tvar args = [];\n\t\t\tfor (i = 0; i < arguments.length; i++) {\n\t\t\t\targs.push(arguments[i]);\n\t\t\t}\n\t\t\thash((k++) + (new Date().getTime()) + args.join('') + Math.random());\n\t\t};\n\n\t\t// if we want to provide a deterministic startup context for our PRNG,\n\t\t// but without directly setting the internal state variables, this allows\n\t\t// us to initialize the mash hash and PRNG's internal state before providing\n\t\t// some hashing input\n\t\trandom.initState = function () {\n\t\t\tmash(); // pass a null arg to force mash hash to init\n\t\t\tfor (i = 0; i < o; i++) {\n\t\t\t\ts[i] = mash(' '); // fill the array with initial mash hash values\n\t\t\t}\n\t\t\tc = 1; // init our multiply-with-carry carry\n\t\t\tp = o; // init our phase\n\t\t};\n\n\t\t// we use this (optional) exported function to signal the JavaScript interpreter\n\t\t// that we're finished using the \"Mash\" hash function so that it can free up the\n\t\t// local \"instance variables\" is will have been maintaining. It's not strictly\n\t\t// necessary, of course, but it's good JavaScript citizenship.\n\t\trandom.done = function () {\n\t\t\tmash = null;\n\t\t};\n\n\t\t// if we called \"uheprng\" with a seed value, then execute random.seed() before returning\n\t\tif (typeof seed !== 'undefined') {\n\t\t\trandom.seed(seed);\n\t\t}\n\n\t\t// Returns a random integer between 0 (inclusive) and range (exclusive)\n\t\trandom.range = function (range) {\n\t\t\treturn random(range);\n\t\t};\n\n\t\t// Returns a random float between 0 (inclusive) and 1 (exclusive)\n\t\trandom.random = function () {\n\t\t\treturn random(Number.MAX_VALUE - 1) / Number.MAX_VALUE;\n\t\t};\n\n\t\t// Returns a random float between min (inclusive) and max (exclusive)\n\t\trandom.floatBetween = function (min, max) {\n\t\t\treturn random.random() * (max - min) + min;\n\t\t};\n\n\t\t// Returns a random integer between min (inclusive) and max (inclusive)\n\t\trandom.intBetween = function (min, max) {\n\t\t\treturn Math.floor(random.random() * (max - min + 1)) + min;\n\t\t};\n\n\t\t// when our main outer \"uheprng\" function is called, after setting up our\n\t\t// initial variables and entropic state, we return an \"instance pointer\"\n\t\t// to the internal anonymous function which can then be used to access\n\t\t// the uheprng's various exported functions. As with the \".done\" function\n\t\t// above, we should set the returned value to 'null' once we're finished\n\t\t// using any of these functions.\n\t\treturn random;\n\t}());\n};\n\n// Modification for use in node:\nuheprng.create = function (seed) {\n\treturn new uheprng(seed);\n};\nmodule.exports = uheprng;\n","'use strict'\n\nconst rand = require('random-seed')\n\nclass Noise {\n constructor (s) {\n this.p = new Uint8Array(512)\n this.seed(s)\n }\n\n gen () {}\n\n seed (s) {\n const rng = rand.create(s || Math.random())\n\n for (let i = 0; i < 256; i++) this.p[i] = i\n for (let i = 0; i < 256; i++) {\n const r = rng(256)\n const temp = this.p[i]\n this.p[i] = this.p[r]\n this.p[r] = temp\n }\n for (let i = 0; i < 256; i++) this.p[i + 256] = this.p[i]\n }\n\n transform (fn) {\n const transformedFn = (...dims) => fn.apply(this, dims)\n\n return transformedFn.bind(this)\n }\n\n octavate (...args) {\n const octaves = args[0]\n const dims = args.slice(1)\n let val = 0\n let max = 0\n\n for (let i = 0; i < octaves; i++) {\n const w = 1 << i\n val += this.gen.apply(this, dims.map(x => x * w)) / w\n }\n\n for (let i = 0; i < octaves; i++) {\n max += 1 / (1 << i)\n }\n\n return val / max\n }\n}\n\nmodule.exports = Noise\n","'use strict'\n\nclass Vec1 {\n constructor (x) {\n this.x = x\n }\n\n dot (x) {\n return this.x * x\n }\n}\n\nconst g1 = [ new Vec1(1), new Vec1(-1) ]\n\nfunction grad1 (p, x) {\n return g1[p[x] % g1.length]\n}\n\nmodule.exports = {\n grad1\n}\n","'use strict'\n\nfunction falloff (...args) {\n const dims = args.slice(1)\n const t = args[0] - dims.reduce((sum, val) => {\n return sum + val * val\n }, 0)\n\n return t * t * t * t\n}\n\nfunction lerp (a, b, t) {\n return a * (1 - t) + b * t\n}\nfunction fade (t) {\n return t * t * t * (10 + t * (-15 + t * 6))\n}\nconst cut1 = falloff.bind(null, 1)\nconst cut = falloff.bind(null, 0.5)\n\nmodule.exports = {\n lerp,\n fade,\n cut1,\n cut\n}\n","'use strict'\n\nconst Noise = require('../util/noise')\nconst { grad1 } = require('../util/1d')\nconst { cut1 } = require('../util/math')\n\nclass Simplex1 extends Noise {\n gen (x) {\n const gx = Math.floor(x) % 256\n const dx = x - gx\n\n const n0 = cut1(dx) * grad1(this.p, gx).dot(dx)\n const n1 = cut1(dx - 1) * grad1(this.p, gx + 1).dot(dx - 1)\n\n return 0.5 * (n0 + n1)\n }\n}\n\nmodule.exports = Simplex1\n","'use strict'\n\nclass Vec2 {\n constructor (x, y) {\n this.x = x\n this.y = y\n }\n\n dot (x, y) {\n return this.x * x + this.y * y\n }\n}\n\nconst g2 = [\n new Vec2(1, 0), new Vec2(1, 1), new Vec2(0, 1), new Vec2(-1, 1),\n new Vec2(-1, 0), new Vec2(-1, -1), new Vec2(0, -1), new Vec2(1, -1)\n]\n\nfunction grad2 (p, x, y) {\n const hash = p[x + p[y]] % g2.length\n return g2[hash]\n}\nconst S2_TO_C = 0.5 * (Math.sqrt(3) - 1)\nconst C_TO_S2 = (3 - Math.sqrt(3)) / 6\n\nmodule.exports = {\n grad2,\n S2_TO_C,\n C_TO_S2\n}\n","'use strict'\n\nconst Noise = require('../util/noise')\nconst { grad2, S2_TO_C, C_TO_S2 } = require('../util/2d')\nconst { cut } = require('../util/math')\n\nclass Simplex2 extends Noise {\n gen (x, y) {\n const skew = (x + y) * S2_TO_C\n const i = Math.trunc(x + skew)\n const j = Math.trunc(y + skew)\n\n const unskew = (i + j) * C_TO_S2\n const gx = i - unskew\n const gy = j - unskew\n\n const dx0 = x - gx\n const dy0 = y - gy\n\n const di = dx0 > dy0 ? 1 : 0\n const dj = dx0 > dy0 ? 0 : 1\n\n const dx1 = dx0 - di + C_TO_S2\n const dy1 = dy0 - dj + C_TO_S2\n const dx2 = dx0 - 1 + 2 * C_TO_S2\n const dy2 = dy0 - 1 + 2 * C_TO_S2\n\n const n0 = cut(dx0, dy0) * grad2(this.p, i, j).dot(dx0, dy0)\n const n1 = cut(dx1, dy1) * grad2(this.p, i + di, j + dj).dot(dx1, dy1)\n const n2 = cut(dx2, dy2) * grad2(this.p, i + 1, j + 1).dot(dx2, dy2)\n\n return 70 * (n0 + n1 + n2)\n }\n}\n\nmodule.exports = Simplex2\n","'use strict'\n\nconst Noise = require('../util/noise')\nconst { grad1 } = require('../util/1d')\nconst { lerp, fade } = require('../util/math')\n\nclass Perlin1 extends Noise {\n gen (x) {\n const gx = Math.floor(x) % 256\n const dx = x - gx\n\n const n0 = grad1(this.p, gx).dot(dx)\n const n1 = grad1(this.p, gx + 1).dot(dx - 1)\n\n return lerp(n0, n1, fade(dx))\n }\n}\n\nmodule.exports = Perlin1\n","'use strict'\n\nconst Noise = require('../util/noise')\nconst { grad2 } = require('../util/2d')\nconst { fade, lerp } = require('../util/math')\n\nclass Perlin2 extends Noise {\n gen (x, y) {\n const gx = Math.trunc(x) % 256\n const gy = Math.trunc(y) % 256\n\n const dx = x - gx\n const dy = y - gy\n\n const n00 = grad2(this.p, gx, gy).dot(dx, dy)\n const n10 = grad2(this.p, gx + 1, gy).dot(dx - 1, dy)\n const n01 = grad2(this.p, gx, gy + 1).dot(dx, dy - 1)\n const n11 = grad2(this.p, gx + 1, gy + 1).dot(dx - 1, dy - 1)\n\n return lerp(\n lerp(n00, n10, fade(dx)),\n lerp(n01, n11, fade(dx)),\n fade(dy)\n )\n }\n}\n\nmodule.exports = Perlin2\n","'use strict'\n\nclass Vec3 {\n constructor (x, y, z) {\n this.x = x\n this.y = y\n this.z = z\n }\n\n dot (x, y, z) {\n return this.x * x + this.y * y + this.z * z\n }\n}\n\nconst g3 = [\n new Vec3(1, 1, 1), new Vec3(-1, 1, 1), new Vec3(1, -1, 1), new Vec3(-1, -1, 1),\n new Vec3(1, 1, 0), new Vec3(-1, 1, 0), new Vec3(1, -1, 0), new Vec3(-1, -1, 0),\n new Vec3(1, 1, -1), new Vec3(-1, 1, -1), new Vec3(1, -1, -1), new Vec3(-1, -1, -1)\n]\n\nfunction grad3 (p, x, y, z) {\n const hash = p[x + p[y + p[z]]] % g3.length\n return g3[hash]\n}\n\nmodule.exports = {\n grad3\n}\n","'use strict'\n\nconst Noise = require('../util/noise')\nconst { grad3 } = require('../util/3d')\nconst { fade, lerp } = require('../util/math')\n\nclass Perlin3 extends Noise {\n gen (x, y, z) {\n const gx = Math.trunc(x) % 256\n const gy = Math.trunc(y) % 256\n const gz = Math.trunc(z) % 256\n\n const dx = x - gx\n const dy = y - gy\n const dz = z - gz\n\n const n000 = grad3(this.p, gx, gy, gz).dot(dx, dy, dz)\n const n100 = grad3(this.p, gx + 1, gy, gz).dot(dx - 1, dy, dz)\n const n010 = grad3(this.p, gx, gy + 1, gz).dot(dx, dy - 1, dz)\n const n110 = grad3(this.p, gx + 1, gy + 1, gz).dot(dx - 1, dy - 1, dz)\n const n001 = grad3(this.p, gx, gy, gz + 1).dot(dx, dy, dz - 1)\n const n101 = grad3(this.p, gx + 1, gy, gz + 1).dot(dx - 1, dy, dz - 1)\n const n011 = grad3(this.p, gx, gy + 1, gz + 1).dot(dx, dy - 1, dz - 1)\n const n111 = grad3(this.p, gx + 1, gy + 1, gz + 1).dot(dx - 1, dy - 1, dz - 1)\n\n return lerp(\n lerp(\n lerp(n000, n100, dx),\n lerp(n010, n110, dx),\n fade(dy)\n ),\n lerp(\n lerp(n001, n101, dx),\n lerp(n011, n111, dx),\n fade(dy)\n ),\n fade(dz)\n )\n }\n}\n\nmodule.exports = Perlin3\n","'use strict'\n\nclass Vec4 {\n constructor (x, y, z, t) {\n this.x = x\n this.y = y\n this.z = z\n this.t = t\n }\n\n dot (x, y, z, t) {\n return this.x * x + this.y * y + this.z * z + this.t * t\n }\n}\n\nconst g4 = [\n new Vec4(0, 1, 1, 1), new Vec4(0, 1, 1, -1), new Vec4(0, 1, -1, 1), new Vec4(0, 1, -1, -1),\n new Vec4(0, -1, 1, 1), new Vec4(0, -1, 1, -1), new Vec4(0, -1, -1, 1), new Vec4(0, -1, -1, -1),\n new Vec4(1, 0, 1, 1), new Vec4(1, 0, 1, -1), new Vec4(1, 0, -1, 1), new Vec4(1, 0, -1, -1),\n new Vec4(-1, 0, 1, 1), new Vec4(-1, 0, 1, -1), new Vec4(-1, 0, -1, 1), new Vec4(-1, 0, -1, -1),\n new Vec4(1, 1, 0, 1), new Vec4(1, 1, 0, -1), new Vec4(1, -1, 0, 1), new Vec4(1, -1, 0, -1),\n new Vec4(-1, 1, 0, 1), new Vec4(-1, 1, 0, -1), new Vec4(-1, -1, 0, 1), new Vec4(-1, -1, 0, -1),\n new Vec4(1, 1, 1, 0), new Vec4(1, 1, -1, 0), new Vec4(1, -1, 1, 0), new Vec4(1, -1, -1, 0),\n new Vec4(-1, 1, 1, 0), new Vec4(-1, 1, -1, 0), new Vec4(-1, -1, 1, 0), new Vec4(-1, -1, -1, 0)\n]\n\nfunction grad4 (p, x, y, z, t) {\n const hash = p[x + p[y + p[z + p[t]]]] % g4.length\n return g4[hash]\n}\n\nmodule.exports = {\n grad4\n}\n","'use strict'\n\nconst Noise = require('../util/noise')\nconst { grad4 } = require('../util/4d')\nconst { fade, lerp } = require('../util/math')\n\nclass Perlin4 extends Noise {\n gen (x, y, z, t) {\n const gx = Math.trunc(x) % 256\n const gy = Math.trunc(y) % 256\n const gz = Math.trunc(z) % 256\n const gt = Math.trunc(t) % 256\n\n const dx = x - gx\n const dy = y - gy\n const dz = z - gz\n const dt = t - gt\n\n const n0000 = grad4(this.p, gx, gy, gz, gt).dot(dx, dy, dz, dt)\n const n1000 = grad4(this.p, gx + 1, gy, gz, gt).dot(dx - 1, dy, dz)\n const n0100 = grad4(this.p, gx, gy + 1, gz, gt).dot(dx, dy - 1, dz)\n const n1100 = grad4(this.p, gx + 1, gy + 1, gz, gt).dot(dx - 1, dy - 1, dz)\n const n0010 = grad4(this.p, gx, gy, gz + 1, gt).dot(dx, dy, dz - 1)\n const n1010 = grad4(this.p, gx + 1, gy, gz + 1, gt).dot(dx - 1, dy, dz - 1)\n const n0110 = grad4(this.p, gx, gy + 1, gz + 1, gt).dot(dx, dy - 1, dz - 1)\n const n1110 = grad4(this.p, gx + 1, gy + 1, gz + 1, gt).dot(dx - 1, dy - 1, dz - 1)\n const n0001 = grad4(this.p, gx, gy, gz, gt + 1).dot(dx, dy, dz, dt - 1)\n const n1001 = grad4(this.p, gx + 1, gy, gz, gt + 1).dot(dx - 1, dy, dz, dt - 1)\n const n0101 = grad4(this.p, gx, gy + 1, gz, gt + 1).dot(dx, dy - 1, dz, dt - 1)\n const n1101 = grad4(this.p, gx + 1, gy + 1, gz, gt + 1).dot(dx - 1, dy - 1, dz, dt - 1)\n const n0011 = grad4(this.p, gx, gy, gz + 1, gt + 1).dot(dx, dy, dz - 1, dt - 1)\n const n1011 = grad4(this.p, gx + 1, gy, gz + 1, gt + 1).dot(dx - 1, dy, dz - 1, dt - 1)\n const n0111 = grad4(this.p, gx, gy + 1, gz + 1, gt + 1).dot(dx, dy - 1, dz - 1, dt - 1)\n const n1111 = grad4(this.p, gx + 1, gy + 1, gz + 1, gt + 1).dot(dx - 1, dy - 1, dz - 1, dt - 1)\n\n return lerp(\n lerp(\n lerp(\n lerp(n0000, n1000, dx),\n lerp(n0100, n1100, dx),\n fade(dy)\n ),\n lerp(\n lerp(n0010, n1010, dx),\n lerp(n0110, n1110, dx),\n fade(dy)\n ),\n fade(dz)\n ),\n lerp(\n lerp(\n lerp(n0001, n1001, dx),\n lerp(n0101, n1101, dx),\n fade(dy)\n ),\n lerp(\n lerp(n0011, n1011, dx),\n lerp(n0111, n1111, dx),\n fade(dy)\n ),\n fade(dz)\n ),\n fade(dt)\n )\n }\n}\n\nmodule.exports = Perlin4\n","'use strict'\n\nconst { lerp, fade } = require('./math')\n\nfunction hashN (p, gs) {\n if (gs.length === 1) return p[gs[0]]\n\n return p[gs[0] + hashN(p, gs.slice(1))]\n}\n\nclass VecN {\n constructor (R) {\n this.R = R\n }\n\n dot (R) {\n let val = 0\n\n for (let i = 0; i < R.length; i++) {\n val += this.R[i] * R[i]\n }\n\n return val\n }\n}\n\nconst gN = []\nfunction generateGN (dim) {\n for (let i = 0; i < dim * 2; i++) {\n const vec = new Array(dim).fill(0)\n\n vec[i % dim] = i / dim >= 1 ? 1 : -1\n gN[i] = new VecN(vec)\n }\n}\n\nfunction lerpN (ns, ds) {\n if (ds.length === 1) return lerp(ns[0], ns[1], fade(ds[0]))\n\n const ns1 = ns.slice(0, Math.floor(ns.length / 2))\n const ns2 = ns.slice(Math.ceil(ns.length / 2))\n\n return lerp(\n lerpN(ns1, ds.slice(0, ds.length - 1)),\n lerpN(ns2, ds.slice(0, ds.length - 1)),\n fade(ds[ds.length - 1])\n )\n}\nfunction getNs (p, dim, gs, ds) {\n const ns = []\n\n if (gN.length === 0) {\n generateGN(dim)\n }\n\n for (let i = 0; i < (2 << (dim - 1)); i++) {\n const gsPerm = gs.slice()\n const dsPerm = ds.slice()\n let temp = i\n\n for (let j = 0; j < dim; j++) {\n if (temp & 1) {\n gsPerm[j] += 1\n dsPerm[j] -= 1\n }\n temp = temp >> 1\n }\n ns[i] = gN[hashN(p, gsPerm) % gN.length].dot(dsPerm)\n }\n\n return ns\n}\n\nmodule.exports = {\n lerpN,\n getNs\n}\n","'use strict'\n\nconst Noise = require('../util/noise')\nconst { lerpN, getNs } = require('../util/nd')\n\nclass PerlinN extends Noise {\n gen (...args) {\n const gs = []\n const ds = []\n\n for (let i = 0; i < args.length; i++) {\n gs[i] = Math.trunc(args[i]) % 256\n ds[i] = args[i] - gs[i]\n }\n\n const ns = getNs(this.p, args.length, gs, ds)\n\n return lerpN(ns, ds)\n }\n}\n\nmodule.exports = PerlinN\n","'use struct'\n\nconst Simplex1 = require('./simplex/simplex1')\nconst Simplex2 = require('./simplex/simplex2')\n\nconst Perlin1 = require('./perlin/perlin1')\nconst Perlin2 = require('./perlin/perlin2')\nconst Perlin3 = require('./perlin/perlin3')\nconst Perlin4 = require('./perlin/perlin4')\nconst PerlinN = require('./perlin/perlinN')\n\nmodule.exports = {\n Simplex1,\n Simplex2,\n Perlin1,\n Perlin2,\n Perlin3,\n Perlin4,\n PerlinN\n}\n","'use strict'\n\nmodule.exports = require('./lib')\n"]}
--------------------------------------------------------------------------------
/images/abs.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipjscott/tumult/31d03d7f722c05319d3d56e20b095c60abad9a99/images/abs.png
--------------------------------------------------------------------------------
/images/invert.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipjscott/tumult/31d03d7f722c05319d3d56e20b095c60abad9a99/images/invert.png
--------------------------------------------------------------------------------
/images/octaves.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipjscott/tumult/31d03d7f722c05319d3d56e20b095c60abad9a99/images/octaves.png
--------------------------------------------------------------------------------
/images/simplex.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipjscott/tumult/31d03d7f722c05319d3d56e20b095c60abad9a99/images/simplex.png
--------------------------------------------------------------------------------
/images/sin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/philipjscott/tumult/31d03d7f722c05319d3d56e20b095c60abad9a99/images/sin.png
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | module.exports = require('./lib')
4 |
--------------------------------------------------------------------------------
/lib/index.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Simplex1 = require('./simplex/simplex1')
4 | const Simplex2 = require('./simplex/simplex2')
5 |
6 | const Perlin1 = require('./perlin/perlin1')
7 | const Perlin2 = require('./perlin/perlin2')
8 | const Perlin3 = require('./perlin/perlin3')
9 | const Perlin4 = require('./perlin/perlin4')
10 | const PerlinN = require('./perlin/perlinN')
11 |
12 | module.exports = {
13 | Simplex1,
14 | Simplex2,
15 | Perlin1,
16 | Perlin2,
17 | Perlin3,
18 | Perlin4,
19 | PerlinN
20 | }
21 |
--------------------------------------------------------------------------------
/lib/perlin/perlin1.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Noise = require('../util/noise')
4 | const { grad1 } = require('../util/1d')
5 | const { lerp, fade } = require('../util/math')
6 |
7 | class Perlin1 extends Noise {
8 | gen (x) {
9 | const gx = Math.floor(x) % 256
10 | const dx = x - gx
11 |
12 | const n0 = grad1(this.p, gx).dot(dx)
13 | const n1 = grad1(this.p, gx + 1).dot(dx - 1)
14 |
15 | return lerp(n0, n1, fade(dx))
16 | }
17 | }
18 |
19 | module.exports = Perlin1
20 |
--------------------------------------------------------------------------------
/lib/perlin/perlin2.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Noise = require('../util/noise')
4 | const { grad2 } = require('../util/2d')
5 | const { fade, lerp } = require('../util/math')
6 |
7 | class Perlin2 extends Noise {
8 | gen (x, y) {
9 | const gx = Math.trunc(x) % 256
10 | const gy = Math.trunc(y) % 256
11 |
12 | const dx = x - gx
13 | const dy = y - gy
14 |
15 | const n00 = grad2(this.p, gx, gy).dot(dx, dy)
16 | const n10 = grad2(this.p, gx + 1, gy).dot(dx - 1, dy)
17 | const n01 = grad2(this.p, gx, gy + 1).dot(dx, dy - 1)
18 | const n11 = grad2(this.p, gx + 1, gy + 1).dot(dx - 1, dy - 1)
19 |
20 | return lerp(
21 | lerp(n00, n10, fade(dx)),
22 | lerp(n01, n11, fade(dx)),
23 | fade(dy)
24 | )
25 | }
26 | }
27 |
28 | module.exports = Perlin2
29 |
--------------------------------------------------------------------------------
/lib/perlin/perlin3.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Noise = require('../util/noise')
4 | const { grad3 } = require('../util/3d')
5 | const { fade, lerp } = require('../util/math')
6 |
7 | class Perlin3 extends Noise {
8 | gen (x, y, z) {
9 | const gx = Math.trunc(x) % 256
10 | const gy = Math.trunc(y) % 256
11 | const gz = Math.trunc(z) % 256
12 |
13 | const dx = x - gx
14 | const dy = y - gy
15 | const dz = z - gz
16 |
17 | const n000 = grad3(this.p, gx, gy, gz).dot(dx, dy, dz)
18 | const n100 = grad3(this.p, gx + 1, gy, gz).dot(dx - 1, dy, dz)
19 | const n010 = grad3(this.p, gx, gy + 1, gz).dot(dx, dy - 1, dz)
20 | const n110 = grad3(this.p, gx + 1, gy + 1, gz).dot(dx - 1, dy - 1, dz)
21 | const n001 = grad3(this.p, gx, gy, gz + 1).dot(dx, dy, dz - 1)
22 | const n101 = grad3(this.p, gx + 1, gy, gz + 1).dot(dx - 1, dy, dz - 1)
23 | const n011 = grad3(this.p, gx, gy + 1, gz + 1).dot(dx, dy - 1, dz - 1)
24 | const n111 = grad3(this.p, gx + 1, gy + 1, gz + 1).dot(dx - 1, dy - 1, dz - 1)
25 |
26 | return lerp(
27 | lerp(
28 | lerp(n000, n100, dx),
29 | lerp(n010, n110, dx),
30 | fade(dy)
31 | ),
32 | lerp(
33 | lerp(n001, n101, dx),
34 | lerp(n011, n111, dx),
35 | fade(dy)
36 | ),
37 | fade(dz)
38 | )
39 | }
40 | }
41 |
42 | module.exports = Perlin3
43 |
--------------------------------------------------------------------------------
/lib/perlin/perlin4.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Noise = require('../util/noise')
4 | const { grad4 } = require('../util/4d')
5 | const { fade, lerp } = require('../util/math')
6 |
7 | class Perlin4 extends Noise {
8 | gen (x, y, z, t) {
9 | const gx = Math.trunc(x) % 256
10 | const gy = Math.trunc(y) % 256
11 | const gz = Math.trunc(z) % 256
12 | const gt = Math.trunc(t) % 256
13 |
14 | const dx = x - gx
15 | const dy = y - gy
16 | const dz = z - gz
17 | const dt = t - gt
18 |
19 | const n0000 = grad4(this.p, gx, gy, gz, gt).dot(dx, dy, dz, dt)
20 | const n1000 = grad4(this.p, gx + 1, gy, gz, gt).dot(dx - 1, dy, dz, dt)
21 | const n0100 = grad4(this.p, gx, gy + 1, gz, gt).dot(dx, dy - 1, dz, dt)
22 | const n1100 = grad4(this.p, gx + 1, gy + 1, gz, gt).dot(dx - 1, dy - 1, dz, dt)
23 | const n0010 = grad4(this.p, gx, gy, gz + 1, gt).dot(dx, dy, dz - 1, dt)
24 | const n1010 = grad4(this.p, gx + 1, gy, gz + 1, gt).dot(dx - 1, dy, dz - 1, dt)
25 | const n0110 = grad4(this.p, gx, gy + 1, gz + 1, gt).dot(dx, dy - 1, dz - 1, dt)
26 | const n1110 = grad4(this.p, gx + 1, gy + 1, gz + 1, gt).dot(dx - 1, dy - 1, dz - 1, dt)
27 | const n0001 = grad4(this.p, gx, gy, gz, gt + 1).dot(dx, dy, dz, dt - 1)
28 | const n1001 = grad4(this.p, gx + 1, gy, gz, gt + 1).dot(dx - 1, dy, dz, dt - 1)
29 | const n0101 = grad4(this.p, gx, gy + 1, gz, gt + 1).dot(dx, dy - 1, dz, dt - 1)
30 | const n1101 = grad4(this.p, gx + 1, gy + 1, gz, gt + 1).dot(dx - 1, dy - 1, dz, dt - 1)
31 | const n0011 = grad4(this.p, gx, gy, gz + 1, gt + 1).dot(dx, dy, dz - 1, dt - 1)
32 | const n1011 = grad4(this.p, gx + 1, gy, gz + 1, gt + 1).dot(dx - 1, dy, dz - 1, dt - 1)
33 | const n0111 = grad4(this.p, gx, gy + 1, gz + 1, gt + 1).dot(dx, dy - 1, dz - 1, dt - 1)
34 | const n1111 = grad4(this.p, gx + 1, gy + 1, gz + 1, gt + 1).dot(dx - 1, dy - 1, dz - 1, dt - 1)
35 |
36 | return lerp(
37 | lerp(
38 | lerp(
39 | lerp(n0000, n1000, dx),
40 | lerp(n0100, n1100, dx),
41 | fade(dy)
42 | ),
43 | lerp(
44 | lerp(n0010, n1010, dx),
45 | lerp(n0110, n1110, dx),
46 | fade(dy)
47 | ),
48 | fade(dz)
49 | ),
50 | lerp(
51 | lerp(
52 | lerp(n0001, n1001, dx),
53 | lerp(n0101, n1101, dx),
54 | fade(dy)
55 | ),
56 | lerp(
57 | lerp(n0011, n1011, dx),
58 | lerp(n0111, n1111, dx),
59 | fade(dy)
60 | ),
61 | fade(dz)
62 | ),
63 | fade(dt)
64 | )
65 | }
66 | }
67 |
68 | module.exports = Perlin4
69 |
--------------------------------------------------------------------------------
/lib/perlin/perlinN.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Noise = require('../util/noise')
4 | const { lerpN, getNs } = require('../util/nd')
5 |
6 | class PerlinN extends Noise {
7 | gen (...args) {
8 | const gs = []
9 | const ds = []
10 |
11 | for (let i = 0; i < args.length; i++) {
12 | gs[i] = Math.trunc(args[i]) % 256
13 | ds[i] = args[i] - gs[i]
14 | }
15 |
16 | const ns = getNs(this.p, args.length, gs, ds)
17 |
18 | return lerpN(ns, ds)
19 | }
20 | }
21 |
22 | module.exports = PerlinN
23 |
--------------------------------------------------------------------------------
/lib/simplex/simplex1.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Noise = require('../util/noise')
4 | const { grad1 } = require('../util/1d')
5 | const { cut1 } = require('../util/math')
6 |
7 | class Simplex1 extends Noise {
8 | gen (x) {
9 | const gx = Math.floor(x) % 256
10 | const dx = x - gx
11 |
12 | const n0 = cut1(dx) * grad1(this.p, gx).dot(dx)
13 | const n1 = cut1(dx - 1) * grad1(this.p, gx + 1).dot(dx - 1)
14 |
15 | return 0.5 * (n0 + n1)
16 | }
17 | }
18 |
19 | module.exports = Simplex1
20 |
--------------------------------------------------------------------------------
/lib/simplex/simplex2.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Noise = require('../util/noise')
4 | const { grad2, S2_TO_C, C_TO_S2 } = require('../util/2d')
5 | const { cut } = require('../util/math')
6 |
7 | class Simplex2 extends Noise {
8 | gen (x, y) {
9 | const skew = (x + y) * S2_TO_C
10 | const i = Math.trunc(x + skew)
11 | const j = Math.trunc(y + skew)
12 |
13 | const unskew = (i + j) * C_TO_S2
14 | const gx = i - unskew
15 | const gy = j - unskew
16 |
17 | const dx0 = x - gx
18 | const dy0 = y - gy
19 |
20 | const di = dx0 > dy0 ? 1 : 0
21 | const dj = dx0 > dy0 ? 0 : 1
22 |
23 | const dx1 = dx0 - di + C_TO_S2
24 | const dy1 = dy0 - dj + C_TO_S2
25 | const dx2 = dx0 - 1 + 2 * C_TO_S2
26 | const dy2 = dy0 - 1 + 2 * C_TO_S2
27 |
28 | const n0 = cut(dx0, dy0) * grad2(this.p, i, j).dot(dx0, dy0)
29 | const n1 = cut(dx1, dy1) * grad2(this.p, i + di, j + dj).dot(dx1, dy1)
30 | const n2 = cut(dx2, dy2) * grad2(this.p, i + 1, j + 1).dot(dx2, dy2)
31 |
32 | return 70 * (n0 + n1 + n2)
33 | }
34 | }
35 |
36 | module.exports = Simplex2
37 |
--------------------------------------------------------------------------------
/lib/util/1d.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | class Vec1 {
4 | constructor (x) {
5 | this.x = x
6 | }
7 |
8 | dot (x) {
9 | return this.x * x
10 | }
11 | }
12 |
13 | const g1 = [ new Vec1(1), new Vec1(-1) ]
14 |
15 | function grad1 (p, x) {
16 | return g1[p[x] % g1.length]
17 | }
18 |
19 | module.exports = {
20 | grad1
21 | }
22 |
--------------------------------------------------------------------------------
/lib/util/2d.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | class Vec2 {
4 | constructor (x, y) {
5 | this.x = x
6 | this.y = y
7 | }
8 |
9 | dot (x, y) {
10 | return this.x * x + this.y * y
11 | }
12 | }
13 |
14 | const g2 = [
15 | new Vec2(1, 0), new Vec2(1, 1), new Vec2(0, 1), new Vec2(-1, 1),
16 | new Vec2(-1, 0), new Vec2(-1, -1), new Vec2(0, -1), new Vec2(1, -1)
17 | ]
18 |
19 | function grad2 (p, x, y) {
20 | const hash = p[x + p[y]] % g2.length
21 | return g2[hash]
22 | }
23 | const S2_TO_C = 0.5 * (Math.sqrt(3) - 1)
24 | const C_TO_S2 = (3 - Math.sqrt(3)) / 6
25 |
26 | module.exports = {
27 | grad2,
28 | S2_TO_C,
29 | C_TO_S2
30 | }
31 |
--------------------------------------------------------------------------------
/lib/util/3d.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | class Vec3 {
4 | constructor (x, y, z) {
5 | this.x = x
6 | this.y = y
7 | this.z = z
8 | }
9 |
10 | dot (x, y, z) {
11 | return this.x * x + this.y * y + this.z * z
12 | }
13 | }
14 |
15 | const g3 = [
16 | new Vec3(1, 1, 1), new Vec3(-1, 1, 1), new Vec3(1, -1, 1), new Vec3(-1, -1, 1),
17 | new Vec3(1, 1, 0), new Vec3(-1, 1, 0), new Vec3(1, -1, 0), new Vec3(-1, -1, 0),
18 | new Vec3(1, 1, -1), new Vec3(-1, 1, -1), new Vec3(1, -1, -1), new Vec3(-1, -1, -1)
19 | ]
20 |
21 | function grad3 (p, x, y, z) {
22 | const hash = p[x + p[y + p[z]]] % g3.length
23 | return g3[hash]
24 | }
25 |
26 | module.exports = {
27 | grad3
28 | }
29 |
--------------------------------------------------------------------------------
/lib/util/4d.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | class Vec4 {
4 | constructor (x, y, z, t) {
5 | this.x = x
6 | this.y = y
7 | this.z = z
8 | this.t = t
9 | }
10 |
11 | dot (x, y, z, t) {
12 | return this.x * x + this.y * y + this.z * z + this.t * t
13 | }
14 | }
15 |
16 | const g4 = [
17 | new Vec4(0, 1, 1, 1), new Vec4(0, 1, 1, -1), new Vec4(0, 1, -1, 1), new Vec4(0, 1, -1, -1),
18 | new Vec4(0, -1, 1, 1), new Vec4(0, -1, 1, -1), new Vec4(0, -1, -1, 1), new Vec4(0, -1, -1, -1),
19 | new Vec4(1, 0, 1, 1), new Vec4(1, 0, 1, -1), new Vec4(1, 0, -1, 1), new Vec4(1, 0, -1, -1),
20 | new Vec4(-1, 0, 1, 1), new Vec4(-1, 0, 1, -1), new Vec4(-1, 0, -1, 1), new Vec4(-1, 0, -1, -1),
21 | new Vec4(1, 1, 0, 1), new Vec4(1, 1, 0, -1), new Vec4(1, -1, 0, 1), new Vec4(1, -1, 0, -1),
22 | new Vec4(-1, 1, 0, 1), new Vec4(-1, 1, 0, -1), new Vec4(-1, -1, 0, 1), new Vec4(-1, -1, 0, -1),
23 | new Vec4(1, 1, 1, 0), new Vec4(1, 1, -1, 0), new Vec4(1, -1, 1, 0), new Vec4(1, -1, -1, 0),
24 | new Vec4(-1, 1, 1, 0), new Vec4(-1, 1, -1, 0), new Vec4(-1, -1, 1, 0), new Vec4(-1, -1, -1, 0)
25 | ]
26 |
27 | function grad4 (p, x, y, z, t) {
28 | const hash = p[x + p[y + p[z + p[t]]]] % g4.length
29 | return g4[hash]
30 | }
31 |
32 | module.exports = {
33 | grad4
34 | }
35 |
--------------------------------------------------------------------------------
/lib/util/math.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | function falloff (...args) {
4 | const dims = args.slice(1)
5 | const t = args[0] - dims.reduce((sum, val) => {
6 | return sum + val * val
7 | }, 0)
8 |
9 | return t * t * t * t
10 | }
11 |
12 | function lerp (a, b, t) {
13 | return a * (1 - t) + b * t
14 | }
15 | function fade (t) {
16 | return t * t * t * (10 + t * (-15 + t * 6))
17 | }
18 | const cut1 = falloff.bind(null, 1)
19 | const cut = falloff.bind(null, 0.5)
20 |
21 | module.exports = {
22 | lerp,
23 | fade,
24 | cut1,
25 | cut
26 | }
27 |
--------------------------------------------------------------------------------
/lib/util/nd.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const { lerp, fade } = require('./math')
4 |
5 | function hashN (p, gs) {
6 | if (gs.length === 1) return p[gs[0]]
7 |
8 | return p[gs[0] + hashN(p, gs.slice(1))]
9 | }
10 |
11 | class VecN {
12 | constructor (R) {
13 | this.R = R
14 | }
15 |
16 | dot (R) {
17 | let val = 0
18 |
19 | for (let i = 0; i < R.length; i++) {
20 | val += this.R[i] * R[i]
21 | }
22 |
23 | return val
24 | }
25 | }
26 |
27 | const gN = []
28 | function generateGN (dim) {
29 | for (let i = 0; i < dim * 2; i++) {
30 | const vec = new Array(dim).fill(0)
31 |
32 | vec[i % dim] = i / dim >= 1 ? 1 : -1
33 | gN[i] = new VecN(vec)
34 | }
35 | }
36 |
37 | function lerpN (ns, ds) {
38 | if (ds.length === 1) return lerp(ns[0], ns[1], fade(ds[0]))
39 |
40 | const ns1 = ns.slice(0, Math.floor(ns.length / 2))
41 | const ns2 = ns.slice(Math.ceil(ns.length / 2))
42 |
43 | return lerp(
44 | lerpN(ns1, ds.slice(0, ds.length - 1)),
45 | lerpN(ns2, ds.slice(0, ds.length - 1)),
46 | fade(ds[ds.length - 1])
47 | )
48 | }
49 | function getNs (p, dim, gs, ds) {
50 | const ns = []
51 |
52 | if (gN.length === 0) {
53 | generateGN(dim)
54 | }
55 |
56 | for (let i = 0; i < (2 << (dim - 1)); i++) {
57 | const gsPerm = gs.slice()
58 | const dsPerm = ds.slice()
59 | let temp = i
60 |
61 | for (let j = 0; j < dim; j++) {
62 | if (temp & 1) {
63 | gsPerm[j] += 1
64 | dsPerm[j] -= 1
65 | }
66 | temp = temp >> 1
67 | }
68 | ns[i] = gN[hashN(p, gsPerm) % gN.length].dot(dsPerm)
69 | }
70 |
71 | return ns
72 | }
73 |
74 | module.exports = {
75 | lerpN,
76 | getNs
77 | }
78 |
--------------------------------------------------------------------------------
/lib/util/noise.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const rand = require('random-seed')
4 |
5 | class Noise {
6 | constructor (s) {
7 | this.p = new Uint8Array(512)
8 | this.seed(s)
9 | }
10 |
11 | gen () {}
12 |
13 | seed (s) {
14 | const rng = rand.create(s || Math.random())
15 |
16 | for (let i = 0; i < 256; i++) this.p[i] = i
17 | for (let i = 0; i < 256; i++) {
18 | const r = rng(256)
19 | const temp = this.p[i]
20 | this.p[i] = this.p[r]
21 | this.p[r] = temp
22 | }
23 | for (let i = 0; i < 256; i++) this.p[i + 256] = this.p[i]
24 | }
25 |
26 | transform (fn) {
27 | const transformedFn = (...dims) => fn.apply(this, dims)
28 |
29 | return transformedFn.bind(this)
30 | }
31 |
32 | octavate (...args) {
33 | const octaves = args[0]
34 | const dims = args.slice(1)
35 | let val = 0
36 | let max = 0
37 |
38 | for (let i = 0; i < octaves; i++) {
39 | const w = 1 << i
40 | val += this.gen.apply(this, dims.map(x => x * w)) / w
41 | }
42 |
43 | for (let i = 0; i < octaves; i++) {
44 | max += 1 / (1 << i)
45 | }
46 |
47 | return val / max
48 | }
49 | }
50 |
51 | module.exports = Noise
52 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "tumult",
3 | "author": "Philip Scott",
4 | "version": "0.0.0-development",
5 | "license": "MIT",
6 | "description": "A Javascript noise library",
7 | "main": "index.js",
8 | "unpkg": "dist/tumult.min.js",
9 | "jsdelivr": "dist/tumult.min.js",
10 | "scripts": {
11 | "lint": "standard",
12 | "fix": "standard --fix",
13 | "test": "standard && nyc ava",
14 | "coverage": "nyc report --reporter=text-lcov | coveralls",
15 | "build": "parcel build --global tumult --out-dir dist --out-file tumult.min.js index.js",
16 | "pages": "parcel build demo/index.html --out-dir pages --public-url '.'",
17 | "travis-deploy-once": "travis-deploy-once --pro",
18 | "semantic-release": "semantic-release"
19 | },
20 | "keywords": [
21 | "noise",
22 | "perlin",
23 | "perlin noise",
24 | "simplex",
25 | "simplex noise",
26 | "procedural generation"
27 | ],
28 | "dependencies": {
29 | "random-seed": "^0.3.0"
30 | },
31 | "devDependencies": {
32 | "ava": "^1.1.0",
33 | "coveralls": "^3.0.2",
34 | "nyc": "^14.1.1",
35 | "parcel-bundler": "^1.10.3",
36 | "semantic-release": "^15.12.1",
37 | "standard": "^12.0.1",
38 | "terrapaint": "^2.0.1",
39 | "travis-deploy-once": "^5.0.9"
40 | },
41 | "repository": {
42 | "type": "git",
43 | "url": "https://github.com/ScottyFillups/tumult.git"
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const test = require('ava')
4 | const tumult = require('../')
5 |
6 | function loopcall (depth, iters, fn, memo = null) {
7 | if (memo === null) {
8 | memo = new Array(depth).fill(0)
9 | }
10 |
11 | for (let i = 0; i < iters; i++) {
12 | if (depth <= 1) {
13 | fn(memo)
14 | } else {
15 | loopcall(depth - 1, iters, fn, memo)
16 | }
17 |
18 | memo[depth - 1] += 1
19 | }
20 |
21 | memo[depth - 1] = 0
22 | }
23 |
24 | function rangeMacro (t, fn) {
25 | // iters will significantly affect performance
26 | const iters = 5
27 | const constructors = [
28 | { name: 'Simplex1', d: 1 },
29 | { name: 'Simplex2', d: 2 },
30 | { name: 'Perlin1', d: 1 },
31 | { name: 'Perlin2', d: 2 },
32 | { name: 'Perlin3', d: 3 },
33 | { name: 'Perlin4', d: 4 },
34 | { name: 'PerlinN', d: 5 }
35 | ]
36 |
37 | constructors.forEach(ctor => {
38 | const n = new tumult[ctor.name]()
39 |
40 | loopcall(ctor.d, iters, args => {
41 | const val = fn(n, args)
42 |
43 | if (!Number.isFinite(val) || val > 1 || val < -1) {
44 | t.fail(`Invalid noise value [${val}]`)
45 | }
46 | })
47 | })
48 |
49 | t.pass()
50 | }
51 |
52 | test('returns value within [-1, 1] for all noise types', rangeMacro,
53 | (n, args) => n.gen(...args.map(x => x / 128)))
54 |
55 | test('returns value within [-1, 1] for all noise types with integer input', rangeMacro,
56 | (n, args) => n.gen(...args))
57 |
58 | test('returns value within [-1, 1] for fractal noise', rangeMacro,
59 | (n, args) => n.octavate(5, ...args))
60 |
61 | test('returns value within [-1, 1] for fractal noise with integer input', rangeMacro,
62 | (n, args) => n.octavate(5, ...args))
63 |
64 | test('noise generators with same seed are equal', t => {
65 | const seed = 'hello'
66 | const a = new tumult.Simplex1(seed)
67 | const b = new tumult.Simplex1(seed)
68 |
69 | for (let i = 0; i < 256; i++) {
70 | const av = a.gen(i / 256)
71 | const bv = b.gen(i / 256)
72 |
73 | if (Math.abs(av - bv) >= Number.EPSILON) {
74 | t.fail()
75 | }
76 | }
77 |
78 | t.pass()
79 | })
80 |
81 | // Note: transform is deprecated; wrap your noise function in another function
82 | test('transform works (I really don\'t know why I added that function...)', t => {
83 | const simplex = new tumult.Simplex1()
84 | const transform = simplex.transform(function (x, y) {
85 | return Math.sin(1 / this.gen(x, y))
86 | })
87 |
88 | for (let i = 0; i < 256; i++) {
89 | for (let j = 0; j < 256; j++) {
90 | const val = transform(i / 256, j / 256)
91 |
92 | if (val > 1 || val < -1) {
93 | t.fail()
94 | }
95 | }
96 | }
97 |
98 | t.pass()
99 | })
100 |
--------------------------------------------------------------------------------