-
Notifications
You must be signed in to change notification settings - Fork 5
/
opensimplex2_noise.jl
589 lines (538 loc) · 30.4 KB
/
opensimplex2_noise.jl
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
abstract type Orientation end
struct OrientStandard <: Orientation end
struct OrientX <: Orientation end
struct OrientXY <: Orientation end
struct OrientXZ <: Orientation end
struct OrientXYZ <: Orientation end
struct OpenSimplex2{N,O<:Orientation} <: NoiseSampler{N}
random_state::RandomState
simplex_state::SimplexState
table::Vector{Float64}
end
@inline function _opensimplex2(dims, seed, table_size, gradients, orientation, smooth)
rs = RandomState(seed)
orientation = os2_orientation_type(Val(orientation))
table = Iterators.take(Iterators.cycle(gradients), table_size) |> collect
T = OpenSimplex2{dims,orientation}
T(rs, SimplexState(T, Val(smooth)), table)
end
@inline os2_orientation_type(::Val{nothing}) = OrientStandard
@inline os2_orientation_type(::Val{:x}) = OrientX
@inline os2_orientation_type(::Val{:xy}) = OrientXY
@inline os2_orientation_type(::Val{:xz}) = OrientXZ
@inline os2_orientation_type(::Val{:xyz}) = OrientXYZ
SimplexState(::Type{<:OpenSimplex2{2}}, ::Val) = SimplexState(0.5, 1.0)
SimplexState(::Type{<:OpenSimplex2{3}}, ::Val{true}) = SimplexState(0.5, 2.6142672496001165)
SimplexState(::Type{<:OpenSimplex2{3}}, ::Val{false}) = SimplexState(0.6, 1.0)
SimplexState(::Type{<:OpenSimplex2{4}}, ::Val{true}) = SimplexState(0.5, 2.323520023997645)
SimplexState(::Type{<:OpenSimplex2{4}}, ::Val{false}) = SimplexState(0.6, 1.0)
# 2D
const OS2_SKEW_2D = 0.366025403784439
const OS2_UNSKEW_2D = -0.21132486540518713
const OS2_NUM_GRADIENTS_EXP_2D = 7
const OS2_NUM_GRADIENTS_2D = 1 << OS2_NUM_GRADIENTS_EXP_2D
const OS2_GRADIENTS_NORMALIZED_2D = [
0.38268343236509, 0.923879532511287, 0.923879532511287, 0.38268343236509,
0.923879532511287, -0.38268343236509, 0.38268343236509, -0.923879532511287,
-0.38268343236509, -0.923879532511287, -0.923879532511287, -0.38268343236509,
-0.923879532511287, 0.38268343236509, -0.38268343236509, 0.923879532511287,
0.130526192220052, 0.99144486137381, 0.608761429008721, 0.793353340291235,
0.793353340291235, 0.608761429008721, 0.99144486137381, 0.130526192220051,
0.99144486137381, -0.130526192220051, 0.793353340291235, -0.60876142900872,
0.608761429008721, -0.793353340291235, 0.130526192220052, -0.99144486137381,
-0.130526192220052, -0.99144486137381, -0.608761429008721, -0.793353340291235,
-0.793353340291235, -0.608761429008721, -0.99144486137381, -0.130526192220052,
-0.99144486137381, 0.130526192220051, -0.793353340291235, 0.608761429008721,
-0.608761429008721, 0.793353340291235, -0.130526192220052, 0.99144486137381]
"""
opensimplex2_2d(; seed=nothing, orient=nothing)
Construct a sampler that outputs 2-dimensional OpenSimplex2 noise when it is sampled from.
# Arguments
- `seed`: An unsigned integer used to seed the random number generator for this sampler, or
`nothing` for non-deterministic results.
- `orient`: Either the symbol `:x` or the value `nothing`:
+ `nothing`: Use the standard orientation.
+ `:x`: The noise space will be re-oriented with the Y axis pointing down the main diagonal to
improve visual isotropy.
"""
function opensimplex2_2d(; seed=nothing, orient=nothing)
size = OS2_NUM_GRADIENTS_2D * 2
gradients = OS2_GRADIENTS_NORMALIZED_2D ./ 0.01001634121365712
_opensimplex2(2, seed, size, gradients, orient, false)
end
@inline orient(::Type{OpenSimplex2{2,OrientStandard}}, x, y) = (x, y) .+ OS2_SKEW_2D .* (x + y)
@inline function orient(::Type{OpenSimplex2{2,OrientX}}, x, y)
xx = x * ROOT_2_OVER_2
yy = y * ROOT_2_OVER_2 * (2OS2_SKEW_2D + 1)
(yy + xx, yy - xx)
end
@inline function grad(table, seed, X, Y, x, y)
hash = (seed ⊻ X ⊻ Y) * HASH_MULTIPLIER
hash ⊻= hash >> (64 - OS2_NUM_GRADIENTS_EXP_2D + 1)
i = trunc(hash) & ((OS2_NUM_GRADIENTS_2D - 1) << 1)
@inbounds t = (table[i+1], table[(i|1)+1])
sum((t .* (x, y)))
end
function sample(sampler::S, x::T, y::T) where {O,S<:OpenSimplex2{2,O},T<:Real}
seed = sampler.random_state.seed
table = sampler.table
state = sampler.simplex_state
falloff = state.falloff
primes = (PRIME_X, PRIME_Y)
tr = orient(S, x, y)
XY = floor.(Int, tr)
vtr = tr .- XY
t = sum(vtr) * OS2_UNSKEW_2D
X1, Y1 = XY .* primes
X2, Y2 = (X1, Y1) .+ primes
x1, y1 = vtr .+ t
us1 = 2OS2_UNSKEW_2D + 1
result = 0.0
a1 = falloff - x1^2 - y1^2
if a1 > 0
result += pow4(a1) * grad(table, seed, X1, Y1, x1, y1)
end
a2 = 2us1 * (1 / OS2_UNSKEW_2D + 2) * t + -2us1^2 + a1
if a2 > 0
x, y = (x1, y1) .- 2OS2_UNSKEW_2D .- 1
result += pow4(a2) * grad(table, seed, X2, Y2, x, y)
end
if y1 > x1
x = x1 - OS2_UNSKEW_2D
y = y1 - OS2_UNSKEW_2D - 1
a3 = falloff - x^2 - y^2
if a3 > 0
result += pow4(a3) * grad(table, seed, X1, Y2, x, y)
end
else
x = x1 - OS2_UNSKEW_2D - 1
y = y1 - OS2_UNSKEW_2D
a4 = falloff - x^2 - y^2
if a4 > 0
result += pow4(a4) * grad(table, seed, X2, Y1, x, y)
end
end
result * state.scale_factor
end
# 3D
const OS2_SEED_FLIP_3D = -0x52d547b2e96ed629
const OS2_FALLBACK_ROTATE_3D = 2 / 3
const OS2_ROTATE_3D_ORTHONORMALIZER = OS2_UNSKEW_2D
const OS2_NUM_GRADIENTS_EXP_3D = 8
const OS2_NUM_GRADIENTS_3D = 1 << OS2_NUM_GRADIENTS_EXP_3D
const OS2_GRADIENTS_NORMALIZED_3D = [
2.22474487139, 2.22474487139, -1.0, 0.0,
2.22474487139, 2.22474487139, 1.0, 0.0,
3.0862664687972017, 1.1721513422464978, 0.0, 0.0,
1.1721513422464978, 3.0862664687972017, 0.0, 0.0,
-2.22474487139, 2.22474487139, -1.0, 0.0,
-2.22474487139, 2.22474487139, 1.0, 0.0,
-1.1721513422464978, 3.0862664687972017, 0.0, 0.0,
-3.0862664687972017, 1.1721513422464978, 0.0, 0.0,
-1.0, -2.22474487139, -2.22474487139, 0.0,
1.0, -2.22474487139, -2.22474487139, 0.0,
0.0, -3.0862664687972017, -1.1721513422464978, 0.0,
0.0, -1.1721513422464978, -3.0862664687972017, 0.0,
-1.0, -2.22474487139, 2.22474487139, 0.0,
1.0, -2.22474487139, 2.22474487139, 0.0,
0.0, -1.1721513422464978, 3.0862664687972017, 0.0,
0.0, -3.0862664687972017, 1.1721513422464978, 0.0,
-2.22474487139, -2.22474487139, -1.0, 0.0,
-2.22474487139, -2.22474487139, 1.0, 0.0,
-3.0862664687972017, -1.1721513422464978, 0.0, 0.0,
-1.1721513422464978, -3.0862664687972017, 0.0, 0.0,
-2.22474487139, -1.0, -2.22474487139, 0.0,
-2.22474487139, 1.0, -2.22474487139, 0.0,
-1.1721513422464978, 0.0, -3.0862664687972017, 0.0,
-3.0862664687972017, 0.0, -1.1721513422464978, 0.0,
-2.22474487139, -1.0, 2.22474487139, 0.0,
-2.22474487139, 1.0, 2.22474487139, 0.0,
-3.0862664687972017, 0.0, 1.1721513422464978, 0.0,
-1.1721513422464978, 0.0, 3.0862664687972017, 0.0,
-1.0, 2.22474487139, -2.22474487139, 0.0,
1.0, 2.22474487139, -2.22474487139, 0.0,
0.0, 1.1721513422464978, -3.0862664687972017, 0.0,
0.0, 3.0862664687972017, -1.1721513422464978, 0.0,
-1.0, 2.22474487139, 2.22474487139, 0.0,
1.0, 2.22474487139, 2.22474487139, 0.0,
0.0, 3.0862664687972017, 1.1721513422464978, 0.0,
0.0, 1.1721513422464978, 3.0862664687972017, 0.0,
2.22474487139, -2.22474487139, -1.0, 0.0,
2.22474487139, -2.22474487139, 1.0, 0.0,
1.1721513422464978, -3.0862664687972017, 0.0, 0.0,
3.0862664687972017, -1.1721513422464978, 0.0, 0.0,
2.22474487139, -1.0, -2.22474487139, 0.0,
2.22474487139, 1.0, -2.22474487139, 0.0,
3.0862664687972017, 0.0, -1.1721513422464978, 0.0,
1.1721513422464978, 0.0, -3.0862664687972017, 0.0,
2.22474487139, -1.0, 2.22474487139, 0.0,
2.22474487139, 1.0, 2.22474487139, 0.0,
1.1721513422464978, 0.0, 3.0862664687972017, 0.0,
3.0862664687972017, 0.0, 1.1721513422464978, 0.0]
"""
opensimplex2_3d(; seed=nothing, smooth=false, orient=nothing)
Construct a sampler that outputs 3-dimensional OpenSimplex2 noise when it is sampled from.
# Arguments
- `seed`: An unsigned integer used to seed the random number generator for this sampler, or
`nothing` for non-deterministic results.
- `smooth`: Specify whether to have continuous gradients.
Simplex variants, even the original Simplex noise by Ken Perlin, overshoot the radial extent for
the signal reconstruction kernel in order to improve the visual of the noise. Normally this is
okay, especially if layering multiple octaves of the noise. However, in some applications, such
as creating height or bump maps, this will produce discontinuities visually identified by
jarring creases in the generated noise.
This option changes the falloff in order to produce smooth continuous noise, however, the
resulting noise may look quite different than the non-smooth option, depending on the Simplex
variant.
The default value is `false`, in order to be true to the original implementation.
- `orient`: Either the symbol `:x` or the value `nothing`:
+ `nothing`: Use the standard orientation.
+ `:x`: The noise space will be re-oriented with the Y axis pointing down the main diagonal to
improve visual isotropy.
+ `:xy`: Re-orient the noise space to have better visual isotropy in the XY plane.
+ `:xz`: Re-orient the noise space to have better visual isotropy in the XZ plane.
"""
function opensimplex2_3d(; seed=nothing, orient=nothing, smooth=false)
size = OS2_NUM_GRADIENTS_3D * 4
gradients = OS2_GRADIENTS_NORMALIZED_3D ./ 0.07969837668935331
_opensimplex2(3, seed, size, gradients, orient, smooth)
end
@inline function orient(::Type{OpenSimplex2{3,OrientStandard}}, x, y, z)
OS2_FALLBACK_ROTATE_3D * (x + y + z) .- (x, y, z)
end
@inline function orient(::Type{OpenSimplex2{3,OrientXY}}, x, y, z)
xy = x + y
zz = z * ROOT_3_OVER_3
xr, yr = (x, y) .+ xy .* OS2_ROTATE_3D_ORTHONORMALIZER .+ zz
zr = xy * -ROOT_3_OVER_3 + zz
(xr, yr, zr)
end
@inline function orient(::Type{OpenSimplex2{3,OrientXZ}}, x, y, z)
orient(OpenSimplex2{3,OrientXY}, x, z, y)
end
@inline function grad(table, seed, X, Y, Z, x, y, z)
hash = ((seed ⊻ X) ⊻ (Y ⊻ Z)) * HASH_MULTIPLIER
hash ⊻= hash >> (64 - OS2_NUM_GRADIENTS_EXP_3D + 2)
i = trunc(hash) & ((OS2_NUM_GRADIENTS_3D - 1) << 2)
@inbounds t = (table[i+1], table[(i|1)+1], table[(i|2)+1])
sum((t .* (x, y, z)))
end
@inline function os2_contribute1(seed, table, a, X, Y, Z, x1, y1, z1, x2, y2, z2, xs, ys, zs)
result = 0.0
if a > 0
result += pow4(a) * grad(table, seed, X, Y, Z, x1, y1, z1)
end
if x2 ≥ y2 && x2 ≥ z2
result += os2_contribute2(seed, table, a + 2x2, X - xs * PRIME_X, Y, Z, x1 + xs, y1, z1)
elseif y2 ≥ x2 && y2 ≥ z2
result += os2_contribute2(seed, table, a + 2y2, X, Y - ys * PRIME_Y, Z, x1, y1 + ys, z1)
else
result += os2_contribute2(seed, table, a + 2z2, X, Y, Z - zs * PRIME_Z, x1, y1, z1 + zs)
end
result
end
@inline function os2_contribute2(seed, table, a, args...)
a > 1 ? pow4(a - 1) * grad(table, seed, args...) : 0.0
end
function sample(sampler::S, x::T, y::T, z::T) where {O,S<:OpenSimplex2{3,O},T<:Real}
seed = sampler.random_state.seed
table = sampler.table
state = sampler.simplex_state
falloff = state.falloff
primes = (PRIME_X, PRIME_Y, PRIME_Z)
tr = orient(S, x, y, z)
V = round.(Int, tr)
XYZ = V .* primes
x1, y1, z1 = tr .- V
s = trunc.(Int, -1 .- (x1, y1, z1)) .| 1
XYZ2 = XYZ .+ s .>> 1 .& primes
xyz2 = s .* .-((x1, y1, z1))
x4, y4, z4 = 0.5 .- xyz2
xyz3 = s .* (x4, y4, z4)
a1 = falloff - x1^2 - (y1^2 + z1^2)
c1 = os2_contribute1(seed, table, a1, XYZ..., x1, y1, z1, xyz2..., s...)
a2 = a1 + 0.75 - x4 - (y4 + z4)
c2 = os2_contribute1(seed ⊻ OS2_SEED_FLIP_3D, table, a2, XYZ2..., xyz3..., x4, y4, z4, .-s...)
(c1 + c2) * state.scale_factor
end
# 4D
const OS2_SEED_OFFSET_4D = 0xe83dc3e0da7164d
const OS2_SKEW_4D = -0.138196601125011
const OS2_UNSKEW_4D = 0.309016994374947
const OS2_LATTICE_STEP_4D = 0.2
const OS2_NUM_GRADIENTS_EXP_4D = 9
const OS2_NUM_GRADIENTS_4D = 1 << OS2_NUM_GRADIENTS_EXP_4D
const OS2_GRADIENTS_NORMALIZED_4D = [
-0.6740059517812944, -0.3239847771997537, -0.3239847771997537, 0.5794684678643381,
-0.7504883828755602, -0.4004672082940195, 0.15296486218853164, 0.5029860367700724,
-0.7504883828755602, 0.15296486218853164, -0.4004672082940195, 0.5029860367700724,
-0.8828161875373585, 0.08164729285680945, 0.08164729285680945, 0.4553054119602712,
-0.4553054119602712, -0.08164729285680945, -0.08164729285680945, 0.8828161875373585,
-0.5029860367700724, -0.15296486218853164, 0.4004672082940195, 0.7504883828755602,
-0.5029860367700724, 0.4004672082940195, -0.15296486218853164, 0.7504883828755602,
-0.5794684678643381, 0.3239847771997537, 0.3239847771997537, 0.6740059517812944,
-0.6740059517812944, -0.3239847771997537, 0.5794684678643381, -0.3239847771997537,
-0.7504883828755602, -0.4004672082940195, 0.5029860367700724, 0.15296486218853164,
-0.7504883828755602, 0.15296486218853164, 0.5029860367700724, -0.4004672082940195,
-0.8828161875373585, 0.08164729285680945, 0.4553054119602712, 0.08164729285680945,
-0.4553054119602712, -0.08164729285680945, 0.8828161875373585, -0.08164729285680945,
-0.5029860367700724, -0.15296486218853164, 0.7504883828755602, 0.4004672082940195,
-0.5029860367700724, 0.4004672082940195, 0.7504883828755602, -0.15296486218853164,
-0.5794684678643381, 0.3239847771997537, 0.6740059517812944, 0.3239847771997537,
-0.6740059517812944, 0.5794684678643381, -0.3239847771997537, -0.3239847771997537,
-0.7504883828755602, 0.5029860367700724, -0.4004672082940195, 0.15296486218853164,
-0.7504883828755602, 0.5029860367700724, 0.15296486218853164, -0.4004672082940195,
-0.8828161875373585, 0.4553054119602712, 0.08164729285680945, 0.08164729285680945,
-0.4553054119602712, 0.8828161875373585, -0.08164729285680945, -0.08164729285680945,
-0.5029860367700724, 0.7504883828755602, -0.15296486218853164, 0.4004672082940195,
-0.5029860367700724, 0.7504883828755602, 0.4004672082940195, -0.15296486218853164,
-0.5794684678643381, 0.6740059517812944, 0.3239847771997537, 0.3239847771997537,
0.5794684678643381, -0.6740059517812944, -0.3239847771997537, -0.3239847771997537,
0.5029860367700724, -0.7504883828755602, -0.4004672082940195, 0.15296486218853164,
0.5029860367700724, -0.7504883828755602, 0.15296486218853164, -0.4004672082940195,
0.4553054119602712, -0.8828161875373585, 0.08164729285680945, 0.08164729285680945,
0.8828161875373585, -0.4553054119602712, -0.08164729285680945, -0.08164729285680945,
0.7504883828755602, -0.5029860367700724, -0.15296486218853164, 0.4004672082940195,
0.7504883828755602, -0.5029860367700724, 0.4004672082940195, -0.15296486218853164,
0.6740059517812944, -0.5794684678643381, 0.3239847771997537, 0.3239847771997537,
-0.753341017856078, -0.37968289875261624, -0.37968289875261624, -0.37968289875261624,
-0.7821684431180708, -0.4321472685365301, -0.4321472685365301, 0.12128480194602098,
-0.7821684431180708, -0.4321472685365301, 0.12128480194602098, -0.4321472685365301,
-0.7821684431180708, 0.12128480194602098, -0.4321472685365301, -0.4321472685365301,
-0.8586508742123365, -0.508629699630796, 0.044802370851755174, 0.044802370851755174,
-0.8586508742123365, 0.044802370851755174, -0.508629699630796, 0.044802370851755174,
-0.8586508742123365, 0.044802370851755174, 0.044802370851755174, -0.508629699630796,
-0.9982828964265062, -0.03381941603233842, -0.03381941603233842, -0.03381941603233842,
-0.37968289875261624, -0.753341017856078, -0.37968289875261624, -0.37968289875261624,
-0.4321472685365301, -0.7821684431180708, -0.4321472685365301, 0.12128480194602098,
-0.4321472685365301, -0.7821684431180708, 0.12128480194602098, -0.4321472685365301,
0.12128480194602098, -0.7821684431180708, -0.4321472685365301, -0.4321472685365301,
-0.508629699630796, -0.8586508742123365, 0.044802370851755174, 0.044802370851755174,
0.044802370851755174, -0.8586508742123365, -0.508629699630796, 0.044802370851755174,
0.044802370851755174, -0.8586508742123365, 0.044802370851755174, -0.508629699630796,
-0.03381941603233842, -0.9982828964265062, -0.03381941603233842, -0.03381941603233842,
-0.37968289875261624, -0.37968289875261624, -0.753341017856078, -0.37968289875261624,
-0.4321472685365301, -0.4321472685365301, -0.7821684431180708, 0.12128480194602098,
-0.4321472685365301, 0.12128480194602098, -0.7821684431180708, -0.4321472685365301,
0.12128480194602098, -0.4321472685365301, -0.7821684431180708, -0.4321472685365301,
-0.508629699630796, 0.044802370851755174, -0.8586508742123365, 0.044802370851755174,
0.044802370851755174, -0.508629699630796, -0.8586508742123365, 0.044802370851755174,
0.044802370851755174, 0.044802370851755174, -0.8586508742123365, -0.508629699630796,
-0.03381941603233842, -0.03381941603233842, -0.9982828964265062, -0.03381941603233842,
-0.37968289875261624, -0.37968289875261624, -0.37968289875261624, -0.753341017856078,
-0.4321472685365301, -0.4321472685365301, 0.12128480194602098, -0.7821684431180708,
-0.4321472685365301, 0.12128480194602098, -0.4321472685365301, -0.7821684431180708,
0.12128480194602098, -0.4321472685365301, -0.4321472685365301, -0.7821684431180708,
-0.508629699630796, 0.044802370851755174, 0.044802370851755174, -0.8586508742123365,
0.044802370851755174, -0.508629699630796, 0.044802370851755174, -0.8586508742123365,
0.044802370851755174, 0.044802370851755174, -0.508629699630796, -0.8586508742123365,
-0.03381941603233842, -0.03381941603233842, -0.03381941603233842, -0.9982828964265062,
-0.3239847771997537, -0.6740059517812944, -0.3239847771997537, 0.5794684678643381,
-0.4004672082940195, -0.7504883828755602, 0.15296486218853164, 0.5029860367700724,
0.15296486218853164, -0.7504883828755602, -0.4004672082940195, 0.5029860367700724,
0.08164729285680945, -0.8828161875373585, 0.08164729285680945, 0.4553054119602712,
-0.08164729285680945, -0.4553054119602712, -0.08164729285680945, 0.8828161875373585,
-0.15296486218853164, -0.5029860367700724, 0.4004672082940195, 0.7504883828755602,
0.4004672082940195, -0.5029860367700724, -0.15296486218853164, 0.7504883828755602,
0.3239847771997537, -0.5794684678643381, 0.3239847771997537, 0.6740059517812944,
-0.3239847771997537, -0.3239847771997537, -0.6740059517812944, 0.5794684678643381,
-0.4004672082940195, 0.15296486218853164, -0.7504883828755602, 0.5029860367700724,
0.15296486218853164, -0.4004672082940195, -0.7504883828755602, 0.5029860367700724,
0.08164729285680945, 0.08164729285680945, -0.8828161875373585, 0.4553054119602712,
-0.08164729285680945, -0.08164729285680945, -0.4553054119602712, 0.8828161875373585,
-0.15296486218853164, 0.4004672082940195, -0.5029860367700724, 0.7504883828755602,
0.4004672082940195, -0.15296486218853164, -0.5029860367700724, 0.7504883828755602,
0.3239847771997537, 0.3239847771997537, -0.5794684678643381, 0.6740059517812944,
-0.3239847771997537, -0.6740059517812944, 0.5794684678643381, -0.3239847771997537,
-0.4004672082940195, -0.7504883828755602, 0.5029860367700724, 0.15296486218853164,
0.15296486218853164, -0.7504883828755602, 0.5029860367700724, -0.4004672082940195,
0.08164729285680945, -0.8828161875373585, 0.4553054119602712, 0.08164729285680945,
-0.08164729285680945, -0.4553054119602712, 0.8828161875373585, -0.08164729285680945,
-0.15296486218853164, -0.5029860367700724, 0.7504883828755602, 0.4004672082940195,
0.4004672082940195, -0.5029860367700724, 0.7504883828755602, -0.15296486218853164,
0.3239847771997537, -0.5794684678643381, 0.6740059517812944, 0.3239847771997537,
-0.3239847771997537, -0.3239847771997537, 0.5794684678643381, -0.6740059517812944,
-0.4004672082940195, 0.15296486218853164, 0.5029860367700724, -0.7504883828755602,
0.15296486218853164, -0.4004672082940195, 0.5029860367700724, -0.7504883828755602,
0.08164729285680945, 0.08164729285680945, 0.4553054119602712, -0.8828161875373585,
-0.08164729285680945, -0.08164729285680945, 0.8828161875373585, -0.4553054119602712,
-0.15296486218853164, 0.4004672082940195, 0.7504883828755602, -0.5029860367700724,
0.4004672082940195, -0.15296486218853164, 0.7504883828755602, -0.5029860367700724,
0.3239847771997537, 0.3239847771997537, 0.6740059517812944, -0.5794684678643381,
-0.3239847771997537, 0.5794684678643381, -0.6740059517812944, -0.3239847771997537,
-0.4004672082940195, 0.5029860367700724, -0.7504883828755602, 0.15296486218853164,
0.15296486218853164, 0.5029860367700724, -0.7504883828755602, -0.4004672082940195,
0.08164729285680945, 0.4553054119602712, -0.8828161875373585, 0.08164729285680945,
-0.08164729285680945, 0.8828161875373585, -0.4553054119602712, -0.08164729285680945,
-0.15296486218853164, 0.7504883828755602, -0.5029860367700724, 0.4004672082940195,
0.4004672082940195, 0.7504883828755602, -0.5029860367700724, -0.15296486218853164,
0.3239847771997537, 0.6740059517812944, -0.5794684678643381, 0.3239847771997537,
-0.3239847771997537, 0.5794684678643381, -0.3239847771997537, -0.6740059517812944,
-0.4004672082940195, 0.5029860367700724, 0.15296486218853164, -0.7504883828755602,
0.15296486218853164, 0.5029860367700724, -0.4004672082940195, -0.7504883828755602,
0.08164729285680945, 0.4553054119602712, 0.08164729285680945, -0.8828161875373585,
-0.08164729285680945, 0.8828161875373585, -0.08164729285680945, -0.4553054119602712,
-0.15296486218853164, 0.7504883828755602, 0.4004672082940195, -0.5029860367700724,
0.4004672082940195, 0.7504883828755602, -0.15296486218853164, -0.5029860367700724,
0.3239847771997537, 0.6740059517812944, 0.3239847771997537, -0.5794684678643381,
0.5794684678643381, -0.3239847771997537, -0.6740059517812944, -0.3239847771997537,
0.5029860367700724, -0.4004672082940195, -0.7504883828755602, 0.15296486218853164,
0.5029860367700724, 0.15296486218853164, -0.7504883828755602, -0.4004672082940195,
0.4553054119602712, 0.08164729285680945, -0.8828161875373585, 0.08164729285680945,
0.8828161875373585, -0.08164729285680945, -0.4553054119602712, -0.08164729285680945,
0.7504883828755602, -0.15296486218853164, -0.5029860367700724, 0.4004672082940195,
0.7504883828755602, 0.4004672082940195, -0.5029860367700724, -0.15296486218853164,
0.6740059517812944, 0.3239847771997537, -0.5794684678643381, 0.3239847771997537,
0.5794684678643381, -0.3239847771997537, -0.3239847771997537, -0.6740059517812944,
0.5029860367700724, -0.4004672082940195, 0.15296486218853164, -0.7504883828755602,
0.5029860367700724, 0.15296486218853164, -0.4004672082940195, -0.7504883828755602,
0.4553054119602712, 0.08164729285680945, 0.08164729285680945, -0.8828161875373585,
0.8828161875373585, -0.08164729285680945, -0.08164729285680945, -0.4553054119602712,
0.7504883828755602, -0.15296486218853164, 0.4004672082940195, -0.5029860367700724,
0.7504883828755602, 0.4004672082940195, -0.15296486218853164, -0.5029860367700724,
0.6740059517812944, 0.3239847771997537, 0.3239847771997537, -0.5794684678643381,
0.03381941603233842, 0.03381941603233842, 0.03381941603233842, 0.9982828964265062,
-0.044802370851755174, -0.044802370851755174, 0.508629699630796, 0.8586508742123365,
-0.044802370851755174, 0.508629699630796, -0.044802370851755174, 0.8586508742123365,
-0.12128480194602098, 0.4321472685365301, 0.4321472685365301, 0.7821684431180708,
0.508629699630796, -0.044802370851755174, -0.044802370851755174, 0.8586508742123365,
0.4321472685365301, -0.12128480194602098, 0.4321472685365301, 0.7821684431180708,
0.4321472685365301, 0.4321472685365301, -0.12128480194602098, 0.7821684431180708,
0.37968289875261624, 0.37968289875261624, 0.37968289875261624, 0.753341017856078,
0.03381941603233842, 0.03381941603233842, 0.9982828964265062, 0.03381941603233842,
-0.044802370851755174, 0.044802370851755174, 0.8586508742123365, 0.508629699630796,
-0.044802370851755174, 0.508629699630796, 0.8586508742123365, -0.044802370851755174,
-0.12128480194602098, 0.4321472685365301, 0.7821684431180708, 0.4321472685365301,
0.508629699630796, -0.044802370851755174, 0.8586508742123365, -0.044802370851755174,
0.4321472685365301, -0.12128480194602098, 0.7821684431180708, 0.4321472685365301,
0.4321472685365301, 0.4321472685365301, 0.7821684431180708, -0.12128480194602098,
0.37968289875261624, 0.37968289875261624, 0.753341017856078, 0.37968289875261624,
0.03381941603233842, 0.9982828964265062, 0.03381941603233842, 0.03381941603233842,
-0.044802370851755174, 0.8586508742123365, -0.044802370851755174, 0.508629699630796,
-0.044802370851755174, 0.8586508742123365, 0.508629699630796, -0.044802370851755174,
-0.12128480194602098, 0.7821684431180708, 0.4321472685365301, 0.4321472685365301,
0.508629699630796, 0.8586508742123365, -0.044802370851755174, -0.044802370851755174,
0.4321472685365301, 0.7821684431180708, -0.12128480194602098, 0.4321472685365301,
0.4321472685365301, 0.7821684431180708, 0.4321472685365301, -0.12128480194602098,
0.37968289875261624, 0.753341017856078, 0.37968289875261624, 0.37968289875261624,
0.9982828964265062, 0.03381941603233842, 0.03381941603233842, 0.03381941603233842,
0.8586508742123365, -0.044802370851755174, -0.044802370851755174, 0.508629699630796,
0.8586508742123365, -0.044802370851755174, 0.508629699630796, -0.044802370851755174,
0.7821684431180708, -0.12128480194602098, 0.4321472685365301, 0.4321472685365301,
0.8586508742123365, 0.508629699630796, -0.044802370851755174, -0.044802370851755174,
0.7821684431180708, 0.4321472685365301, -0.12128480194602098, 0.4321472685365301,
0.7821684431180708, 0.4321472685365301, 0.4321472685365301, -0.12128480194602098,
0.753341017856078, 0.37968289875261624, 0.37968289875261624, 0.37968289875261624]
"""
opensimplex2_4d(; seed=nothing, smooth=false, orient=nothing)
Construct a sampler that outputs 4-dimensional OpenSimplex2 noise when it is sampled from.
# Arguments
- `seed`: An unsigned integer used to seed the random number generator for this sampler, or
`nothing` for non-deterministic results.
- `smooth`: Specify whether to have continuous gradients.
Simplex variants, even the original Simplex noise by Ken Perlin, overshoot the radial extent for
the signal reconstruction kernel in order to improve the visual of the noise. Normally this is
okay, especially if layering multiple octaves of the noise. However, in some applications, such
as creating height or bump maps, this will produce discontinuities visually identified by
jarring creases in the generated noise.
This option changes the falloff in order to produce smooth continuous noise, however, the
resulting noise may look quite different than the non-smooth option, depending on the Simplex
variant.
The default value is `false`, in order to be true to the original implementation.
- `orient`: Either the symbol `:x` or the value `nothing`:
+ `nothing`: Use the standard orientation.
+ `:x`: The noise space will be re-oriented with the Y axis pointing down the main diagonal to
improve visual isotropy.
+ `:xy`: Re-orient the noise space to have better visual isotropy in the XY plane.
+ `:xz`: Re-orient the noise space to have better visual isotropy in the XZ plane.
+ `:xyz`: Re-orient the noise space to be better suited for time-varied animations, where
the W axis is time.
"""
function opensimplex2_4d(; seed=nothing, orient=nothing, smooth=false)
size = OS2_NUM_GRADIENTS_4D * 4
gradients = OS2_GRADIENTS_NORMALIZED_4D ./ 0.0220065933241897
_opensimplex2(4, seed, size, gradients, orient, smooth)
end
@inline function orient(::Type{OpenSimplex2{4,OrientStandard}}, x, y, z, w)
(x, y, z, w) .+ OS2_SKEW_4D .* (x + y + z + w)
end
@inline function orient(::Type{OpenSimplex2{4,OrientXY}}, x, y, z, w)
xy = x + y
ww = w * 0.2236067977499788
zw = z * 0.28867513459481294226 + ww
xr, yr = (x, y) .+ zw .+ xy .* -0.21132486540518699998
zr = xy * -0.57735026918962599998 + zw
wr = z * -0.866025403784439 + ww
(xr, yr, zr, wr)
end
@inline function orient(::Type{OpenSimplex2{4,OrientXZ}}, x, y, z, w)
orient(OpenSimplex2{4,OrientXY}, x, z, y, w)
end
@inline function orient(::Type{OpenSimplex2{4,OrientXYZ}}, x, y, z, w)
xyz = -(x + y + z)
ww = w * 0.2236067977499788
s = xyz / 6 + ww
xs, ys, zs = (x, y, z) .+ s
ws = xyz * 0.5 + ww
(xs, ys, zs, ws)
end
@inline function grad(table, seed, X, Y, Z, W, x, y, z, w)
hash = seed ⊻ (X ⊻ Y) ⊻ (Z ⊻ W) * HASH_MULTIPLIER
hash ⊻= hash >> (64 - OS2_NUM_GRADIENTS_EXP_4D + 2)
i = trunc(hash) & ((OS2_NUM_GRADIENTS_4D - 1) << 2)
@inbounds t = (table[i+1], table[(i|1)+1], table[(i|2)+1], table[(i|3)+1])
sum((t .* (x, y, z, w)))
end
function sample(sampler::S, x::T, y::T, z::T, w::T) where {O,S<:OpenSimplex2{4,O},T<:Real}
seed = sampler.random_state.seed
table = sampler.table
state = sampler.simplex_state
falloff = state.falloff
primes = (PRIME_X, PRIME_Y, PRIME_Z, PRIME_W)
tr = orient(S, x, y, z, w)
X1, Y1, Z1, W1 = floor.(Int, tr)
X2, Y2, Z2, W2 = (X1, Y1, Z1, W1) .* primes
v = tr .- (X1, Y1, Z1, W1)
sv = sum(v)
lattice = trunc(Int, sv * 1.25)
lattice_offset = lattice * -OS2_LATTICE_STEP_4D
x1, y1, z1, w1 = v .+ lattice_offset
ssi = (sv + 4lattice_offset) * OS2_UNSKEW_4D
seed += lattice * OS2_SEED_OFFSET_4D
result = 0.0
for i in 0:4
score = 1 + ssi * (-1 / OS2_UNSKEW_4D)
if x1 ≥ y1 && x1 ≥ z1 && x1 ≥ w1 && x1 ≥ score
X2 += PRIME_X
x1 -= 1
ssi -= OS2_UNSKEW_4D
elseif y1 > x1 && y1 ≥ z1 && y1 ≥ w1 && y1 ≥ score
Y2 += PRIME_Y
y1 -= 1
ssi -= OS2_UNSKEW_4D
elseif z1 > x1 && z1 > y1 && z1 ≥ w1 && z1 ≥ score
Z2 += PRIME_Z
z1 -= 1
ssi -= OS2_UNSKEW_4D
elseif w1 > x1 && w1 > y1 && w1 > z1 && w1 ≥ score
W2 += PRIME_W
w1 -= 1
ssi -= OS2_UNSKEW_4D
end
xyzw = (x1, y1, z1, w1) .+ ssi
a = sum(xyzw .^ 2)
if a < falloff
result += pow4(a - falloff) * grad(table, seed, X2, Y2, Z2, W2, xyzw...)
end
if i !== 4
x1, y1, z1, w1 = (x1, y1, z1, w1) .+ OS2_LATTICE_STEP_4D
ssi += OS2_LATTICE_STEP_4D * 4OS2_UNSKEW_4D
seed -= OS2_SEED_OFFSET_4D
if i == lattice
X2, Y2, Z2, W2 = (X2, Y2, Z2, W2) .- primes
seed += 5OS2_SEED_OFFSET_4D
end
end
end
result * state.scale_factor
end