perlin

package
v0.1.0-rc1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package perlin is a wrapper of github.com/aquilax/go-perlin.

Which implements github.com/KEINOS/go-noise/noise interface.

Index

Examples

Constants

View Source
const (
	// Alpha is the default smoothness of Perlin noise.
	Alpha = 2.
	// Beta is the default scale of Perlin noise.
	Beta = 2.
	// Iteration is the default number of iterations of Perlin noise.
	Iteration = int32(3)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {
	// Smoothness is the weight when the sum is formed. Which is the alpha value
	// in Ken Perlin's article. Default is 2. The smaller the number, the more
	// noise is generated.
	Smoothness float64
	// Scale is the harmonic scaling/spacing, typically 2. Which is the beta value
	// in Ken Perlin's article. Default is 2.
	Scale float64
	// Iteration is the number of times the noise is generated. Which is the number
	// of iterations in Ken Perlin's article. Default is 3.
	Iteration int32
	// Seed holds the seed value for the noise.
	Seed int64
}

Generator holds parameters of Perlin noise.

func New

func New(seed int64) *Generator

New returns a noise generator instance of seeded Perlin noise.

Example (Eval32_more_than_three_dimmentions)
package main

import (
	"fmt"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const (
		seed = 100
	)

	p := perlin.New(seed)

	// It only supports up to three dimmentions.
	fmt.Printf("%0.0f", p.Eval32(0.0001, 0.0001, 0.0001, 0.0001))

}
Output:

0
Example (Eval32_one_dimmention)
package main

import (
	"fmt"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const (
		seed       = 100
		smoothness = 100
	)

	p := perlin.New(seed)

	for x := float32(0); x < 3; x++ {
		fmt.Printf(
			"%0.0f;%0.4f\n",
			x,
			p.Eval32(x/smoothness),
		)
	}

}
Output:

0;0.0000
1;-0.0026
2;-0.0046
Example (Eval32_three_dimmentions)
package main

import (
	"fmt"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const (
		seed       = 100
		smoothness = 100
	)

	p := perlin.New(seed)

	for z := float32(0); z < 2; z++ {
		for y := float32(0); y < 2; y++ {
			for x := float32(0); x < 2; x++ {
				fmt.Printf(
					"%0.0f;%0.0f;%0.0f; %0.4f\n",
					x, y, z,
					p.Eval32(x/smoothness, y/smoothness, z/smoothness),
				)
			}
		}
	}

}
Output:

0;0;0; 0.0000
1;0;0; -0.0146
0;1;0; -0.0104
1;1;0; -0.0249
0;0;1; 0.0257
1;0;1; 0.0112
0;1;1; 0.0154
1;1;1; 0.0009
Example (Eval32_two_dimmentions)
package main

import (
	"fmt"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const (
		seed       = 100
		smoothness = 100
	)

	p := perlin.New(seed)

	for y := float32(0); y < 3; y++ {
		for x := float32(0); x < 3; x++ {
			fmt.Printf(
				"%0.0f;%0.0f; %0.4f\n",
				x, y,
				p.Eval32(x/smoothness, y/smoothness),
			)
		}
	}

}
Output:

0;0; 0.0000
1;0; -0.0285
2;0; -0.0602
0;1; -0.0152
1;1; -0.0437
2;1; -0.0753
0;2; -0.0328
1;2; -0.0612
2;2; -0.0927
Example (Eval64_more_than_three_dimmentions)
package main

import (
	"fmt"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const (
		seed       = 100
		smoothness = 100
	)

	p := perlin.New(seed)

	// It only supports up to three dimmentions.
	fmt.Printf("%0.0f", p.Eval64(0.0001, 0.0001, 0.0001, 0.0001))

}
Output:

0
Example (Eval64_one_dimmention)
package main

import (
	"fmt"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const seed = 100

	p := perlin.New(seed)

	for x := 0.; x < 3; x++ {
		fmt.Printf("%0.0f;%0.4f\n", x, p.Eval64(x/10))
	}

}
Output:

0;0.0000
1;-0.0086
2;-0.0017
Example (Eval64_three_dimmentions)
package main

import (
	"fmt"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const seed = 100

	p := perlin.New(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.Eval64(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
Example (Eval64_two_dimmentions)
package main

import (
	"fmt"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const seed = 100

	p := perlin.New(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.Eval64(x/10, y/10))
		}
	}

}
Output:

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

func (*Generator) Eval32

func (n *Generator) Eval32(dim ...float32) float32

Eval32 returns a float32 Perlin noise value for the given coordinates. It is a conversion of float64 to float32 to support Eval32 interface.

func (*Generator) Eval64

func (n *Generator) Eval64(dim ...float64) float64

Eval64 returns a float64 Perlin noise value for the given coordinates.

func (*Generator) SetEval32

func (n *Generator) SetEval32(f func(seed int64, dim ...float32) float32) error

SetEval32 is an implementation of noise.Generator interface. It will always return an error.

Example
package main

import (
	"fmt"
	"log"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const seed = 100

	p := perlin.New(seed)

	err := p.SetEval32(func(seed int64, dim ...float32) float32 {
		return 0
	})

	// User-defined functions cannot be assigned to Perlin types. Use Custom type instead.
	if err == nil {
		log.Fatal("Perlin type should return an error on SetEval32")
	}

	fmt.Println(err.Error())

}
Output:

float32 evaluation function is already set. You can not set custom function in Perlin type

func (*Generator) SetEval64

func (n *Generator) SetEval64(f func(seed int64, dim ...float64) float64) error

SetEval64 is an implementation of noise.Generator interface. It will always return an error.

Example
package main

import (
	"fmt"
	"log"

	"github.com/KEINOS/go-noise/pkg/perlin"
)

func main() {
	const seed = 100

	p := perlin.New(seed)

	// User-defined functions cannot be assigned to Perlin types. Use Custom type instead.
	err := p.SetEval64(func(seed int64, dim ...float64) float64 {
		return 0
	})

	if err == nil {
		log.Fatal("Perlin type should return an error on SetEval32")
	}

	fmt.Println(err.Error())

}
Output:

float64 evaluation function is already set. You can not set custom function in Perlin type

Jump to

Keyboard shortcuts

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