The `noise`

module adds functions for dealing with *solid noise*. Solid noise generators are simply infinite surfaces, and thus can use all the normal surface machinery.

### Perlin noise generators

`Surface.noise(`

(Surface constructor)
*sx,sy[,r]*)

Constructs a new surface based on classical Perlin noise, scaled by (sx,sy). (A random generator can optionally be passed as the third argument.)

`Surface.simplexNoise(`

(Surface constructor)
*sx,sy[,n]*)

Constructs a new surface based on simplex Perlin noise, scaled by (sx,sy). (A random generator can optionally be passed as the third argument.)

### Cellular noise generators

All of the cellular noise generators are based around distributing *feature points* randomly throughout the texture space. The values of the surface are then a function of the displacements from the sample point to all the feature points within a 2x2 square centered on the sample point. For example, the default voronoi {Worley 1996} generator computes the *minimum distance* to any feature point as its value, while sparse convolution {Frisvad 2007} assigns a cubic (pseudo-Gaussian) kernel to each feature point with random magnitude, and then computes the sum of all such kernels.

`Surface.voronoi(`

*sx,sy,[,gen,r])*

Constructs a cellular surface, according to the scale factorssx,syand the optional generator objectgenand random generatorr. The default generator object produces Worley F1 noise (i.e., distance to the nearest feature point).

The generator object, if specified, can have any or all of the following fields:

`density`

-- specifies how many, on average, features should be present in a unit area of space. The default is 3.`points(`

-- given a random generator*r,m*)*r*and a point count*m*, computes and returns an array of*m*feature points located within a unit square. Points must be objects having at last an*.x*and*.y*field giving their location, but can otherwise have any structure or distribution you need. Defaults to simply computing*m*random points, with no attributes other than position, in the unit square.`distance(`

-- Computes and returns the "distance" between the point*p,x,y*)*p*(as returned from`.points()`

`) to the point (`

*x,y*). Defaults to computing the squared Euclidean distance.`reduce(`

-- Given two "distances", computes their combination. Given zero distances, this function should return its identity value (e.g., Infinity for*[a,b]*)`min`

, 0 for`+`

, etc.). Defaults to`Math.min`

.`output(`

-- Given the final value computed by*v*)`reduce`

, perform any post-processing needed and return the final output value. E.g., if using squared distances,`output`

can take the square root of the final value, in order to give the true Euclidean distance. Defaults to`Math.sqrt`

.

`Surface.sparseNoise(`

*sx,sy*)

Constructs a new surface using sparse convolution noise {Frisvad 2007}.

`Surface.gaborNoise(`

*sx,sy[,gen]*)

Constructs a new Gabor noise {Lagae 2009} surface, optionally using the generator function`to control the spectrum of the generated noise. The generator function is passed a point`

gen(p,r)pand a random generatorrand should set two fields on the point:

`orient`

-- should give the random orientation of the feature point, in radians.`freq`

-- should give the frequency of the feature point.

Note that Gabor noise requires a higher density of points than Worley or sparse convolution noise, and is thus somewhat more expensive to evaluate.

### Utility routines

`SeedRand(`

(global constructor)
*seed*)

Returns a new random number generator with the specifiedseed. This is needed since the normal`Math.random()`

random number generator does not allow us to specify the seed, which is necessary for many noise generation functions. This generator uses the "invertible mapping" technique described in {Klimov 2003}. Note that very large seeds can cause... problems with the generated sequence.

(SeedRand method)
*sr*.random()

Returns the next (pseudo-)random value in the range 0.0 to 1.0.

(SeedRand method)
*sr*.seed(*seed*)

Sets the seed of the generator to the specified value.