@@ -28,11 +28,64 @@ Better rank ordering method by Stefan Gustavson in 2012.
2828 */
2929
3030
31+ // these #__PURE__ comments help uglifyjs with dead code removal
32+ //
33+ const F2 = /*#__PURE__*/ 0.5 * ( Math . sqrt ( 3.0 ) - 1.0 ) ;
34+ const G2 = /*#__PURE__*/ ( 3.0 - Math . sqrt ( 3.0 ) ) / 6.0 ;
35+ const F3 = 1.0 / 3.0 ;
36+ const G3 = 1.0 / 6.0 ;
37+ const F4 = /*#__PURE__*/ ( Math . sqrt ( 5.0 ) - 1.0 ) / 4.0 ;
38+ const G4 = /*#__PURE__*/ ( 5.0 - Math . sqrt ( 5.0 ) ) / 20.0 ;
39+
3140// I'm really not sure why this | 0 (basically a coercion to int)
3241// is making this faster but I get ~5 million ops/sec more on the
3342// benchmarks across the board or a ~10% speedup.
3443const fastFloor = ( x : number ) => Math . floor ( x ) | 0 ;
3544
45+ const grad2 = /*#__PURE__*/ new Float64Array ( [ 1 , 1 ,
46+ - 1 , 1 ,
47+ 1 , - 1 ,
48+
49+ - 1 , - 1 ,
50+ 1 , 0 ,
51+ - 1 , 0 ,
52+
53+ 1 , 0 ,
54+ - 1 , 0 ,
55+ 0 , 1 ,
56+
57+ 0 , - 1 ,
58+ 0 , 1 ,
59+ 0 , - 1 ] ) ;
60+
61+ // double seems to be faster than single or int's
62+ // probably because most operations are in double precision
63+ const grad3 = /*#__PURE__*/ new Float64Array ( [ 1 , 1 , 0 ,
64+ - 1 , 1 , 0 ,
65+ 1 , - 1 , 0 ,
66+
67+ - 1 , - 1 , 0 ,
68+ 1 , 0 , 1 ,
69+ - 1 , 0 , 1 ,
70+
71+ 1 , 0 , - 1 ,
72+ - 1 , 0 , - 1 ,
73+ 0 , 1 , 1 ,
74+
75+ 0 , - 1 , 1 ,
76+ 0 , 1 , - 1 ,
77+ 0 , - 1 , - 1 ] ) ;
78+
79+ // double is a bit quicker here as well
80+ const grad4 = /*#__PURE__*/ new Float64Array ( [ 0 , 1 , 1 , 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , 1 , 0 , 1 , - 1 , - 1 ,
81+ 0 , - 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 ,
82+ 1 , 0 , 1 , 1 , 1 , 0 , 1 , - 1 , 1 , 0 , - 1 , 1 , 1 , 0 , - 1 , - 1 ,
83+ - 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , - 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 ,
84+ 1 , 1 , 0 , 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , 1 , 1 , - 1 , 0 , - 1 ,
85+ - 1 , 1 , 0 , 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 , 0 , 1 , - 1 , - 1 , 0 , - 1 ,
86+ 1 , 1 , 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , 1 , 0 , 1 , - 1 , - 1 , 0 ,
87+ - 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 , 0 ] ) ;
88+
3689/**
3790 * A random() function, must return a number in the interval [0,1), just like Math.random().
3891 */
@@ -54,27 +107,7 @@ export type NoiseFunction2D = (x: number, y: number) => number;
54107 * @returns {NoiseFunction2D }
55108 */
56109export function createNoise2D ( random : RandomFn = Math . random ) : NoiseFunction2D {
57- const F2 = 0.5 * ( Math . sqrt ( 3.0 ) - 1.0 ) ;
58- const G2 = ( 3.0 - Math . sqrt ( 3.0 ) ) / 6.0 ;
59-
60110 const perm = buildPermutationTable ( random ) ;
61- // these consts are moved in here to help webpack with treeshaking
62- const grad2 = new Float64Array ( [ 1 , 1 ,
63- - 1 , 1 ,
64- 1 , - 1 ,
65-
66- - 1 , - 1 ,
67- 1 , 0 ,
68- - 1 , 0 ,
69-
70- 1 , 0 ,
71- - 1 , 0 ,
72- 0 , 1 ,
73-
74- 0 , - 1 ,
75- 0 , 1 ,
76- 0 , - 1 ] ) ;
77-
78111 // precalculating this yields a little ~3% performance improvement.
79112 const permGrad2x = new Float64Array ( perm ) . map ( v => grad2 [ ( v % 12 ) * 2 ] ) ;
80113 const permGrad2y = new Float64Array ( perm ) . map ( v => grad2 [ ( v % 12 ) * 2 + 1 ] ) ;
@@ -164,28 +197,7 @@ export type NoiseFunction3D = (x: number, y: number, z: number) => number;
164197 * @returns {NoiseFunction3D }
165198 */
166199export function createNoise3D ( random : RandomFn = Math . random ) : NoiseFunction3D {
167- const F3 = 1.0 / 3.0 ;
168- const G3 = 1.0 / 6.0 ;
169-
170200 const perm = buildPermutationTable ( random ) ;
171- // double seems to be faster than single or int's
172- // probably because most operations are in double precision
173- const grad3 = new Float64Array ( [ 1 , 1 , 0 ,
174- - 1 , 1 , 0 ,
175- 1 , - 1 , 0 ,
176-
177- - 1 , - 1 , 0 ,
178- 1 , 0 , 1 ,
179- - 1 , 0 , 1 ,
180-
181- 1 , 0 , - 1 ,
182- - 1 , 0 , - 1 ,
183- 0 , 1 , 1 ,
184-
185- 0 , - 1 , 1 ,
186- 0 , 1 , - 1 ,
187- 0 , - 1 , - 1 ] ) ;
188-
189201 // precalculating these seems to yield a speedup of over 15%
190202 const permGrad3x = new Float64Array ( perm ) . map ( v => grad3 [ ( v % 12 ) * 3 ] ) ;
191203 const permGrad3y = new Float64Array ( perm ) . map ( v => grad3 [ ( v % 12 ) * 3 + 1 ] ) ;
@@ -330,20 +342,8 @@ export type NoiseFunction4D = (x: number, y: number, z: number, w: number) => nu
330342 * @returns {NoiseFunction3D }
331343 */
332344export function createNoise4D ( random : RandomFn = Math . random ) {
333- const F4 = ( Math . sqrt ( 5.0 ) - 1.0 ) / 4.0 ;
334- const G4 = ( 5.0 - Math . sqrt ( 5.0 ) ) / 20.0 ;
335-
336345 const perm = buildPermutationTable ( random ) ;
337346 // precalculating these leads to a ~10% speedup
338- // double is a bit quicker here as well
339- const grad4 = new Float64Array ( [ 0 , 1 , 1 , 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , 1 , 0 , 1 , - 1 , - 1 ,
340- 0 , - 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 ,
341- 1 , 0 , 1 , 1 , 1 , 0 , 1 , - 1 , 1 , 0 , - 1 , 1 , 1 , 0 , - 1 , - 1 ,
342- - 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , - 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 ,
343- 1 , 1 , 0 , 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , 1 , 1 , - 1 , 0 , - 1 ,
344- - 1 , 1 , 0 , 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 , 0 , 1 , - 1 , - 1 , 0 , - 1 ,
345- 1 , 1 , 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , 1 , 0 , 1 , - 1 , - 1 , 0 ,
346- - 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 , 0 ] ) ;
347347 const permGrad4x = new Float64Array ( perm ) . map ( v => grad4 [ ( v % 32 ) * 4 ] ) ;
348348 const permGrad4y = new Float64Array ( perm ) . map ( v => grad4 [ ( v % 32 ) * 4 + 1 ] ) ;
349349 const permGrad4z = new Float64Array ( perm ) . map ( v => grad4 [ ( v % 32 ) * 4 + 2 ] ) ;
0 commit comments