-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
FastNoiseLite.h
2586 lines (2196 loc) · 105 KB
/
FastNoiseLite.h
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
// MIT License
//
// Copyright(c) 2023 Jordan Peck ([email protected])
// Copyright(c) 2023 Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// .'',;:cldxkO00KKXXNNWWWNNXKOkxdollcc::::::;:::ccllloooolllllllllooollc:,'... ...........',;cldxkO000Okxdlc::;;;,,;;;::cclllllll
// ..',;:ldxO0KXXNNNNNNNNXXK0kxdolcc::::::;;;,,,,,,;;;;;;;;;;:::cclllllc:;'.... ...........',;:ldxO0KXXXK0Okxdolc::;;;;::cllodddddo
// ...',:loxO0KXNNNNNXXKK0Okxdolc::;::::::::;;;,,'''''.....''',;:clllllc:;,'............''''''''',;:loxO0KXNNNNNXK0Okxdollccccllodxxxxxxd
// ....';:ldkO0KXXXKK00Okxdolcc:;;;;;::cclllcc:;;,''..... ....',;clooddolcc:;;;;,,;;;;;::::;;;;;;:cloxk0KXNWWWWWWNXKK0Okxddoooddxxkkkkkxx
// .....';:ldxkOOOOOkxxdolcc:;;;,,,;;:cllooooolcc:;'... ..,:codxkkkxddooollloooooooollcc:::::clodkO0KXNWWWWWWNNXK00Okxxxxxxxxkkkkxxx
// . ....';:cloddddo___________,,,,;;:clooddddoolc:,... ..,:ldx__00OOOkkk___kkkkkkxxdollc::::cclodkO0KXXNNNNNNXXK0OOkxxxxxxxxxxxxddd
// .......',;:cccc:| |,,,;;:cclooddddoll:;'.. ..';cox| \KKK000| |KK00OOkxdocc___;::clldxxkO0KKKKK00Okkxdddddddddddddddoo
// .......'',,,,,''| ________|',,;;::cclloooooolc:;'......___:ldk| \KK000| |XKKK0Okxolc| |;;::cclodxxkkkkxxdoolllcclllooodddooooo
// ''......''''....| | ....'',,,,;;;::cclloooollc:;,''.'| |oxk| \OOO0| |KKK00Oxdoll|___|;;;;;::ccllllllcc::;;,,;;;:cclloooooooo
// ;;,''.......... | |_____',,;;;____:___cllo________.___| |___| \xkk| |KK_______ool___:::;________;;;_______...'',;;:ccclllloo
// c:;,''......... | |:::/ ' |lo/ | | \dx| |0/ \d| |cc/ |'/ \......',,;;:ccllo
// ol:;,'..........| _____|ll/ __ |o/ ______|____ ___| | \o| |/ ___ \| |o/ ______|/ ___ \ .......'',;:clo
// dlc;,...........| |::clooo| / | |x\___ \KXKKK0| |dol| |\ \| | | | | |d\___ \..| | / / ....',:cl
// xoc;'... .....'| |llodddd| \__| |_____\ \KKK0O| |lc:| |'\ | |___| | |_____\ \.| |_/___/... ...',;:c
// dlc;'... ....',;| |oddddddo\ | |Okkx| |::;| |..\ |\ /| | | \ |... ....',;:c
// ol:,'.......',:c|___|xxxddollc\_____,___|_________/ddoll|___|,,,|___|...\_____|:\ ______/l|___|_________/...\________|'........',;::cc
// c:;'.......';:codxxkkkkxxolc::;::clodxkOO0OOkkxdollc::;;,,''''',,,,''''''''''',,'''''',;:loxkkOOkxol:;,'''',,;:ccllcc:;,'''''',;::ccll
// ;,'.......',:codxkOO0OOkxdlc:;,,;;:cldxxkkxxdolc:;;,,''.....'',;;:::;;,,,'''''........,;cldkO0KK0Okdoc::;;::cloodddoolc:;;;;;::ccllooo
// .........',;:lodxOO0000Okdoc:,,',,;:clloddoolc:;,''.......'',;:clooollc:;;,,''.......',:ldkOKXNNXX0Oxdolllloddxxxxxxdolccccccllooodddd
// . .....';:cldxkO0000Okxol:;,''',,;::cccc:;,,'.......'',;:cldxxkkxxdolc:;;,'.......';coxOKXNWWWNXKOkxddddxxkkkkkkxdoollllooddxxxxkkk
// ....',;:codxkO000OOxdoc:;,''',,,;;;;,''.......',,;:clodkO00000Okxolc::;,,''..',;:ldxOKXNWWWNNK0OkkkkkkkkkkkxxddooooodxxkOOOOO000
// ....',;;clodxkkOOOkkdolc:;,,,,,,,,'..........,;:clodxkO0KKXKK0Okxdolcc::;;,,,;;:codkO0XXNNNNXKK0OOOOOkkkkxxdoollloodxkO0KKKXXXXX
//
// VERSION: 1.1.1
// https://github.com/Auburn/FastNoiseLite
#ifndef FASTNOISELITE_H
#define FASTNOISELITE_H
#include <cmath>
class FastNoiseLite
{
public:
enum NoiseType
{
NoiseType_OpenSimplex2,
NoiseType_OpenSimplex2S,
NoiseType_Cellular,
NoiseType_Perlin,
NoiseType_ValueCubic,
NoiseType_Value
};
enum RotationType3D
{
RotationType3D_None,
RotationType3D_ImproveXYPlanes,
RotationType3D_ImproveXZPlanes
};
enum FractalType
{
FractalType_None,
FractalType_FBm,
FractalType_Ridged,
FractalType_PingPong,
FractalType_DomainWarpProgressive,
FractalType_DomainWarpIndependent
};
enum CellularDistanceFunction
{
CellularDistanceFunction_Euclidean,
CellularDistanceFunction_EuclideanSq,
CellularDistanceFunction_Manhattan,
CellularDistanceFunction_Hybrid
};
enum CellularReturnType
{
CellularReturnType_CellValue,
CellularReturnType_Distance,
CellularReturnType_Distance2,
CellularReturnType_Distance2Add,
CellularReturnType_Distance2Sub,
CellularReturnType_Distance2Mul,
CellularReturnType_Distance2Div
};
enum DomainWarpType
{
DomainWarpType_OpenSimplex2,
DomainWarpType_OpenSimplex2Reduced,
DomainWarpType_BasicGrid
};
/// <summary>
/// Create new FastNoise object with optional seed
/// </summary>
FastNoiseLite(int seed = 1337)
{
mSeed = seed;
mFrequency = 0.01f;
mNoiseType = NoiseType_OpenSimplex2;
mRotationType3D = RotationType3D_None;
mTransformType3D = TransformType3D_DefaultOpenSimplex2;
mFractalType = FractalType_None;
mOctaves = 3;
mLacunarity = 2.0f;
mGain = 0.5f;
mWeightedStrength = 0.0f;
mPingPongStrength = 2.0f;
mFractalBounding = 1 / 1.75f;
mCellularDistanceFunction = CellularDistanceFunction_EuclideanSq;
mCellularReturnType = CellularReturnType_Distance;
mCellularJitterModifier = 1.0f;
mDomainWarpType = DomainWarpType_OpenSimplex2;
mWarpTransformType3D = TransformType3D_DefaultOpenSimplex2;
mDomainWarpAmp = 1.0f;
}
/// <summary>
/// Sets seed used for all noise types
/// </summary>
/// <remarks>
/// Default: 1337
/// </remarks>
void SetSeed(int seed) { mSeed = seed; }
/// <summary>
/// Sets frequency for all noise types
/// </summary>
/// <remarks>
/// Default: 0.01
/// </remarks>
void SetFrequency(float frequency) { mFrequency = frequency; }
/// <summary>
/// Sets noise algorithm used for GetNoise(...)
/// </summary>
/// <remarks>
/// Default: OpenSimplex2
/// </remarks>
void SetNoiseType(NoiseType noiseType)
{
mNoiseType = noiseType;
UpdateTransformType3D();
}
/// <summary>
/// Sets domain rotation type for 3D Noise and 3D DomainWarp.
/// Can aid in reducing directional artifacts when sampling a 2D plane in 3D
/// </summary>
/// <remarks>
/// Default: None
/// </remarks>
void SetRotationType3D(RotationType3D rotationType3D)
{
mRotationType3D = rotationType3D;
UpdateTransformType3D();
UpdateWarpTransformType3D();
}
/// <summary>
/// Sets method for combining octaves in all fractal noise types
/// </summary>
/// <remarks>
/// Default: None
/// Note: FractalType_DomainWarp... only affects DomainWarp(...)
/// </remarks>
void SetFractalType(FractalType fractalType) { mFractalType = fractalType; }
/// <summary>
/// Sets octave count for all fractal noise types
/// </summary>
/// <remarks>
/// Default: 3
/// </remarks>
void SetFractalOctaves(int octaves)
{
mOctaves = octaves;
CalculateFractalBounding();
}
/// <summary>
/// Sets octave lacunarity for all fractal noise types
/// </summary>
/// <remarks>
/// Default: 2.0
/// </remarks>
void SetFractalLacunarity(float lacunarity) { mLacunarity = lacunarity; }
/// <summary>
/// Sets octave gain for all fractal noise types
/// </summary>
/// <remarks>
/// Default: 0.5
/// </remarks>
void SetFractalGain(float gain)
{
mGain = gain;
CalculateFractalBounding();
}
/// <summary>
/// Sets octave weighting for all none DomainWarp fratal types
/// </summary>
/// <remarks>
/// Default: 0.0
/// Note: Keep between 0...1 to maintain -1...1 output bounding
/// </remarks>
void SetFractalWeightedStrength(float weightedStrength) { mWeightedStrength = weightedStrength; }
/// <summary>
/// Sets strength of the fractal ping pong effect
/// </summary>
/// <remarks>
/// Default: 2.0
/// </remarks>
void SetFractalPingPongStrength(float pingPongStrength) { mPingPongStrength = pingPongStrength; }
/// <summary>
/// Sets distance function used in cellular noise calculations
/// </summary>
/// <remarks>
/// Default: Distance
/// </remarks>
void SetCellularDistanceFunction(CellularDistanceFunction cellularDistanceFunction) { mCellularDistanceFunction = cellularDistanceFunction; }
/// <summary>
/// Sets return type from cellular noise calculations
/// </summary>
/// <remarks>
/// Default: EuclideanSq
/// </remarks>
void SetCellularReturnType(CellularReturnType cellularReturnType) { mCellularReturnType = cellularReturnType; }
/// <summary>
/// Sets the maximum distance a cellular point can move from it's grid position
/// </summary>
/// <remarks>
/// Default: 1.0
/// Note: Setting this higher than 1 will cause artifacts
/// </remarks>
void SetCellularJitter(float cellularJitter) { mCellularJitterModifier = cellularJitter; }
/// <summary>
/// Sets the warp algorithm when using DomainWarp(...)
/// </summary>
/// <remarks>
/// Default: OpenSimplex2
/// </remarks>
void SetDomainWarpType(DomainWarpType domainWarpType)
{
mDomainWarpType = domainWarpType;
UpdateWarpTransformType3D();
}
/// <summary>
/// Sets the maximum warp distance from original position when using DomainWarp(...)
/// </summary>
/// <remarks>
/// Default: 1.0
/// </remarks>
void SetDomainWarpAmp(float domainWarpAmp) { mDomainWarpAmp = domainWarpAmp; }
/// <summary>
/// 2D noise at given position using current settings
/// </summary>
/// <returns>
/// Noise output bounded between -1...1
/// </returns>
template <typename FNfloat>
float GetNoise(FNfloat x, FNfloat y) const
{
Arguments_must_be_floating_point_values<FNfloat>();
TransformNoiseCoordinate(x, y);
switch (mFractalType)
{
default:
return GenNoiseSingle(mSeed, x, y);
case FractalType_FBm:
return GenFractalFBm(x, y);
case FractalType_Ridged:
return GenFractalRidged(x, y);
case FractalType_PingPong:
return GenFractalPingPong(x, y);
}
}
/// <summary>
/// 3D noise at given position using current settings
/// </summary>
/// <returns>
/// Noise output bounded between -1...1
/// </returns>
template <typename FNfloat>
float GetNoise(FNfloat x, FNfloat y, FNfloat z) const
{
Arguments_must_be_floating_point_values<FNfloat>();
TransformNoiseCoordinate(x, y, z);
switch (mFractalType)
{
default:
return GenNoiseSingle(mSeed, x, y, z);
case FractalType_FBm:
return GenFractalFBm(x, y, z);
case FractalType_Ridged:
return GenFractalRidged(x, y, z);
case FractalType_PingPong:
return GenFractalPingPong(x, y, z);
}
}
/// <summary>
/// 2D warps the input position using current domain warp settings
/// </summary>
/// <example>
/// Example usage with GetNoise
/// <code>DomainWarp(x, y)
/// noise = GetNoise(x, y)</code>
/// </example>
template <typename FNfloat>
void DomainWarp(FNfloat& x, FNfloat& y) const
{
Arguments_must_be_floating_point_values<FNfloat>();
switch (mFractalType)
{
default:
DomainWarpSingle(x, y);
break;
case FractalType_DomainWarpProgressive:
DomainWarpFractalProgressive(x, y);
break;
case FractalType_DomainWarpIndependent:
DomainWarpFractalIndependent(x, y);
break;
}
}
/// <summary>
/// 3D warps the input position using current domain warp settings
/// </summary>
/// <example>
/// Example usage with GetNoise
/// <code>DomainWarp(x, y, z)
/// noise = GetNoise(x, y, z)</code>
/// </example>
template <typename FNfloat>
void DomainWarp(FNfloat& x, FNfloat& y, FNfloat& z) const
{
Arguments_must_be_floating_point_values<FNfloat>();
switch (mFractalType)
{
default:
DomainWarpSingle(x, y, z);
break;
case FractalType_DomainWarpProgressive:
DomainWarpFractalProgressive(x, y, z);
break;
case FractalType_DomainWarpIndependent:
DomainWarpFractalIndependent(x, y, z);
break;
}
}
private:
template <typename T>
struct Arguments_must_be_floating_point_values;
enum TransformType3D
{
TransformType3D_None,
TransformType3D_ImproveXYPlanes,
TransformType3D_ImproveXZPlanes,
TransformType3D_DefaultOpenSimplex2
};
int mSeed;
float mFrequency;
NoiseType mNoiseType;
RotationType3D mRotationType3D;
TransformType3D mTransformType3D;
FractalType mFractalType;
int mOctaves;
float mLacunarity;
float mGain;
float mWeightedStrength;
float mPingPongStrength;
float mFractalBounding;
CellularDistanceFunction mCellularDistanceFunction;
CellularReturnType mCellularReturnType;
float mCellularJitterModifier;
DomainWarpType mDomainWarpType;
TransformType3D mWarpTransformType3D;
float mDomainWarpAmp;
template <typename T>
struct Lookup
{
static const T Gradients2D[];
static const T Gradients3D[];
static const T RandVecs2D[];
static const T RandVecs3D[];
};
static float FastMin(float a, float b) { return a < b ? a : b; }
static float FastMax(float a, float b) { return a > b ? a : b; }
static float FastAbs(float f) { return f < 0 ? -f : f; }
static float FastSqrt(float f) { return sqrtf(f); }
template <typename FNfloat>
static int FastFloor(FNfloat f) { return f >= 0 ? (int)f : (int)f - 1; }
template <typename FNfloat>
static int FastRound(FNfloat f) { return f >= 0 ? (int)(f + 0.5f) : (int)(f - 0.5f); }
static float Lerp(float a, float b, float t) { return a + t * (b - a); }
static float InterpHermite(float t) { return t * t * (3 - 2 * t); }
static float InterpQuintic(float t) { return t * t * t * (t * (t * 6 - 15) + 10); }
static float CubicLerp(float a, float b, float c, float d, float t)
{
float p = (d - c) - (a - b);
return t * t * t * p + t * t * ((a - b) - p) + t * (c - a) + b;
}
static float PingPong(float t)
{
t -= (int)(t * 0.5f) * 2;
return t < 1 ? t : 2 - t;
}
void CalculateFractalBounding()
{
float gain = FastAbs(mGain);
float amp = gain;
float ampFractal = 1.0f;
for (int i = 1; i < mOctaves; i++)
{
ampFractal += amp;
amp *= gain;
}
mFractalBounding = 1 / ampFractal;
}
// Hashing
static const int PrimeX = 501125321;
static const int PrimeY = 1136930381;
static const int PrimeZ = 1720413743;
static int Hash(int seed, int xPrimed, int yPrimed)
{
int hash = seed ^ xPrimed ^ yPrimed;
hash *= 0x27d4eb2d;
return hash;
}
static int Hash(int seed, int xPrimed, int yPrimed, int zPrimed)
{
int hash = seed ^ xPrimed ^ yPrimed ^ zPrimed;
hash *= 0x27d4eb2d;
return hash;
}
static float ValCoord(int seed, int xPrimed, int yPrimed)
{
int hash = Hash(seed, xPrimed, yPrimed);
hash *= hash;
hash ^= hash << 19;
return hash * (1 / 2147483648.0f);
}
static float ValCoord(int seed, int xPrimed, int yPrimed, int zPrimed)
{
int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
hash *= hash;
hash ^= hash << 19;
return hash * (1 / 2147483648.0f);
}
float GradCoord(int seed, int xPrimed, int yPrimed, float xd, float yd) const
{
int hash = Hash(seed, xPrimed, yPrimed);
hash ^= hash >> 15;
hash &= 127 << 1;
float xg = Lookup<float>::Gradients2D[hash];
float yg = Lookup<float>::Gradients2D[hash | 1];
return xd * xg + yd * yg;
}
float GradCoord(int seed, int xPrimed, int yPrimed, int zPrimed, float xd, float yd, float zd) const
{
int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
hash ^= hash >> 15;
hash &= 63 << 2;
float xg = Lookup<float>::Gradients3D[hash];
float yg = Lookup<float>::Gradients3D[hash | 1];
float zg = Lookup<float>::Gradients3D[hash | 2];
return xd * xg + yd * yg + zd * zg;
}
void GradCoordOut(int seed, int xPrimed, int yPrimed, float& xo, float& yo) const
{
int hash = Hash(seed, xPrimed, yPrimed) & (255 << 1);
xo = Lookup<float>::RandVecs2D[hash];
yo = Lookup<float>::RandVecs2D[hash | 1];
}
void GradCoordOut(int seed, int xPrimed, int yPrimed, int zPrimed, float& xo, float& yo, float& zo) const
{
int hash = Hash(seed, xPrimed, yPrimed, zPrimed) & (255 << 2);
xo = Lookup<float>::RandVecs3D[hash];
yo = Lookup<float>::RandVecs3D[hash | 1];
zo = Lookup<float>::RandVecs3D[hash | 2];
}
void GradCoordDual(int seed, int xPrimed, int yPrimed, float xd, float yd, float& xo, float& yo) const
{
int hash = Hash(seed, xPrimed, yPrimed);
int index1 = hash & (127 << 1);
int index2 = (hash >> 7) & (255 << 1);
float xg = Lookup<float>::Gradients2D[index1];
float yg = Lookup<float>::Gradients2D[index1 | 1];
float value = xd * xg + yd * yg;
float xgo = Lookup<float>::RandVecs2D[index2];
float ygo = Lookup<float>::RandVecs2D[index2 | 1];
xo = value * xgo;
yo = value * ygo;
}
void GradCoordDual(int seed, int xPrimed, int yPrimed, int zPrimed, float xd, float yd, float zd, float& xo, float& yo, float& zo) const
{
int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
int index1 = hash & (63 << 2);
int index2 = (hash >> 6) & (255 << 2);
float xg = Lookup<float>::Gradients3D[index1];
float yg = Lookup<float>::Gradients3D[index1 | 1];
float zg = Lookup<float>::Gradients3D[index1 | 2];
float value = xd * xg + yd * yg + zd * zg;
float xgo = Lookup<float>::RandVecs3D[index2];
float ygo = Lookup<float>::RandVecs3D[index2 | 1];
float zgo = Lookup<float>::RandVecs3D[index2 | 2];
xo = value * xgo;
yo = value * ygo;
zo = value * zgo;
}
// Generic noise gen
template <typename FNfloat>
float GenNoiseSingle(int seed, FNfloat x, FNfloat y) const
{
switch (mNoiseType)
{
case NoiseType_OpenSimplex2:
return SingleSimplex(seed, x, y);
case NoiseType_OpenSimplex2S:
return SingleOpenSimplex2S(seed, x, y);
case NoiseType_Cellular:
return SingleCellular(seed, x, y);
case NoiseType_Perlin:
return SinglePerlin(seed, x, y);
case NoiseType_ValueCubic:
return SingleValueCubic(seed, x, y);
case NoiseType_Value:
return SingleValue(seed, x, y);
default:
return 0;
}
}
template <typename FNfloat>
float GenNoiseSingle(int seed, FNfloat x, FNfloat y, FNfloat z) const
{
switch (mNoiseType)
{
case NoiseType_OpenSimplex2:
return SingleOpenSimplex2(seed, x, y, z);
case NoiseType_OpenSimplex2S:
return SingleOpenSimplex2S(seed, x, y, z);
case NoiseType_Cellular:
return SingleCellular(seed, x, y, z);
case NoiseType_Perlin:
return SinglePerlin(seed, x, y, z);
case NoiseType_ValueCubic:
return SingleValueCubic(seed, x, y, z);
case NoiseType_Value:
return SingleValue(seed, x, y, z);
default:
return 0;
}
}
// Noise Coordinate Transforms (frequency, and possible skew or rotation)
template <typename FNfloat>
void TransformNoiseCoordinate(FNfloat& x, FNfloat& y) const
{
x *= mFrequency;
y *= mFrequency;
switch (mNoiseType)
{
case NoiseType_OpenSimplex2:
case NoiseType_OpenSimplex2S:
{
const FNfloat SQRT3 = (FNfloat)1.7320508075688772935274463415059;
const FNfloat F2 = 0.5f * (SQRT3 - 1);
FNfloat t = (x + y) * F2;
x += t;
y += t;
}
break;
default:
break;
}
}
template <typename FNfloat>
void TransformNoiseCoordinate(FNfloat& x, FNfloat& y, FNfloat& z) const
{
x *= mFrequency;
y *= mFrequency;
z *= mFrequency;
switch (mTransformType3D)
{
case TransformType3D_ImproveXYPlanes:
{
FNfloat xy = x + y;
FNfloat s2 = xy * -(FNfloat)0.211324865405187;
z *= (FNfloat)0.577350269189626;
x += s2 - z;
y = y + s2 - z;
z += xy * (FNfloat)0.577350269189626;
}
break;
case TransformType3D_ImproveXZPlanes:
{
FNfloat xz = x + z;
FNfloat s2 = xz * -(FNfloat)0.211324865405187;
y *= (FNfloat)0.577350269189626;
x += s2 - y;
z += s2 - y;
y += xz * (FNfloat)0.577350269189626;
}
break;
case TransformType3D_DefaultOpenSimplex2:
{
const FNfloat R3 = (FNfloat)(2.0 / 3.0);
FNfloat r = (x + y + z) * R3; // Rotation, not skew
x = r - x;
y = r - y;
z = r - z;
}
break;
default:
break;
}
}
void UpdateTransformType3D()
{
switch (mRotationType3D)
{
case RotationType3D_ImproveXYPlanes:
mTransformType3D = TransformType3D_ImproveXYPlanes;
break;
case RotationType3D_ImproveXZPlanes:
mTransformType3D = TransformType3D_ImproveXZPlanes;
break;
default:
switch (mNoiseType)
{
case NoiseType_OpenSimplex2:
case NoiseType_OpenSimplex2S:
mTransformType3D = TransformType3D_DefaultOpenSimplex2;
break;
default:
mTransformType3D = TransformType3D_None;
break;
}
break;
}
}
// Domain Warp Coordinate Transforms
template <typename FNfloat>
void TransformDomainWarpCoordinate(FNfloat& x, FNfloat& y) const
{
switch (mDomainWarpType)
{
case DomainWarpType_OpenSimplex2:
case DomainWarpType_OpenSimplex2Reduced:
{
const FNfloat SQRT3 = (FNfloat)1.7320508075688772935274463415059;
const FNfloat F2 = 0.5f * (SQRT3 - 1);
FNfloat t = (x + y) * F2;
x += t;
y += t;
}
break;
default:
break;
}
}
template <typename FNfloat>
void TransformDomainWarpCoordinate(FNfloat& x, FNfloat& y, FNfloat& z) const
{
switch (mWarpTransformType3D)
{
case TransformType3D_ImproveXYPlanes:
{
FNfloat xy = x + y;
FNfloat s2 = xy * -(FNfloat)0.211324865405187;
z *= (FNfloat)0.577350269189626;
x += s2 - z;
y = y + s2 - z;
z += xy * (FNfloat)0.577350269189626;
}
break;
case TransformType3D_ImproveXZPlanes:
{
FNfloat xz = x + z;
FNfloat s2 = xz * -(FNfloat)0.211324865405187;
y *= (FNfloat)0.577350269189626;
x += s2 - y;
z += s2 - y;
y += xz * (FNfloat)0.577350269189626;
}
break;
case TransformType3D_DefaultOpenSimplex2:
{
const FNfloat R3 = (FNfloat)(2.0 / 3.0);
FNfloat r = (x + y + z) * R3; // Rotation, not skew
x = r - x;
y = r - y;
z = r - z;
}
break;
default:
break;
}
}
void UpdateWarpTransformType3D()
{
switch (mRotationType3D)
{
case RotationType3D_ImproveXYPlanes:
mWarpTransformType3D = TransformType3D_ImproveXYPlanes;
break;
case RotationType3D_ImproveXZPlanes:
mWarpTransformType3D = TransformType3D_ImproveXZPlanes;
break;
default:
switch (mDomainWarpType)
{
case DomainWarpType_OpenSimplex2:
case DomainWarpType_OpenSimplex2Reduced:
mWarpTransformType3D = TransformType3D_DefaultOpenSimplex2;
break;
default:
mWarpTransformType3D = TransformType3D_None;
break;
}
break;
}
}
// Fractal FBm
template <typename FNfloat>
float GenFractalFBm(FNfloat x, FNfloat y) const
{
int seed = mSeed;
float sum = 0;
float amp = mFractalBounding;
for (int i = 0; i < mOctaves; i++)
{
float noise = GenNoiseSingle(seed++, x, y);
sum += noise * amp;
amp *= Lerp(1.0f, FastMin(noise + 1, 2) * 0.5f, mWeightedStrength);
x *= mLacunarity;
y *= mLacunarity;
amp *= mGain;
}
return sum;
}
template <typename FNfloat>
float GenFractalFBm(FNfloat x, FNfloat y, FNfloat z) const
{
int seed = mSeed;
float sum = 0;
float amp = mFractalBounding;
for (int i = 0; i < mOctaves; i++)
{
float noise = GenNoiseSingle(seed++, x, y, z);
sum += noise * amp;
amp *= Lerp(1.0f, (noise + 1) * 0.5f, mWeightedStrength);
x *= mLacunarity;
y *= mLacunarity;
z *= mLacunarity;
amp *= mGain;
}
return sum;
}
// Fractal Ridged
template <typename FNfloat>
float GenFractalRidged(FNfloat x, FNfloat y) const
{
int seed = mSeed;
float sum = 0;
float amp = mFractalBounding;
for (int i = 0; i < mOctaves; i++)
{
float noise = FastAbs(GenNoiseSingle(seed++, x, y));
sum += (noise * -2 + 1) * amp;
amp *= Lerp(1.0f, 1 - noise, mWeightedStrength);
x *= mLacunarity;
y *= mLacunarity;
amp *= mGain;
}
return sum;
}
template <typename FNfloat>
float GenFractalRidged(FNfloat x, FNfloat y, FNfloat z) const
{
int seed = mSeed;
float sum = 0;
float amp = mFractalBounding;
for (int i = 0; i < mOctaves; i++)
{
float noise = FastAbs(GenNoiseSingle(seed++, x, y, z));
sum += (noise * -2 + 1) * amp;
amp *= Lerp(1.0f, 1 - noise, mWeightedStrength);
x *= mLacunarity;
y *= mLacunarity;
z *= mLacunarity;
amp *= mGain;
}
return sum;
}
// Fractal PingPong
template <typename FNfloat>
float GenFractalPingPong(FNfloat x, FNfloat y) const
{
int seed = mSeed;
float sum = 0;
float amp = mFractalBounding;
for (int i = 0; i < mOctaves; i++)
{
float noise = PingPong((GenNoiseSingle(seed++, x, y) + 1) * mPingPongStrength);
sum += (noise - 0.5f) * 2 * amp;
amp *= Lerp(1.0f, noise, mWeightedStrength);
x *= mLacunarity;
y *= mLacunarity;
amp *= mGain;
}
return sum;
}
template <typename FNfloat>
float GenFractalPingPong(FNfloat x, FNfloat y, FNfloat z) const
{
int seed = mSeed;
float sum = 0;
float amp = mFractalBounding;
for (int i = 0; i < mOctaves; i++)
{
float noise = PingPong((GenNoiseSingle(seed++, x, y, z) + 1) * mPingPongStrength);
sum += (noise - 0.5f) * 2 * amp;
amp *= Lerp(1.0f, noise, mWeightedStrength);
x *= mLacunarity;
y *= mLacunarity;
z *= mLacunarity;
amp *= mGain;
}
return sum;
}
// Simplex/OpenSimplex2 Noise
template <typename FNfloat>
float SingleSimplex(int seed, FNfloat x, FNfloat y) const
{
// 2D OpenSimplex2 case uses the same algorithm as ordinary Simplex.
const float SQRT3 = 1.7320508075688772935274463415059f;
const float G2 = (3 - SQRT3) / 6;
/*
* --- Skew moved to TransformNoiseCoordinate method ---
* const FNfloat F2 = 0.5f * (SQRT3 - 1);
* FNfloat s = (x + y) * F2;
* x += s; y += s;
*/
int i = FastFloor(x);
int j = FastFloor(y);
float xi = (float)(x - i);
float yi = (float)(y - j);