-
Notifications
You must be signed in to change notification settings - Fork 5
/
opensimplex_noise.jl
1244 lines (1203 loc) · 45.5 KB
/
opensimplex_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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
struct OpenSimplex{N} <: NoiseSampler{N}
random_state::RandomState
perlin_state::PerlinState
simplex_state::SimplexState
table::Vector{UInt8}
end
@inline function _opensimplex(dims, seed, smooth)
T = OpenSimplex{dims}
rs = RandomState(seed)
# 0:3:45 and 48:3:69
table = shuffle(rs.rng, [repeat(0x00:0x03:0x2d, 11); repeat(0x30:0x03:0x45, 10)])
T(rs, PerlinState(rs), SimplexState(T, Val(smooth)), table)
end
SimplexState(::Type{<:OpenSimplex{2}}, ::Val{true}) = SimplexState(1, 0.8923035729617332)
SimplexState(::Type{<:OpenSimplex{2}}, ::Val{false}) = SimplexState(2, 1 / 40.7)
SimplexState(::Type{<:OpenSimplex{3}}, ::Val{true}) = SimplexState(1, 0.3777736183312849)
SimplexState(::Type{<:OpenSimplex{3}}, ::Val{false}) = SimplexState(2, 1 / 103)
SimplexState(::Type{<:OpenSimplex{4}}, ::Val{true}) = SimplexState(1, 1.4485981091431033)
SimplexState(::Type{<:OpenSimplex{4}}, ::Val{false}) = SimplexState(2, 1 / 30)
# 2D
const OS_GRADIENTS_2D = Int8.((5, 2, 2, 5, -5, 2, -2, 5, 5, -2, 2, -5, -5, -2, -2, -5))
const OS_STRETCH_2D = (1 / sqrt(3) - 1) / 2
const OS_SQUISH_2D = (sqrt(3) - 1) / 2
"""
opensimplex_2d(; seed=nothing, smooth=false)
Construct a sampler that outputs 2-dimensional legacy OpenSimplex 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.
# See also:
- [`opensimplex2_2d`](@ref opensimplex2_2d)
- [`openSimplex2s_2d`](@ref opensimplex2s_2d)
"""
opensimplex_2d(; seed=nothing, smooth=false) = _opensimplex(2, seed, smooth)
@inline function contribute(sampler::OpenSimplex{2}, falloff, X, Y, x, y)
t = sampler.perlin_state.table
g = OS_GRADIENTS_2D
@inbounds i = (t[t[X+1]+Y+1] & 14) + 1
a = falloff - x^2 - y^2
@inbounds a > 0 ? pow4(a) * (g[i] * x + g[i+1] * y) : 0.0
end
function sample(sampler::OpenSimplex{2}, x::T, y::T) where {T<:Real}
s = (x, y) .+ ((x + y) * OS_STRETCH_2D)
state = sampler.simplex_state
falloff = state.falloff
X0, Y0 = floor.(Int, s)
X1, Y1 = (X0, Y0) .& 255 .+ 1
x1, y1 = (x, y) .- ((X0 + Y0) * OS_SQUISH_2D) .- (X0, Y0)
y2, x3 = (y1, x1) .- OS_SQUISH_2D
x2, y3 = (x1, y1) .- 1 .- OS_SQUISH_2D
Xs, Ys = s .- (X0, Y0)
XYs = Xs + Ys
c1 = contribute(sampler, falloff, X1 + 1, Y1, x2, y2)
c2 = contribute(sampler, falloff, X1, Y1 + 1, x3, y3)
result = c1 + c2
if XYs ≤ 1
Zs = 1 - XYs
if Zs > Xs || Zs > Ys
if Xs > Ys
X2, y4 = (X1, y1) .+ 1
Y2, x4 = (Y1, x1) .- 1
else
X2, y4 = (X1, y1) .- 1
Y2, x4 = (Y1, x1) .+ 1
end
else
X2, Y2 = X1 + 1, Y1 + 1
x4, y4 = (x1, y1) .- 1 .- 2OS_SQUISH_2D
end
else
Zs = 2 - XYs
if Zs < Xs || Zs < Ys
if Xs > Ys
X2, Y2 = X1 + 2, Y1
x4, y4 = (x1, y1) .- 2 .- 2OS_SQUISH_2D
else
X2, Y2 = X1, Y1 + 1
x4 = x1 - 2OS_SQUISH_2D
y4 = y1 - 2 - 2OS_SQUISH_2D
end
else
X2, Y2 = X1, Y1
x4, y4 = x1, y1
end
X1, Y1 = (X1, Y1) .+ 1
x1, y1 = (x1, y1) .- 2OS_SQUISH_2D .- 1
end
c1 = contribute(sampler, falloff, X1, Y1, x1, y1)
c2 = contribute(sampler, falloff, X2, Y2, x4, y4)
(result + c1 + c2) * state.scale_factor
end
# 3D
const OS_STRETCH_3D = 1 / -6
const OS_SQUISH_3D = 1 / 3
const OS_GRADIENTS_3D =
Int8.((
-11, 4, 4, -4, 11, 4, -4, 4, 11, 11, 4, 4, 4, 11, 4, 4, 4, 11, -11, -4, 4, -4, -11, 4, -4,
-4, 11, 11, -4, 4, 4, -11, 4, 4, -4, 11, -11, 4, -4, -4, 11, -4, -4, 4, -11, 11, 4, -4, 4,
11, -4, 4, 4, -11, -11, -4, -4, -4, -11, -4, -4, -4, -11, 11, -4, -4, 4, 11, -4, 4, -4, -11,
))
"""
opensimplex_3d(; seed=nothing, smooth=false)
Construct a sampler that outputs 3-dimensional legacy OpenSimplex 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.
# See also:
- [`opensimplex2_3d`](@ref opensimplex2_3d)
- [`opensimplex2s_3d`](@ref opensimplex2s_3d)
"""
opensimplex_3d(; seed=nothing, smooth=false) = _opensimplex(3, seed, smooth)
@inline function contribute(sampler::OpenSimplex{3}, falloff, X, Y, Z, x, y, z)
t = sampler.table
g = OS_GRADIENTS_3D
@inbounds i = t[(Z+t[(Y+t[(X&0xff+1)])&0xff+1])&0xff+1] + 1
a = falloff - x^2 - y^2 - z^2
@inbounds a > 0 ? pow4(a) * (g[i] * x + g[i+1] * y + g[i+2] * z) : 0.0
end
function sample(sampler::OpenSimplex{3}, x::T, y::T, z::T) where {T<:Real}
s = (x, y, z) .+ ((x + y + z) * OS_STRETCH_3D)
state = sampler.simplex_state
falloff = state.falloff
X0, Y0, Z0 = floor.(Int, s)
X1, Y1, Z1 = (X0, Y0, Z0) .& 255 .+ 1
x1, y1, z1 = (x, y, z) .- ((X0 + Y0 + Z0) * OS_SQUISH_3D) .- (X0, Y0, Z0)
Xs, Ys, Zs = s .- (X0, Y0, Z0)
XYZs = Xs + Ys + Zs
result = 0.0
if XYZs ≤ 1
p1, p2, s1, s2, Ws = 1, 2, Xs, Ys, 1 - XYZs
if s1 ≥ s2 && Zs > s2
s2, p2 = Zs, 4
elseif s1 < s2 && Zs > s1
s1, p1 = Zs, 4
end
if Ws > s1 || Ws > s2
c = s2 > s1 ? p2 : p1
if iszero(c & 1)
X2, X3, x8, x9 = X1 - 1, X1, x1 + 1, x1
else
X, x = X1 + 1, x1 - 1
X2, X3, x8, x9 = X, X, x, x
end
if iszero(c & 2)
Y2, Y3, y8, y9 = Y1, Y1, y1, y1
if iszero(c & 1)
Y3 -= 1
y9 += 1
else
Y2 -= 1
y8 += 1
end
else
Y, y = Y1 + 1, y1 - 1
Y2, Y3, y8, y9 = Y, Y, y, y
end
if iszero(c & 4)
Z2, Z3, z8, z9 = Z1, Z1 - 1, z1, z1 + 1
else
Z, z = Z1 + 1, z1 - 1
Z2, Z3, z8, z9 = Z, Z, z, z
end
else
c = (p1 | p2) & 0xff
if iszero(c & 1)
X2, X3 = X1, X1 - 1
x8 = x1 - 2OS_SQUISH_3D
x9 = x1 + 1 - OS_SQUISH_3D
else
X = X1 + 1
X2, X3 = X, X
x8 = x1 - 1 - 2OS_SQUISH_3D
x9 = x1 - 1 - OS_SQUISH_3D
end
if iszero(c & 2)
Y2, Y3 = Y1, Y1 - 1
y8 = y1 - 2OS_SQUISH_3D
y9 = y1 + 1 - OS_SQUISH_3D
else
Y = Y1 + 1
Y2, Y3 = Y, Y
y8 = y1 - 1 - 2OS_SQUISH_3D
y9 = y1 - 1 - OS_SQUISH_3D
end
if iszero(c & 4)
Z2, Z3 = Z1, Z1 - 1
z8 = z1 - 2OS_SQUISH_3D
z9 = z1 + 1 - OS_SQUISH_3D
else
Z = Z1 + 1
Z2, Z3 = Z, Z
z8 = z1 - 1 - 2OS_SQUISH_3D
z9 = z1 - 1 - OS_SQUISH_3D
end
end
y2, z2, x3 = (y1, z1, x1) .- OS_SQUISH_3D
x2, y3, z4 = (x1, y1, z1) .- 1 .- OS_SQUISH_3D
z3, x4, y4 = z2, x3, y2
c1 = contribute(sampler, falloff, X1, Y1, Z1, x1, y1, z1)
c2 = contribute(sampler, falloff, X1 + 1, Y1, Z1, x2, y2, z2)
c3 = contribute(sampler, falloff, X1, Y1 + 1, Z1, x3, y3, z3)
c4 = contribute(sampler, falloff, X1, Y1, Z1 + 1, x4, y4, z4)
result += c1 + c2 + c3 + c4
elseif XYZs ≥ 2
p1, p2, s1, s2, Ws = 6, 5, Xs, Ys, 3 - XYZs
if s1 ≤ s2 && Zs < s2
s2, p2 = Zs, 3
elseif s1 > s2 && Zs < s1
s1, p1 = Zs, 3
end
if Ws < s1 || Ws < s2
c = s2 < s1 ? p2 : p1
if !iszero(c & 1)
X2, X3 = X1 + 2, X1 + 1
x8 = x1 - 2 - 3OS_SQUISH_3D
x9 = x1 - 1 - 3OS_SQUISH_3D
else
x = x1 - 3OS_SQUISH_3D
X2, X3, x8, x9 = X1, X1, x, x
end
if !iszero(c & 2)
Y = Y1 + 1
y = y1 - 1 - 3OS_SQUISH_3D
Y2, Y3, y8, y9 = Y, Y, y, y
if !iszero(c & 1)
Y3 += 1
y9 -= 1
else
Y2 += 1
y8 -= 1
end
else
y = y1 - 3OS_SQUISH_3D
Y2, Y3, y8, y9 = Y1, Y1, y, y
end
if !iszero(c & 4)
Z2, Z3 = Z1 + 1, Z1 + 2
z8 = z1 - 1 - 3OS_SQUISH_3D
z9 = z1 - 2 - 3OS_SQUISH_3D
else
z = z1 - 3OS_SQUISH_3D
Z2, Z3, z8, z9 = Z1, Z1, z, z
end
else
c = (p1 & p2) & 0xff
if !iszero(c & 1)
X2, X3 = X1 + 1, X1 + 2
x8 = x1 - 1 - OS_SQUISH_3D
x9 = x1 - 2 - 2OS_SQUISH_3D
else
X2, X3 = X1, X1
x8 = x1 - OS_SQUISH_3D
x9 = x1 - 2OS_SQUISH_3D
end
if !iszero(c & 2)
Y2, Y3 = Y1 + 1, Y1 + 2
y8 = y1 - 1 - OS_SQUISH_3D
y9 = y1 - 2 - 2OS_SQUISH_3D
else
Y2, Y3 = Y1, Y1
y8 = y1 - OS_SQUISH_3D
y9 = y1 - 2OS_SQUISH_3D
end
if !iszero(c & 4)
Z2, Z3 = Z1 + 1, Z1 + 2
z8 = z1 - 1 - OS_SQUISH_3D
z9 = z1 - 2 - 2OS_SQUISH_3D
else
Z2, Z3 = Z1, Z1
z8 = z1 - OS_SQUISH_3D
z9 = z1 - 2OS_SQUISH_3D
end
end
z3, x4, y4 = (z1, x1, y1) .- 1 .- 2OS_SQUISH_3D
x3, y2, z2 = x4, y4, z3
z4, y3, x2 = (z1, y1, x1) .- 2OS_SQUISH_3D
x1, y1, z1 = (x1, y1, z1) .- 1 .- 3OS_SQUISH_3D
c1 = contribute(sampler, falloff, X1 + 1, Y1 + 1, Z1, x4, y4, z4)
c2 = contribute(sampler, falloff, X1 + 1, Y1, Z1 + 1, x3, y3, z3)
c3 = contribute(sampler, falloff, X1, Y1 + 1, Z1 + 1, x2, y2, z2)
c4 = contribute(sampler, falloff, X1 + 1, Y1 + 1, Z1 + 1, x1, y1, z1)
result += c1 + c2 + c3 + c4
else
p3, p4, p5 = Xs + Ys, Xs + Zs, Ys + Zs
if p3 > 1
s1, p1, p1_farthest = p3 - 1, 3, true
else
s1, p1, p1_farthest = 1 - p3, 4, false
end
if p4 > 1
s2, p2, p2_farthest = p4 - 1, 5, true
else
s2, p2, p2_farthest = 1 - p4, 2, false
end
if p5 > 1
s = p5 - 1
if s1 ≤ s2 && s1 < s
s1, p1, p1_farthest = s, 6, true
elseif s1 > s2 && s2 < s
s2, p2, p2_farthest = s, 6, true
end
else
s = 1 - p5
if s1 ≤ s2 && s1 < s
s1, p1, p1_farthest = s, 1, false
elseif s1 > s2 && s2 < s
s2, p2, p2_farthest = s, 1, false
end
end
if p1_farthest === p2_farthest
if p1_farthest === true
c = p1 & p2
x8, y8, z8 = (x1, y1, z1) .- 1 .- 3OS_SQUISH_3D
X2, Y2, Z2 = Z1 + 1, Y1 + 1, Z1 + 1
if !iszero(c & 1)
x9 = x1 - 2 - 2OS_SQUISH_3D
y9, z9 = (y1, z1) .- 2OS_SQUISH_3D
X3, Y3, Z3 = X1 + 2, Y1, Z1
elseif !iszero(c & 2)
x9, z9 = (x1, z1) .- 2OS_SQUISH_3D
y9 = y1 - 2 - 2OS_SQUISH_3D
X3, Y3, Z3 = X1, Y1 + 2, Z1
else
x9, y9 = (x1, y1) .- 2OS_SQUISH_3D
z9 = z1 - 2 - 2OS_SQUISH_3D
X3, Y3, Z3 = X1, Y1, Z1 + 2
end
else
c = p1 | p2
x8, y8, z8, X2, Y2, Z2 = x1, y1, z1, X1, Y1, Z1
if iszero(c & 1)
x9 = x1 + 1 - OS_SQUISH_3D
y9, z9 = (y1, z1) .- 1 .- OS_SQUISH_3D
X3, Y3, Z3 = X1 - 1, Y1 + 1, Z1 + 1
elseif iszero(c & 2)
x9, z9 = (x1, z1) .- 1 .- OS_SQUISH_3D
y9 = y1 + 1 - OS_SQUISH_3D
X3, Y3, Z3 = X1 + 1, Y1 - 1, Z1 + 1
else
x9, y9 = (x1, y1) .- 1 .- OS_SQUISH_3D
z9 = z1 + 1 - OS_SQUISH_3D
X3, Y3, Z3 = X1 + 1, Y1 + 1, Z1 - 1
end
end
else
c1 = p1_farthest ? p1 : p2
c2 = p1_farthest ? p2 : p1
if iszero(c1 & 1)
x8 = x1 + 1 - OS_SQUISH_3D
y8, z8 = (y1, z1) .- 1 .- OS_SQUISH_3D
X2, Y2, Z2 = X1 - 1, Y1 + 1, Z1 + 1
elseif iszero(c1 & 2)
x8, z8 = (x1, z1) .- 1 .- OS_SQUISH_3D
y8 = y1 + 1 - OS_SQUISH_3D
X2, Y2, Z2 = X1 + 1, Y1 - 1, Z1 + 1
else
x8, y8 = (x1, y1) .- 1 .- OS_SQUISH_3D
z8 = z1 + 1 - OS_SQUISH_3D
X2, Y2, Z2 = X1 + 1, Y1 + 1, Z1 - 1
end
x9, y9, z9 = (x1, y1, z1) .- 2OS_SQUISH_3D
X3, Y3, Z3 = X1, Y1, Z1
if !iszero(c2 & 1)
x9 -= 2
X3 += 2
elseif !iszero(c2 & 2)
y9 -= 2
Y3 += 2
else
z9 -= 2
Z3 += 2
end
end
y2, z2, x3 = (y1, z1, x1) .- OS_SQUISH_3D
x5, y5, z6 = (x1, y1, z1) .- 1 .- 2OS_SQUISH_3D
x2, y3, z4 = (x1, y1, z1) .- 1 .- OS_SQUISH_3D
z5, y6, x7 = (z1, y1, x1) .- 2OS_SQUISH_3D
z3, x4, y4, x6, y7, z7 = z2, x3, y2, x5, y5, z6
c1 = contribute(sampler, falloff, X1 + 1, Y1, Z1, x2, y2, z2)
c2 = contribute(sampler, falloff, X1, Y1 + 1, Z1, x3, y3, z3)
c3 = contribute(sampler, falloff, X1, Y1, Z1 + 1, x4, y4, z4)
c4 = contribute(sampler, falloff, X1 + 1, Y1 + 1, Z1, x5, y5, z5)
c5 = contribute(sampler, falloff, X1 + 1, Y1, Z1 + 1, x6, y6, z6)
c6 = contribute(sampler, falloff, X1, Y1 + 1, Z1 + 1, x7, y7, z7)
result += c1 + c2 + c3 + c4 + c5 + c6
end
c1 = contribute(sampler, falloff, X2, Y2, Z2, x8, y8, z8)
c2 = contribute(sampler, falloff, X3, Y3, Z3, x9, y9, z9)
(result + c1 + c2) * state.scale_factor
end
# 4D
const OS_STRETCH_4D = (1 / sqrt(5) - 1) / 4
const OS_SQUISH_4D = (sqrt(5) - 1) / 4
const OS_GRADIENTS_4D = [
3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, -3, 1, 1, 1, -1, 3, 1, 1, -1, 1, 3, 1, -1,
1, 1, 3, 3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3, 1, 1, -1, 1, 3, -3, -1, 1, 1, -1, -3, 1, 1, -1,
-1, 3, 1, -1, -1, 1, 3, 3, 1, -1, 1, 1, 3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3, -3, 1, 1, 1, -1,
3, -1, 1, -1, 1, -3, 1, -1, 1, -1, 3, 3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3, 1, 1, -1, -1,
3, -3, -1, -1, 1, -1, -3, -1, 1, -1, -1, -3, 1, -1, -1, -1, 3, 3, 1, 1, -1, 1, 3, 1, -1, 1,
1, 3, -1, 1, 1, 1, -3, -3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3, -1, -1, 1, 1, -3, 3, -1, 1, -1,
1, -3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3, -3, -1, 1, -1, -1, 3, 1, -1, -1, -1, 3, -1, -1,
-1, 1, -3, 3, 1, -1, -1, 1, 3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3, -3, 1, -1, -1, -1, 3, -1,
-1, -1, 1, -3, -1, -1, 1, -1, -3, 3, -1, -1, -1, 1, -3, -1, -1, 1, -1, -3, -1, 1, -1, -1,
-3, -3, -1, -1, -1, -1, -3, -1, -1, -1, -1, 3, -1, -1, -1, -1, -3]
"""
opensimplex_4d(; seed=nothing, smooth=false)
Construct a sampler that outputs 4-dimensional legacy OpenSimplex 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.
# See also:
- [`opensimplex2_4d`](@ref opensimplex2_4d)
- [`opensimplex2s_4d`](@ref opensimplex2s_4d)
"""
opensimplex_4d(; seed=nothing, smooth=false) = _opensimplex(4, seed, smooth)
@inline function contribute(sampler::OpenSimplex{4}, falloff, X, Y, Z, W, x, y, z, w)
t = sampler.perlin_state.table
g = OS_GRADIENTS_4D
@inbounds i = t[(W+t[(Z+t[(Y+t[(X&0xff+1)])&0xff+1])&0xff+1]&0xff+1)] & 252 + 1
a = falloff - x^2 - y^2 - z^2 - w^2
@inbounds a > 0 ? pow4(a) * (g[i] * x + g[i+1] * y + g[i+2] * z + g[i+3] * w) : 0.0
end
function sample(sampler::OpenSimplex{4}, x::T, y::T, z::T, w::T) where {T<:Real}
s = (x, y, z, w) .+ ((x + y + z + w) * OS_STRETCH_4D)
state = sampler.simplex_state
falloff = state.falloff
X0, Y0, Z0, W0 = floor.(Int, s)
X1, Y1, Z1, W1 = (X0, Y0, Z0, W0) .& 255 .+ 1
x1, y1, z1, w1 = (x, y, z, w) .- ((X0 + Y0 + Z0 + W0) * OS_SQUISH_4D) .- (X0, Y0, Z0, W0)
Xs, Ys, Zs, Ws = s .- (X0, Y0, Z0, W0)
XYZWs = Xs + Ys + Zs + Ws
result = 0.0
if XYZWs ≤ 1
p1, p2, s1, s2, Us = 1, 2, Xs, Ys, 1 - XYZWs
if s1 ≥ s2 && Zs > s2
s2, p2 = Zs, 4
elseif s1 < s2 && Zs > s1
s1, p1 = Zs, 4
end
if s1 ≥ s2 && Ws > s2
s2, p2 = Ws, 8
elseif s1 < s2 && Ws > s1
s1, p1 = Ws, 8
end
if Us > s1 || Us > s2
c = s2 > s1 ? p2 : p1
if iszero(c & 1)
X2, X3, X4, x12, x13, x14 = X1 - 1, X1, X1, x1 + 1, x1, x1
else
X, = X1 + 1
x = x1 - 1
X2, X3, X4, x12, x13, x14 = X, X, X, x, x, x
end
if iszero(c & 2)
Y2, Y3, Y4, y12, y13, y14 = Y1, Y1, Y1, y1, y1, y1
if (c & 1) == 1
Y2 -= 1
y12 += 1
else
Y3 -= 1
y13 += 1
end
else
_Y1, _y = Y1 + 1, y1 - 1
Y2, Y3, Y4, y12, y13, y14 = _Y1, _Y1, _Y1, _y, _y, _y
end
if iszero(c & 4)
Z12, Z13, Z14, z12, z13, z14 = Z1, Z1, Z1, z1, z1, z1
if !iszero(c & 3)
if (c & 3) !== 3
Z13 -= 1
z13 += 1
end
else
Z14 -= 1
z14 += 1
end
else
_Z1, _z = Z1 + 1, z1 - 1
Z12, Z13, Z14, z12, z13, z14 = _Z1, _Z1, _Z1, _z, _z, _z
end
if iszero(c & 8)
W12, W13, W14, w12, w13, w14 = W1, W1, W1 - 1, w1, w1, w1 + 1
else
_W1, _w = W1 + 1, w1 - 1
W12, W13, W14, w12, w13, w14 = _W1, _W1, _W1, _w, _w, _w
end
else
c = p1 | p2
if iszero(c & 1)
X2, X3, X4 = X1, X1 - 1, X1
x12 = x1 - 2OS_SQUISH_4D
x13 = x1 + 1 - OS_SQUISH_4D
x14 = x1 - OS_SQUISH_4D
else
X, x = X1 + 1, x1 - 1 - OS_SQUISH_4D
X2, X3, X4, x13, x14 = X, X, X, x, x
x12 = x1 - 1 - 2OS_SQUISH_4D
end
if iszero(c & 2)
_y = y1 - OS_SQUISH_4D
Y2, Y3, Y4, y13, y14 = Y1, Y1, Y1, _y, _y
y12 = y1 - 2OS_SQUISH_4D
if (c & 1) == 1
Y3 -= 1
y13 += 1
else
Y4 -= 1
y14 += 1
end
else
_Y1 = Y1 + 1
_y = y1 - 1 - OS_SQUISH_4D
Y2, Y3, Y4, y13, y14 = _Y1, _Y1, _Y1, _y, _y
y12 = y1 - 1 - 2OS_SQUISH_4D
end
if iszero(c & 4)
_z = z1 - OS_SQUISH_4D
Z12, Z13, Z14, z13, z14 = Z1, Z1, Z1, _z, _z
z12 = z1 - 2OS_SQUISH_4D
if (c & 3) == 3
Z13 -= 1
z13 += 1
else
Z14 -= 1
z14 += 1
end
else
_Z1 = Z1 + 1
_z = z1 - 1 - OS_SQUISH_4D
Z12, Z13, Z14, z13, z14 = _Z1, _Z1, _Z1, _z, _z
z12 = z1 - 1 - 2OS_SQUISH_4D
end
if iszero(c & 8)
W12, W13, W14 = W1, W1, W1 - 1
w12 = w1 - 2OS_SQUISH_4D
w13 = w1 - OS_SQUISH_4D
w14 = w1 + 1 - OS_SQUISH_4D
else
_W1 = W1 + 1
_w = w1 - 1 - OS_SQUISH_4D
W12, W13, W14, w13, w14 = _W1, _W1, _W1, _w, _w
w12 = w1 - 1 - 2OS_SQUISH_4D
end
end
y2, z2, w2, x3 = (y1, z1, w1, x1) .- OS_SQUISH_4D
x2, y3, z4, w5 = (x1, y1, z1, w1) .- 1 .- OS_SQUISH_4D
z3, w3, x4, y4, w4, x5, y5, z5 = (z2, w2, x3, y2, w2, x3, y2, z2)
c1 = contribute(sampler, falloff, X1, Y1, Z1, W1, x1, y1, z1, w1)
c2 = contribute(sampler, falloff, X1 + 1, Y1, Z1, W1, x2, y2, z2, w2)
c3 = contribute(sampler, falloff, X1, Y1 + 1, Z1, W1, x3, y3, z3, w3)
c4 = contribute(sampler, falloff, X1, Y1, Z1 + 1, W1, x4, y4, z4, w4)
c5 = contribute(sampler, falloff, X1, Y1, Z1, W1 + 1, x5, y5, z5, w5)
result += c1 + c2 + c3 + c4 + c5
elseif XYZWs ≥ 3
p1, p2, s1, s2, Us = 14, 13, Xs, Ys, 4 - XYZWs
if s1 ≤ s2 && Zs < s2
s2, p2 = Zs, 11
elseif s1 > s2 && Zs < s1
s1, p1 = Zs, 11
end
if s1 ≤ s2 && Ws < s2
s2, p2 = Ws, 7
elseif s1 > s2 && Ws < s1
s1, p1 = Ws, 7
end
if Us < s1 || Us < s2
c = s2 < s1 ? p2 : p1
if !iszero(c & 1)
X = X1 + 1
x = x1 - 1 - 4OS_SQUISH_4D
X2 = X1 + 2
x12 = x1 - 2 - 4OS_SQUISH_4D
X3, X4, x13, x14 = X, X, x, x
else
x = x1 - 4OS_SQUISH_4D
X2, X3, X4, x12, x13, x14 = X1, X1, X1, x, x, x
end
if !iszero(c & 2)
_Y1 = Y1 + 1
_y = y1 - 1 - 4OS_SQUISH_4D
Y2, Y3, Y4, y12, y13, y14 = _Y1, _Y1, _Y1, _y, _y, _y
if !iszero(c & 1)
Y3 += 1
y13 -= 1
else
Y2 += 1
y12 -= 1
end
else
_y = y1 - 4OS_SQUISH_4D
Y2, Y3, Y4, y12, y13, y14 = Y1, Y1, Y1, _y, _y, _y
end
if !iszero(c & 4)
_Z1 = Z1 + 1
_z = z1 - 1 - 4OS_SQUISH_4D
Z12, Z13, Z14, z12, z13, z14 = _Z1, _Z1, _Z1, _z, _z, _z
if (c & 3) !== 3
if !iszero(c & 3)
Z13 += 1
z13 -= 1
end
else
Z14 += 1
z14 -= 1
end
else
_z = z1 - 4OS_SQUISH_4D
Z12, Z13, Z14, z12, z13, z14 = Z1, Z1, Z1, _z, _z, _z
end
if !iszero(c & 8)
_W1 = W1 + 1
_w = w1 - 1 - 4OS_SQUISH_4D
W12, W13, w12, w13 = _W1, _W1, _w, _w
W14 = W1 + 2
w14 = w1 - 2 - 4OS_SQUISH_4D
else
_w = w1 - 4OS_SQUISH_4D
W12, W13, W14, w12, w13, w14 = W1, W1, W1, _w, _w, _w
end
else
c = p1 & p2
if !iszero(c & 1)
X = X1 + 1
X2, X3, X4 = X, X1 + 2, X
x12 = x1 - 1 - 2OS_SQUISH_4D
x13 = x1 - 2 - 3OS_SQUISH_4D
x14 = x1 - 1 - 3OS_SQUISH_4D
else
x = x1 - 3OS_SQUISH_4D
X2, X3, X4, x13, x14 = X1, X1, X1, x, x
x12 = x1 - 2OS_SQUISH_4D
end
if !iszero(c & 2)
_Y1 = Y1 + 1
_y = y1 - 1 - 3OS_SQUISH_4D
Y2, Y3, Y4, y13, y14 = _Y1, _Y1, _Y1, _y, _y
y12 = y1 - 1 - 2OS_SQUISH_4D
if !iszero(c & 1)
Y4 += 1
y14 -= 1
else
Y3 += 1
y13 -= 1
end
else
_y = y1 - 3OS_SQUISH_4D
Y2, Y3, Y4, y13, y14 = Y1, Y1, Y1, _y, _y
y12 = y1 - 2OS_SQUISH_4D
end
if !iszero(c & 4)
_Z1 = Z1 + 1
_z = z1 - 1 - 3OS_SQUISH_4D
Z12, Z13, Z14, z13, z14 = _Z1, _Z1, _Z1, _z, _z
z12 = z1 - 1 - 2OS_SQUISH_4D
if !iszero(c & 3)
Z14 += 1
z14 -= 1
else
Z13 += 1
z13 -= 1
end
else
_z = z1 - 3OS_SQUISH_4D
Z12, Z13, Z14, z13, z14 = Z1, Z1, Z1, _z, _z
z12 = z1 - 2OS_SQUISH_4D
end
if !iszero(c & 8)
_W1 = W1 + 1
W12, W13, W14 = _W1, _W1, W1 + 2
w12 = w1 - 1 - 2OS_SQUISH_4D
w13 = w1 - 1 - 3OS_SQUISH_4D
w14 = w1 - 2 - 3OS_SQUISH_4D
else
_w = w1 - 3OS_SQUISH_4D
W12, W13, W14, w13, w14 = W1, W1, W1, _w, _w
w12 = w1 - 2OS_SQUISH_4D
end
end
w4, x5, y5, z5 = (w1, x1, y1, z1) .- 1 .- 3OS_SQUISH_4D
w5, z4, y3, x2 = (w1, z1, y1, x1) .- 3OS_SQUISH_4D
x4, y4, x3, z3, w3, y2, z2, w2 = x5, y5, x5, z5, w4, y5, z5, w4
x1, y1, z1, w1 = (x1, y1, z1, w1) .- 1 .- 4OS_SQUISH_4D
c1 = contribute(sampler, falloff, X1 + 1, Y1 + 1, Z1 + 1, W1, x5, y5, z5, w5)
c2 = contribute(sampler, falloff, X1 + 1, Y1 + 1, Z1, W1 + 1, x4, y4, z4, w4)
c3 = contribute(sampler, falloff, X1 + 1, Y1, Z1 + 1, W1 + 1, x3, y3, z3, w3)
c4 = contribute(sampler, falloff, X1, Y1 + 1, Z1 + 1, W1 + 1, x2, y2, z2, w2)
c5 = contribute(sampler, falloff, X1 + 1, Y1 + 1, Z1 + 1, W1 + 1, x1, y1, z1, w1)
result += c1 + c2 + c3 + c4 + c5
elseif XYZWs ≤ 2
p1, p2, s1, s2, p1_bigger, p2_bigger = 0, 0, 0.0, 0.0, true, true
p3 = 2 - XYZWs + Xs
p4 = 2 - XYZWs + Ys
p5 = 2 - XYZWs + Zs
p6 = 2 - XYZWs + Ws
XYs, ZWs, XZs, YWs, XWs, YZs = Xs + Ys, Zs + Ws, Xs + Zs, Ys + Ws, Xs + Ws, Ys + Zs
if XYs > ZWs
s1, p1 = XYs, 3
else
s1, p1 = ZWs, 12
end
if XZs > YWs
s2, p2 = XZs, 5
else
s2, p2 = YWs, 10
end
if XWs > YZs
s = XWs
if s1 ≥ s2 && s > s2
s2, p2 = s, 9
elseif s1 < s2 && s > s1
s1, p1 = s, 9
end
else
s = YZs
if s1 ≥ s2 && s > s2
s2, p2 = s, 6
elseif s1 < s2 && s > s1
s1, p1 = s, 6
end
end
if s1 ≥ s2 && p3 > s2
s2, p2, p2_bigger = p3, 1, false
elseif s1 < s2 && p3 > s1
s1, p1, p1_bigger = p3, 1, false
end
if s1 ≥ s2 && p4 > s2
s2, p2, p2_bigger = p4, 2, false
elseif s1 < s2 && p4 > s1
s1, p1, p1_bigger = p4, 2, false
end
if s1 ≥ s2 && p5 > s2
s2, p2, p2_bigger = p5, 4, false
elseif s1 < s2 && p5 > s1
s1, p1, p1_bigger = p5, 4, false
end
if s1 ≥ s2 && p6 > s2
s2, p2, p2_bigger = p6, 8, false
elseif s1 < s2 && p6 > s1
s1, p1, p1_bigger = p6, 8, false
end
if p1_bigger === p2_bigger
if p1_bigger === true
c1 = p1 | p2
c2 = p1 & p2
if iszero(c1 & 1)
X2, X3 = X1, X1 - 1
x12 = x1 - 3OS_SQUISH_4D
x13 = x1 + 1 - 2OS_SQUISH_4D
else
X = X1 + 1
X2, X3 = X, X
x12 = x1 - 1 - 3OS_SQUISH_4D
x13 = x1 - 1 - 2OS_SQUISH_4D
end
if iszero(c1 & 2)
Y2, Y3 = Y1, Y1 - 1
y12 = y1 - 3OS_SQUISH_4D
y13 = y1 + 1 - 2OS_SQUISH_4D
else
_Y1 = Y1 + 1
Y2, Y3 = _Y1, _Y1
y12 = y1 - 1 - 3OS_SQUISH_4D
y13 = y1 - 1 - 2OS_SQUISH_4D
end
if iszero(c1 & 4)
Z12, Z13 = Z1, Z1 - 1
z12 = z1 - 3OS_SQUISH_4D
z13 = z1 + 1 - 2OS_SQUISH_4D
else
_Z1 = Z1 + 1
Z12, Z13 = _Z1, _Z1
z12 = z1 - 1 - 3OS_SQUISH_4D
z13 = z1 - 1 - 2OS_SQUISH_4D
end
if iszero(c1 & 8)
W12, W13 = W1, W1 - 1
w12 = w1 - 3OS_SQUISH_4D
w13 = w1 + 1 - 2OS_SQUISH_4D
else
_W1 = W1 + 1
W12, W13 = _W1, _W1
w12 = w1 - 1 - 3OS_SQUISH_4D
w13 = w1 - 1 - 2OS_SQUISH_4D
end
X4, Y4, Z14, W14 = X1, Y1, Z1, W1
x14, y14, z14, w14 = (x1, y1, z1, w1) .- 2OS_SQUISH_4D
if !iszero(c2 & 1)
X4 += 2
x14 -= 2
elseif !iszero(c2 & 2)
Y4 += 2
y14 -= 2
elseif !iszero(c2 & 4)
Z14 += 2
z14 -= 2
else
W14 += 2
w14 -= 2
end
else
c = p1 | p2
X4, Y4, Z14, W14 = X1, Y1, Z1, W1
x14, y14, z14, w14 = x1, y1, z1, w1
if iszero(c & 1)
X2, X3 = X1 - 1, X1
x12 = x1 + 1 - OS_SQUISH_4D
x13 = x1 - OS_SQUISH_4D
else
X = X1 + 1
x = x1 - 1 - OS_SQUISH_4D
X2, X3, x12, x13 = X, X, x, x
end
if iszero(c & 2)
_y = y1 - OS_SQUISH_4D
Y2, Y3, y12, y13 = Y1, Y1, _y, _y
if (c & 1) == 1
Y2 -= 1
y12 += 1
else
Y3 -= 1
y13 += 1
end
else
_Y1 = Y1 + 1
_y = y1 - 1 - OS_SQUISH_4D
Y2, Y3, y12, y13 = _Y1, _Y1, _y, _y
end
if iszero(c & 4)
_z = z1 - OS_SQUISH_4D
Z12, Z13, z12, z13 = Z1, Z1, _z, _z
if (c & 3) == 3
Z12 -= 1
z12 += 1
else
Z13 -= 1
z13 += 1
end
else
_Z1 = Z1 + 1
_z = z1 - 1 - OS_SQUISH_4D
Z12, Z13, z12, z13 = _Z1, _Z1, _z, _z
end
if iszero(c & 8)
W12, W13 = W1, W1 - 1
w12 = w1 - OS_SQUISH_4D
w13 = w1 + 1 - OS_SQUISH_4D
else
_W1 = W1 + 1
_w = w1 - 1 - OS_SQUISH_4D
W12, W13, w12, w13 = _W1, _W1, _w, _w
end
end
else
c1 = p1_bigger === true ? p1 : p2
c2 = p1_bigger === true ? p2 : p1
if iszero(c1 & 1)
X2, X3 = X1 - 1, X1
x12 = x1 + 1 - OS_SQUISH_4D
x13 = x1 - OS_SQUISH_4D
else
X = X1 + 1
x = x1 - 1 - OS_SQUISH_4D
X2, X3, x12, x13 = X, X, x, x
end
if iszero(c1 & 2)
_y = y1 - OS_SQUISH_4D
Y2, Y3, y12, y13 = Y1, Y1, _y, _y
if (c1 & 1) == 1
Y2 -= 1
y12 += 1
else
Y3 -= 1
y13 += 1
end
else
_Y1 = Y1 + 1
_y = y1 - 1 - OS_SQUISH_4D
Y2, Y3, y12, y13 = _Y1, _Y1, _y, _y
end
if iszero(c1 & 4)
_z = z1 - OS_SQUISH_4D
Z12, Z13, z12, z13 = Z1, Z1, _z, _z
if (c1 & 3) == 3
Z12 -= 1
z12 += 1
else
Z13 -= 1
z13 += 1
end
else
_Z1 = Z1 + 1
_z = z1 - 1 - OS_SQUISH_4D
Z12, Z13, z12, z13 = _Z1, _Z1, _z, _z
end
if iszero(c1 & 8)
W12, W13 = W1, W1 - 1
w12 = w1 - OS_SQUISH_4D
w13 = w1 + 1 - OS_SQUISH_4D
else
_W1 = W1 + 1
_w = w1 - 1 - OS_SQUISH_4D
W12, W13, w12, w13 = _W1, _W1, _w, _w
end
X4, Y4, Z14, W14 = X1, Y1, Z1, W1
x14, y14, z14, w14 = (x1, y1, z1, w1) .- 2OS_SQUISH_4D
if !iszero(c2 & 1)
X4 += 2
x14 -= 2
elseif !iszero(c2 & 2)
Y4 += 2
y14 -= 2
elseif !iszero(c2 & 4)
Z14 += 2
z14 -= 2
else
W14 += 2
w14 -= 2
end
end
y2, z2, w2, x3 = (y1, z1, w1, x1) .- OS_SQUISH_4D
z6, w6, y7, x9 = (z1, w1, y1, x1) .- 2OS_SQUISH_4D
x6, y6, z7, w8 = (x1, y1, z1, w1) .- 1 .- 2OS_SQUISH_4D
x2, y3, z4, w5 = (x1, y1, z1, w1) .- 1 .- OS_SQUISH_4D
z3, w3, x4, y4, w4, x5, y5, z5 = z2, w2, x3, y2, w2, x3, y2, z2
x7, w7, x8, y8, z8, y9, z9, w9 = x6, w6, x6, y7, z6, y6, z7, w6
x10, y10, z10, w10, x11, y11, z11, w11 = x9, y6, z6, w8, x9, y7, z7, w8
c1 = contribute(sampler, falloff, X1 + 1, Y1, Z1, W1, x2, y2, z2, w2)
c2 = contribute(sampler, falloff, X1, Y1 + 1, Z1, W1, x3, y3, z3, w3)