-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathNoise.cs
1473 lines (1256 loc) · 56.1 KB
/
Noise.cs
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
using System;
namespace TinkerWorX.AccidentalNoiseLibrary
{
public delegate Double InterpolationDelegate(Double value);
public delegate Double Noise2DDelegate(Double x, Double y, Int32 seed, InterpolationDelegate interp);
public delegate Double Noise3DDelegate(Double x, Double y, Double z, Int32 seed, InterpolationDelegate interp);
public delegate Double Noise4DDelegate(Double x, Double y, Double z, Double w, Int32 seed, InterpolationDelegate interp);
public delegate Double Noise6DDelegate(Double x, Double y, Double z, Double w, Double u, Double v, Int32 seed, InterpolationDelegate interp);
public static class Noise
{
public const Int32 MAX_SOURCES = 20;
internal static Int32 FastFloor(Double t)
{
return (t > 0 ? (Int32)t : (Int32)t - 1);
}
internal static Double ArrayDot(Double[] arr, Double a, Double b)
{
return a*arr[0] + b*arr[1];
}
internal static Double ArrayDot(Double[] arr, Double a, Double b, Double c)
{
return a*arr[0] + b*arr[1] + c*arr[2];
}
internal static Double ArrayDot(Double[] arr, Double x, Double y, Double z, Double w)
{
return x*arr[0] + y*arr[1] + z*arr[2] + w*arr[3];
}
internal static Double ArrayDot(Double[] arr, Double x, Double y, Double z, Double w, Double u, Double v)
{
return x*arr[0] + y*arr[1] + z*arr[2] + w*arr[3] + u*arr[4] + v*arr[5];
}
internal static void AddDistance(Double[] f, Double[] disp, Double testdist, Double testdisp)
{
// Compare the given distance to the ones already in f
if (testdist >= f[3]) return;
var index = 3;
while (index > 0 && testdist < f[index - 1]) index--;
for (var i = 3; i-- > index; )
{
f[i + 1] = f[i];
disp[i + 1] = disp[i];
}
f[index] = testdist;
disp[index] = testdisp;
}
// Interpolation functions
public static Double NoInterpolation(Double t)
{
return 0;
}
public static Double LinearInterpolation(Double t)
{
return t;
}
public static Double HermiteInterpolation(Double t)
{
return (t * t * (3 - 2 * t));
}
public static Double QuinticInterpolation(Double t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
}
// Edge/Face/Cube/Hypercube interpolation
internal static Double Lerp(Double s, Double v1, Double v2)
{
return v1 + s * (v2 - v1);
}
#region Hashing
// The "new" FNV-1A hashing
private const UInt32 FNV_32_PRIME = 0x01000193;
private const UInt32 FNV_32_INIT = 2166136261;
private const UInt32 FNV_MASK_8 = (1 << 8) - 1;
internal static UInt32 FNV32Buffer(Int32[] uintBuffer, UInt32 len)
{
//NOTE: Completely untested.
var buffer = new byte[len];
Buffer.BlockCopy(uintBuffer, 0, buffer, 0, buffer.Length);
var hval = FNV_32_INIT;
for (var i = 0; i < len;)
{
hval ^= buffer[i++];
hval *= FNV_32_PRIME;
}
return hval;
}
internal static UInt32 FNV32Buffer(Double[] doubleBuffer, UInt32 len)
{
//NOTE: Completely untested.
var buffer = new byte[len];
Buffer.BlockCopy(doubleBuffer, 0, buffer, 0, buffer.Length);
var hval = FNV_32_INIT;
for (var i = 0; i < len;)
{
hval ^= buffer[i++];
hval *= FNV_32_PRIME;
}
return hval;
}
internal static UInt32 FNV32Buffer(Byte[] buffer, UInt32 len)
{
var hval = FNV_32_INIT;
for (var i = 0; i < len;)
{
hval ^= buffer[i++];
hval *= FNV_32_PRIME;
}
return hval;
}
internal static UInt32 FNV1A_3d(Double x, Double y, Double z, Int32 seed)
{
Double[] d = {x, y, z, seed};
return FNV32Buffer(d, sizeof (Double)*4);
}
internal static Byte XORFoldHash(UInt32 hash)
{
// Implement XOR-folding to reduce from 32 to 8-bit hash
return (byte)((hash >> 8) ^ (hash & FNV_MASK_8));
}
// FNV-based coordinate hashes
internal static UInt32 HashCoordinates(Int32 x, Int32 y, Int32 seed)
{
Int32[] d = { x, y, seed };
return XORFoldHash(FNV32Buffer(d, sizeof(Int32) * 3));
}
internal static UInt32 HashCoordinates(Int32 x, Int32 y, Int32 z, Int32 seed)
{
Int32[] d = { x, y, z, seed };
return XORFoldHash(FNV32Buffer(d, sizeof(Int32) * 4));
}
internal static UInt32 HashCoordinates(Int32 x, Int32 y, Int32 z, Int32 w, Int32 seed)
{
Int32[] d = { x, y, z, w, seed };
return XORFoldHash(FNV32Buffer(d, sizeof(Int32) * 5));
}
internal static UInt32 HashCoordinates(Int32 x, Int32 y, Int32 z, Int32 w, Int32 u, Int32 v, Int32 seed)
{
Int32[] d = { x, y, z, w, u, v, seed };
return XORFoldHash(FNV32Buffer(d, sizeof(Int32) * 7));
}
internal static UInt32 HashCoordinates(Double x, Double y, Int32 seed)
{
Double[] d = {x, y, seed};
return XORFoldHash(FNV32Buffer(d, sizeof (Double)*3));
}
internal static UInt32 HashCoordinates(Double x, Double y, Double z, Int32 seed)
{
Double[] d = {x, y, z, seed};
return XORFoldHash(FNV32Buffer(d, sizeof (Double)*4));
}
internal static UInt32 HashCoordinates(Double x, Double y, Double z, Double w, Int32 seed)
{
Double[] d = {x, y, z, w, seed};
return XORFoldHash(FNV32Buffer(d, sizeof (Double)*5));
}
internal static UInt32 HashCoordinates(Double x, Double y, Double z, Double w, Double u, Double v, Int32 seed)
{
Double[] d = {x, y, z, w, u, v, seed};
return XORFoldHash(FNV32Buffer(d, sizeof (Double)*7));
}
internal delegate Double WorkerNoise2(Double x, Double y, Int32 ix, Int32 iy, Int32 seed);
internal delegate Double WorkerNoise3(Double x, Double y, Double z, Int32 ix, Int32 iy, Int32 iz, Int32 seed);
internal delegate Double WorkerNoise4(Double x, Double y, Double z, Double w, Int32 ix, Int32 iy, Int32 iz, Int32 iw, Int32 seed);
internal delegate Double WorkerNoise6(Double x, Double y, Double z, Double w, Double u, Double v, Int32 ix, Int32 iy, Int32 iz, Int32 iw, Int32 iu, Int32 iv, Int32 seed);
// Noise generators
internal static Double InternalValueNoise(
Double x, Double y,
Int32 ix, Int32 iy,
Int32 seed)
{
var noise = Noise.HashCoordinates(ix, iy, seed) / 255.0;
return noise * 2.0 - 1.0;
}
internal static Double InternalValueNoise(
Double x, Double y, Double z,
Int32 ix, Int32 iy, Int32 iz,
Int32 seed)
{
var noise = Noise.HashCoordinates(ix, iy, iz, seed) / (255.0);
return noise * 2.0 - 1.0;
}
internal static Double InternalValueNoise(
Double x, Double y, Double z, Double w,
Int32 ix, Int32 iy, Int32 iz, Int32 iw,
Int32 seed)
{
var noise = Noise.HashCoordinates(ix, iy, iz, iw, seed) / 255.0;
return noise * 2.0 - 1.0;
}
internal static Double InternalValueNoise(
Double x, Double y, Double z, Double w, Double u, Double v,
Int32 ix, Int32 iy, Int32 iz, Int32 iw, Int32 iu, Int32 iv,
Int32 seed)
{
var noise = Noise.HashCoordinates(ix, iy, iz, iw, iu, iv, seed) / 255.0;
return noise * 2.0 - 1.0;
}
internal static Double InternalGradientNoise(
Double x, Double y,
Int32 ix, Int32 iy,
Int32 seed)
{
var hash = Noise.HashCoordinates(ix, iy, seed);
var dx = x - ix;
var dy = y - iy;
return (dx * NoiseLookupTable.Gradient2D[hash, 0] +
dy * NoiseLookupTable.Gradient2D[hash, 1]);
}
internal static Double InternalGradientNoise(
Double x, Double y, Double z,
Int32 ix, Int32 iy, Int32 iz,
Int32 seed)
{
var hash = Noise.HashCoordinates(ix, iy, iz, seed);
var dx = x - ix;
var dy = y - iy;
var dz = z - iz;
return (dx * NoiseLookupTable.Gradient3D[hash, 0] +
dy * NoiseLookupTable.Gradient3D[hash, 1] +
dz * NoiseLookupTable.Gradient3D[hash, 2]);
}
internal static Double InternalGradientNoise(
Double x, Double y, Double z, Double w,
Int32 ix, Int32 iy, Int32 iz, Int32 iw,
Int32 seed)
{
var hash = Noise.HashCoordinates(ix, iy, iz, iw, seed);
var dx = x - ix;
var dy = y - iy;
var dz = z - iz;
var dw = w - iw;
return (dx * NoiseLookupTable.Gradient4D[hash, 0] +
dy * NoiseLookupTable.Gradient4D[hash, 1] +
dz * NoiseLookupTable.Gradient4D[hash, 2] +
dw * NoiseLookupTable.Gradient4D[hash, 3]);
}
internal static Double InternalGradientNoise(
Double x, Double y, Double z, Double w, Double u, Double v,
Int32 ix, Int32 iy, Int32 iz, Int32 iw, Int32 iu, Int32 iv,
Int32 seed)
{
var hash = Noise.HashCoordinates(ix, iy, iz, iw, iu, iv, seed);
var dx = x - ix;
var dy = y - iy;
var dz = z - iz;
var dw = w - iw;
var du = u - iu;
var dv = v - iv;
return (dx * NoiseLookupTable.Gradient6D[hash, 0] +
dy * NoiseLookupTable.Gradient6D[hash, 1] +
dz * NoiseLookupTable.Gradient6D[hash, 2] +
dw * NoiseLookupTable.Gradient6D[hash, 3] +
du * NoiseLookupTable.Gradient6D[hash, 4] +
dv * NoiseLookupTable.Gradient6D[hash, 5]);
}
internal static Double interpolate_X_2(
Double x, Double y, Double xs,
Int32 x0, Int32 x1, Int32 iy,
Int32 seed, WorkerNoise2 noisefunc)
{
var v1 = noisefunc(x, y, x0, iy, seed);
var v2 = noisefunc(x, y, x1, iy, seed);
return Lerp(xs, v1, v2);
}
internal static Double interpolate_XY_2(
Double x, Double y, Double xs, Double ys,
Int32 x0, Int32 x1, Int32 y0, Int32 y1,
Int32 seed, WorkerNoise2 noisefunc)
{
var v1 = interpolate_X_2(x, y, xs, x0, x1, y0, seed, noisefunc);
var v2 = interpolate_X_2(x, y, xs, x0, x1, y1, seed, noisefunc);
return Lerp(ys, v1, v2);
}
internal static Double interpolate_X_3(
Double x, Double y, Double z, Double xs,
Int32 x0, Int32 x1, Int32 iy, Int32 iz,
Int32 seed, WorkerNoise3 noisefunc)
{
var v1 = noisefunc(x, y, z, x0, iy, iz, seed);
var v2 = noisefunc(x, y, z, x1, iy, iz, seed);
return Lerp(xs, v1, v2);
}
internal static Double interpolate_XY_3(
Double x, Double y, Double z, Double xs, Double ys,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 iz,
Int32 seed, WorkerNoise3 noisefunc)
{
var v1 = interpolate_X_3(x, y, z, xs, x0, x1, y0, iz, seed, noisefunc);
var v2 = interpolate_X_3(x, y, z, xs, x0, x1, y1, iz, seed, noisefunc);
return Lerp(ys, v1, v2);
}
internal static Double interpolate_XYZ_3(
Double x, Double y, Double z, Double xs, Double ys, Double zs,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 z0, Int32 z1,
Int32 seed, WorkerNoise3 noisefunc)
{
var v1 = interpolate_XY_3(x, y, z, xs, ys, x0, x1, y0, y1, z0, seed, noisefunc);
var v2 = interpolate_XY_3(x, y, z, xs, ys, x0, x1, y0, y1, z1, seed, noisefunc);
return Lerp(zs, v1, v2);
}
internal static Double interpolate_X_4(
Double x, Double y, Double z, Double w, Double xs,
Int32 x0, Int32 x1, Int32 iy, Int32 iz, Int32 iw,
Int32 seed, WorkerNoise4 noisefunc)
{
var v1 = noisefunc(x, y, z, w, x0, iy, iz, iw, seed);
var v2 = noisefunc(x, y, z, w, x1, iy, iz, iw, seed);
return Lerp(xs, v1, v2);
}
internal static Double interpolate_XY_4(
Double x, Double y, Double z, Double w, Double xs, Double ys,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 iz, Int32 iw,
Int32 seed, WorkerNoise4 noisefunc)
{
var v1 = interpolate_X_4(x, y, z, w, xs, x0, x1, y0, iz, iw, seed, noisefunc);
var v2 = interpolate_X_4(x, y, z, w, xs, x0, x1, y1, iz, iw, seed, noisefunc);
return Lerp(ys, v1, v2);
}
internal static Double interpolate_XYZ_4(
Double x, Double y, Double z, Double w, Double xs, Double ys, Double zs,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 z0, Int32 z1, Int32 iw,
Int32 seed, WorkerNoise4 noisefunc)
{
var v1 = interpolate_XY_4(x, y, z, w, xs, ys, x0, x1, y0, y1, z0, iw, seed, noisefunc);
var v2 = interpolate_XY_4(x, y, z, w, xs, ys, x0, x1, y0, y1, z1, iw, seed, noisefunc);
return Lerp(zs, v1, v2);
}
internal static Double interpolate_XYZW_4(
Double x, Double y, Double z, Double w, Double xs, Double ys, Double zs, Double ws,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 z0, Int32 z1, Int32 w0, Int32 w1,
Int32 seed, WorkerNoise4 noisefunc)
{
var v1 = interpolate_XYZ_4(x, y, z, w, xs, ys, zs, x0, x1, y0, y1, z0, z1, w0, seed, noisefunc);
var v2 = interpolate_XYZ_4(x, y, z, w, xs, ys, zs, x0, x1, y0, y1, z0, z1, w1, seed, noisefunc);
return Lerp(ws, v1, v2);
}
internal static Double interpolate_X_6(
Double x, Double y, Double z, Double w, Double u, Double v, Double xs,
Int32 x0, Int32 x1, Int32 iy, Int32 iz, Int32 iw, Int32 iu, Int32 iv,
Int32 seed, WorkerNoise6 noisefunc)
{
var v1 = noisefunc(x, y, z, w, u, v, x0, iy, iz, iw, iu, iv, seed);
var v2 = noisefunc(x, y, z, w, u, v, x1, iy, iz, iw, iu, iv, seed);
return Lerp(xs, v1, v2);
}
internal static Double interpolate_XY_6(
Double x, Double y, Double z, Double w, Double u, Double v,
Double xs, Double ys,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 iz, Int32 iw, Int32 iu, Int32 iv,
Int32 seed, WorkerNoise6 noisefunc)
{
var v1 = interpolate_X_6(x, y, z, w, u, v, xs, x0, x1, y0, iz, iw, iu, iv, seed, noisefunc);
var v2 = interpolate_X_6(x, y, z, w, u, v, xs, x0, x1, y1, iz, iw, iu, iv, seed, noisefunc);
return Lerp(ys, v1, v2);
}
internal static Double interpolate_XYZ_6(
Double x, Double y, Double z, Double w, Double u, Double v,
Double xs, Double ys, Double zs,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 z0, Int32 z1, Int32 iw, Int32 iu, Int32 iv,
Int32 seed, WorkerNoise6 noisefunc)
{
var v1 = interpolate_XY_6(x, y, z, w, u, v, xs, ys, x0, x1, y0, y1, z0, iw, iu, iv, seed, noisefunc);
var v2 = interpolate_XY_6(x, y, z, w, u, v, xs, ys, x0, x1, y0, y1, z1, iw, iu, iv, seed, noisefunc);
return Lerp(zs, v1, v2);
}
internal static Double interpolate_XYZW_6(
Double x, Double y, Double z, Double w, Double u, Double v,
Double xs, Double ys, Double zs, Double ws,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 z0, Int32 z1, Int32 w0, Int32 w1, Int32 iu, Int32 iv,
Int32 seed, WorkerNoise6 noisefunc)
{
var v1 = interpolate_XYZ_6(x, y, z, w, u, v, xs, ys, zs, x0, x1, y0, y1, z0, z1, w0, iu, iv, seed, noisefunc);
var v2 = interpolate_XYZ_6(x, y, z, w, u, v, xs, ys, zs, x0, x1, y0, y1, z0, z1, w1, iu, iv, seed, noisefunc);
return Lerp(ws, v1, v2);
}
internal static Double interpolate_XYZWU_6(
Double x, Double y, Double z, Double w, Double u, Double v,
Double xs, Double ys, Double zs, Double ws, Double us,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 z0, Int32 z1, Int32 w0, Int32 w1, Int32 u0, Int32 u1, Int32 iv,
Int32 seed, WorkerNoise6 noisefunc)
{
var v1 = interpolate_XYZW_6(x, y, z, w, u, v, xs, ys, zs, ws, x0, x1, y0, y1, z0, z1, w0, w1, u0, iv, seed, noisefunc);
var v2 = interpolate_XYZW_6(x, y, z, w, u, v, xs, ys, zs, ws, x0, x1, y0, y1, z0, z1, w0, w1, u1, iv, seed, noisefunc);
return Lerp(us, v1, v2);
}
internal static Double interpolate_XYZWUV_6(
Double x, Double y, Double z, Double w, Double u, Double v,
Double xs, Double ys, Double zs, Double ws, Double us, Double vs,
Int32 x0, Int32 x1, Int32 y0, Int32 y1, Int32 z0, Int32 z1, Int32 w0, Int32 w1, Int32 u0, Int32 u1, Int32 v0, Int32 v1,
Int32 seed, WorkerNoise6 noisefunc)
{
var val1 = interpolate_XYZWU_6(x, y, z, w, u, v, xs, ys, zs, ws, us, x0, x1, y0, y1, z0, z1, w0, w1, u0, u1, v0, seed, noisefunc);
var val2 = interpolate_XYZWU_6(x, y, z, w, u, v, xs, ys, zs, ws, us, x0, x1, y0, y1, z0, z1, w0, w1, u0, u1, v1, seed, noisefunc);
return Lerp(vs, val1, val2);
}
// The usable noise functions
public static Double ValueNoise(Double x, Double y, Int32 seed, InterpolationDelegate interp)
{
var x0 = FastFloor(x);
var y0 = FastFloor(y);
var x1 = x0 + 1;
var y1 = y0 + 1;
var xs = interp((x - x0));
var ys = interp((y - y0));
return interpolate_XY_2(x, y, xs, ys, x0, x1, y0, y1, seed, InternalValueNoise);
}
public static Double ValueNoise(Double x, Double y, Double z, Int32 seed, InterpolationDelegate interp)
{
var x0 = FastFloor(x);
var y0 = FastFloor(y);
var z0 = FastFloor(z);
var x1 = x0 + 1;
var y1 = y0 + 1;
var z1 = z0 + 1;
var xs = interp((x - x0));
var ys = interp((y - y0));
var zs = interp((z - z0));
return interpolate_XYZ_3(x, y, z, xs, ys, zs, x0, x1, y0, y1, z0, z1, seed, InternalValueNoise);
}
public static Double ValueNoise(Double x, Double y, Double z, Double w, Int32 seed, InterpolationDelegate interp)
{
var x0 = FastFloor(x);
var y0 = FastFloor(y);
var z0 = FastFloor(z);
var w0 = FastFloor(w);
var x1 = x0 + 1;
var y1 = y0 + 1;
var z1 = z0 + 1;
var w1 = w0 + 1;
var xs = interp((x - x0));
var ys = interp((y - y0));
var zs = interp((z - z0));
var ws = interp((w - w0));
return interpolate_XYZW_4(x, y, z, w, xs, ys, zs, ws, x0, x1, y0, y1, z0, z1, w0, w1, seed, InternalValueNoise);
}
public static Double ValueNoise(Double x, Double y, Double z, Double w, Double u, Double v, Int32 seed, InterpolationDelegate interp)
{
var x0 = FastFloor(x);
var y0 = FastFloor(y);
var z0 = FastFloor(z);
var w0 = FastFloor(w);
var u0 = FastFloor(u);
var v0 = FastFloor(v);
var x1 = x0 + 1;
var y1 = y0 + 1;
var z1 = z0 + 1;
var w1 = w0 + 1;
var u1 = u0 + 1;
var v1 = v0 + 1;
var xs = interp((x - x0));
var ys = interp((y - y0));
var zs = interp((z - z0));
var ws = interp((w - w0));
var us = interp((u - u0));
var vs = interp((v - v0));
return interpolate_XYZWUV_6(x, y, z, w, u, v, xs, ys, zs, ws, us, vs, x0, x1, y0, y1, z0, z1, w0, w1, u0, u1, v0, v1, seed, InternalValueNoise);
}
public static Double GradientNoise(Double x, Double y, Int32 seed, InterpolationDelegate interp)
{
var x0 = FastFloor(x);
var y0 = FastFloor(y);
var x1 = x0 + 1;
var y1 = y0 + 1;
var xs = interp((x - x0));
var ys = interp((y - y0));
return interpolate_XY_2(x, y, xs, ys, x0, x1, y0, y1, seed, InternalGradientNoise);
}
public static Double GradientNoise(Double x, Double y, Double z, Int32 seed, InterpolationDelegate interp)
{
var x0 = FastFloor(x);
var y0 = FastFloor(y);
var z0 = FastFloor(z);
var x1 = x0 + 1;
var y1 = y0 + 1;
var z1 = z0 + 1;
var xs = interp((x - x0));
var ys = interp((y - y0));
var zs = interp((z - z0));
return interpolate_XYZ_3(x, y, z, xs, ys, zs, x0, x1, y0, y1, z0, z1, seed, InternalGradientNoise);
}
public static Double GradientNoise(Double x, Double y, Double z, Double w, Int32 seed, InterpolationDelegate interp)
{
var x0 = FastFloor(x);
var y0 = FastFloor(y);
var z0 = FastFloor(z);
var w0 = FastFloor(w);
var x1 = x0 + 1;
var y1 = y0 + 1;
var z1 = z0 + 1;
var w1 = w0 + 1;
var xs = interp((x - x0));
var ys = interp((y - y0));
var zs = interp((z - z0));
var ws = interp((w - w0));
return interpolate_XYZW_4(x, y, z, w, xs, ys, zs, ws, x0, x1, y0, y1, z0, z1, w0, w1, seed, InternalGradientNoise);
}
public static Double GradientNoise(Double x, Double y, Double z, Double w, Double u, Double v, Int32 seed, InterpolationDelegate interp)
{
var x0 = FastFloor(x);
var y0 = FastFloor(y);
var z0 = FastFloor(z);
var w0 = FastFloor(w);
var u0 = FastFloor(u);
var v0 = FastFloor(v);
var x1 = x0 + 1;
var y1 = y0 + 1;
var z1 = z0 + 1;
var w1 = w0 + 1;
var u1 = u0 + 1;
var v1 = v0 + 1;
var xs = interp((x - x0));
var ys = interp((y - y0));
var zs = interp((z - z0));
var ws = interp((w - w0));
var us = interp((u - u0));
var vs = interp((v - v0));
return interpolate_XYZWUV_6(x, y, z, w, u, v, xs, ys, zs, ws, us, vs, x0, x1, y0, y1, z0, z1, w0, w1, u0, u1, v0, v1, seed, InternalGradientNoise);
}
public static Double GradientValueNoise(Double x, Double y, Int32 seed, InterpolationDelegate interp)
{
return ValueNoise(x, y, seed, interp) + GradientNoise(x, y, seed, interp);
}
public static Double GradientValueNoise(Double x, Double y, Double z, Int32 seed, InterpolationDelegate interp)
{
return ValueNoise(x, y, z, seed, interp) + GradientNoise(x, y, z, seed, interp);
}
public static Double GradientValueNoise(Double x, Double y, Double z, Double w, Int32 seed, InterpolationDelegate interp)
{
return ValueNoise(x, y, z, w, seed, interp) + GradientNoise(x, y, z, w, seed, interp);
}
public static Double GradientValueNoise(Double x, Double y, Double z, Double w, Double u, Double v, Int32 seed, InterpolationDelegate interp)
{
return ValueNoise(x, y, z, w, u, v, seed, interp) + GradientNoise(x, y, z, w, u, v, seed, interp);
}
public static Double WhiteNoise(Double x, Double y, Int32 seed, InterpolationDelegate interp)
{
return NoiseLookupTable.WhiteNoise[(Byte)HashCoordinates(x, y, seed)];
}
public static Double WhiteNoise(Double x, Double y, Double z, Int32 seed, InterpolationDelegate interp)
{
return NoiseLookupTable.WhiteNoise[(Byte)HashCoordinates(x, y, z, seed)];
}
public static Double WhiteNoise(Double x, Double y, Double z, Double w, Int32 seed, InterpolationDelegate interp)
{
return NoiseLookupTable.WhiteNoise[(Byte)HashCoordinates(x, y, z, w, seed)];
}
public static Double WhiteNoise(Double x, Double y, Double z, Double w, Double u, Double v, Int32 seed, InterpolationDelegate interp)
{
return NoiseLookupTable.WhiteNoise[(Byte)HashCoordinates(x, y, z, w, u, v, seed)];
}
public static void CellularFunction(Double x, Double y, Int32 seed, Double[] f, Double[] disp)
{
var xInt = FastFloor(x);
var yInt = FastFloor(y);
for (var c = 0; c < 4; ++c)
{
f[c] = 99999.0;
disp[c] = 0.0;
}
for (var ycur = yInt - 3; ycur <= yInt + 3; ++ycur)
{
for (var xcur = xInt - 3; xcur <= xInt + 3; ++xcur)
{
var xpos = xcur + InternalValueNoise(x, y, xcur, ycur, seed);
var ypos = ycur + InternalValueNoise(x, y, xcur, ycur, seed + 1);
var xdist = xpos - x;
var ydist = ypos - y;
var dist = (xdist * xdist + ydist * ydist);
var xval = FastFloor(xpos);
var yval = FastFloor(ypos);
var dsp = InternalValueNoise(x, y, xval, yval, seed + 3);
AddDistance(f, disp, dist, dsp);
}
}
}
public static void CellularFunction(Double x, Double y, Double z, Int32 seed, Double[] f, Double[] disp)
{
var xInt = FastFloor(x);
var yInt = FastFloor(y);
var zInt = FastFloor(z);
for (var c = 0; c < 4; ++c)
{
f[c] = 99999.0;
disp[c] = 0.0;
}
for (var zcur = zInt - 2; zcur <= zInt + 2; ++zcur)
{
for (var ycur = yInt - 2; ycur <= yInt + 2; ++ycur)
{
for (var xcur = xInt - 2; xcur <= xInt + 2; ++xcur)
{
var xpos = xcur + InternalValueNoise(x, y, z, xcur, ycur, zcur, seed);
var ypos = ycur + InternalValueNoise(x, y, z, xcur, ycur, zcur, seed + 1);
var zpos = zcur + InternalValueNoise(x, y, z, xcur, ycur, zcur, seed + 2);
var xdist = xpos - x;
var ydist = ypos - y;
var zdist = zpos - z;
var dist = (xdist * xdist + ydist * ydist + zdist * zdist);
var xval = FastFloor(xpos);
var yval = FastFloor(ypos);
var zval = FastFloor(zpos);
var dsp = InternalValueNoise(x, y, z, xval, yval, zval, seed + 3);
AddDistance(f, disp, dist, dsp);
}
}
}
}
public static void CellularFunction(Double x, Double y, Double z, Double w, Int32 seed, Double[] f, Double[] disp)
{
var xInt = FastFloor(x);
var yInt = FastFloor(y);
var zInt = FastFloor(z);
var wInt = FastFloor(w);
for (var c = 0; c < 4; ++c)
{
f[c] = 99999.0;
disp[c] = 0.0;
}
for (var wcur = wInt - 2; wcur <= wInt + 2; ++wcur)
{
for (var zcur = zInt - 2; zcur <= zInt + 2; ++zcur)
{
for (var ycur = yInt - 2; ycur <= yInt + 2; ++ycur)
{
for (var xcur = xInt - 2; xcur <= xInt + 2; ++xcur)
{
var xpos = xcur + InternalValueNoise(x, y, z, w, xcur, ycur, zcur, wcur, seed);
var ypos = ycur + InternalValueNoise(x, y, z, w, xcur, ycur, zcur, wcur, seed + 1);
var zpos = zcur + InternalValueNoise(x, y, z, w, xcur, ycur, zcur, wcur, seed + 2);
var wpos = wcur + InternalValueNoise(x, y, z, w, xcur, ycur, zcur, wcur, seed + 3);
var xdist = xpos - x;
var ydist = ypos - y;
var zdist = zpos - z;
var wdist = wpos - w;
var dist = (xdist * xdist + ydist * ydist + zdist * zdist + wdist * wdist);
var xval = FastFloor(xpos);
var yval = FastFloor(ypos);
var zval = FastFloor(zpos);
var wval = FastFloor(wpos);
var dsp = InternalValueNoise(x, y, z, w, xval, yval, zval, wval, seed + 3);
AddDistance(f, disp, dist, dsp);
}
}
}
}
}
public static void CellularFunction(Double x, Double y, Double z, Double w, Double u, Double v, Int32 seed, Double[] f, Double[] disp)
{
var xInt = FastFloor(x);
var yInt = FastFloor(y);
var zInt = FastFloor(z);
var wInt = FastFloor(w);
var uInt = FastFloor(u);
var vInt = FastFloor(v);
for (var c = 0; c < 4; ++c)
{
f[c] = 99999.0;
disp[c] = 0.0;
}
for (var vcur = vInt - 1; vcur <= vInt + 1; ++vcur)
{
for (var ucur = uInt - 1; ucur <= uInt + 1; ++ucur)
{
for (var wcur = wInt - 2; wcur <= wInt + 2; ++wcur)
{
for (var zcur = zInt - 2; zcur <= zInt + 2; ++zcur)
{
for (var ycur = yInt - 2; ycur <= yInt + 2; ++ycur)
{
for (var xcur = xInt - 2; xcur <= xInt + 2; ++xcur)
{
var xpos = xcur + InternalValueNoise(x, y, z, w, u, v, xcur, ycur, zcur, wcur, ucur, vcur, seed);
var ypos = ycur + InternalValueNoise(x, y, z, w, u, v, xcur, ycur, zcur, wcur, ucur, vcur, seed + 1);
var zpos = zcur + InternalValueNoise(x, y, z, w, u, v, xcur, ycur, zcur, wcur, ucur, vcur, seed + 2);
var wpos = wcur + InternalValueNoise(x, y, z, w, u, v, xcur, ycur, zcur, wcur, ucur, vcur, seed + 3);
var upos = ucur + InternalValueNoise(x, y, z, w, u, v, xcur, ycur, zcur, wcur, ucur, vcur, seed + 4);
var vpos = vcur + InternalValueNoise(x, y, z, w, u, v, xcur, ycur, zcur, wcur, ucur, vcur, seed + 5);
var xdist = xpos - x;
var ydist = ypos - y;
var zdist = zpos - z;
var wdist = wpos - w;
var udist = upos - u;
var vdist = vpos - v;
var dist = (xdist * xdist + ydist * ydist + zdist * zdist + wdist * wdist + udist * udist + vdist * vdist);
var xval = FastFloor(xpos);
var yval = FastFloor(ypos);
var zval = FastFloor(zpos);
var wval = FastFloor(wpos);
var uval = FastFloor(upos);
var vval = FastFloor(vpos);
var dsp = InternalValueNoise(x, y, z, w, u, v, xval, yval, zval, wval, uval, vval, seed + 6);
AddDistance(f, disp, dist, dsp);
}
}
}
}
}
}
}
private const Double F2 = 0.36602540378443864676372317075294;
private const Double G2 = 0.21132486540518711774542560974902;
private const Double F3 = 1.0 / 3.0;
private const Double G3 = 1.0 / 6.0;
private static readonly Int32[,] Simplex = {
{0, 1, 2, 3}, {0, 1, 3, 2}, {0, 0, 0, 0}, {0, 2, 3, 1},
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 3, 0},
{0, 2, 1, 3}, {0, 0, 0, 0}, {0, 3, 1, 2}, {0, 3, 2, 1},
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 3, 2, 0},
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
{1, 2, 0, 3}, {0, 0, 0, 0}, {1, 3, 0, 2}, {0, 0, 0, 0},
{0, 0, 0, 0}, {0, 0, 0, 0}, {2, 3, 0, 1}, {2, 3, 1, 0},
{1, 0, 2, 3}, {1, 0, 3, 2}, {0, 0, 0, 0}, {0, 0, 0, 0},
{0, 0, 0, 0}, {2, 0, 3, 1}, {0, 0, 0, 0}, {2, 1, 3, 0},
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
{2, 0, 1, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
{3, 0, 1, 2}, {3, 0, 2, 1}, {0, 0, 0, 0}, {3, 1, 2, 0},
{2, 1, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
{3, 1, 0, 2}, {0, 0, 0, 0}, {3, 2, 0, 1}, {3, 2, 1, 0}
};
internal struct VectorOrdering4
{
internal Double Coordinates;
internal Int32 X;
internal Int32 Y;
internal Int32 Z;
internal Int32 W;
internal VectorOrdering4(Double v, Int32 x, Int32 y, Int32 z, Int32 w)
{
this.Coordinates = v;
this.X = x;
this.Y = y;
this.Z = z;
this.W = w;
}
}
internal static Int32 VectorOrdering4Compare(VectorOrdering4 v1, VectorOrdering4 v2)
{
return v1.Coordinates.CompareTo(v2.Coordinates);
}
internal struct VectorOrdering
{
internal Double Value;
internal Int32 Axis;
internal VectorOrdering(Double v, Int32 a)
{
Value = v;
Axis = a;
}
}
internal static Int32 VectorOrderingCompare(VectorOrdering v1, VectorOrdering v2)
{
return v1.Value.CompareTo(v2.Value);
}
internal static void SortBy4(Double[] l1, Int32[] l2)
{
var a = new VectorOrdering[4];
for (var c = 0; c < 4; c += 1)
{
a[c].Value = l1[c];
a[c].Axis = l2[c];
}
Array.Sort(a, VectorOrderingCompare);
for (var c = 0; c < 4; c += 1)
{
l2[c] = a[c].Axis;
}
}
internal static void SortBy6(Double[] l1, Int32[] l2)
{
var a = new VectorOrdering[6];
for (var c = 0; c < 6; c += 1)
{
a[c].Value = l1[c];
a[c].Axis = l2[c];
}
Array.Sort(a, VectorOrderingCompare);
for (var c = 0; c < 6; c += 1)
{
l2[c] = a[c].Axis;
}
}
public static Double SimplexNoise(Double x, Double y, Int32 seed, InterpolationDelegate interp)
{
Double s = (x + y)*F2;
int i = FastFloor(x + s);
int j = FastFloor(y + s);
Double t = (i + j)*G2;
Double X0 = i - t;
Double Y0 = j - t;
Double x0 = x - X0;
Double y0 = y - Y0;
int i1, j1;
if (x0 > y0)
{
i1 = 1;
j1 = 0;
}
else
{
i1 = 0;
j1 = 1;
}
Double x1 = x0 - (Double) i1 + G2;
Double y1 = y0 - (Double) j1 + G2;
Double x2 = x0 - 1.0 + 2.0*G2;
Double y2 = y0 - 1.0 + 2.0*G2;
// Hash the triangle coordinates to index the gradient table
uint h0 = HashCoordinates(i, j, (int) seed);
uint h1 = HashCoordinates(i + i1, j + j1, (int) seed);
uint h2 = HashCoordinates(i + 1, j + 1, (int) seed);
// Now, index the tables
Double[] g0 = {NoiseLookupTable.Gradient2D[h0, 0], NoiseLookupTable.Gradient2D[h0, 1]};
Double[] g1 = {NoiseLookupTable.Gradient2D[h1, 0], NoiseLookupTable.Gradient2D[h1, 1]};
Double[] g2 = {NoiseLookupTable.Gradient2D[h2, 0], NoiseLookupTable.Gradient2D[h2, 1]};
Double n0, n1, n2;
// Calculate the contributions from the 3 corners
Double t0 = 0.5 - x0*x0 - y0*y0;
if (t0 < 0) n0 = 0;
else
{
t0 *= t0;
n0 = t0*t0*ArrayDot(g0, x0, y0);
}
Double t1 = 0.5 - x1*x1 - y1*y1;
if (t1 < 0) n1 = 0;
else
{
t1 *= t1;
n1 = t1*t1*ArrayDot(g1, x1, y1);
}
Double t2 = 0.5 - x2*x2 - y2*y2;
if (t2 < 0) n2 = 0;
else