/
perlin.go
285 lines (248 loc) · 11.2 KB
/
perlin.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package noise
import (
"math"
"github.com/EliCDavis/polyform/math/sample"
"github.com/EliCDavis/vector/vector2"
"github.com/EliCDavis/vector/vector3"
)
/*
* Everything here is implemented from the blog post :
* https://gpfault.net/posts/perlin-noise.txt.html
* and
* IMPROVED NOISE by Ken Perlin
*/
var randvals = []float64{
0.8704284167043097, 0.34381794949226285, 0.20347651223593433, 0.6936267453762557,
0.31492088227316795, 0.17628107042323715, 0.5894548796802268, 0.36207042982046467,
0.2098541259451625, 0.5368667694137306, 0.9079312480291066, 0.1447804291061865,
0.9351266781348699, 0.19244789853112976, 0.027581673668102935, 0.4295947978719412,
0.18247495259966917, 0.17805128506349477, 0.59218532812359, 0.5483899369985801,
0.09254491901667805, 0.24688096571768492, 0.006756920935624189, 0.07987715815316698,
0.32765522886116405, 0.04139438878897006, 0.8679133091761289, 0.19040201792272304,
0.33829041945133254, 0.09766739566945581, 0.6960339535542752, 0.2339870411837779,
0.6653001474197455, 0.5653375392106565, 0.5565825349563516, 0.6264984303415653,
0.4817678133462635, 0.9137230156315765, 0.962977575242074, 0.7535576669131003,
0.6590340877209775, 0.46414805881260235, 0.33272940446058796, 0.6877678129498426,
0.45250947350649295, 0.20457744207199435, 0.5724448957536952, 0.9798893165112026,
0.5222828303919786, 0.24993553784893008, 0.8824366757918849, 0.8860878035380997,
0.8608275500278695, 0.2212425499070798, 0.7680678518299935, 0.31340332898263856,
0.48618470548166615, 0.24275518320888922, 0.011942774692459412, 0.7628626815884714,
0.31319696475398673, 0.30917928938524675, 0.9096478185771337, 0.535645268320293,
0.3997949397424305, 0.7808287332826014, 0.8860462025898967, 0.5480879142285682,
0.258917710199575, 0.7928514503858257, 0.7167293130403909, 0.45359254331078014,
0.39811726219764787, 0.28598288854757237, 0.9363448337707183, 0.03586765363555644,
0.5016599180934189, 0.8610121408298264, 0.7501892578071321, 0.9559837471978927,
0.45319617482371055, 0.13782168400027106, 0.16246789640666304, 0.2780340080827213,
0.25223352674235455, 0.6858641692298615, 0.05671201716621521, 0.9515571978580988,
0.4662938761805062, 0.1413720403634111, 0.6425222116877194, 0.8742063490337564,
0.2577796253795894, 0.5313520534852778, 0.9541957665881069, 0.36689843315556936,
0.08714136476478629, 0.6979727712707209, 0.8665059177214127, 0.7891054924109948,
0.6014078325321426, 0.4471632398461891, 0.8804372599856021, 0.6621434961551356,
0.1438271311191448, 0.1674369161747027, 0.6941325365302466, 0.8428141235734272,
0.6594071158885231, 0.45895338544786113, 0.688770464563643, 0.8006565616688901,
0.6982693441194612, 0.9114509333793817, 0.38101020181205447, 0.9288700097660287,
0.9177693606782669, 0.7920037275039264, 0.0367754890323877, 0.42157908169273584,
0.723674161250542, 0.8492137248546607, 0.19135097453720906, 0.4279359002088332,
0.928697310763485, 0.8940628044422461, 0.9196848730464706, 0.7777182797612412,
0.4098584971008481, 0.8079786752988427, 0.8117403285389861, 0.18225588635292622,
0.9689167856358409, 0.5822394209380057, 0.5087817829207337, 0.033148303304662674,
0.5211438011211189, 0.2602665199471166, 0.5122497402695882, 0.9356743311694065,
0.31713405136010486, 0.5508526804098395, 0.6029342740995718, 0.42211407205270435,
0.03202701252677498, 0.109385626310468, 0.8436456638364529, 0.44803341910971617,
0.2971287860337639, 0.9151367940440329, 0.8426361614284981, 0.9784186815538478,
0.8028471322210211, 0.5213526126635832, 0.2002620080970987, 0.5490488087474357,
0.5279504325839439, 0.006796722338321182, 0.5265218020955242, 0.8179928465123476,
0.6057343244257596, 0.771278328736622, 0.41575684586903994, 0.3409806454285662,
0.150747073673821, 0.1734683950008502, 0.45843929035518016, 0.46220423998422144,
0.3816698057241863, 0.7877455792907055, 0.2179128675886266, 0.6241053797524747,
0.5381105103546531, 0.41984301688737946, 0.9568855866333323, 0.6641537200359071,
0.6377871025863648, 0.26691130413327, 0.8031592567165593, 0.8774295343626533,
0.1939555539936748, 0.8612395586723123, 0.8020779045940338, 0.3265149904050053,
0.8235432263781146, 0.6263749753699897, 0.2703337928924714, 0.7503830368949362,
0.5090622936771221, 0.34991039142807834, 0.6424786447228512, 0.9197268591261023,
0.28924334282373376, 0.841187651328485, 0.270962100501974, 0.756410041652152,
0.6790131522181968, 0.724784473882375, 0.1366331831720342, 0.3746843061419438,
0.42425371033267356, 0.05444160039644741, 0.2900592289904913, 0.15074865577097651,
0.39039231052233636, 0.48451080612620956, 0.22350634951658144, 0.2654725344790494,
0.30989248245248935, 0.03238716813412146, 0.7744753963120066, 0.2659481214864856,
0.4827492208061901, 0.05025401123057338, 0.791289417934957, 0.8969404864201656,
0.7805644166056944, 0.8831511439508759, 0.20065785507064948, 0.1590760918065608,
0.5895324410174767, 0.6232674279536328, 0.4379293292182298, 0.708331150364883,
0.04885480263475972, 0.04652842238575272, 0.9053855988598549, 0.3617416251026946,
0.21541470870117507, 0.5797430276075937, 0.12468835555159852, 0.024796671162763628,
0.07591891794777905, 0.3558664685922752, 0.45068302525520787, 0.30625455367271637,
0.7905187566761203, 0.5280382647544786, 0.83027358073379, 0.39390697677746345,
0.1268853620277801, 0.346362365423482, 0.985084118785369, 0.7476722131876661,
0.11742369703699773, 0.4507068181589886, 0.5128875887645592, 0.4608293202864875,
0.16675852053711648, 0.23259465618181574, 0.3754230072378064, 0.21166186674917586,
0.5188597230718879, 0.5656091134135179, 0.5737223060700867,
}
// Cubic Hermite Curve
func CubicHermite(t float64) float64 {
return t * t * (3.0 - 2.0*t)
}
// Quintic interpolation curve
func QuinticInterpolation(t float64) float64 {
return t * t * t * (t*(t*6-15) + 10)
}
func grad(p float64) float64 {
if randvals[int(math.Abs(math.Round(p)))%len(randvals)] > 0.5 {
return 1.0
}
return -1.0
}
func gradientOverValues2D(vals []float64) func(vector2.Float64) vector2.Float64 {
return func(pDirty vector2.Float64) vector2.Float64 {
p := vector2.New(math.Abs(pDirty.X()), math.Abs(pDirty.Y()))
width := float64(len(vals))
xVal := vals[int(math.Round(p.X()))%len(vals)]
yVal := vals[int(math.Round(p.Y()+(xVal*width)))%len(vals)]
zVal := vals[int(math.Round(yVal*width))%len(vals)]
wVal := vals[int(math.Round(zVal*width))%len(vals)]
v := vector2.New(
xVal,
wVal,
)
return v.Scale(2).
Sub(vector2.One[float64]()).
Normalized()
}
}
func Noise3D(p vector3.Float64, fade sample.FloatToFloat, grad sample.Vec3ToVec3) float64 {
/* Calculate lattice points. */
p0 := p.Floor()
p1 := p0.Add(vector3.New(1.0, 0.0, 0.0))
p2 := p0.Add(vector3.New(0.0, 1.0, 0.0))
p3 := p0.Add(vector3.New(1.0, 1.0, 0.0))
p4 := p0.Add(vector3.New(0.0, 0.0, 1.0))
p5 := p4.Add(vector3.New(1.0, 0.0, 0.0))
p6 := p4.Add(vector3.New(0.0, 1.0, 0.0))
p7 := p4.Add(vector3.New(1.0, 1.0, 0.0))
/* Look up gradients at lattice points. */
g0 := grad(p0)
g1 := grad(p1)
g2 := grad(p2)
g3 := grad(p3)
g4 := grad(p4)
g5 := grad(p5)
g6 := grad(p6)
g7 := grad(p7)
t0 := p.X() - p0.X()
fade_t0 := fade(t0)
t1 := p.Y() - p0.Y()
fade_t1 := fade(t1)
t2 := p.Z() - p0.Z()
fade_t2 := fade(t2)
p0p1 := (1.0-fade_t0)*g0.Dot(p.Sub(p0)) + fade_t0*g1.Dot(p.Sub(p1))
p2p3 := (1.0-fade_t0)*g2.Dot(p.Sub(p2)) + fade_t0*g3.Dot(p.Sub(p3))
p4p5 := (1.0-fade_t0)*g4.Dot(p.Sub(p4)) + fade_t0*g5.Dot(p.Sub(p5))
p6p7 := (1.0-fade_t0)*g6.Dot(p.Sub(p6)) + fade_t0*g7.Dot(p.Sub(p7))
y1 := (1.0-fade_t1)*p0p1 + fade_t1*p2p3
y2 := (1.0-fade_t1)*p4p5 + fade_t1*p6p7
return (1.0-fade_t2)*y1 + fade_t2*y2
}
func Noise2D(p vector2.Float64, f sample.FloatToFloat, g sample.Vec2ToVec2) float64 {
/* Calculate lattice points. */
p0 := p.Floor()
p1 := p0.Add(vector2.Right[float64]())
p2 := p0.Add(vector2.Up[float64]())
p3 := p0.Add(vector2.One[float64]())
/* Look up gradients at lattice points. */
g0 := g(p0)
g1 := g(p1)
g2 := g(p2)
g3 := g(p3)
t0 := p.X() - p0.X()
fade_t0 := f(t0) /* Used for interpolation in horizontal direction */
t1 := p.Y() - p0.Y()
fade_t1 := f(t1) /* Used for interpolation in vertical direction. */
// Calculate dot products and interpolate
p0p1 := ((1.0 - fade_t0) * g0.Dot(p.Sub(p0))) + (fade_t0 * g1.Dot(p.Sub(p1))) /* between upper two lattice points */
p2p3 := ((1.0 - fade_t0) * g2.Dot(p.Sub(p2))) + (fade_t0 * g3.Dot(p.Sub(p3))) /* between lower two lattice points */
// Calculate final result
return (((1.0 - fade_t1) * p0p1) + (fade_t1 * p2p3)) + .5
}
// https://gpfault.net/posts/perlin-noise.txt.html
//
// (1 - F(p-p0))g(p0)(p-p0) + F(p-p0)g(p1)(p-p1)
func Noise1D(p float64, f, g sample.FloatToFloat) float64 {
p0 := math.Floor(p)
p1 := p0 + 1
g0 := g(p0)
g1 := g(p1)
pm0 := p - p0
pm1 := p - p1
fv0 := f(pm0)
return ((1. - fv0) * g0 * pm0) + (fv0 * g1 * pm1)
}
func Perlin1D(x float64) float64 {
return Noise1D(x, QuinticInterpolation, grad)
}
func Perlin2D(v vector2.Float64) float64 {
return Noise2D(v, QuinticInterpolation, gradientOverValues2D(randvals))
}
func Grad(hash int, x, y, z float64) float64 {
var h = hash & 15
var u = y
if h < 8 {
u = x
}
var v = y
if h >= 4 {
if h == 12 || h == 14 {
v = x
} else {
v = z
}
}
left := -u
if (h & 1) == 0 {
left = u
}
right := -v
if (h & 2) == 0 {
right = v
}
return left + right
}
func lerp(t, a, b float64) float64 {
return a + t*(b-a)
}
func Perlin3D(pos vector3.Float64) float64 {
vInt := pos.FloorToInt()
X := vInt.X() & 0xff
Y := vInt.Y() & 0xff
Z := vInt.Z() & 0xff
x := pos.X() - float64(vInt.X())
y := pos.Y() - float64(vInt.Y())
z := pos.Z() - float64(vInt.Z())
var u = QuinticInterpolation(x)
var v = QuinticInterpolation(y)
var w = QuinticInterpolation(z)
var A = (perm[X] + Y) & 0xff
var B = (perm[X+1] + Y) & 0xff
var AA = (perm[A] + Z) & 0xff
var BA = (perm[B] + Z) & 0xff
var AB = (perm[A+1] + Z) & 0xff
var BB = (perm[B+1] + Z) & 0xff
return lerp(w, lerp(v, lerp(u, Grad(perm[AA], x, y, z), Grad(perm[BA], x-1, y, z)),
lerp(u, Grad(perm[AB], x, y-1, z), Grad(perm[BB], x-1, y-1, z))),
lerp(v, lerp(u, Grad(perm[AA+1], x, y, z-1), Grad(perm[BA+1], x-1, y, z-1)),
lerp(u, Grad(perm[AB+1], x, y-1, z-1), Grad(perm[BB+1], x-1, y-1, z-1))))
}
var perm []int = []int{
151, 160, 137, 91, 90, 15,
131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123,
5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
151,
}