Documentation ¶
Overview ¶
Package noise is a small library for generating noise. It contains implementations for Perlin and Simplex noise in two dimensions. All noises use a seed and a lookup table to create consistent outputs for the same seed, even from different noise generators. All noises implement the Noiser interface.
Octave noise is also provided, although must be composed of other noises to produce the resulting smoothed noise.
This package provides two different kinds of Perlin noise: A very fast 4-point interpolation using a simple fading function that has a zero second derivative at the end of the interpolation ranges ensuring a smoother result, and a slow 16-point interpolation using five Catmull-Rom splines. The former generates noise that can sometimes betray the regular rectangular pattern of the gradient used to interpolate, while the latter can elminate these artifacts. Furthermore, the noise sampled at coordinate points will all be zero. It is knowwn that the Catmull-Rom spline interpolation method contains gridlike artifacts.
// Perlin noise with 4-point interpolation perlinGenerator := noise.NewPerlin(1) val := perlinGenerator.Noise(0.5, 0.5) // Perlin noise with 16-point interpolation catmullRomPerlinGenerator := noise.NewPerlinCatmullRom(1) val := catmullRomPerlinGenerator.Noise(0.5, 0.5)
Simplex noise uses simplexes to efficiently interpolate noise instead of a regular rectangular grid. This can result in a different skewed repetitive pattern along the simplexes used for interpolation.
// Simplex noise with simplex interpolation simplexGenerator := noise.NewSimplex(1) val := simplexGenerator.Noise(0.5, 0.5)
Octave noise combines several different noises with increasing persistence which diminishes the amplitude of subsequently-added noise and widens its sampling frequency. The gain and lacunarity are both held constant. Octave noise composed of the same constituent noise with the same seeds with a persistence of one-half is sometimes referred to as pink noise, fractional noise, or fractal noise.
// Pink noise with two octaves. pinkNoiseGenerator := noise.NewOctaveNoise(0.5) pinkNoiseGenerator.AddOctave(noise.NewPerlin(1)) pinkNoiseGenerator.AddOctave(noise.NewPerlin(1)) val := pinkNoiseGenerator.Noise(0.5, 0.5)
A utility function is provided to help write out Noisers to greyscale PNG images. It uses goroutines to parallelize sampling due to the slowness of some noise generation methods.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteGreyImagePng ¶
func WriteGreyImagePng(w io.Writer, noiser Noiser, minX, minY, numberSamplesX, numberSamplesY int, noiseSampleDelta float64) error
WriteGreyImagePng handles writing a Noiser out to a PNG image. It samples the noise space in increments specified by noiseSampleDelta. The image is generated using multiple goroutines to handle slower methods. It also normalizes the image to provide full greyscale values.
The minimum X and minimum Y determine the lower-left point in noise space to begin sampling. The number of samples to do in the X and Y directions also determines the width and height of the resulting image. Finally, the noise sampling delta determines the distance between two sampled points in noise space. Setting this parameter to one will yield a black image for Perlin Noise.
Types ¶
type Noiser ¶
Noiser generates noise for a point. The noise never changes for the same point and Noiser.
type OctaveNoise ¶
type OctaveNoise struct {
// contains filtered or unexported fields
}
OctaveNoise uses other Noisers to create more noises composed on one another using constant gain and lacunarity.
func NewOctaveNoise ¶
func NewOctaveNoise(persistence float64) *OctaveNoise
NewOctaveNoise creates an octave-based noise with the given persistence. A typical persistence value is around one-half.
func (*OctaveNoise) AddOctave ¶
func (o *OctaveNoise) AddOctave(n Noiser)
AddOctave adds the Noiser. Persistence is applied in the order they are added. Noisers added later will have a higher sampling frequency but a lower amplitude if the persistence was less than one.
func (*OctaveNoise) Noise ¶
func (o *OctaveNoise) Noise(x, y float64) float64
Noise generates noise for the given input.
type Perlin ¶
type Perlin struct {
// contains filtered or unexported fields
}
Perlin implements simple Perlin noise using a fading function whose second derivative is zero at the interpolation boundaries. This results in a smoother visualization.
type PerlinCatmullRom ¶
type PerlinCatmullRom struct {
// contains filtered or unexported fields
}
PerlinCatmullRom creates Perlin noise using centripetal Catmull-Rom spline interpolation. This is slow.
Warning: this contains visual gridline artifacts.
func NewPerlinCatmullRom ¶
func NewPerlinCatmullRom(splineCacheSize int, seed int64) *PerlinCatmullRom
Constructs a new source of noise using a centripetal Catmull-Rom spline interpolation, with splines having the given number of points. The seed is used to ensure idential PerlinCatmullRoms will return the same noise values for the same inputs.
Warning: this contains visual gridline artifacts.
func (*PerlinCatmullRom) Noise ¶
func (s *PerlinCatmullRom) Noise(x, y float64) float64
Noise creates Perlin noise using Catmull-Rom spline interpolations, which is considerably slower than the simple variant of Perlin noise.
type Simplex ¶
type Simplex struct {
// contains filtered or unexported fields
}
Simplex implements simplex noise generation in two dimensions.
func NewSimplex ¶
NewSimplex returns a new source of simplex noise. Identical seeds generate identical noise for the same inputs.