@@ -27,62 +27,12 @@ Better rank ordering method by Stefan Gustavson in 2012.
2727 SOFTWARE.
2828 */
2929
30- const F2 = 0.5 * ( Math . sqrt ( 3.0 ) - 1.0 ) ;
31- const G2 = ( 3.0 - Math . sqrt ( 3.0 ) ) / 6.0 ;
32- const F3 = 1.0 / 3.0 ;
33- const G3 = 1.0 / 6.0 ;
34- const F4 = ( Math . sqrt ( 5.0 ) - 1.0 ) / 4.0 ;
35- const G4 = ( 5.0 - Math . sqrt ( 5.0 ) ) / 20.0 ;
3630
3731// I'm really not sure why this | 0 (basically a coercion to int)
3832// is making this faster but I get ~5 million ops/sec more on the
3933// benchmarks across the board or a ~10% speedup.
4034const fastFloor = ( x : number ) => Math . floor ( x ) | 0 ;
4135
42- const grad2 = new Float64Array ( [ 1 , 1 ,
43- - 1 , 1 ,
44- 1 , - 1 ,
45-
46- - 1 , - 1 ,
47- 1 , 0 ,
48- - 1 , 0 ,
49-
50- 1 , 0 ,
51- - 1 , 0 ,
52- 0 , 1 ,
53-
54- 0 , - 1 ,
55- 0 , 1 ,
56- 0 , - 1 ] ) ;
57-
58- // double seems to be faster than single or int's
59- // probably because most operations are in double precision
60- const grad3 = new Float64Array ( [ 1 , 1 , 0 ,
61- - 1 , 1 , 0 ,
62- 1 , - 1 , 0 ,
63-
64- - 1 , - 1 , 0 ,
65- 1 , 0 , 1 ,
66- - 1 , 0 , 1 ,
67-
68- 1 , 0 , - 1 ,
69- - 1 , 0 , - 1 ,
70- 0 , 1 , 1 ,
71-
72- 0 , - 1 , 1 ,
73- 0 , 1 , - 1 ,
74- 0 , - 1 , - 1 ] ) ;
75-
76- // double is a bit quicker here as well
77- const grad4 = new Float64Array ( [ 0 , 1 , 1 , 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , 1 , 0 , 1 , - 1 , - 1 ,
78- 0 , - 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 ,
79- 1 , 0 , 1 , 1 , 1 , 0 , 1 , - 1 , 1 , 0 , - 1 , 1 , 1 , 0 , - 1 , - 1 ,
80- - 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , - 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 ,
81- 1 , 1 , 0 , 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , 1 , 1 , - 1 , 0 , - 1 ,
82- - 1 , 1 , 0 , 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 , 0 , 1 , - 1 , - 1 , 0 , - 1 ,
83- 1 , 1 , 1 , 0 , 1 , 1 , - 1 , 0 , 1 , - 1 , 1 , 0 , 1 , - 1 , - 1 , 0 ,
84- - 1 , 1 , 1 , 0 , - 1 , 1 , - 1 , 0 , - 1 , - 1 , 1 , 0 , - 1 , - 1 , - 1 , 0 ] ) ;
85-
8636/**
8737 * A random() function, must return a number in the interval [0,1), just like Math.random().
8838 */
@@ -104,7 +54,27 @@ export type NoiseFunction2D = (x: number, y: number) => number;
10454 * @returns {NoiseFunction2D }
10555 */
10656export 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+
10760 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+
10878 // precalculating this yields a little ~3% performance improvement.
10979 const permGrad2x = new Float64Array ( perm ) . map ( v => grad2 [ ( v % 12 ) * 2 ] ) ;
11080 const permGrad2y = new Float64Array ( perm ) . map ( v => grad2 [ ( v % 12 ) * 2 + 1 ] ) ;
@@ -194,7 +164,28 @@ export type NoiseFunction3D = (x: number, y: number, z: number) => number;
194164 * @returns {NoiseFunction3D }
195165 */
196166export function createNoise3D ( random : RandomFn = Math . random ) : NoiseFunction3D {
167+ const F3 = 1.0 / 3.0 ;
168+ const G3 = 1.0 / 6.0 ;
169+
197170 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+
198189 // precalculating these seems to yield a speedup of over 15%
199190 const permGrad3x = new Float64Array ( perm ) . map ( v => grad3 [ ( v % 12 ) * 3 ] ) ;
200191 const permGrad3y = new Float64Array ( perm ) . map ( v => grad3 [ ( v % 12 ) * 3 + 1 ] ) ;
@@ -339,8 +330,20 @@ export type NoiseFunction4D = (x: number, y: number, z: number, w: number) => nu
339330 * @returns {NoiseFunction3D }
340331 */
341332export 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+
342336 const perm = buildPermutationTable ( random ) ;
343337 // 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 ] ) ;
344347 const permGrad4x = new Float64Array ( perm ) . map ( v => grad4 [ ( v % 32 ) * 4 ] ) ;
345348 const permGrad4y = new Float64Array ( perm ) . map ( v => grad4 [ ( v % 32 ) * 4 + 1 ] ) ;
346349 const permGrad4z = new Float64Array ( perm ) . map ( v => grad4 [ ( v % 32 ) * 4 + 2 ] ) ;
0 commit comments