-
Notifications
You must be signed in to change notification settings - Fork 1
/
perlin1d.go
85 lines (64 loc) · 1.62 KB
/
perlin1d.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Based on algorithm given at http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
*
* Borrowed heavily from https://github.com/iand/perlin/blob/master/perlin.go
*
* MIT License http://opensource.org/licenses/MIT
*/
package perlinnoise
import "math"
func noise(x uint64) float64 {
fn := (x << 13) ^ x
return (1.0 - float64((fn*(fn*fn*15731+789221)+1376312589)&0x7fffffff)/1073741824.0)
}
func smoothedNoise(x float64) float64 {
//return Noise(x)/2 + Noise(x-1)/4 + Noise(x+1)/4
xint := uint64(math.Trunc(x))
return noise(xint)/2 + noise(xint-1)/4 + noise(xint+1)/4
}
func interpolate(a, b, x float64) float64 {
/*
ft = x * 3.1415927
f = (1 - cos(ft)) * .5
return a*(1-f) + b*f
*/
ft := x * math.Pi
f := (1 - math.Cos(ft)) * 0.5
return a*(1-f) + b*f
}
func interpolateNoise(x float64) float64 {
/*
integer_X = int(x)
fractional_X = x - integer_X
v1 = SmoothedNoise1(integer_X)
v2 = SmoothedNoise1(integer_X + 1)
return Interpolate(v1 , v2 , fractional_X)
*/
xint := math.Trunc(x)
xfrac := x - xint
v1 := smoothedNoise(xint)
v2 := smoothedNoise(xint + 1)
return interpolate(v1, v2, xfrac)
}
func PerlinNoise1d(x, f, a float64) (value float64) {
/*
total = 0
p = persistence
n = Number_Of_Octaves - 1
loop i from 0 to n
frequency = 2i
amplitude = p^i
total = total + InterpolatedNoisei(x * frequency) * amplitude
end of i loop
return total
*/
//var frequency float64 = 0.1;
//var amplitude float64 = 1;
octaves := 6
for i := 0; i < octaves; i++ {
value += interpolateNoise(x*f) * a
f *= 2
a /= 2
}
return
}