-
Notifications
You must be signed in to change notification settings - Fork 2
/
noise.fxh
1685 lines (1426 loc) · 64.9 KB
/
noise.fxh
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
////////////////////////////////////////////////////////////////
//
// CCBY 2017 everyoneishappy.com
//
////////////////////////////////////////////////////////////////
#define NOISE_FXH
#ifndef CALC_FXH
#include <packs\happy.fxh\calc.fxh> // used for gradient ops
#endif
/*
/////////////////////////////////////////////////////////////////////////////
//
// USAGE
//
/////////////////////////////////////////////////////////////////////////////
basis functions include:
sine
valueNoise
perlin
simplex
worleyFast
worley
they can be used like this:
float [basis] (float2 p) // 2D scalar noise
float [basis] (float3 p) // 3D scalar noise
float2 [basis]2 (float2 p) // 2D vector noise
float3 [basis]3 (float3 p) // 3D vector noise
float3 [basis]DFV float3(p) // return a divergence-free 3D vector field (DFV) of the noise
float3 [basis]Grad (float2 p) // 2D scalar noise as .x and gradient returned as .yz
float4 [basis]Grad (float3 p) // 3D scalar noise as .x and gradient returned as .yzw
float2 [basis]Grad2 (float2 p, out float2 gradX, out float2 gradY) // 2D vector noise and X,Y gradients
float3 [basis]Grad3 (float3 p, out float3 gradX, out float3 gradY, out float3 gradZ) // 3D vector noise and X,Y,Z gradients
worley functions can also have thier signature extended for more options
eg: worley(p, cellDistance, cellFunction)
distance metrics include:
EuclideanSquared
Euclidean
Chebyshev
Manhattan
Minkowski
Cubes
Cell functions include:
F1
F2
F2MinusF1
F1PlusF2
Average
Crackle
instances are already made for each, so you canjust write the name, or use an interface as a selector:
iCellDist cellDistance <string linkclass="EuclideanSquared,Euclidean,Chebyshev,Manhattan,Minkowski,Cubes";>;
iCellFunc cellFunction <string linkclass="F1,F2,F2MinusF1,F1PlusF2,Average,Crackle";>;
*/
/////////////////////////////////////////////////////////////////////////////
//
// Common FBM Parameters helper macro
//
/////////////////////////////////////////////////////////////////////////////
#define FBMPARS(name) \
float name##Frequency = 1; \
float name##Persistence = 0.5; \
float name##Lacunarity = 2; \
int name##Octaves = 4;
#define FBMARGS(name) name##Frequency, name##Persistence, name##Lacunarity, name##Octaves
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// 2D and 3D vector helper macros
//
/////////////////////////////////////////////////////////////////////////////
// domain offset defualts for calling the same function is several places
#ifndef NOISEOFFSETS
#define NOISEOFFSETS float2(67, 197)
#endif
// takes a scalar function FUNCTIONNAME and defines a new function returning 2D vector called [FUNCTIONNAME]2
#define NOISE2DVECTORFUNCTION(FUNCTIONNAME) \
float2 FUNCTIONNAME##2(float2 p) \
{return float2(FUNCTIONNAME(p), FUNCTIONNAME (p+NOISEOFFSETS.x));};
// takes a scalar function FUNCTIONNAME and defines a new function returning 2D vector & gradients called [FUNCTIONNAME]2
#define NOISE2DVECTORGRADFUNCTION(FUNCTIONNAME) \
float2 FUNCTIONNAME##2(float2 p, out float2 gradX, out float2 gradY) \
{ \
float3 nx = FUNCTIONNAME(p); \
float3 ny = FUNCTIONNAME(p+NOISEOFFSETS.x); \
gradX = nx.yz; \
gradY = ny.yz; \
return float2(nx.x, ny.x); \
};
// takes a scalar function FUNCTIONNAME and defines a new function returning 3D vector called [FUNCTIONNAME]3
#define NOISE3DVECTORFUNCTION(FUNCTIONNAME) \
float3 FUNCTIONNAME##3(float3 p) \
{return float3(FUNCTIONNAME(p), FUNCTIONNAME (p+NOISEOFFSETS.x), FUNCTIONNAME(p+NOISEOFFSETS.y));};
// takes a scalar function FUNCTIONNAME and defines a new function returning 3D vector & gradients called [FUNCTIONNAME]3
#define NOISE3DVECTORGRADFUNCTION(FUNCTIONNAME) \
float3 FUNCTIONNAME##3(float3 p, out float3 gradX, out float3 gradY, out float3 gradZ) \
{ \
float4 nx = FUNCTIONNAME(p); \
float4 ny = FUNCTIONNAME(p+NOISEOFFSETS.x); \
float4 nz = FUNCTIONNAME(p+NOISEOFFSETS.y); \
gradX = nx.yzw; \
gradY = ny.yzw; \
gradZ = nz.yzw; \
return float3(nx.x, ny.x, nz.x); \
};
//###############################################################################
// Hash without Sine
// Creative Commons Attribution-ShareAlike 4.0 International Public License
// Created by David Hoskins.
// https://www.shadertoy.com/view/4djSRW
//----------------------------------------------------------------------------------------
// 1 out, 1 in...
float hash11(float p)
{
p = frac(p * .1031);
p *= p + 19.19;
p *= p + p;
return frac(p);
}
//----------------------------------------------------------------------------------------
// 1 out, 2 in...
float hash12(float2 p)
{
float3 p3 = frac(float3(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 19.19);
return frac((p3.x + p3.y) * p3.z);
}
//----------------------------------------------------------------------------------------
// 1 out, 3 in...
float hash13(float3 p3)
{
p3 = frac(p3 * .1031);
p3 += dot(p3, p3.yzx + 19.19);
return frac((p3.x + p3.y) * p3.z);
}
//----------------------------------------------------------------------------------------
// 2 out, 1 in...
float2 hash21(float p)
{
float3 p3 = frac(float3(p.xxx) * float3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx + 19.19);
return frac((p3.xx+p3.yz)*p3.zy);
}
//----------------------------------------------------------------------------------------
/// 2 out, 2 in...
float2 hash22(float2 p)
{
float3 p3 = frac(float3(p.xyx) * float3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx+19.19);
return frac((p3.xx+p3.yz)*p3.zy);
}
//----------------------------------------------------------------------------------------
/// 2 out, 3 in...
float2 hash23(float3 p3)
{
p3 = frac(p3 * float3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx+19.19);
return frac((p3.xx+p3.yz)*p3.zy);
}
//----------------------------------------------------------------------------------------
// 3 out, 1 in...
float3 hash31(float p)
{
float3 p3 = frac(float3(p.xxx) * float3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx+19.19);
return frac((p3.xxy+p3.yzz)*p3.zyx);
}
//----------------------------------------------------------------------------------------
/// 3 out, 2 in...
float3 hash32(float2 p)
{
float3 p3 = frac(float3(p.xyx) * float3(.1031, .1030, .0973));
p3 += dot(p3, p3.yxz+19.19);
return frac((p3.xxy+p3.yzz)*p3.zyx);
}
//----------------------------------------------------------------------------------------
/// 3 out, 3 in...
float3 hash33(float3 p3)
{
p3 = frac(p3 * float3(.1031, .1030, .0973));
p3 += dot(p3, p3.yxz+19.19);
return frac((p3.xxy + p3.yxx)*p3.zyx);
}
//----------------------------------------------------------------------------------------
// 4 out, 1 in...
float4 hash41(float p)
{
float4 p4 = frac(float4(p.xxxx) * float4(.1031, .1030, .0973, .1099));
p4 += dot(p4, p4.wzxy+19.19);
return frac((p4.xxyz+p4.yzzw)*p4.zywx);
}
//----------------------------------------------------------------------------------------
// 4 out, 2 in...
float4 hash42(float2 p)
{
float4 p4 = frac(float4(p.xyxy) * float4(.1031, .1030, .0973, .1099));
p4 += dot(p4, p4.wzxy+19.19);
return frac((p4.xxyz+p4.yzzw)*p4.zywx);
}
//----------------------------------------------------------------------------------------
// 4 out, 3 in...
float4 hash43(float3 p)
{
float4 p4 = frac(float4(p.xyzx) * float4(.1031, .1030, .0973, .1099));
p4 += dot(p4, p4.wzxy+19.19);
return frac((p4.xxyz+p4.yzzw)*p4.zywx);
}
//----------------------------------------------------------------------------------------
// 4 out, 4 in...
float4 hash44(float4 p4)
{
p4 = frac(p4 * float4(.1031, .1030, .0973, .1099));
p4 += dot(p4, p4.wzxy+19.19);
return frac((p4.xxyz+p4.yzzw)*p4.zywx);
}
//----------------------------------------------------------------------------------------
//###############################################################################
////////////////////////////////////////////////////////////////
//
// Random Noise Basis
//
////////////////////////////////////////////////////////////////
#ifndef RANDOM_ITERATIONS
#define RANDOM_ITERATIONS 4
#endif
float random (float2 p)
{
float a = 0.0;
for (int t = 0; t < RANDOM_ITERATIONS; t++)
{
float v = float(t+1)*.152;
float2 pos = (p.xy * v * 1500. + 50.);
float3 p3 = frac(float3(pos.xyx) * .1031);
p3 += dot(p3, p3.yzx + 19.19);
a += frac((p3.x + p3.y) * p3.z);
}
return a / float(RANDOM_ITERATIONS);
}
float random (float3 p)
{
float a = 0.0;
for (int t = 0; t < RANDOM_ITERATIONS; t++)
{
float v = float(t+1)*.132;
float3 pos = (p * v * 1500. + 50.0);
float3 p3 = frac(pos * float3(.1031, .1030, .0973));
p3 += dot(p3, p3.yxz+19.19);
a += frac((p3.x + p3.y) * p3.z);
}
return a / float(RANDOM_ITERATIONS);
}
//gradient functions are just random, for random, man
NOISE2DVECTORFUNCTION(random)
//NOISE2DVECTORGRADFUNCTION(valueNoiseGrad)
NOISE3DVECTORFUNCTION(random)
//NOISE3DVECTORGRADFUNCTION(valueNoiseGrad)
// return a divergence-free 3D vector field (DFV)
float3 randomDFV(float3 p, float offset = 67)
{
//float4 n1 = valueNoiseGrad(p);
//float4 n2 = valueNoiseGrad(p+offset);
//return cross(n1.yzw, n2.yzw);
return random3(p);
}
////////////////////////////////////////////////////////////////
//
// Noise Basis Functions
// textureless basis functions below mostly from https://github.com/BrianSharpe/Wombat/
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// Value Noise Basis
//
////////////////////////////////////////////////////////////////
//2D
float valueNoise(float2 p)
{
// establish our grid cell and unit position
float2 Pi = floor(p);
float2 Pf = p - Pi;
// calculate the hash.
float4 Pt = float4( Pi.xy, Pi.xy + 1.0 );
Pt = Pt - floor(Pt * ( 1.0 / 71.0 )) * 71.0;
Pt += float2( 26.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float4 hash = frac( Pt * ( 1.0 / 951.135664 ) );
// blend the results and return
float2 blend = Pf * Pf * Pf * (Pf * (Pf * 6.0 - 15.0) + 10.0);
float4 blend2 = float4( blend, float2( 1.0 - blend ) );
return dot( hash, blend2.zxzx * blend2.wwyy );
}
//2D w/ gradient
float3 valueNoiseGrad(float2 p)
{
// https://github.com/BrianSharpe/Wombat/blob/master/Value2D_Deriv.glsl
// establish our grid cell and unit position
float2 Pi = floor(p);
float2 Pf = p - Pi;
// calculate the hash.
float4 Pt = float4( Pi.xy, Pi.xy + 1.0 );
Pt = Pt - floor(Pt * ( 1.0 / 71.0 )) * 71.0;
Pt += float2( 26.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float4 hash = frac( Pt * ( 1.0 / 951.135664 ) );
// blend the results and return
float4 blend = Pf.xyxy * Pf.xyxy * ( Pf.xyxy * ( Pf.xyxy * ( Pf.xyxy * float2( 6.0, 0.0 ).xxyy + float2( -15.0, 30.0 ).xxyy ) + float2( 10.0, -60.0 ).xxyy ) + float2( 0.0, 30.0 ).xxyy );
float4 res0 = lerp( hash.xyxz, hash.zwyw, blend.yyxx );
return float3( res0.x, 0.0, 0.0 ) + ( res0.yyw - res0.xxz ) * blend.xzw;
}
//3D
float valueNoise(float3 p)
{
// establish our grid cell and unit position
float3 Pi = floor(p);
float3 Pf = p - Pi;
float3 Pf_min1 = Pf - 1.0;
// clamp the domain
Pi.xyz = Pi.xyz - floor(Pi.xyz * ( 1.0 / 69.0 )) * 69.0;
float3 Pi_inc1 = step( Pi, ( 69.0 - 1.5 ) ) * ( Pi + 1.0 );
// calculate the hash
float4 Pt = float4( Pi.xy, Pi_inc1.xy ) + float2( 50.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float2 hash_mod = float2( 1.0 / ( 635.298681 + float2( Pi.z, Pi_inc1.z ) * 48.500388 ) );
float4 hash_lowz = frac( Pt * hash_mod.xxxx );
float4 hash_highz = frac( Pt * hash_mod.yyyy );
// blend the results and return
float3 blend = Pf * Pf * Pf * (Pf * (Pf * 6.0 - 15.0) + 10.0);
float4 res0 = lerp( hash_lowz, hash_highz, blend.z );
float4 blend2 = float4( blend.xy, float2( 1.0 - blend.xy ) );
return dot( res0, blend2.zxzx * blend2.wwyy );
}
//3D w/ gradient
float4 valueNoiseGrad(float3 p)
{
// establish our grid cell and unit position
float3 Pi = floor(p);
float3 Pf = p - Pi;
float3 Pf_min1 = Pf - 1.0;
// clamp the domain
Pi.xyz = Pi.xyz - floor(Pi.xyz * ( 1.0 / 69.0 )) * 69.0;
float3 Pi_inc1 = step( Pi, ( 69.0 - 1.5 ) ) * ( Pi + 1.0 );
// calculate the hash
float4 Pt = float4( Pi.xy, Pi_inc1.xy ) + float2( 50.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float2 hash_mod = float2( 1.0 / ( 635.298681 + float2( Pi.z, Pi_inc1.z ) * 48.500388 ) );
float4 hash_lowz = frac( Pt * hash_mod.xxxx );
float4 hash_highz = frac( Pt * hash_mod.yyyy );
// blend the results and return
float3 blend = Pf * Pf * Pf * (Pf * (Pf * 6.0 - 15.0) + 10.0);
float3 blendDeriv = Pf * Pf * (Pf * (Pf * 30.0 - 60.0) + 30.0);
float4 res0 = lerp( hash_lowz, hash_highz, blend.z );
float4 res1 = lerp( res0.xyxz, res0.zwyw, blend.yyxx );
float4 res3 = lerp( float4( hash_lowz.xy, hash_highz.xy ), float4( hash_lowz.zw, hash_highz.zw ), blend.y );
float2 res4 = lerp( res3.xz, res3.yw, blend.x );
return float4( res1.x, 0.0, 0.0, 0.0 ) + ( float4( res1.yyw, res4.y ) - float4( res1.xxz, res4.x ) ) * float4( blend.x, blendDeriv );
}
NOISE2DVECTORFUNCTION(valueNoise)
NOISE2DVECTORGRADFUNCTION(valueNoiseGrad)
NOISE3DVECTORFUNCTION(valueNoise)
NOISE3DVECTORGRADFUNCTION(valueNoiseGrad)
// return a divergence-free 3D vector field (DFV)
float3 valueNoiseDFV(float3 p, float offset = 67)
{
float4 n1 = valueNoiseGrad(p);
float4 n2 = valueNoiseGrad(p+offset);
return cross(n1.yzw, n2.yzw);
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// Perlin Basis
//
////////////////////////////////////////////////////////////////
#define SOMELARGEFLOATS float3( 635.298681, 682.357502, 668.926525 )
#define ZINC float3( 48.500388, 65.294118, 63.934599 )
//2D
float perlin(float2 p)
{
// establish our grid cell and unit position
float2 Pi = floor(p);
float4 Pf_Pfmin1 = p.xyxy - float4( Pi, Pi + 1.0 );
// calculate the hash
float4 Pt = float4( Pi.xy, Pi.xy + 1.0 );
Pt = Pt - floor(Pt * ( 1.0 / 71.0 )) * 71.0;
Pt += float2( 26.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float4 hash_x = frac( Pt * ( 1.0 / 951.135664 ) );
float4 hash_y = frac( Pt * ( 1.0 / 642.949883 ) );
// calculate the gradient results
float4 grad_x = hash_x - 0.49999;
float4 grad_y = hash_y - 0.49999;
float4 grad_results = rsqrt( grad_x * grad_x + grad_y * grad_y ) * ( grad_x * Pf_Pfmin1.xzxz + grad_y * Pf_Pfmin1.yyww );
// Classic Perlin Interpolation
grad_results *= 1.4142135623730950488016887242097; // scale things to a strict -1.0->1.0 range *= 1.0/sqrt(0.5)
float2 blend = Pf_Pfmin1.xy * Pf_Pfmin1.xy * Pf_Pfmin1.xy * (Pf_Pfmin1.xy * (Pf_Pfmin1.xy * 6.0 - 15.0) + 10.0);
float4 blend2 = float4( blend, float2( 1.0 - blend ) );
return dot( grad_results, blend2.zxzx * blend2.wwyy );
}
//2D w/ gradient
float3 perlinGrad(float2 p )
{
// establish our grid cell and unit position
float2 Pi = floor(p);
float4 Pf_Pfmin1 = p.xyxy - float4( Pi, Pi + 1.0 );
// calculate the hash
float4 Pt = float4( Pi.xy, Pi.xy + 1.0 );
Pt = Pt - floor(Pt * ( 1.0 / 71.0 )) * 71.0;
Pt += float2( 26.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float4 hash_x = frac( Pt * ( 1.0 / 951.135664 ) );
float4 hash_y = frac( Pt * ( 1.0 / 642.949883 ) );
// calculate the gradient results
float4 grad_x = hash_x - 0.49999;
float4 grad_y = hash_y - 0.49999;
float4 norm = rsqrt( grad_x * grad_x + grad_y * grad_y );
grad_x *= norm;
grad_y *= norm;
float4 dotval = ( grad_x * Pf_Pfmin1.xzxz + grad_y * Pf_Pfmin1.yyww );
// C2 Interpolation
float4 blend = Pf_Pfmin1.xyxy * Pf_Pfmin1.xyxy * ( Pf_Pfmin1.xyxy * ( Pf_Pfmin1.xyxy * ( Pf_Pfmin1.xyxy * float2( 6.0, 0.0 ).xxyy + float2( -15.0, 30.0 ).xxyy ) + float2( 10.0, -60.0 ).xxyy ) + float2( 0.0, 30.0 ).xxyy );
// Convert our data to a more parallel format
float3 dotval0_grad0 = float3( dotval.x, grad_x.x, grad_y.x );
float3 dotval1_grad1 = float3( dotval.y, grad_x.y, grad_y.y );
float3 dotval2_grad2 = float3( dotval.z, grad_x.z, grad_y.z );
float3 dotval3_grad3 = float3( dotval.w, grad_x.w, grad_y.w );
// evaluate common constants
float3 k0_gk0 = dotval1_grad1 - dotval0_grad0;
float3 k1_gk1 = dotval2_grad2 - dotval0_grad0;
float3 k2_gk2 = dotval3_grad3 - dotval2_grad2 - k0_gk0;
// calculate final noise + deriv
float3 results = dotval0_grad0
+ blend.x * k0_gk0
+ blend.y * ( k1_gk1 + blend.x * k2_gk2 );
results.yz += blend.zw * ( float2( k0_gk0.x, k1_gk1.x ) + blend.yx * k2_gk2.xx );
return results * 1.4142135623730950488016887242097; // scale things to a strict -1.0->1.0 range *= 1.0/sqrt(0.5)
}
// 3D
float perlin(float3 p)
{
// establish our grid cell and unit position
float3 Pi = floor(p);
float3 Pf = p - Pi;
float3 Pf_min1 = Pf - 1.0;
// clamp the domain
Pi.xyz = Pi.xyz - floor(Pi.xyz * ( 1.0 / 69.0 )) * 69.0;
float3 Pi_inc1 = step( Pi, 69.0 - 1.5) * ( Pi + 1.0 );
// calculate the hash
float4 Pt = float4( Pi.xy, Pi_inc1.xy ) + float2( 50.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float3 lowz_mod = float3( 1.0 / ( SOMELARGEFLOATS + Pi.zzz * ZINC ) );
float3 highz_mod = float3( 1.0 / ( SOMELARGEFLOATS + Pi_inc1.zzz * ZINC ) );
float4 hashx0 = frac( Pt * lowz_mod.xxxx );
float4 hashx1 = frac( Pt * highz_mod.xxxx );
float4 hashy0 = frac( Pt * lowz_mod.yyyy );
float4 hashy1 = frac( Pt * highz_mod.yyyy );
float4 hashz0 = frac( Pt * lowz_mod.zzzz );
float4 hashz1 = frac( Pt * highz_mod.zzzz );
// calculate the gradients
float4 grad_x0 = hashx0 - 0.49999;
float4 grad_y0 = hashy0 - 0.49999;
float4 grad_z0 = hashz0 - 0.49999;
float4 grad_x1 = hashx1 - 0.49999;
float4 grad_y1 = hashy1 - 0.49999;
float4 grad_z1 = hashz1 - 0.49999;
float4 grad_results_0 = rsqrt( grad_x0 * grad_x0 + grad_y0 * grad_y0 + grad_z0 * grad_z0 ) * ( float2( Pf.x, Pf_min1.x ).xyxy * grad_x0 + float2( Pf.y, Pf_min1.y ).xxyy * grad_y0 + Pf.zzzz * grad_z0 );
float4 grad_results_1 = rsqrt( grad_x1 * grad_x1 + grad_y1 * grad_y1 + grad_z1 * grad_z1 ) * ( float2( Pf.x, Pf_min1.x ).xyxy * grad_x1 + float2( Pf.y, Pf_min1.y ).xxyy * grad_y1 + Pf_min1.zzzz * grad_z1 );
// Classic Perlin Interpolation
float3 blend = Pf * Pf * Pf * (Pf * (Pf * 6.0 - 15.0) + 10.0);
float4 res0 = lerp( grad_results_0, grad_results_1, blend.z );
float4 blend2 = float4( blend.xy, float2( 1.0 - blend.xy ) );
float final = dot( res0, blend2.zxzx * blend2.wwyy );
return ( final * 1.1547005383792515290182975610039); // scale things to a strict -1.0->1.0 range *= 1.0/sqrt(0.75)
}
// 3D w/ gradient
float4 perlinGrad(float3 p)
{
// establish our grid cell and unit position
float3 Pi = floor(p);
float3 Pf = p - Pi;
float3 Pf_min1 = Pf - 1.0;
// clamp the domain
Pi.xyz = Pi.xyz - floor(Pi.xyz * ( 1.0 / 69.0 )) * 69.0;
float3 Pi_inc1 = step( Pi, 69.0 - 1.5) * ( Pi + 1.0 );
// calculate the hash
float4 Pt = float4( Pi.xy, Pi_inc1.xy ) + float2( 50.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float3 lowz_mod = float3( 1.0 / ( SOMELARGEFLOATS + Pi.zzz * ZINC ) );
float3 highz_mod = float3( 1.0 / ( SOMELARGEFLOATS + Pi_inc1.zzz * ZINC ) );
float4 hashx0 = frac( Pt * lowz_mod.xxxx );
float4 hashx1 = frac( Pt * highz_mod.xxxx );
float4 hashy0 = frac( Pt * lowz_mod.yyyy );
float4 hashy1 = frac( Pt * highz_mod.yyyy );
float4 hashz0 = frac( Pt * lowz_mod.zzzz );
float4 hashz1 = frac( Pt * highz_mod.zzzz );
// calculate the gradients
float4 grad_x0 = hashx0 - 0.49999;
float4 grad_y0 = hashy0 - 0.49999;
float4 grad_z0 = hashz0 - 0.49999;
float4 grad_x1 = hashx1 - 0.49999;
float4 grad_y1 = hashy1 - 0.49999;
float4 grad_z1 = hashz1 - 0.49999;
float4 norm_0 = rsqrt( grad_x0 * grad_x0 + grad_y0 * grad_y0 + grad_z0 * grad_z0 );
float4 norm_1 = rsqrt( grad_x1 * grad_x1 + grad_y1 * grad_y1 + grad_z1 * grad_z1 );
grad_x0 *= norm_0;
grad_y0 *= norm_0;
grad_z0 *= norm_0;
grad_x1 *= norm_1;
grad_y1 *= norm_1;
grad_z1 *= norm_1;
// calculate the dot products
float4 dotval_0 = float2( Pf.x, Pf_min1.x ).xyxy * grad_x0 + float2( Pf.y, Pf_min1.y ).xxyy * grad_y0 + Pf.zzzz * grad_z0;
float4 dotval_1 = float2( Pf.x, Pf_min1.x ).xyxy * grad_x1 + float2( Pf.y, Pf_min1.y ).xxyy * grad_y1 + Pf_min1.zzzz * grad_z1;
// C2 Interpolation
float3 blend = Pf * Pf * Pf * (Pf * (Pf * 6.0 - 15.0) + 10.0);
float3 blendDeriv = Pf * Pf * (Pf * (Pf * 30.0 - 60.0) + 30.0);
// the following is based off Milo Yips derivation, but modified for parallel execution
// http://stackoverflow.com/a/14141774
// Convert our data to a more parallel format
float4 dotval0_grad0 = float4( dotval_0.x, grad_x0.x, grad_y0.x, grad_z0.x );
float4 dotval1_grad1 = float4( dotval_0.y, grad_x0.y, grad_y0.y, grad_z0.y );
float4 dotval2_grad2 = float4( dotval_0.z, grad_x0.z, grad_y0.z, grad_z0.z );
float4 dotval3_grad3 = float4( dotval_0.w, grad_x0.w, grad_y0.w, grad_z0.w );
float4 dotval4_grad4 = float4( dotval_1.x, grad_x1.x, grad_y1.x, grad_z1.x );
float4 dotval5_grad5 = float4( dotval_1.y, grad_x1.y, grad_y1.y, grad_z1.y );
float4 dotval6_grad6 = float4( dotval_1.z, grad_x1.z, grad_y1.z, grad_z1.z );
float4 dotval7_grad7 = float4( dotval_1.w, grad_x1.w, grad_y1.w, grad_z1.w );
// evaluate common constants
float4 k0_gk0 = dotval1_grad1 - dotval0_grad0;
float4 k1_gk1 = dotval2_grad2 - dotval0_grad0;
float4 k2_gk2 = dotval4_grad4 - dotval0_grad0;
float4 k3_gk3 = dotval3_grad3 - dotval2_grad2 - k0_gk0;
float4 k4_gk4 = dotval5_grad5 - dotval4_grad4 - k0_gk0;
float4 k5_gk5 = dotval6_grad6 - dotval4_grad4 - k1_gk1;
float4 k6_gk6 = (dotval7_grad7 - dotval6_grad6) - (dotval5_grad5 - dotval4_grad4) - k3_gk3;
// calculate final noise + deriv
float u = blend.x;
float v = blend.y;
float w = blend.z;
float4 result = dotval0_grad0
+ u * ( k0_gk0 + v * k3_gk3 )
+ v * ( k1_gk1 + w * k5_gk5 )
+ w * ( k2_gk2 + u * ( k4_gk4 + v * k6_gk6 ) );
result.y += dot( float4( k0_gk0.x, k3_gk3.x * v, float2( k4_gk4.x, k6_gk6.x * v ) * w ), float4( blendDeriv.xxxx ) );
result.z += dot( float4( k1_gk1.x, k3_gk3.x * u, float2( k5_gk5.x, k6_gk6.x * u ) * w ), float4( blendDeriv.yyyy ) );
result.w += dot( float4( k2_gk2.x, k4_gk4.x * u, float2( k5_gk5.x, k6_gk6.x * u ) * v ), float4( blendDeriv.zzzz ) );
return result * 1.1547005383792515290182975610039; // scale things to a strict -1.0->1.0 range *= 1.0/sqrt(0.75)
}
NOISE2DVECTORFUNCTION(perlin)
NOISE2DVECTORGRADFUNCTION(perlinGrad)
NOISE3DVECTORFUNCTION(perlin)
NOISE3DVECTORGRADFUNCTION(perlinGrad)
// return a divergence-free vector field (DFV) in 3D
float3 perlinDFV(float3 p, float offset = 67)
{
float4 n1 = perlinGrad(p);
float4 n2 = perlinGrad(p+offset);
return cross(n1.yzw, n2.yzw);
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// Simplex Basis
//
////////////////////////////////////////////////////////////////
//2D
float simplex(float2 p)
{
// https://github.com/BrianSharpe/Wombat/blob/master/SimplexPerlin2D.glsl
// simplex math constants
const float SKEWFACTOR = 0.36602540378443864676372317075294; // 0.5*(sqrt(3.0)-1.0)
const float UNSKEWFACTOR = 0.21132486540518711774542560974902; // (3.0-sqrt(3.0))/6.0
const float SIMPLEX_TRI_HEIGHT = 0.70710678118654752440084436210485; // sqrt( 0.5 ) height of simplex triangle
const float3 SIMPLEX_POINTS = float3( 1.0-UNSKEWFACTOR, -UNSKEWFACTOR, 1.0-2.0*UNSKEWFACTOR ); // simplex triangle geo
// establish our grid cell.
p *= SIMPLEX_TRI_HEIGHT; // scale space so we can have an approx feature size of 1.0
float2 Pi = floor( p + dot( p, float2( SKEWFACTOR.xx ) ) );
// calculate the hash
float4 Pt = float4( Pi.xy, Pi.xy + 1.0 );
Pt = Pt - floor(Pt * ( 1.0 / 71.0 )) * 71.0;
Pt += float2( 26.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float4 hash_x = frac( Pt * ( 1.0 / 951.135664 ) );
float4 hash_y = frac( Pt * ( 1.0 / 642.949883 ) );
// establish floattors to the 3 corners of our simplex triangle
float2 v0 = Pi - dot( Pi, float2( UNSKEWFACTOR.xx ) ) - p;
float4 v1pos_v1hash = (v0.x < v0.y) ? float4(SIMPLEX_POINTS.xy, hash_x.y, hash_y.y) : float4(SIMPLEX_POINTS.yx, hash_x.z, hash_y.z);
float4 v12 = float4( v1pos_v1hash.xy, SIMPLEX_POINTS.zz ) + v0.xyxy;
// calculate the dotproduct of our 3 corner floattors with 3 random normalized floattors
float3 grad_x = float3( hash_x.x, v1pos_v1hash.z, hash_x.w ) - 0.49999;
float3 grad_y = float3( hash_y.x, v1pos_v1hash.w, hash_y.w ) - 0.49999;
float3 grad_results = rsqrt( grad_x * grad_x + grad_y * grad_y ) * ( grad_x * float3( v0.x, v12.xz ) + grad_y * float3( v0.y, v12.yw ) );
// Normalization factor to scale the final result to a strict 1.0->-1.0 range
// http://briansharpe.wordpress.com/2012/01/13/simplex-noise/#comment-36
const float FINAL_NORMALIZATION = 99.204334582718712976990005025589;
// evaluate and return
float3 m = float3( v0.x, v12.xz ) * float3( v0.x, v12.xz ) + float3( v0.y, v12.yw ) * float3( v0.y, v12.yw );
m = max(0.5 - m, 0.0);
m = m*m;
return dot(m*m, grad_results) * FINAL_NORMALIZATION;
}
//3D
float simplex(float3 p)
{
// https://github.com/BrianSharpe/Wombat/blob/master/SimplexPerlin3D.glsl
// simplex math constants
const float SKEWFACTOR = 1.0/3.0;
const float UNSKEWFACTOR = 1.0/6.0;
const float SIMPLEX_CORNER_POS = 0.5;
const float SIMPLEX_TETRAHADRON_HEIGHT = 0.70710678118654752440084436210485 ; // sqrt( 0.5 )
// establish our grid cell.
p *= SIMPLEX_TETRAHADRON_HEIGHT; // scale space so we can have an approx feature size of 1.0
float3 Pi = floor( p + dot( p, SKEWFACTOR) );
// Find the vectors to the corners of our simplex tetrahedron
float3 x0 = p - Pi + dot(Pi, UNSKEWFACTOR);
float3 g = step(x0.yzx, x0.xyz);
float3 l = 1.0 - g;
float3 Pi_1 = min( g.xyz, l.zxy );
float3 Pi_2 = max( g.xyz, l.zxy );
float3 x1 = x0 - Pi_1 + UNSKEWFACTOR;
float3 x2 = x0 - Pi_2 + SKEWFACTOR;
float3 x3 = x0 - SIMPLEX_CORNER_POS;
// pack them into a parallel-friendly arrangement
float4 v1234_x = float4( x0.x, x1.x, x2.x, x3.x );
float4 v1234_y = float4( x0.y, x1.y, x2.y, x3.y );
float4 v1234_z = float4( x0.z, x1.z, x2.z, x3.z );
// clamp the domain of our grid cell
Pi.xyz = Pi.xyz - floor(Pi.xyz * ( 1.0 / 69.0 )) * 69.0;
float3 Pi_inc1 = step( Pi, 69.0 - 1.5) * ( Pi + 1.0 );
// generate the random vectors
float4 Pt = float4( Pi.xy, Pi_inc1.xy ) + float2( 50.0, 161.0 ).xyxy;
Pt *= Pt;
float4 V1xy_V2xy = lerp( Pt.xyxy, Pt.zwzw, float4( Pi_1.xy, Pi_2.xy ) );
Pt = float4( Pt.x, V1xy_V2xy.xz, Pt.z ) * float4( Pt.y, V1xy_V2xy.yw, Pt.w );
float3 lowz_mods = float3( 1.0 / ( SOMELARGEFLOATS.xyz + Pi.zzz * ZINC.xyz ) );
float3 highz_mods = float3( 1.0 / ( SOMELARGEFLOATS.xyz + Pi_inc1.zzz * ZINC.xyz ) );
Pi_1 = ( Pi_1.z < 0.5 ) ? lowz_mods : highz_mods;
Pi_2 = ( Pi_2.z < 0.5 ) ? lowz_mods : highz_mods;
float4 hash_0 = frac( Pt * float4( lowz_mods.x, Pi_1.x, Pi_2.x, highz_mods.x ) ) - 0.49999;
float4 hash_1 = frac( Pt * float4( lowz_mods.y, Pi_1.y, Pi_2.y, highz_mods.y ) ) - 0.49999;
float4 hash_2 = frac( Pt * float4( lowz_mods.z, Pi_1.z, Pi_2.z, highz_mods.z ) ) - 0.49999;
// evaluate gradients
float4 grad_results = rsqrt( hash_0 * hash_0 + hash_1 * hash_1 + hash_2 * hash_2 ) * ( hash_0 * v1234_x + hash_1 * v1234_y + hash_2 * v1234_z );
// Normalization factor to scale the final result to a strict 1.0->-1.0 range
// http://briansharpe.wordpress.com/2012/01/13/simplex-noise/#comment-36
const float FINAL_NORMALIZATION = 37.837227241611314102871574478976;
// evaulate the kernel weights ( use (0.5-x*x)^3 instead of (0.6-x*x)^4 to fix discontinuities )
float4 kernel_weights = v1234_x * v1234_x + v1234_y * v1234_y + v1234_z * v1234_z;
kernel_weights = max(0.5 - kernel_weights, 0.0);
kernel_weights = kernel_weights*kernel_weights*kernel_weights;
// sum with the kernel and return
return dot( kernel_weights, grad_results ) * FINAL_NORMALIZATION;
}
//2D w/ gradient
float3 simplexGrad(float2 p)
{
// simplex math constants
const float SKEWFACTOR = 0.36602540378443864676372317075294; // 0.5*(sqrt(3.0)-1.0)
const float UNSKEWFACTOR = 0.21132486540518711774542560974902; // (3.0-sqrt(3.0))/6.0
const float SIMPLEX_TRI_HEIGHT = 0.70710678118654752440084436210485; // sqrt( 0.5 ) height of simplex triangle
const float3 SIMPLEX_POINTS = float3( 1.0-UNSKEWFACTOR, -UNSKEWFACTOR, 1.0-2.0*UNSKEWFACTOR ); // simplex triangle geo
// establish our grid cell.
p *= SIMPLEX_TRI_HEIGHT; // scale space so we can have an approx feature size of 1.0
float2 Pi = floor( p + dot( p, float2( SKEWFACTOR.xx ) ) );
// calculate the hash
float4 Pt = float4( Pi.xy, Pi.xy + 1.0 );
Pt = Pt - floor(Pt * ( 1.0 / 71.0 )) * 71.0;
Pt += float2( 26.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float4 hash_x = frac( Pt * ( 1.0 / 951.135664 ) );
float4 hash_y = frac( Pt * ( 1.0 / 642.949883 ) );
// establish floattors to the 3 corners of our simplex triangle
float2 v0 = Pi - dot( Pi, float2( UNSKEWFACTOR.xx ) ) - p;
float4 v1pos_v1hash = (v0.x < v0.y) ? float4(SIMPLEX_POINTS.xy, hash_x.y, hash_y.y) : float4(SIMPLEX_POINTS.yx, hash_x.z, hash_y.z);
float4 v12 = float4( v1pos_v1hash.xy, SIMPLEX_POINTS.zz ) + v0.xyxy;
// calculate the dotproduct of our 3 corner floattors with 3 random normalized floattors
float3 grad_x = float3( hash_x.x, v1pos_v1hash.z, hash_x.w ) - 0.49999;
float3 grad_y = float3( hash_y.x, v1pos_v1hash.w, hash_y.w ) - 0.49999;
float3 norm = rsqrt( grad_x * grad_x + grad_y * grad_y );
grad_x *= norm;
grad_y *= norm;
float3 grad_results = grad_x * float3( v0.x, v12.xz ) + grad_y * float3( v0.y, v12.yw );
// evaluate the kernel
float3 m = float3( v0.x, v12.xz ) * float3( v0.x, v12.xz ) + float3( v0.y, v12.yw ) * float3( v0.y, v12.yw );
m = max(0.5 - m, 0.0);
float3 m2 = m*m;
float3 m4 = m2*m2;
// calc the derivatives
float3 temp = 8.0 * m2 * m * grad_results;
float xderiv = dot( temp, float3( v0.x, v12.xz ) ) - dot( m4, grad_x );
float yderiv = dot( temp, float3( v0.y, v12.yw ) ) - dot( m4, grad_y );
// Normalization factor to scale the final result to a strict 1.0->-1.0 range
// http://briansharpe.wordpress.com/2012/01/13/simplex-noise/#comment-36
const float FINAL_NORMALIZATION = 99.204334582718712976990005025589;
// sum and return all results as a float3
return float3( dot( m4, grad_results ), xderiv, yderiv ) * FINAL_NORMALIZATION;
}
//3D w/ gradient
float4 simplexGrad(float3 p)
{
const float SKEWFACTOR = 1.0/3.0;
const float UNSKEWFACTOR = 1.0/6.0;
const float SIMPLEX_CORNER_POS = 0.5;
const float SIMPLEX_TETRAHADRON_HEIGHT = 0.70710678118654752440084436210485 ; // sqrt( 0.5 )
// establish our grid cell.
p *= SIMPLEX_TETRAHADRON_HEIGHT; // scale space so we can have an approx feature size of 1.0
float3 Pi = floor( p + dot( p, SKEWFACTOR) );
// Find the vectors to the corners of our simplex tetrahedron
float3 x0 = p - Pi + dot(Pi, UNSKEWFACTOR);
float3 g = step(x0.yzx, x0.xyz);
float3 l = 1.0 - g;
float3 Pi_1 = min( g.xyz, l.zxy );
float3 Pi_2 = max( g.xyz, l.zxy );
float3 x1 = x0 - Pi_1 + UNSKEWFACTOR;
float3 x2 = x0 - Pi_2 + SKEWFACTOR;
float3 x3 = x0 - SIMPLEX_CORNER_POS;
// pack them into a parallel-friendly arrangement
float4 v1234_x = float4( x0.x, x1.x, x2.x, x3.x );
float4 v1234_y = float4( x0.y, x1.y, x2.y, x3.y );
float4 v1234_z = float4( x0.z, x1.z, x2.z, x3.z );
// clamp the domain of our grid cell
Pi.xyz = Pi.xyz - floor(Pi.xyz * ( 1.0 / 69.0 )) * 69.0;
float3 Pi_inc1 = step(Pi, 69.0 - 1.5) * ( Pi + 1.0 );
// generate the random vectors
float4 Pt = float4( Pi.xy, Pi_inc1.xy ) + float2( 50.0, 161.0 ).xyxy;
Pt *= Pt;
float4 V1xy_V2xy = lerp( Pt.xyxy, Pt.zwzw, float4( Pi_1.xy, Pi_2.xy ) );
Pt = float4( Pt.x, V1xy_V2xy.xz, Pt.z ) * float4( Pt.y, V1xy_V2xy.yw, Pt.w );
float3 lowz_mods = float3( 1.0 / ( SOMELARGEFLOATS.xyz + Pi.zzz * ZINC.xyz ) );
float3 highz_mods = float3( 1.0 / ( SOMELARGEFLOATS.xyz + Pi_inc1.zzz * ZINC.xyz ) );
Pi_1 = ( Pi_1.z < 0.5 ) ? lowz_mods : highz_mods;
Pi_2 = ( Pi_2.z < 0.5 ) ? lowz_mods : highz_mods;
float4 hash_0 = frac( Pt * float4( lowz_mods.x, Pi_1.x, Pi_2.x, highz_mods.x ) ) - 0.49999;
float4 hash_1 = frac( Pt * float4( lowz_mods.y, Pi_1.y, Pi_2.y, highz_mods.y ) ) - 0.49999;
float4 hash_2 = frac( Pt * float4( lowz_mods.z, Pi_1.z, Pi_2.z, highz_mods.z ) ) - 0.49999;
// normalize random gradient vectors
float4 norm = rsqrt( hash_0 * hash_0 + hash_1 * hash_1 + hash_2 * hash_2 );
hash_0 *= norm;
hash_1 *= norm;
hash_2 *= norm;
// evaluate gradients
float4 grad_results = hash_0 * v1234_x + hash_1 * v1234_y + hash_2 * v1234_z;
// evaulate the kernel weights ( use (0.5-x*x)^3 instead of (0.6-x*x)^4 to fix discontinuities )
float4 m = v1234_x * v1234_x + v1234_y * v1234_y + v1234_z * v1234_z;
m = max(0.5 - m, 0.0);
float4 m2 = m*m;
float4 m3 = m*m2;
// calc the derivatives
float4 temp = -6.0 * m2 * grad_results;
float xderiv = dot( temp, v1234_x ) + dot( m3, hash_0 );
float yderiv = dot( temp, v1234_y ) + dot( m3, hash_1 );
float zderiv = dot( temp, v1234_z ) + dot( m3, hash_2 );
// Normalization factor to scale the final result to a strict 1.0->-1.0 range
// http://briansharpe.wordpress.com/2012/01/13/simplex-noise/#comment-36
const float FINAL_NORMALIZATION = 37.837227241611314102871574478976;
// sum and return all results as a float3
return float4( dot( m3, grad_results ), xderiv, yderiv, zderiv ) * FINAL_NORMALIZATION;
}
NOISE2DVECTORFUNCTION(simplex)
NOISE2DVECTORGRADFUNCTION(simplexGrad)
NOISE3DVECTORFUNCTION(simplex)
NOISE3DVECTORGRADFUNCTION(simplexGrad)
// return a divergence-free vector field (DFV)
float3 simplexDFV(float3 p, float offset = 67)
{
float4 n1 = simplexGrad(p);
float4 n2 = simplexGrad(p+offset);
return cross(n1.yzw, n2.yzw);
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// Worley Basis (fast version, only gets F1
//
////////////////////////////////////////////////////////////////
//2D
float worleyFast(float2 p)
{
// https://github.com/BrianSharpe/Wombat/blob/master/Cellular2D.glsl
const float JITTER_WINDOW = 0.25; // 0.25 will guarentee no artifacts
// establish our grid cell and unit position
float2 Pi = floor(p);
float2 Pf = p - Pi;
// calculate the hash
float4 Pt = float4( Pi.xy, Pi.xy + 1.0 );
Pt = Pt - floor(Pt * ( 1.0 / 71.0 )) * 71.0;
Pt += float2( 26.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float4 hash_x = frac( Pt * ( 1.0 / 951.135664 ) );
float4 hash_y = frac( Pt * ( 1.0 / 642.949883 ) );
// generate the 4 points
hash_x = hash_x * 2.0 - 1.0;
hash_y = hash_y * 2.0 - 1.0;
hash_x = ( ( hash_x * hash_x * hash_x ) - sign( hash_x ) ) * JITTER_WINDOW + float4( 0.0, 1.0, 0.0, 1.0 );
hash_y = ( ( hash_y * hash_y * hash_y ) - sign( hash_y ) ) * JITTER_WINDOW + float4( 0.0, 0.0, 1.0, 1.0 );
// return the closest squared distance
float4 dx = Pf.xxxx - hash_x;
float4 dy = Pf.yyyy - hash_y;
float4 d = dx * dx + dy * dy;
d.xy = min(d.xy, d.zw);
return min(d.x, d.y) * ( 1.0 / 1.125 ); // return a value scaled to 0.0->1.0
}
//3D
float worleyFast(float3 p)
{
// establish our grid cell and unit position
float3 Pi = floor(p);
float3 Pf = p - Pi;
// clamp the domain
Pi.xyz = Pi.xyz - floor(Pi.xyz * ( 1.0 / 69.0 )) * 69.0;
float3 Pi_inc1 = step( Pi, 69.0 - 1.5 ) * ( Pi + 1.0 );
// calculate the hash ( over -1.0->1.0 range )
float4 Pt = float4( Pi.xy, Pi_inc1.xy ) + float2( 50.0, 161.0 ).xyxy;
Pt *= Pt;
Pt = Pt.xzxz * Pt.yyww;
float3 lowz_mod = float3( 1.0 / ( SOMELARGEFLOATS + Pi.zzz * ZINC ) );
float3 highz_mod = float3( 1.0 / ( SOMELARGEFLOATS + Pi_inc1.zzz * ZINC ) );
float4 hash_x0 = frac( Pt * lowz_mod.xxxx ) * 2.0 - 1.0;
float4 hash_x1 = frac( Pt * highz_mod.xxxx ) * 2.0 - 1.0;
float4 hash_y0 = frac( Pt * lowz_mod.yyyy ) * 2.0 - 1.0;
float4 hash_y1 = frac( Pt * highz_mod.yyyy ) * 2.0 - 1.0;
float4 hash_z0 = frac( Pt * lowz_mod.zzzz ) * 2.0 - 1.0;