perlin

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2021 License: MIT Imports: 2 Imported by: 93 modules, 87 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Overview

Package perlin provides coherent noise function over 1, 2 or 3 dimensions This code is go adaptation based on C implementation that can be found here: http://git.gnome.org/browse/gegl/tree/operations/common/perlin/perlin.c (original copyright Ken Perlin)

Index

Examples

Constants

View Source
const (
	B  = 0x100
	N  = 0x1000
	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
package main

import (
	"fmt"
	"math/rand"

	"github.com/aquilax/go-perlin"
)

const (
	alpha       = 2.
	beta        = 2.
	n           = 3
	seed  int64 = 100
)

func main() {
	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
package main

import (
	"fmt"

	"github.com/aquilax/go-perlin"
)

const (
	alpha       = 2.
	beta        = 2.
	n           = 3
	seed  int64 = 100
)

func main() {
	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
package main

import (
	"fmt"

	"github.com/aquilax/go-perlin"
)

const (
	alpha       = 2.
	beta        = 2.
	n           = 3
	seed  int64 = 100
)

func main() {
	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
package main

import (
	"fmt"

	"github.com/aquilax/go-perlin"
)

const (
	alpha       = 2.
	beta        = 2.
	n           = 3
	seed  int64 = 100
)

func main() {
	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
⌂ 换网址