/
perlin2.go
187 lines (149 loc) · 4.84 KB
/
perlin2.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package perlin
import (
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/raidancampbell/go-gfx/internal"
"math/rand"
)
func Perlin2DTemporal(cfg *pixelgl.WindowConfig, imd *imdraw.IMDraw, offset float64) []pixel.Vec {
cfg.Title = "perlin-waves"
points := make([]pixel.Vec, internal.WINDOW_WIDTH)
for x := 0.; x < float64(len(points)); x+= 1 {
points[int(x)] = pixel.Vec {
X: x,
Y: float64(internal.WINDOW_HEIGHT)/2 +
(doPerlin2D(internal.Coord2D{X: (x + offset*5)/140, Y: offset * 5/140}) * float64(internal.WINDOW_HEIGHT) /4) +
(doPerlin2D(internal.Coord2D{X: (x + offset*10)/50, Y: offset * 5/50}) * float64(internal.WINDOW_HEIGHT)/4)/4,
}
}
imd.Push(points...)
imd.Line(1)
return points
}
func Perlin2DSpatial(cfg *pixelgl.WindowConfig, win *pixelgl.Window) {
cfg.Title = "perlin-2D"
var px = make([]uint8, len(win.Canvas().Pixels()))
idx := 0
baseNoise := GenerateWhiteNoise(internal.WINDOW_WIDTH, internal.WINDOW_HEIGHT)
perlin := GeneratePerlinNoise(baseNoise, 7)
for y := 0; y < int(win.Bounds().H()); y++ {
for x := 0; x < int(win.Bounds().W()); x++ {
n := perlin[x][y]
px[idx] = uint8(255 * n) //R
idx++
px[idx] = uint8(255 * n) //G
idx++
px[idx] = uint8(255 * n) // B
idx++
px[idx] = uint8(255) //A
idx++
}
}
win.Canvas().SetPixels(px)
}
func GenerateSmoothNoise(baseNoise [][]float64, octave int) [][]float64 {
width := len(baseNoise)
height := len(baseNoise[0])
smoothNoise := make([][]float64, width)
for i := range smoothNoise {
smoothNoise[i] = make([]float64, height)
}
samplePeriod := 1 << octave // calculates 2 ^ k
sampleFrequency := 1.0 / float64(samplePeriod)
for i := 0; i < width; i++ {
//calculate the horizontal sampling indices
sampleI0 := (i / samplePeriod) * samplePeriod
sampleI1 := (sampleI0 + samplePeriod) % width //wrap around
horizontalBlend := float64(i - sampleI0) * sampleFrequency
for j := 0; j < height; j++ {
//calculate the vertical sampling indices
sampleJ0 := (j / samplePeriod) * samplePeriod
sampleJ1 := (sampleJ0 + samplePeriod) % height //wrap around
verticalBlend := float64(j - sampleJ0) * sampleFrequency
//blend the top two corners
top := Interpolate(baseNoise[sampleI0][sampleJ0], baseNoise[sampleI1][sampleJ0], float64(horizontalBlend))
//blend the bottom two corners
bottom := Interpolate(baseNoise[sampleI0][sampleJ1], baseNoise[sampleI1][sampleJ1], float64(horizontalBlend))
//final blend
smoothNoise[i][j] = Interpolate(top, bottom, float64(verticalBlend))
}
}
return smoothNoise
}
func Interpolate(x0, x1, alpha float64) float64 {
return x0*(1-alpha) + alpha*x1
}
func GeneratePerlinNoise(baseNoise [][]float64, octaveCount int) [][]float64 {
width := len(baseNoise)
height := len(baseNoise[0])
smoothNoise := make([][][]float64, octaveCount)
persistance := 0.8
//generate smooth noise
for i := 0; i < octaveCount; i++ {
smoothNoise[i] = GenerateSmoothNoise(baseNoise, i)
}
perlinNoise := make([][]float64, width)
for i := range perlinNoise {
perlinNoise[i] = make([]float64, height)
}
amplitude := 1.0
totalAmplitude := 0.0
//blend noise together
for octave := octaveCount - 1; octave >= 0; octave-- {
amplitude *= persistance
totalAmplitude += amplitude
for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude
}
}
}
//normalisation
for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
perlinNoise[i][j] /= totalAmplitude
}
}
return perlinNoise
}
func GenerateWhiteNoise(width, height int) [][]float64 {
noise := make([][]float64, width)
for i := range noise {
noise[i] = make([]float64, height)
}
for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
noise[i][j] = rand.Float64()
}
}
return noise
}
func doPerlin2D(p internal.Coord2D) float64 {
/* Calculate lattice points. */
p0 := p.Floor()
p1 := p0.Add(internal.New2D(1., 0.))
p2 := p0.Add(internal.New2D(0., 1.))
p3 := p0.Add(internal.New2D(1., 1.))
/* Look up gradients at lattice points. */
g0 := grad2D(p0)
g1 := grad2D(p1)
g2 := grad2D(p2)
g3 := grad2D(p3)
fadeT0 := fade(p.X - p0.X) /* Used for interpolation in horizontal direction */
fadeT1 := fade(p.Y - p0.Y) /* Used for interpolation in vertical direction. */
/* Calculate dot products and interpolate.*/
p0p1 := (1.0-fadeT0)*g0.Dot(p.Sub(p0)) + fadeT0*g1.Dot(p.Sub(p1)) // between upper two lattice points
p2p3 := (1.0-fadeT0)*g2.Dot(p.Sub(p2)) + fadeT0*g3.Dot(p.Sub(p3)) // between lower two lattice points
/* Calculate final result */
return (1.0-fadeT1)*p0p1 + fadeT1*p2p3
}
func grad2D(c internal.Coord2D) internal.Coord2D {
ret := internal.Coord2D{
X: perlinLUT[int(c.X)%len(perlinLUT)],
Y: perlinLUT[int(c.Y)%len(perlinLUT)],
}.
Scale(2.).
Sub(internal.New2D(1, 1))
return ret.Normalized()
}