-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstack.go
39 lines (33 loc) · 761 Bytes
/
stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package noise
import (
"math/rand"
"github.com/EliCDavis/polyform/math/sample"
"github.com/EliCDavis/vector/vector2"
)
type Stack2DEntry struct {
Scalar float64
Amplitude float64
}
type Stack2D struct {
f sample.Vec2ToFloat
entries []Stack2DEntry
}
func PerlinStack(entries ...Stack2DEntry) Stack2D {
newVals := make([]float64, 512)
for i := 0; i < len(newVals); i++ {
newVals[i] = rand.Float64()
}
return Stack2D{
f: func(v vector2.Float64) float64 {
return Noise2D(v, QuinticInterpolation, gradientOverValues2D(newVals))
},
entries: entries,
}
}
func (s2d Stack2D) Value(v vector2.Float64) float64 {
sum := 0.
for _, entry := range s2d.entries {
sum += s2d.f(v.Scale(entry.Scalar)) * entry.Amplitude
}
return sum
}