This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathopensimplex_base.go
2178 lines (2007 loc) · 55.8 KB
/
opensimplex_base.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
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
package opensimplex
import "math"
// Vanilla opensimplex implementation, matching Kurt Spencer's Java
// reference implementation as exactly as possible.
// A seeded Noise instance. Reusing a Noise instance (rather than recreating it
// from a known seed) will save some calculation time.
type noise struct {
perm []int16
permGradIndex3D []int16
}
// Returns a random noise value in two dimensions. Repeated calls with the same
// x/y inputs will have the same output.
func (s *noise) Eval2(x, y float64) float64 {
// Place input coordinates onto grid.
stretchOffset := (x + y) * stretchConstant2D
xs := float64(x + stretchOffset)
ys := float64(y + stretchOffset)
// Floor to get grid coordinates of rhombus (stretched square) super-cell origin.
xsb := int32(math.Floor(xs))
ysb := int32(math.Floor(ys))
// Skew out to get actual coordinates of rhombus origin. We'll need these later.
squishOffset := float64(xsb+ysb) * squishConstant2D
xb := float64(xsb) + squishOffset
yb := float64(ysb) + squishOffset
// Compute grid coordinates relative to rhombus origin.
xins := xs - float64(xsb)
yins := ys - float64(ysb)
// Sum those together to get a value that determines which region we're in.
inSum := xins + yins
// Positions relative to origin point.
dx0 := x - xb
dy0 := y - yb
// We'll be defining these inside the next block and using them afterwards.
var dx_ext, dy_ext float64
var xsv_ext, ysv_ext int32
value := float64(0)
// Contribution (1,0)
dx1 := dx0 - 1 - squishConstant2D
dy1 := dy0 - 0 - squishConstant2D
attn1 := 2 - dx1*dx1 - dy1*dy1
if attn1 > 0 {
attn1 *= attn1
value += attn1 * attn1 * s.extrapolate2(xsb+1, ysb+0, dx1, dy1)
}
// Contribution (0,1)
dx2 := dx0 - 0 - squishConstant2D
dy2 := dy0 - 1 - squishConstant2D
attn2 := 2 - dx2*dx2 - dy2*dy2
if attn2 > 0 {
attn2 *= attn2
value += attn2 * attn2 * s.extrapolate2(xsb+0, ysb+1, dx2, dy2)
}
if inSum <= 1 { // We're inside the triangle (2-Simplex) at (0,0)
zins := 1 - inSum
if zins > xins || zins > yins { // (0,0) is one of the closest two triangular vertices
if xins > yins {
xsv_ext = xsb + 1
ysv_ext = ysb - 1
dx_ext = dx0 - 1
dy_ext = dy0 + 1
} else {
xsv_ext = xsb - 1
ysv_ext = ysb + 1
dx_ext = dx0 + 1
dy_ext = dy0 - 1
}
} else { // (1,0) and (0,1) are the closest two vertices.
xsv_ext = xsb + 1
ysv_ext = ysb + 1
dx_ext = dx0 - 1 - 2*squishConstant2D
dy_ext = dy0 - 1 - 2*squishConstant2D
}
} else { // We're inside the triangle (2-Simplex) at (1,1)
zins := 2 - inSum
if zins < xins || zins < yins { // (0,0) is one of the closest two triangular vertices
if xins > yins {
xsv_ext = xsb + 2
ysv_ext = ysb + 0
dx_ext = dx0 - 2 - 2*squishConstant2D
dy_ext = dy0 + 0 - 2*squishConstant2D
} else {
xsv_ext = xsb + 0
ysv_ext = ysb + 2
dx_ext = dx0 + 0 - 2*squishConstant2D
dy_ext = dy0 - 2 - 2*squishConstant2D
}
} else { // (1,0) and (0,1) are the closest two vertices.
dx_ext = dx0
dy_ext = dy0
xsv_ext = xsb
ysv_ext = ysb
}
xsb += 1
ysb += 1
dx0 = dx0 - 1 - 2*squishConstant2D
dy0 = dy0 - 1 - 2*squishConstant2D
}
// Contribution (0,0) or (1,1)
attn0 := 2 - dx0*dx0 - dy0*dy0
if attn0 > 0 {
attn0 *= attn0
value += attn0 * attn0 * s.extrapolate2(xsb, ysb, dx0, dy0)
}
// Extra Vertex
attn_ext := 2 - dx_ext*dx_ext - dy_ext*dy_ext
if attn_ext > 0 {
attn_ext *= attn_ext
value += attn_ext * attn_ext * s.extrapolate2(xsv_ext, ysv_ext, dx_ext, dy_ext)
}
return value / normConstant2D
}
// Returns a random noise value in three dimensions.
func (s *noise) Eval3(x, y, z float64) float64 {
// Place input coordinates on simplectic honeycomb.
stretchOffset := (x + y + z) * stretchConstant3D
xs := float64(x + stretchOffset)
ys := float64(y + stretchOffset)
zs := float64(z + stretchOffset)
// Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.
xsb := int32(math.Floor(xs))
ysb := int32(math.Floor(ys))
zsb := int32(math.Floor(zs))
// Skew out to get actual coordinates of rhombohedron origin. We'll need these later.
squishOffset := float64(xsb+ysb+zsb) * squishConstant3D
xb := float64(xsb) + squishOffset
yb := float64(ysb) + squishOffset
zb := float64(zsb) + squishOffset
// Compute simplectic honeycomb coordinates relative to rhombohedral origin.
xins := xs - float64(xsb)
yins := ys - float64(ysb)
zins := zs - float64(zsb)
// Sum those together to get a value that determines which region we're in.
inSum := xins + yins + zins
// Positions relative to origin point.
dx0 := x - xb
dy0 := y - yb
dz0 := z - zb
// We'll be defining these inside the next block and using them afterwards.
var dx_ext0, dy_ext0, dz_ext0 float64
var dx_ext1, dy_ext1, dz_ext1 float64
var xsv_ext0, ysv_ext0, zsv_ext0 int32
var xsv_ext1, ysv_ext1, zsv_ext1 int32
value := float64(0)
if inSum <= 1 { // We're inside the tetrahedron (3-Simplex) at (0,0,0)
// Determine which two of (0,0,1), (0,1,0), (1,0,0) are closest.
aPoint := byte(0x01)
bPoint := byte(0x02)
aScore := xins
bScore := yins
if aScore >= bScore && zins > bScore {
bScore = zins
bPoint = 0x04
} else if aScore < bScore && zins > aScore {
aScore = zins
aPoint = 0x04
}
// Now we determine the two lattice points not part of the tetrahedron that may contribute.
// This depends on the closest two tetrahedral vertices, including (0,0,0)
wins := 1 - inSum
if wins > aScore || wins > bScore { // (0,0,0) is one of the closest two tetrahedral vertices.
var c byte // Our other closest vertex is the closest out of a and b.
if bScore > aScore {
c = bPoint
} else {
c = aPoint
}
if (c & 0x01) == 0 {
xsv_ext0 = xsb - 1
xsv_ext1 = xsb
dx_ext0 = dx0 + 1
dx_ext1 = dx0
} else {
xsv_ext1 = xsb + 1
xsv_ext0 = xsv_ext1
dx_ext1 = dx0 - 1
dx_ext0 = dx_ext1
}
if (c & 0x02) == 0 {
ysv_ext1 = ysb
ysv_ext0 = ysv_ext1
dy_ext1 = dy0
dy_ext0 = dy_ext1
if (c & 0x01) == 0 {
ysv_ext1 -= 1
dy_ext1 += 1
} else {
ysv_ext0 -= 1
dy_ext0 += 1
}
} else {
ysv_ext1 = ysb + 1
ysv_ext0 = ysv_ext1
dy_ext1 = dy0 - 1
dy_ext0 = dy_ext1
}
if (c & 0x04) == 0 {
zsv_ext0 = zsb
zsv_ext1 = zsb - 1
dz_ext0 = dz0
dz_ext1 = dz0 + 1
} else {
zsv_ext1 = zsb + 1
zsv_ext0 = zsv_ext1
dz_ext1 = dz0 - 1
dz_ext0 = dz_ext1
}
} else { // (0,0,0) is not one of the closest two tetrahedral vertices.
c := aPoint | bPoint // Our two extra vertices are determined by the closest two.
if (c & 0x01) == 0 {
xsv_ext0 = xsb
xsv_ext1 = xsb - 1
dx_ext0 = dx0 - 2*squishConstant3D
dx_ext1 = dx0 + 1 - squishConstant3D
} else {
xsv_ext1 = xsb + 1
xsv_ext0 = xsv_ext1
dx_ext0 = dx0 - 1 - 2*squishConstant3D
dx_ext1 = dx0 - 1 - squishConstant3D
}
if (c & 0x02) == 0 {
ysv_ext0 = ysb
ysv_ext1 = ysb - 1
dy_ext0 = dy0 - 2*squishConstant3D
dy_ext1 = dy0 + 1 - squishConstant3D
} else {
ysv_ext1 = ysb + 1
ysv_ext0 = ysv_ext1
dy_ext0 = dy0 - 1 - 2*squishConstant3D
dy_ext1 = dy0 - 1 - squishConstant3D
}
if (c & 0x04) == 0 {
zsv_ext0 = zsb
zsv_ext1 = zsb - 1
dz_ext0 = dz0 - 2*squishConstant3D
dz_ext1 = dz0 + 1 - squishConstant3D
} else {
zsv_ext1 = zsb + 1
zsv_ext0 = zsv_ext1
dz_ext0 = dz0 - 1 - 2*squishConstant3D
dz_ext1 = dz0 - 1 - squishConstant3D
}
}
// Contribution (0,0,0)
attn0 := 2 - dx0*dx0 - dy0*dy0 - dz0*dz0
if attn0 > 0 {
attn0 *= attn0
value += attn0 * attn0 * s.extrapolate3(xsb+0, ysb+0, zsb+0, dx0, dy0, dz0)
}
// Contribution (1,0,0)
dx1 := dx0 - 1 - squishConstant3D
dy1 := dy0 - 0 - squishConstant3D
dz1 := dz0 - 0 - squishConstant3D
attn1 := 2 - dx1*dx1 - dy1*dy1 - dz1*dz1
if attn1 > 0 {
attn1 *= attn1
value += attn1 * attn1 * s.extrapolate3(xsb+1, ysb+0, zsb+0, dx1, dy1, dz1)
}
// Contribution (0,1,0)
dx2 := dx0 - 0 - squishConstant3D
dy2 := dy0 - 1 - squishConstant3D
dz2 := dz1
attn2 := 2 - dx2*dx2 - dy2*dy2 - dz2*dz2
if attn2 > 0 {
attn2 *= attn2
value += attn2 * attn2 * s.extrapolate3(xsb+0, ysb+1, zsb+0, dx2, dy2, dz2)
}
// Contribution (0,0,1)
dx3 := dx2
dy3 := dy1
dz3 := dz0 - 1 - squishConstant3D
attn3 := 2 - dx3*dx3 - dy3*dy3 - dz3*dz3
if attn3 > 0 {
attn3 *= attn3
value += attn3 * attn3 * s.extrapolate3(xsb+0, ysb+0, zsb+1, dx3, dy3, dz3)
}
} else if inSum >= 2 { // We're inside the tetrahedron (3-Simplex) at (1,1,1)
// Determine which two tetrahedral vertices are the closest, out of (1,1,0), (1,0,1), (0,1,1) but not (1,1,1).
aPoint := byte(0x06)
aScore := xins
bPoint := byte(0x05)
bScore := yins
if aScore <= bScore && zins < bScore {
bScore = zins
bPoint = 0x03
} else if aScore > bScore && zins < aScore {
aScore = zins
aPoint = 0x03
}
// Now we determine the two lattice points not part of the tetrahedron that may contribute.
// This depends on the closest two tetrahedral vertices, including (1,1,1)
wins := 3 - inSum
if wins < aScore || wins < bScore { // (1,1,1) is one of the closest two tetrahedral vertices.
var c byte // Our other closest vertex is the closest out of a and b.
if bScore < aScore {
c = bPoint
} else {
c = aPoint
}
if (c & 0x01) != 0 {
xsv_ext0 = xsb + 2
xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 2 - 3*squishConstant3D
dx_ext1 = dx0 - 1 - 3*squishConstant3D
} else {
xsv_ext1 = xsb
xsv_ext0 = xsv_ext1
dx_ext1 = dx0 - 3*squishConstant3D
dx_ext0 = dx_ext1
}
if (c & 0x02) != 0 {
ysv_ext1 = ysb + 1
ysv_ext0 = ysv_ext1
dy_ext1 = dy0 - 1 - 3*squishConstant3D
dy_ext0 = dy_ext1
if (c & 0x01) != 0 {
ysv_ext1 += 1
dy_ext1 -= 1
} else {
ysv_ext0 += 1
dy_ext0 -= 1
}
} else {
ysv_ext1 = ysb
ysv_ext0 = ysv_ext1
dy_ext1 = dy0 - 3*squishConstant3D
dy_ext0 = dy_ext1
}
if (c & 0x04) != 0 {
zsv_ext0 = zsb + 1
zsv_ext1 = zsb + 2
dz_ext0 = dz0 - 1 - 3*squishConstant3D
dz_ext1 = dz0 - 2 - 3*squishConstant3D
} else {
zsv_ext1 = zsb
zsv_ext0 = zsv_ext1
dz_ext1 = dz0 - 3*squishConstant3D
dz_ext0 = dz_ext1
}
} else { // (1,1,1) is not one of the closest two tetrahedral vertices.
c := aPoint & bPoint // Our two extra vertices are determined by the closest two.
if (c & 0x01) != 0 {
xsv_ext0 = xsb + 1
xsv_ext1 = xsb + 2
dx_ext0 = dx0 - 1 - squishConstant3D
dx_ext1 = dx0 - 2 - 2*squishConstant3D
} else {
xsv_ext1 = xsb
xsv_ext0 = xsv_ext1
dx_ext0 = dx0 - squishConstant3D
dx_ext1 = dx0 - 2*squishConstant3D
}
if (c & 0x02) != 0 {
ysv_ext0 = ysb + 1
ysv_ext1 = ysb + 2
dy_ext0 = dy0 - 1 - squishConstant3D
dy_ext1 = dy0 - 2 - 2*squishConstant3D
} else {
ysv_ext1 = ysb
ysv_ext0 = ysv_ext1
dy_ext0 = dy0 - squishConstant3D
dy_ext1 = dy0 - 2*squishConstant3D
}
if (c & 0x04) != 0 {
zsv_ext0 = zsb + 1
zsv_ext1 = zsb + 2
dz_ext0 = dz0 - 1 - squishConstant3D
dz_ext1 = dz0 - 2 - 2*squishConstant3D
} else {
zsv_ext1 = zsb
zsv_ext0 = zsv_ext1
dz_ext0 = dz0 - squishConstant3D
dz_ext1 = dz0 - 2*squishConstant3D
}
}
// Contribution (1,1,0)
dx3 := dx0 - 1 - 2*squishConstant3D
dy3 := dy0 - 1 - 2*squishConstant3D
dz3 := dz0 - 0 - 2*squishConstant3D
attn3 := 2 - dx3*dx3 - dy3*dy3 - dz3*dz3
if attn3 > 0 {
attn3 *= attn3
value += attn3 * attn3 * s.extrapolate3(xsb+1, ysb+1, zsb+0, dx3, dy3, dz3)
}
// Contribution (1,0,1)
dx2 := dx3
dy2 := dy0 - 0 - 2*squishConstant3D
dz2 := dz0 - 1 - 2*squishConstant3D
attn2 := 2 - dx2*dx2 - dy2*dy2 - dz2*dz2
if attn2 > 0 {
attn2 *= attn2
value += attn2 * attn2 * s.extrapolate3(xsb+1, ysb+0, zsb+1, dx2, dy2, dz2)
}
// Contribution (0,1,1)
dx1 := dx0 - 0 - 2*squishConstant3D
dy1 := dy3
dz1 := dz2
attn1 := 2 - dx1*dx1 - dy1*dy1 - dz1*dz1
if attn1 > 0 {
attn1 *= attn1
value += attn1 * attn1 * s.extrapolate3(xsb+0, ysb+1, zsb+1, dx1, dy1, dz1)
}
// Contribution (1,1,1)
dx0 = dx0 - 1 - 3*squishConstant3D
dy0 = dy0 - 1 - 3*squishConstant3D
dz0 = dz0 - 1 - 3*squishConstant3D
attn0 := 2 - dx0*dx0 - dy0*dy0 - dz0*dz0
if attn0 > 0 {
attn0 *= attn0
value += attn0 * attn0 * s.extrapolate3(xsb+1, ysb+1, zsb+1, dx0, dy0, dz0)
}
} else { // We're inside the octahedron (Rectified 3-Simplex) in between.
var aScore, bScore float64
var aPoint, bPoint byte
var aIsFurtherSide, bIsFurtherSide bool
// Decide between point (0,0,1) and (1,1,0) as closest
p1 := xins + yins
if p1 > 1 {
aScore = p1 - 1
aPoint = 0x03
aIsFurtherSide = true
} else {
aScore = 1 - p1
aPoint = 0x04
aIsFurtherSide = false
}
// Decide between point (0,1,0) and (1,0,1) as closest
p2 := xins + zins
if p2 > 1 {
bScore = p2 - 1
bPoint = 0x05
bIsFurtherSide = true
} else {
bScore = 1 - p2
bPoint = 0x02
bIsFurtherSide = false
}
// The closest out of the two (1,0,0) and (0,1,1) will replace the furthest out of the two decided above, if closer.
p3 := yins + zins
if p3 > 1 {
score := p3 - 1
if aScore <= bScore && aScore < score {
aScore = score
aPoint = 0x06
aIsFurtherSide = true
} else if aScore > bScore && bScore < score {
bScore = score
bPoint = 0x06
bIsFurtherSide = true
}
} else {
score := 1 - p3
if aScore <= bScore && aScore < score {
aScore = score
aPoint = 0x01
aIsFurtherSide = false
} else if aScore > bScore && bScore < score {
bScore = score
bPoint = 0x01
bIsFurtherSide = false
}
}
// Where each of the two closest points are determines how the extra two vertices are calculated.
if aIsFurtherSide == bIsFurtherSide {
if aIsFurtherSide { // Both closest points on (1,1,1) side
// One of the two extra points is (1,1,1)
dx_ext0 = dx0 - 1 - 3*squishConstant3D
dy_ext0 = dy0 - 1 - 3*squishConstant3D
dz_ext0 = dz0 - 1 - 3*squishConstant3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb + 1
// Other extra point is based on the shared axis.
c := aPoint & bPoint
if (c & 0x01) != 0 {
dx_ext1 = dx0 - 2 - 2*squishConstant3D
dy_ext1 = dy0 - 2*squishConstant3D
dz_ext1 = dz0 - 2*squishConstant3D
xsv_ext1 = xsb + 2
ysv_ext1 = ysb
zsv_ext1 = zsb
} else if (c & 0x02) != 0 {
dx_ext1 = dx0 - 2*squishConstant3D
dy_ext1 = dy0 - 2 - 2*squishConstant3D
dz_ext1 = dz0 - 2*squishConstant3D
xsv_ext1 = xsb
ysv_ext1 = ysb + 2
zsv_ext1 = zsb
} else {
dx_ext1 = dx0 - 2*squishConstant3D
dy_ext1 = dy0 - 2*squishConstant3D
dz_ext1 = dz0 - 2 - 2*squishConstant3D
xsv_ext1 = xsb
ysv_ext1 = ysb
zsv_ext1 = zsb + 2
}
} else { // Both closest points on (0,0,0) side
// One of the two extra points is (0,0,0)
dx_ext0 = dx0
dy_ext0 = dy0
dz_ext0 = dz0
xsv_ext0 = xsb
ysv_ext0 = ysb
zsv_ext0 = zsb
// Other extra point is based on the omitted axis.
c := aPoint | bPoint
if (c & 0x01) == 0 {
dx_ext1 = dx0 + 1 - squishConstant3D
dy_ext1 = dy0 - 1 - squishConstant3D
dz_ext1 = dz0 - 1 - squishConstant3D
xsv_ext1 = xsb - 1
ysv_ext1 = ysb + 1
zsv_ext1 = zsb + 1
} else if (c & 0x02) == 0 {
dx_ext1 = dx0 - 1 - squishConstant3D
dy_ext1 = dy0 + 1 - squishConstant3D
dz_ext1 = dz0 - 1 - squishConstant3D
xsv_ext1 = xsb + 1
ysv_ext1 = ysb - 1
zsv_ext1 = zsb + 1
} else {
dx_ext1 = dx0 - 1 - squishConstant3D
dy_ext1 = dy0 - 1 - squishConstant3D
dz_ext1 = dz0 + 1 - squishConstant3D
xsv_ext1 = xsb + 1
ysv_ext1 = ysb + 1
zsv_ext1 = zsb - 1
}
}
} else { // One point on (0,0,0) side, one point on (1,1,1) side
var c1, c2 byte
if aIsFurtherSide {
c1 = aPoint
c2 = bPoint
} else {
c1 = bPoint
c2 = aPoint
}
// One contribution is a permutation of (1,1,-1)
if (c1 & 0x01) == 0 {
dx_ext0 = dx0 + 1 - squishConstant3D
dy_ext0 = dy0 - 1 - squishConstant3D
dz_ext0 = dz0 - 1 - squishConstant3D
xsv_ext0 = xsb - 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb + 1
} else if (c1 & 0x02) == 0 {
dx_ext0 = dx0 - 1 - squishConstant3D
dy_ext0 = dy0 + 1 - squishConstant3D
dz_ext0 = dz0 - 1 - squishConstant3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb - 1
zsv_ext0 = zsb + 1
} else {
dx_ext0 = dx0 - 1 - squishConstant3D
dy_ext0 = dy0 - 1 - squishConstant3D
dz_ext0 = dz0 + 1 - squishConstant3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb - 1
}
// One contribution is a permutation of (0,0,2)
dx_ext1 = dx0 - 2*squishConstant3D
dy_ext1 = dy0 - 2*squishConstant3D
dz_ext1 = dz0 - 2*squishConstant3D
xsv_ext1 = xsb
ysv_ext1 = ysb
zsv_ext1 = zsb
if (c2 & 0x01) != 0 {
dx_ext1 -= 2
xsv_ext1 += 2
} else if (c2 & 0x02) != 0 {
dy_ext1 -= 2
ysv_ext1 += 2
} else {
dz_ext1 -= 2
zsv_ext1 += 2
}
}
// Contribution (1,0,0)
dx1 := dx0 - 1 - squishConstant3D
dy1 := dy0 - 0 - squishConstant3D
dz1 := dz0 - 0 - squishConstant3D
attn1 := 2 - dx1*dx1 - dy1*dy1 - dz1*dz1
if attn1 > 0 {
attn1 *= attn1
value += attn1 * attn1 * s.extrapolate3(xsb+1, ysb+0, zsb+0, dx1, dy1, dz1)
}
// Contribution (0,1,0)
dx2 := dx0 - 0 - squishConstant3D
dy2 := dy0 - 1 - squishConstant3D
dz2 := dz1
attn2 := 2 - dx2*dx2 - dy2*dy2 - dz2*dz2
if attn2 > 0 {
attn2 *= attn2
value += attn2 * attn2 * s.extrapolate3(xsb+0, ysb+1, zsb+0, dx2, dy2, dz2)
}
// Contribution (0,0,1)
dx3 := dx2
dy3 := dy1
dz3 := dz0 - 1 - squishConstant3D
attn3 := 2 - dx3*dx3 - dy3*dy3 - dz3*dz3
if attn3 > 0 {
attn3 *= attn3
value += attn3 * attn3 * s.extrapolate3(xsb+0, ysb+0, zsb+1, dx3, dy3, dz3)
}
// Contribution (1,1,0)
dx4 := dx0 - 1 - 2*squishConstant3D
dy4 := dy0 - 1 - 2*squishConstant3D
dz4 := dz0 - 0 - 2*squishConstant3D
attn4 := 2 - dx4*dx4 - dy4*dy4 - dz4*dz4
if attn4 > 0 {
attn4 *= attn4
value += attn4 * attn4 * s.extrapolate3(xsb+1, ysb+1, zsb+0, dx4, dy4, dz4)
}
// Contribution (1,0,1)
dx5 := dx4
dy5 := dy0 - 0 - 2*squishConstant3D
dz5 := dz0 - 1 - 2*squishConstant3D
attn5 := 2 - dx5*dx5 - dy5*dy5 - dz5*dz5
if attn5 > 0 {
attn5 *= attn5
value += attn5 * attn5 * s.extrapolate3(xsb+1, ysb+0, zsb+1, dx5, dy5, dz5)
}
// Contribution (0,1,1)
dx6 := dx0 - 0 - 2*squishConstant3D
dy6 := dy4
dz6 := dz5
attn6 := 2 - dx6*dx6 - dy6*dy6 - dz6*dz6
if attn6 > 0 {
attn6 *= attn6
value += attn6 * attn6 * s.extrapolate3(xsb+0, ysb+1, zsb+1, dx6, dy6, dz6)
}
}
// First extra vertex
attn_ext0 := 2 - dx_ext0*dx_ext0 - dy_ext0*dy_ext0 - dz_ext0*dz_ext0
if attn_ext0 > 0 {
attn_ext0 *= attn_ext0
value += attn_ext0 * attn_ext0 * s.extrapolate3(xsv_ext0, ysv_ext0, zsv_ext0, dx_ext0, dy_ext0, dz_ext0)
}
// Second extra vertex
attn_ext1 := 2 - dx_ext1*dx_ext1 - dy_ext1*dy_ext1 - dz_ext1*dz_ext1
if attn_ext1 > 0 {
attn_ext1 *= attn_ext1
value += attn_ext1 * attn_ext1 * s.extrapolate3(xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1)
}
return value / normConstant3D
}
// Returns a random noise value in four dimensions.
func (s *noise) Eval4(x, y, z, w float64) float64 {
// Place input coordinates on simplectic honeycomb.
stretchOffset := (x + y + z + w) * stretchConstant4D
xs := x + stretchOffset
ys := y + stretchOffset
zs := z + stretchOffset
ws := w + stretchOffset
// Floor to get simplectic honeycomb coordinates of rhombo-hypercube super-cell origin.
xsb := int32(math.Floor(xs))
ysb := int32(math.Floor(ys))
zsb := int32(math.Floor(zs))
wsb := int32(math.Floor(ws))
// Skew out to get actual coordinates of stretched rhombo-hypercube origin. We'll need these later.
squishOffset := float64(xsb+ysb+zsb+wsb) * squishConstant4D
xb := float64(xsb) + squishOffset
yb := float64(ysb) + squishOffset
zb := float64(zsb) + squishOffset
wb := float64(wsb) + squishOffset
// Compute simplectic honeycomb coordinates relative to rhombo-hypercube origin.
xins := xs - float64(xsb)
yins := ys - float64(ysb)
zins := zs - float64(zsb)
wins := ws - float64(wsb)
// Sum those together to get a value that determines which region we're in.
inSum := xins + yins + zins + wins
// Positions relative to origin point.
dx0 := x - xb
dy0 := y - yb
dz0 := z - zb
dw0 := w - wb
// We'll be defining these inside the next block and using them afterwards.
var dx_ext0, dy_ext0, dz_ext0, dw_ext0 float64
var dx_ext1, dy_ext1, dz_ext1, dw_ext1 float64
var dx_ext2, dy_ext2, dz_ext2, dw_ext2 float64
var xsv_ext0, ysv_ext0, zsv_ext0, wsv_ext0 int32
var xsv_ext1, ysv_ext1, zsv_ext1, wsv_ext1 int32
var xsv_ext2, ysv_ext2, zsv_ext2, wsv_ext2 int32
var value float64 = 0
if inSum <= 1 { // We're inside the pentachoron (4-Simplex) at (0,0,0,0)
// Determine which two of (0,0,0,1), (0,0,1,0), (0,1,0,0), (1,0,0,0) are closest.
var aPoint byte = 0x01
aScore := xins
var bPoint byte = 0x02
bScore := yins
if aScore >= bScore && zins > bScore {
bScore = zins
bPoint = 0x04
} else if aScore < bScore && zins > aScore {
aScore = zins
aPoint = 0x04
}
if aScore >= bScore && wins > bScore {
bScore = wins
bPoint = 0x08
} else if aScore < bScore && wins > aScore {
aScore = wins
aPoint = 0x08
}
// Now we determine the three lattice points not part of the pentachoron that may contribute.
// This depends on the closest two pentachoron vertices, including (0,0,0,0)
uins := 1 - inSum
if uins > aScore || uins > bScore { // (0,0,0,0) is one of the closest two pentachoron vertices.
var c byte
// Our other closest vertex is the closest out of a and b.
if bScore > aScore {
c = bPoint
} else {
c = aPoint
}
if (c & 0x01) == 0 {
xsv_ext0 = xsb - 1
xsv_ext2 = xsb
xsv_ext1 = xsv_ext2
dx_ext0 = dx0 + 1
dx_ext2 = dx0
dx_ext1 = dx_ext2
} else {
xsv_ext2 = xsb + 1
xsv_ext1 = xsv_ext2
xsv_ext0 = xsv_ext1
dx_ext2 = dx0 - 1
dx_ext1 = dx_ext2
dx_ext0 = dx_ext1
}
if (c & 0x02) == 0 {
ysv_ext2 = ysb
ysv_ext1 = ysv_ext2
ysv_ext0 = ysv_ext1
dy_ext2 = dy0
dy_ext1 = dy_ext2
dy_ext0 = dy_ext1
if (c & 0x01) == 0x01 {
ysv_ext0 -= 1
dy_ext0 += 1
} else {
ysv_ext1 -= 1
dy_ext1 += 1
}
} else {
ysv_ext2 = ysb + 1
ysv_ext1 = ysv_ext2
ysv_ext0 = ysv_ext1
dy_ext2 = dy0 - 1
dy_ext1 = dy_ext2
dy_ext0 = dy_ext1
}
if (c & 0x04) == 0 {
zsv_ext2 = zsb
zsv_ext1 = zsv_ext2
zsv_ext0 = zsv_ext1
dz_ext2 = dz0
dz_ext1 = dz_ext2
dz_ext0 = dz_ext1
if (c & 0x03) != 0 {
if (c & 0x03) == 0x03 {
zsv_ext0 -= 1
dz_ext0 += 1
} else {
zsv_ext1 -= 1
dz_ext1 += 1
}
} else {
zsv_ext2 -= 1
dz_ext2 += 1
}
} else {
zsv_ext2 = zsb + 1
zsv_ext1 = zsv_ext2
zsv_ext0 = zsv_ext1
dz_ext2 = dz0 - 1
dz_ext1 = dz_ext2
dz_ext0 = dz_ext1
}
if (c & 0x08) == 0 {
wsv_ext1 = wsb
wsv_ext0 = wsv_ext1
wsv_ext2 = wsb - 1
dw_ext1 = dw0
dw_ext0 = dw_ext1
dw_ext2 = dw0 + 1
} else {
wsv_ext2 = wsb + 1
wsv_ext1 = wsv_ext2
wsv_ext0 = wsv_ext1
dw_ext2 = dw0 - 1
dw_ext1 = dw_ext2
dw_ext0 = dw_ext1
}
} else { // (0,0,0,0) is not one of the closest two pentachoron vertices.
c := aPoint | bPoint // Our three extra vertices are determined by the closest two.
if (c & 0x01) == 0 {
xsv_ext2 = xsb
xsv_ext0 = xsv_ext2
xsv_ext1 = xsb - 1
dx_ext0 = dx0 - 2*squishConstant4D
dx_ext1 = dx0 + 1 - squishConstant4D
dx_ext2 = dx0 - squishConstant4D
} else {
xsv_ext2 = xsb + 1
xsv_ext1 = xsv_ext2
xsv_ext0 = xsv_ext1
dx_ext0 = dx0 - 1 - 2*squishConstant4D
dx_ext2 = dx0 - 1 - squishConstant4D
dx_ext1 = dx_ext2
}
if (c & 0x02) == 0 {
ysv_ext2 = ysb
ysv_ext1 = ysv_ext2
ysv_ext0 = ysv_ext1
dy_ext0 = dy0 - 2*squishConstant4D
dy_ext2 = dy0 - squishConstant4D
dy_ext1 = dy_ext2
if (c & 0x01) == 0x01 {
ysv_ext1 -= 1
dy_ext1 += 1
} else {
ysv_ext2 -= 1
dy_ext2 += 1
}
} else {
ysv_ext2 = ysb + 1
ysv_ext1 = ysv_ext2
ysv_ext0 = ysv_ext1
dy_ext0 = dy0 - 1 - 2*squishConstant4D
dy_ext2 = dy0 - 1 - squishConstant4D
dy_ext1 = dy_ext2
}
if (c & 0x04) == 0 {
zsv_ext2 = zsb
zsv_ext1 = zsv_ext2
zsv_ext0 = zsv_ext1
dz_ext0 = dz0 - 2*squishConstant4D
dz_ext2 = dz0 - squishConstant4D
dz_ext1 = dz_ext2
if (c & 0x03) == 0x03 {
zsv_ext1 -= 1
dz_ext1 += 1
} else {
zsv_ext2 -= 1
dz_ext2 += 1
}
} else {
zsv_ext2 = zsb + 1
zsv_ext1 = zsv_ext2
zsv_ext0 = zsv_ext1
dz_ext0 = dz0 - 1 - 2*squishConstant4D
dz_ext2 = dz0 - 1 - squishConstant4D
dz_ext1 = dz_ext2
}
if (c & 0x08) == 0 {
wsv_ext1 = wsb
wsv_ext0 = wsv_ext1
wsv_ext2 = wsb - 1
dw_ext0 = dw0 - 2*squishConstant4D
dw_ext1 = dw0 - squishConstant4D
dw_ext2 = dw0 + 1 - squishConstant4D
} else {
wsv_ext2 = wsb + 1
wsv_ext1 = wsv_ext2
wsv_ext0 = wsv_ext1
dw_ext0 = dw0 - 1 - 2*squishConstant4D
dw_ext2 = dw0 - 1 - squishConstant4D
dw_ext1 = dw_ext2
}
}
// Contribution (0,0,0,0)
attn0 := 2 - dx0*dx0 - dy0*dy0 - dz0*dz0 - dw0*dw0
if attn0 > 0 {
attn0 *= attn0
value += attn0 * attn0 * s.extrapolate4(xsb+0, ysb+0, zsb+0, wsb+0, dx0, dy0, dz0, dw0)
}
// Contribution (1,0,0,0)
dx1 := dx0 - 1 - squishConstant4D
dy1 := dy0 - 0 - squishConstant4D
dz1 := dz0 - 0 - squishConstant4D
dw1 := dw0 - 0 - squishConstant4D
attn1 := 2 - dx1*dx1 - dy1*dy1 - dz1*dz1 - dw1*dw1
if attn1 > 0 {
attn1 *= attn1
value += attn1 * attn1 * s.extrapolate4(xsb+1, ysb+0, zsb+0, wsb+0, dx1, dy1, dz1, dw1)
}
// Contribution (0,1,0,0)
dx2 := dx0 - 0 - squishConstant4D
dy2 := dy0 - 1 - squishConstant4D
dz2 := dz1
dw2 := dw1
attn2 := 2 - dx2*dx2 - dy2*dy2 - dz2*dz2 - dw2*dw2
if attn2 > 0 {
attn2 *= attn2
value += attn2 * attn2 * s.extrapolate4(xsb+0, ysb+1, zsb+0, wsb+0, dx2, dy2, dz2, dw2)
}
// Contribution (0,0,1,0)
dx3 := dx2
dy3 := dy1
dz3 := dz0 - 1 - squishConstant4D
dw3 := dw1
attn3 := 2 - dx3*dx3 - dy3*dy3 - dz3*dz3 - dw3*dw3
if attn3 > 0 {
attn3 *= attn3
value += attn3 * attn3 * s.extrapolate4(xsb+0, ysb+0, zsb+1, wsb+0, dx3, dy3, dz3, dw3)
}
// Contribution (0,0,0,1)
dx4 := dx2