perlin

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2023 License: MIT Imports: 2 Imported by: 0

README

GO Perlin Noise generator Go Reference

Adapted for go from GEGL

Example: perlin.Noise1D

alpha = 2
beta = 2
n = 3

PerlinNoise1D

Using perlin.Noise2D with termbox to generate terrain in terminal:

PerlinNoise2D

Documentation

Index

Examples

Constants

View Source
const (
	B  = 0x10
	N  = 0x100
	BM = 0xff
)

General constants

Variables

This section is empty.

Functions

This section is empty.

Types

type Perlin

type Perlin struct {
	// contains filtered or unexported fields
}

Perlin is the noise generator

func NewPerlin

func NewPerlin(alpha, beta float64, n int32, seed int64) *Perlin

NewPerlin creates new Perlin noise generator In what follows "alpha" is the weight when the sum is formed. Typically, it is 2, As this approaches 1 the function is noisier. "beta" is the harmonic scaling/spacing, typically 2, n is the number of iterations and seed is the math.rand seed value to use

func NewPerlinRandSource

func NewPerlinRandSource(alpha, beta float64, n int32, source rand.Source) *Perlin

NewPerlinRandSource creates new Perlin noise generator In what follows "alpha" is the weight when the sum is formed. Typically, it is 2, As this approaches 1 the function is noisier. "beta" is the harmonic scaling/spacing, typically 2, n is the number of iterations and source is source of pseudo-random int64 values

Example
p := perlin.NewPerlinRandSource(alpha, beta, n, rand.NewSource(seed))
for x := 0.; x < 3; x++ {
	fmt.Printf("%0.0f;%0.4f\n", x, p.Noise1D(x/10))
}
Output:

0;0.0000
1;-0.0086
2;-0.0017

func (*Perlin) Noise1D

func (p *Perlin) Noise1D(x float64) float64

Noise1D generates 1-dimensional Perlin Noise value

Example
p := perlin.NewPerlin(alpha, beta, n, seed)
for x := 0.; x < 3; x++ {
	fmt.Printf("%0.0f;%0.4f\n", x, p.Noise1D(x/10))
}
Output:

0;0.0000
1;-0.0086
2;-0.0017

func (*Perlin) Noise2D

func (p *Perlin) Noise2D(x, y float64) float64

Noise2D Generates 2-dimensional Perlin Noise value

Example
p := perlin.NewPerlin(alpha, beta, n, seed)
for x := 0.; x < 2; x++ {
	for y := 0.; y < 2; y++ {
		fmt.Printf("%0.0f;%0.0f;%0.4f\n", x, y, p.Noise2D(x/10, y/10))
	}
}
Output:

0;0;0.0000
0;1;-0.2002
1;0;-0.3389
1;1;-0.5045

func (*Perlin) Noise3D

func (p *Perlin) Noise3D(x, y, z float64) float64

Noise3D Generates 3-dimensional Perlin Noise value

Example
p := perlin.NewPerlin(alpha, beta, n, seed)
for x := 0.; x < 2; x++ {
	for y := 0.; y < 2; y++ {
		for z := 0.; z < 2; z++ {
			fmt.Printf("%0.0f;%0.0f;%0.0f;%0.4f\n", x, y, z, p.Noise3D(x/10, y/10, z/10))
		}
	}
}
Output:

0;0;0;0.0000
0;0;1;0.2616
0;1;0;-0.0755
0;1;1;0.2020
1;0;0;-0.2138
1;0;1;0.0616
1;1;0;-0.2208
1;1;1;0.0304

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL