2020-05-09 17:34:43 +00:00
|
|
|
package tins2020
|
|
|
|
|
|
|
|
import "opslag.de/schobers/geom/noise"
|
|
|
|
|
|
|
|
type NoiseMap interface {
|
|
|
|
Value(x, y int32) float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNoiseMap(seed int64) NoiseMap {
|
|
|
|
return &noiseMap{
|
|
|
|
noise: noise.NewPerlin(seed),
|
|
|
|
alpha: 2,
|
|
|
|
beta: 4,
|
|
|
|
harmonics: 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRandomNoiseMap(seed int64) NoiseMap {
|
|
|
|
return &randomNoiseMap{noise.NewPerlin(seed)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type noiseMap struct {
|
|
|
|
noise *noise.Perlin
|
|
|
|
alpha, beta float64
|
|
|
|
harmonics int
|
|
|
|
}
|
|
|
|
|
|
|
|
func clipNormalized(x float64) float64 {
|
|
|
|
if x < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if x > 1 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2020-05-09 17:37:21 +00:00
|
|
|
// Value generates the noise value for an x/y pair.
|
2020-05-09 17:34:43 +00:00
|
|
|
func (m *noiseMap) Value(x, y int32) float64 {
|
|
|
|
value := m.noise.Noise2D(float64(x)*.01, float64(y)*.01, m.alpha, m.beta, m.harmonics)*.565 + .5
|
|
|
|
return clipNormalized(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
type randomNoiseMap struct {
|
|
|
|
*noise.Perlin
|
|
|
|
}
|
|
|
|
|
2020-05-09 17:37:21 +00:00
|
|
|
// Value generates the noise value for an x/y pair.
|
2020-05-09 17:34:43 +00:00
|
|
|
func (m randomNoiseMap) Value(x, y int32) float64 {
|
|
|
|
value := m.Noise2D(float64(x)*.53, float64(y)*.53, 1.01, 2, 2)*.5 + .5
|
|
|
|
return clipNormalized(value)
|
|
|
|
}
|