-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperlin.go
More file actions
32 lines (28 loc) · 1.01 KB
/
perlin.go
File metadata and controls
32 lines (28 loc) · 1.01 KB
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
package noisy
// Perlin implements SourceInterface.
//
// - Frequency sets the first (biggest) Octave.
// - Lacunarity sets the multiplier to the frequency for each successive Octave.
// - Persistence sets the amplitude for each successive Octave.
// - OctaveCount sets the number of Octaves to generate and blend.
// - Seed sets the random seed, useful to regenerate the same image if required.
type Perlin struct {
Frequency, Lacunarity, Persistence float64
OctaveCount, Seed int
}
// GetValue returns the value between [-1;1] for a given 3D position.
func (perlin Perlin) GetValue(x, y, z float64) float64 {
value := 0.0
persistence := 1.0
frequency := perlin.Frequency
seed := perlin.Seed
for range perlin.OctaveCount {
value += persistence * getNoise(seed, x*frequency, y*frequency, z*frequency)
// prepare the persistency & frequency for the next octave
persistence *= perlin.Persistence
frequency *= perlin.Lacunarity
// offset the seed to avoid seeing patterns
seed++
}
return value
}