Skip to content

Commit 3e3b327

Browse files
committed
Added Simplex Noise functions.
1 parent 34713ab commit 3e3b327

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

v3/src/math/noise/SimplexNoise.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* A fast javascript implementation of simplex noise by Jonas Wagner
3+
*
4+
* Based on a speed-improved simplex noise algorithm for 2D, 3D and 4D in Java.
5+
* Which is based on example code by Stefan Gustavson (stegu@itn.liu.se).
6+
* With Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
7+
* Better rank ordering method by Stefan Gustavson in 2012.
8+
*
9+
*
10+
* Copyright (C) 2016 Jonas Wagner
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining
13+
* a copy of this software and associated documentation files (the
14+
* "Software"), to deal in the Software without restriction, including
15+
* without limitation the rights to use, copy, modify, merge, publish,
16+
* distribute, sublicense, and/or sell copies of the Software, and to
17+
* permit persons to whom the Software is furnished to do so, subject to
18+
* the following conditions:
19+
*
20+
* The above copyright notice and this permission notice shall be
21+
* included in all copies or substantial portions of the Software.
22+
*
23+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30+
*
31+
*/
32+
33+
var F2 = 0.5 * (Math.sqrt(3.0) - 1.0);
34+
var G2 = (3.0 - Math.sqrt(3.0)) / 6.0;
35+
36+
function buildPermutationTable (random)
37+
{
38+
var i;
39+
var p = new Uint8Array(256);
40+
41+
for (i = 0; i < 256; i++)
42+
{
43+
p[i] = i;
44+
}
45+
46+
for (i = 0; i < 255; i++)
47+
{
48+
var r = i + ~~(random() * (256 - i));
49+
var aux = p[i];
50+
p[i] = p[r];
51+
p[r] = aux;
52+
}
53+
54+
return p;
55+
}
56+
57+
var SimplexNoise = function (random)
58+
{
59+
if (!random) { random = Math.random; }
60+
61+
this.p = buildPermutationTable(random);
62+
this.perm = new Uint8Array(512);
63+
this.permMod12 = new Uint8Array(512);
64+
65+
for (var i = 0; i < 512; i++)
66+
{
67+
this.perm[i] = this.p[i & 255];
68+
this.permMod12[i] = this.perm[i] % 12;
69+
}
70+
};
71+
72+
SimplexNoise.prototype.constructor = SimplexNoise;
73+
74+
SimplexNoise.prototype = {
75+
76+
grad3: new Float32Array(
77+
[ 1, 1, 0 ], [ -1, 1, 0 ], [ 1, -1, 0 ], [ -1, -1, 0 ],
78+
[ 1, 0, 1 ], [ -1, 0, 1 ], [ 1, 0, -1 ], [ -1, 0, -1 ],
79+
[ 0, 1, 1 ], [ 0, -1, 1 ], [ 0, 1, -1 ], [ 0, -1, -1 ]),
80+
81+
noise2D: function (xin, yin)
82+
{
83+
var permMod12 = this.permMod12;
84+
var perm = this.perm;
85+
var grad3 = this.grad3;
86+
var n0 = 0; // Noise contributions from the three corners
87+
var n1 = 0;
88+
var n2 = 0;
89+
90+
// Skew the input space to determine which simplex cell we're in
91+
var s = (xin + yin) * F2; // Hairy factor for 2D
92+
var i = Math.floor(xin + s);
93+
var j = Math.floor(yin + s);
94+
var t = (i + j) * G2;
95+
var X0 = i - t; // Unskew the cell origin back to (x,y) space
96+
var Y0 = j - t;
97+
var x0 = xin - X0; // The x,y distances from the cell origin
98+
var y0 = yin - Y0;
99+
100+
// For the 2D case, the simplex shape is an equilateral triangle.
101+
// Determine which simplex we are in.
102+
var i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
103+
104+
if (x0 > y0)
105+
{
106+
// lower triangle, XY order: (0,0)->(1,0)->(1,1)
107+
i1 = 1;
108+
j1 = 0;
109+
}
110+
else
111+
{
112+
// upper triangle, YX order: (0,0)->(0,1)->(1,1)
113+
i1 = 0;
114+
j1 = 1;
115+
}
116+
117+
// A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
118+
// a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
119+
// c = (3-sqrt(3))/6
120+
var x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
121+
var y1 = y0 - j1 + G2;
122+
var x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
123+
var y2 = y0 - 1.0 + 2.0 * G2;
124+
125+
// Work out the hashed gradient indices of the three simplex corners
126+
var ii = i & 255;
127+
var jj = j & 255;
128+
129+
// Calculate the contribution from the three corners
130+
var t0 = 0.5 - x0 * x0 - y0 * y0;
131+
132+
if (t0 >= 0)
133+
{
134+
var gi0 = permMod12[ii + perm[jj]] * 3;
135+
t0 *= t0;
136+
n0 = t0 * t0 * (grad3[gi0] * x0 + grad3[gi0 + 1] * y0); // (x,y) of grad3 used for 2D gradient
137+
}
138+
139+
var t1 = 0.5 - x1 * x1 - y1 * y1;
140+
141+
if (t1 >= 0)
142+
{
143+
var gi1 = permMod12[ii + i1 + perm[jj + j1]] * 3;
144+
t1 *= t1;
145+
n1 = t1 * t1 * (grad3[gi1] * x1 + grad3[gi1 + 1] * y1);
146+
}
147+
148+
var t2 = 0.5 - x2 * x2 - y2 * y2;
149+
150+
if (t2 >= 0)
151+
{
152+
var gi2 = permMod12[ii + 1 + perm[jj + 1]] * 3;
153+
t2 *= t2;
154+
n2 = t2 * t2 * (grad3[gi2] * x2 + grad3[gi2 + 1] * y2);
155+
}
156+
157+
// Add contributions from each corner to get the final noise value.
158+
// The result is scaled to return values in the interval [-1,1].
159+
return 70.0 * (n0 + n1 + n2);
160+
}
161+
};
162+
163+
module.exports = SimplexNoise;

0 commit comments

Comments
 (0)