forked from BrianSharpe/GPU-Noise-Lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_noise_lib.glsl
More file actions
2515 lines (2198 loc) · 111 KB
/
gpu_noise_lib.glsl
File metadata and controls
2515 lines (2198 loc) · 111 KB
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
//
// Code repository for GPU noise development blog
// http://briansharpe.wordpress.com
// https://github.com/BrianSharpe
//
// I'm not one for copyrights. Use the code however you wish.
// All I ask is that credit be given back to the blog or myself when appropriate.
// And also to let me know if you come up with any changes, improvements, thoughts or interesting uses for this stuff. :)
// Thanks!
//
// Brian Sharpe
// brisharpe CIRCLE_A yahoo DOT com
// http://briansharpe.wordpress.com
// https://github.com/BrianSharpe
//
//
// NoiseLib TODO
//
// 1) Ensure portability across different cards
// 2) 16bit and 24bit implementations of hashes and noises
// 3) Lift various noise implementations out to individual self-contained files
// 4) Implement texture-based versions
//
//
// Permutation polynomial idea is from Stefan Gustavson's and Ian McEwan's work at...
// http://github.com/ashima/webgl-noise
// http://www.itn.liu.se/~stegu/GLSL-cellular
//
// http://briansharpe.wordpress.com/2011/10/01/gpu-texture-free-noise/
//
vec4 SGPP_coord_prepare(vec4 x) { return x - floor(x * ( 1.0 / 289.0 )) * 289.0; }
vec3 SGPP_coord_prepare(vec3 x) { return x - floor(x * ( 1.0 / 289.0 )) * 289.0; }
vec4 SGPP_permute(vec4 x) { return fract( x * ( ( 34.0 / 289.0 ) * x + ( 1.0 / 289.0 ) ) ) * 289.0; }
vec4 SGPP_resolve(vec4 x) { return fract( x * ( 7.0 / 288.0 ) ); }
vec4 SGPP_hash_2D( vec2 gridcell ) // generates a random number for each of the 4 cell corners
{
// gridcell is assumed to be an integer coordinate
vec4 hash_coord = SGPP_coord_prepare( vec4( gridcell.xy, gridcell.xy + 1.0 ) );
return SGPP_resolve( SGPP_permute( SGPP_permute( hash_coord.xzxz ) + hash_coord.yyww ) );
}
void SGPP_hash_2D( vec2 gridcell, out vec4 hash_0, out vec4 hash_1 ) // generates 2 random numbers for each of the 4 cell corners
{
// gridcell is assumed to be an integer coordinate
vec4 hash_coord = SGPP_coord_prepare( vec4( gridcell.xy, gridcell.xy + 1.0 ) );
hash_0 = SGPP_permute( SGPP_permute( hash_coord.xzxz ) + hash_coord.yyww );
hash_1 = SGPP_resolve( SGPP_permute( hash_0 ) );
hash_0 = SGPP_resolve( hash_0 );
}
void SGPP_hash_3D( vec3 gridcell, out vec4 lowz_hash, out vec4 highz_hash ) // generates a random number for each of the 8 cell corners
{
// gridcell is assumed to be an integer coordinate
gridcell = SGPP_coord_prepare( gridcell );
vec3 gridcell_inc1 = step( gridcell, vec3( 287.5 ) ) * ( gridcell + 1.0 );
highz_hash = SGPP_permute( SGPP_permute( vec2( gridcell.x, gridcell_inc1.x ).xyxy ) + vec2( gridcell.y, gridcell_inc1.y ).xxyy );
lowz_hash = SGPP_resolve( SGPP_permute( highz_hash + gridcell.zzzz ) );
highz_hash = SGPP_resolve( SGPP_permute( highz_hash + gridcell_inc1.zzzz ) );
}
void SGPP_hash_3D( vec3 gridcell,
vec3 v1_mask, // user definable v1 and v2. ( 0's and 1's )
vec3 v2_mask,
out vec4 hash_0,
out vec4 hash_1,
out vec4 hash_2 ) // generates 3 random numbers for each of the 4 3D cell corners. cell corners: v0=0,0,0 v3=1,1,1 the other two are user definable
{
vec3 coords0 = gridcell.xyz - floor(gridcell.xyz * ( 1.0 / 289.0 )) * 289.0;
vec3 coords3 = step( coords0, vec3( 287.5 ) ) * ( coords0 + 1.0 );
vec3 coords1 = mix( coords0, coords3, v1_mask );
vec3 coords2 = mix( coords0, coords3, v2_mask );
hash_2 = SGPP_permute( SGPP_permute( SGPP_permute( vec4( coords0.x, coords1.x, coords2.x, coords3.x ) ) + vec4( coords0.y, coords1.y, coords2.y, coords3.y ) ) + vec4( coords0.z, coords1.z, coords2.z, coords3.z ) );
hash_0 = SGPP_resolve( hash_2 );
hash_1 = SGPP_resolve( hash_2 = SGPP_permute( hash_2 ) );
hash_2 = SGPP_resolve( SGPP_permute( hash_2 ) );
}
void SGPP_hash_3D( vec3 gridcell,
out vec4 lowz_hash_0,
out vec4 lowz_hash_1,
out vec4 lowz_hash_2,
out vec4 highz_hash_0,
out vec4 highz_hash_1,
out vec4 highz_hash_2 ) // generates 3 random numbers for each of the 8 cell corners
{
// gridcell is assumed to be an integer coordinate
gridcell = SGPP_coord_prepare( gridcell );
vec3 gridcell_inc1 = step( gridcell, vec3( 287.5 ) ) * ( gridcell + 1.0 );
highz_hash_2 = SGPP_permute( SGPP_permute( vec2( gridcell.x, gridcell_inc1.x ).xyxy ) + vec2( gridcell.y, gridcell_inc1.y ).xxyy );
lowz_hash_0 = SGPP_resolve( lowz_hash_2 = SGPP_permute( highz_hash_2 + gridcell.zzzz ) );
highz_hash_0 = SGPP_resolve( highz_hash_2 = SGPP_permute( highz_hash_2 + gridcell_inc1.zzzz ) );
lowz_hash_1 = SGPP_resolve( lowz_hash_2 = SGPP_permute( lowz_hash_2 ) );
highz_hash_1 = SGPP_resolve( highz_hash_2 = SGPP_permute( highz_hash_2 ) );
lowz_hash_2 = SGPP_resolve( SGPP_permute( lowz_hash_2 ) );
highz_hash_2 = SGPP_resolve( SGPP_permute( highz_hash_2 ) );
}
//
// implementation of the blumblumshub hash
// as described in MNoise paper http://www.cs.umbc.edu/~olano/papers/mNoise.pdf
//
// http://briansharpe.wordpress.com/2011/10/01/gpu-texture-free-noise/
//
vec4 BBS_coord_prepare(vec4 x) { return x - floor(x * ( 1.0 / 61.0 )) * 61.0; }
vec3 BBS_coord_prepare(vec3 x) { return x - floor(x * ( 1.0 / 61.0 )) * 61.0; }
vec4 BBS_permute(vec4 x) { return fract( x * x * ( 1.0 / 61.0 )) * 61.0; }
vec4 BBS_permute_and_resolve(vec4 x) { return fract( x * x * ( 1.0 / 61.0 ) ); }
vec4 BBS_hash_2D( vec2 gridcell ) // generates a random number for each of the 4 cell corners
{
// gridcell is assumed to be an integer coordinate
vec4 hash_coord = BBS_coord_prepare( vec4( gridcell.xy, gridcell.xy + 1.0 ) );
vec4 p = BBS_permute( hash_coord.xzxz /* * 7.0 */ ); // * 7.0 will increase variance close to origin
return BBS_permute_and_resolve( p + hash_coord.yyww );
}
vec4 BBS_hash_hq_2D( vec2 gridcell ) // generates a hq random number for each of the 4 cell corners
{
// gridcell is assumed to be an integer coordinate
vec4 hash_coord = BBS_coord_prepare( vec4( gridcell.xy, gridcell.xy + 1.0 ) );
vec4 p = BBS_permute( hash_coord.xzxz /* * 7.0 */ ); // * 7.0 will increase variance close to origin
p = BBS_permute( p + hash_coord.yyww );
return BBS_permute_and_resolve( p + hash_coord.xzxz );
}
void BBS_hash_3D( vec3 gridcell, out vec4 lowz_hash, out vec4 highz_hash ) // generates a random number for each of the 8 cell corners
{
// gridcell is assumed to be an integer coordinate
// was having precision issues here with 61.0 60.0 fixes it. need to test on other cards.
const float DOMAIN = 60.0;
gridcell.xyz = gridcell.xyz - floor(gridcell.xyz * ( 1.0 / DOMAIN )) * DOMAIN;
vec3 gridcell_inc1 = step( gridcell, vec3( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
vec4 p = BBS_permute( vec2( gridcell.x, gridcell_inc1.x ).xyxy /* * 7.0 */ ); // * 7.0 will increase variance close to origin
p = BBS_permute( p + vec2( gridcell.y, gridcell_inc1.y ).xxyy );
lowz_hash = BBS_permute_and_resolve( p + gridcell.zzzz );
highz_hash = BBS_permute_and_resolve( p + gridcell_inc1.zzzz );
}
//
// FAST32_hash
// A very fast hashing function. Requires 32bit support.
// http://briansharpe.wordpress.com/2011/11/15/a-fast-and-simple-32bit-floating-point-hash-function/
//
// The 2D hash formula takes the form....
// hash = mod( coord.x * coord.x * coord.y * coord.y, SOMELARGEFLOAT ) / SOMELARGEFLOAT
// We truncate and offset the domain to the most interesting part of the noise.
// SOMELARGEFLOAT should be in the range of 400.0->1000.0 and needs to be hand picked. Only some give good results.
// A 3D hash is achieved by offsetting the SOMELARGEFLOAT value by the Z coordinate
//
vec4 FAST32_hash_2D( vec2 gridcell ) // generates a random number for each of the 4 cell corners
{
// gridcell is assumed to be an integer coordinate
const vec2 OFFSET = vec2( 26.0, 161.0 );
const float DOMAIN = 71.0;
const float SOMELARGEFLOAT = 951.135664;
vec4 P = vec4( gridcell.xy, gridcell.xy + 1.0 );
P = P - floor(P * ( 1.0 / DOMAIN )) * DOMAIN; // truncate the domain
P += OFFSET.xyxy; // offset to interesting part of the noise
P *= P; // calculate and return the hash
return fract( P.xzxz * P.yyww * ( 1.0 / SOMELARGEFLOAT ) );
}
void FAST32_hash_2D( vec2 gridcell, out vec4 hash_0, out vec4 hash_1 ) // generates 2 random numbers for each of the 4 cell corners
{
// gridcell is assumed to be an integer coordinate
const vec2 OFFSET = vec2( 26.0, 161.0 );
const float DOMAIN = 71.0;
const vec2 SOMELARGEFLOATS = vec2( 951.135664, 642.949883 );
vec4 P = vec4( gridcell.xy, gridcell.xy + 1.0 );
P = P - floor(P * ( 1.0 / DOMAIN )) * DOMAIN;
P += OFFSET.xyxy;
P *= P;
P = P.xzxz * P.yyww;
hash_0 = fract( P * ( 1.0 / SOMELARGEFLOATS.x ) );
hash_1 = fract( P * ( 1.0 / SOMELARGEFLOATS.y ) );
}
void FAST32_hash_2D( vec2 gridcell,
out vec4 hash_0,
out vec4 hash_1,
out vec4 hash_2 ) // generates 3 random numbers for each of the 4 cell corners
{
// gridcell is assumed to be an integer coordinate
const vec2 OFFSET = vec2( 26.0, 161.0 );
const float DOMAIN = 71.0;
const vec3 SOMELARGEFLOATS = vec3( 951.135664, 642.949883, 803.202459 );
vec4 P = vec4( gridcell.xy, gridcell.xy + 1.0 );
P = P - floor(P * ( 1.0 / DOMAIN )) * DOMAIN;
P += OFFSET.xyxy;
P *= P;
P = P.xzxz * P.yyww;
hash_0 = fract( P * ( 1.0 / SOMELARGEFLOATS.x ) );
hash_1 = fract( P * ( 1.0 / SOMELARGEFLOATS.y ) );
hash_2 = fract( P * ( 1.0 / SOMELARGEFLOATS.z ) );
}
vec4 FAST32_hash_2D_Cell( vec2 gridcell ) // generates 4 different random numbers for the single given cell point
{
// gridcell is assumed to be an integer coordinate
const vec2 OFFSET = vec2( 26.0, 161.0 );
const float DOMAIN = 71.0;
const vec4 SOMELARGEFLOATS = vec4( 951.135664, 642.949883, 803.202459, 986.973274 );
vec2 P = gridcell - floor(gridcell * ( 1.0 / DOMAIN )) * DOMAIN;
P += OFFSET.xy;
P *= P;
return fract( (P.x * P.y) * ( 1.0 / SOMELARGEFLOATS.xyzw ) );
}
vec4 FAST32_hash_3D_Cell( vec3 gridcell ) // generates 4 different random numbers for the single given cell point
{
// gridcell is assumed to be an integer coordinate
// TODO: these constants need tweaked to find the best possible noise.
// probably requires some kind of brute force computational searching or something....
const vec2 OFFSET = vec2( 50.0, 161.0 );
const float DOMAIN = 69.0;
const vec4 SOMELARGEFLOATS = vec4( 635.298681, 682.357502, 668.926525, 588.255119 );
const vec4 ZINC = vec4( 48.500388, 65.294118, 63.934599, 63.279683 );
// truncate the domain
gridcell.xyz = gridcell - floor(gridcell * ( 1.0 / DOMAIN )) * DOMAIN;
gridcell.xy += OFFSET.xy;
gridcell.xy *= gridcell.xy;
return fract( ( gridcell.x * gridcell.y ) * ( 1.0 / ( SOMELARGEFLOATS + gridcell.zzzz * ZINC ) ) );
}
void FAST32_hash_3D( vec3 gridcell, out vec4 lowz_hash, out vec4 highz_hash ) // generates a random number for each of the 8 cell corners
{
// gridcell is assumed to be an integer coordinate
// TODO: these constants need tweaked to find the best possible noise.
// probably requires some kind of brute force computational searching or something....
const vec2 OFFSET = vec2( 50.0, 161.0 );
const float DOMAIN = 69.0;
const float SOMELARGEFLOAT = 635.298681;
const float ZINC = 48.500388;
// truncate the domain
gridcell.xyz = gridcell.xyz - floor(gridcell.xyz * ( 1.0 / DOMAIN )) * DOMAIN;
vec3 gridcell_inc1 = step( gridcell, vec3( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
// calculate the noise
vec4 P = vec4( gridcell.xy, gridcell_inc1.xy ) + OFFSET.xyxy;
P *= P;
P = P.xzxz * P.yyww;
highz_hash.xy = vec2( 1.0 / ( SOMELARGEFLOAT + vec2( gridcell.z, gridcell_inc1.z ) * ZINC ) );
lowz_hash = fract( P * highz_hash.xxxx );
highz_hash = fract( P * highz_hash.yyyy );
}
void FAST32_hash_3D( vec3 gridcell,
vec3 v1_mask, // user definable v1 and v2. ( 0's and 1's )
vec3 v2_mask,
out vec4 hash_0,
out vec4 hash_1,
out vec4 hash_2 ) // generates 3 random numbers for each of the 4 3D cell corners. cell corners: v0=0,0,0 v3=1,1,1 the other two are user definable
{
// gridcell is assumed to be an integer coordinate
// TODO: these constants need tweaked to find the best possible noise.
// probably requires some kind of brute force computational searching or something....
const vec2 OFFSET = vec2( 50.0, 161.0 );
const float DOMAIN = 69.0;
const vec3 SOMELARGEFLOATS = vec3( 635.298681, 682.357502, 668.926525 );
const vec3 ZINC = vec3( 48.500388, 65.294118, 63.934599 );
// truncate the domain
gridcell.xyz = gridcell.xyz - floor(gridcell.xyz * ( 1.0 / DOMAIN )) * DOMAIN;
vec3 gridcell_inc1 = step( gridcell, vec3( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
// compute x*x*y*y for the 4 corners
vec4 P = vec4( gridcell.xy, gridcell_inc1.xy ) + OFFSET.xyxy;
P *= P;
vec4 V1xy_V2xy = mix( P.xyxy, P.zwzw, vec4( v1_mask.xy, v2_mask.xy ) ); // apply mask for v1 and v2
P = vec4( P.x, V1xy_V2xy.xz, P.z ) * vec4( P.y, V1xy_V2xy.yw, P.w );
// get the lowz and highz mods
vec3 lowz_mods = vec3( 1.0 / ( SOMELARGEFLOATS.xyz + gridcell.zzz * ZINC.xyz ) );
vec3 highz_mods = vec3( 1.0 / ( SOMELARGEFLOATS.xyz + gridcell_inc1.zzz * ZINC.xyz ) );
// apply mask for v1 and v2 mod values
v1_mask = ( v1_mask.z < 0.5 ) ? lowz_mods : highz_mods;
v2_mask = ( v2_mask.z < 0.5 ) ? lowz_mods : highz_mods;
// compute the final hash
hash_0 = fract( P * vec4( lowz_mods.x, v1_mask.x, v2_mask.x, highz_mods.x ) );
hash_1 = fract( P * vec4( lowz_mods.y, v1_mask.y, v2_mask.y, highz_mods.y ) );
hash_2 = fract( P * vec4( lowz_mods.z, v1_mask.z, v2_mask.z, highz_mods.z ) );
}
vec4 FAST32_hash_3D( vec3 gridcell,
vec3 v1_mask, // user definable v1 and v2. ( 0's and 1's )
vec3 v2_mask ) // generates 1 random number for each of the 4 3D cell corners. cell corners: v0=0,0,0 v3=1,1,1 the other two are user definable
{
// gridcell is assumed to be an integer coordinate
// TODO: these constants need tweaked to find the best possible noise.
// probably requires some kind of brute force computational searching or something....
const vec2 OFFSET = vec2( 50.0, 161.0 );
const float DOMAIN = 69.0;
const float SOMELARGEFLOAT = 635.298681;
const float ZINC = 48.500388;
// truncate the domain
gridcell.xyz = gridcell.xyz - floor(gridcell.xyz * ( 1.0 / DOMAIN )) * DOMAIN;
vec3 gridcell_inc1 = step( gridcell, vec3( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
// compute x*x*y*y for the 4 corners
vec4 P = vec4( gridcell.xy, gridcell_inc1.xy ) + OFFSET.xyxy;
P *= P;
vec4 V1xy_V2xy = mix( P.xyxy, P.zwzw, vec4( v1_mask.xy, v2_mask.xy ) ); // apply mask for v1 and v2
P = vec4( P.x, V1xy_V2xy.xz, P.z ) * vec4( P.y, V1xy_V2xy.yw, P.w );
// get the z mod vals
vec2 V1z_V2z = vec2( v1_mask.z < 0.5 ? gridcell.z : gridcell_inc1.z, v2_mask.z < 0.5 ? gridcell.z : gridcell_inc1.z );
vec4 mod_vals = vec4( 1.0 / ( SOMELARGEFLOAT + vec4( gridcell.z, V1z_V2z, gridcell_inc1.z ) * ZINC ) );
// compute the final hash
return fract( P * mod_vals );
}
void FAST32_hash_3D( vec3 gridcell,
out vec4 lowz_hash_0,
out vec4 lowz_hash_1,
out vec4 lowz_hash_2,
out vec4 highz_hash_0,
out vec4 highz_hash_1,
out vec4 highz_hash_2 ) // generates 3 random numbers for each of the 8 cell corners
{
// gridcell is assumed to be an integer coordinate
// TODO: these constants need tweaked to find the best possible noise.
// probably requires some kind of brute force computational searching or something....
const vec2 OFFSET = vec2( 50.0, 161.0 );
const float DOMAIN = 69.0;
const vec3 SOMELARGEFLOATS = vec3( 635.298681, 682.357502, 668.926525 );
const vec3 ZINC = vec3( 48.500388, 65.294118, 63.934599 );
// truncate the domain
gridcell.xyz = gridcell.xyz - floor(gridcell.xyz * ( 1.0 / DOMAIN )) * DOMAIN;
vec3 gridcell_inc1 = step( gridcell, vec3( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
// calculate the noise
vec4 P = vec4( gridcell.xy, gridcell_inc1.xy ) + OFFSET.xyxy;
P *= P;
P = P.xzxz * P.yyww;
vec3 lowz_mod = vec3( 1.0 / ( SOMELARGEFLOATS.xyz + gridcell.zzz * ZINC.xyz ) );
vec3 highz_mod = vec3( 1.0 / ( SOMELARGEFLOATS.xyz + gridcell_inc1.zzz * ZINC.xyz ) );
lowz_hash_0 = fract( P * lowz_mod.xxxx );
highz_hash_0 = fract( P * highz_mod.xxxx );
lowz_hash_1 = fract( P * lowz_mod.yyyy );
highz_hash_1 = fract( P * highz_mod.yyyy );
lowz_hash_2 = fract( P * lowz_mod.zzzz );
highz_hash_2 = fract( P * highz_mod.zzzz );
}
void FAST32_hash_3D( vec3 gridcell,
out vec4 lowz_hash_0,
out vec4 lowz_hash_1,
out vec4 lowz_hash_2,
out vec4 lowz_hash_3,
out vec4 highz_hash_0,
out vec4 highz_hash_1,
out vec4 highz_hash_2,
out vec4 highz_hash_3 ) // generates 4 random numbers for each of the 8 cell corners
{
// gridcell is assumed to be an integer coordinate
// TODO: these constants need tweaked to find the best possible noise.
// probably requires some kind of brute force computational searching or something....
const vec2 OFFSET = vec2( 50.0, 161.0 );
const float DOMAIN = 69.0;
const vec4 SOMELARGEFLOATS = vec4( 635.298681, 682.357502, 668.926525, 588.255119 );
const vec4 ZINC = vec4( 48.500388, 65.294118, 63.934599, 63.279683 );
// truncate the domain
gridcell.xyz = gridcell.xyz - floor(gridcell.xyz * ( 1.0 / DOMAIN )) * DOMAIN;
vec3 gridcell_inc1 = step( gridcell, vec3( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
// calculate the noise
vec4 P = vec4( gridcell.xy, gridcell_inc1.xy ) + OFFSET.xyxy;
P *= P;
P = P.xzxz * P.yyww;
lowz_hash_3.xyzw = vec4( 1.0 / ( SOMELARGEFLOATS.xyzw + gridcell.zzzz * ZINC.xyzw ) );
highz_hash_3.xyzw = vec4( 1.0 / ( SOMELARGEFLOATS.xyzw + gridcell_inc1.zzzz * ZINC.xyzw ) );
lowz_hash_0 = fract( P * lowz_hash_3.xxxx );
highz_hash_0 = fract( P * highz_hash_3.xxxx );
lowz_hash_1 = fract( P * lowz_hash_3.yyyy );
highz_hash_1 = fract( P * highz_hash_3.yyyy );
lowz_hash_2 = fract( P * lowz_hash_3.zzzz );
highz_hash_2 = fract( P * highz_hash_3.zzzz );
lowz_hash_3 = fract( P * lowz_hash_3.wwww );
highz_hash_3 = fract( P * highz_hash_3.wwww );
}
//
// FastHash32_2
//
// An alternative to FastHash32
// - slightly slower
// - can have a larger domain
// - allows for a 4D implementation
//
// (eg)4D is computed like so....
// coord = mod( coord, DOMAIN );
// coord = ( coord * SCALE ) + OFFSET;
// coord *= coord;
// hash = mod( coord.x * coord.y * coord.z * coord.w, SOMELARGEFLOAT ) / SOMELARGEFLOAT;
//
vec4 FAST32_2_hash_2D( vec2 gridcell ) // generates a random number for each of the 4 cell corners
{
// gridcell is assumed to be an integer coordinate
const vec2 OFFSET = vec2( 403.839172, 377.242706 );
const float DOMAIN = 69.0; // NOTE: this can most likely be extended with some tweaking of the other parameters
const float SOMELARGEFLOAT = 32745.708984;
const vec2 SCALE = vec2( 2.009842, 1.372549 );
vec4 P = vec4( gridcell.xy, gridcell.xy + 1.0 );
P = P - floor(P * ( 1.0 / DOMAIN )) * DOMAIN;
P = ( P * SCALE.xyxy ) + OFFSET.xyxy;
P *= P;
return fract( P.xzxz * P.yyww * ( 1.0 / SOMELARGEFLOAT ) );
}
void FAST32_2_hash_3D( vec3 gridcell,
out vec4 z0_hash, // vec4 == ( x0y0, x1y0, x0y1, x1y1 )
out vec4 z1_hash ) // generates a random number for each of the 8 cell corners
{
// gridcell is assumed to be an integer coordinate
const vec3 OFFSET = vec3( 55.882355, 63.167774, 52.941177 );
const float DOMAIN = 69.0; // NOTE: this can most likely be extended with some tweaking of the other parameters
const float SOMELARGEFLOAT = 69412.070313;
const vec3 SCALE = vec3( 0.235142, 0.205890, 0.216449 );
// truncate the domain
gridcell = gridcell - floor(gridcell * ( 1.0 / DOMAIN )) * DOMAIN;
vec3 gridcell_inc1 = step( gridcell, vec3( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
// calculate the noise
gridcell = ( gridcell * SCALE ) + OFFSET;
gridcell_inc1 = ( gridcell_inc1 * SCALE ) + OFFSET;
gridcell *= gridcell;
gridcell_inc1 *= gridcell_inc1;
vec4 x0y0_x1y0_x0y1_x1y1 = vec4( gridcell.x, gridcell_inc1.x, gridcell.x, gridcell_inc1.x ) * vec4( gridcell.yy, gridcell_inc1.yy );
z0_hash = fract( x0y0_x1y0_x0y1_x1y1 * gridcell.zzzz * ( 1.0 / SOMELARGEFLOAT ) );
z1_hash = fract( x0y0_x1y0_x0y1_x1y1 * gridcell_inc1.zzzz * ( 1.0 / SOMELARGEFLOAT ) );
}
void FAST32_2_hash_4D( vec4 gridcell,
out vec4 z0w0_hash, // vec4 == ( x0y0, x1y0, x0y1, x1y1 )
out vec4 z1w0_hash,
out vec4 z0w1_hash,
out vec4 z1w1_hash )
{
// gridcell is assumed to be an integer coordinate
// TODO: these constants need tweaked to find the best possible noise.
// probably requires some kind of brute force computational searching or something....
const vec4 OFFSET = vec4( 16.841230, 18.774548, 16.873274, 13.664607 );
const float DOMAIN = 69.0;
const float SOMELARGEFLOAT = 47165.636719;
const vec4 SCALE = vec4( 0.102007, 0.114473, 0.139651, 0.084550 );
// truncate the domain
gridcell = gridcell - floor(gridcell * ( 1.0 / DOMAIN )) * DOMAIN;
vec4 gridcell_inc1 = step( gridcell, vec4( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
// calculate the noise
gridcell = ( gridcell * SCALE ) + OFFSET;
gridcell_inc1 = ( gridcell_inc1 * SCALE ) + OFFSET;
gridcell *= gridcell;
gridcell_inc1 *= gridcell_inc1;
vec4 x0y0_x1y0_x0y1_x1y1 = vec4( gridcell.x, gridcell_inc1.x, gridcell.x, gridcell_inc1.x ) * vec4( gridcell.yy, gridcell_inc1.yy );
vec4 z0w0_z1w0_z0w1_z1w1 = vec4( gridcell.z, gridcell_inc1.z, gridcell.z, gridcell_inc1.z ) * vec4( gridcell.ww, gridcell_inc1.ww ) * ( 1.0 / SOMELARGEFLOAT );
z0w0_hash = fract( x0y0_x1y0_x0y1_x1y1 * z0w0_z1w0_z0w1_z1w1.xxxx );
z1w0_hash = fract( x0y0_x1y0_x0y1_x1y1 * z0w0_z1w0_z0w1_z1w1.yyyy );
z0w1_hash = fract( x0y0_x1y0_x0y1_x1y1 * z0w0_z1w0_z0w1_z1w1.zzzz );
z1w1_hash = fract( x0y0_x1y0_x0y1_x1y1 * z0w0_z1w0_z0w1_z1w1.wwww );
}
void FAST32_2_hash_4D( vec4 gridcell,
out vec4 z0w0_hash_0, // vec4 == ( x0y0, x1y0, x0y1, x1y1 )
out vec4 z0w0_hash_1,
out vec4 z0w0_hash_2,
out vec4 z0w0_hash_3,
out vec4 z1w0_hash_0,
out vec4 z1w0_hash_1,
out vec4 z1w0_hash_2,
out vec4 z1w0_hash_3,
out vec4 z0w1_hash_0,
out vec4 z0w1_hash_1,
out vec4 z0w1_hash_2,
out vec4 z0w1_hash_3,
out vec4 z1w1_hash_0,
out vec4 z1w1_hash_1,
out vec4 z1w1_hash_2,
out vec4 z1w1_hash_3 )
{
// gridcell is assumed to be an integer coordinate
// TODO: these constants need tweaked to find the best possible noise.
// probably requires some kind of brute force computational searching or something....
const vec4 OFFSET = vec4( 16.841230, 18.774548, 16.873274, 13.664607 );
const float DOMAIN = 69.0;
const vec4 SOMELARGEFLOATS = vec4( 56974.746094, 47165.636719, 55049.667969, 49901.273438 );
const vec4 SCALE = vec4( 0.102007, 0.114473, 0.139651, 0.084550 );
// truncate the domain
gridcell = gridcell - floor(gridcell * ( 1.0 / DOMAIN )) * DOMAIN;
vec4 gridcell_inc1 = step( gridcell, vec4( DOMAIN - 1.5 ) ) * ( gridcell + 1.0 );
// calculate the noise
gridcell = ( gridcell * SCALE ) + OFFSET;
gridcell_inc1 = ( gridcell_inc1 * SCALE ) + OFFSET;
gridcell *= gridcell;
gridcell_inc1 *= gridcell_inc1;
vec4 x0y0_x1y0_x0y1_x1y1 = vec4( gridcell.x, gridcell_inc1.x, gridcell.x, gridcell_inc1.x ) * vec4( gridcell.yy, gridcell_inc1.yy );
vec4 z0w0_z1w0_z0w1_z1w1 = vec4( gridcell.z, gridcell_inc1.z, gridcell.z, gridcell_inc1.z ) * vec4( gridcell.ww, gridcell_inc1.ww );
vec4 hashval = x0y0_x1y0_x0y1_x1y1 * z0w0_z1w0_z0w1_z1w1.xxxx;
z0w0_hash_0 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.x ) );
z0w0_hash_1 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.y ) );
z0w0_hash_2 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.z ) );
z0w0_hash_3 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.w ) );
hashval = x0y0_x1y0_x0y1_x1y1 * z0w0_z1w0_z0w1_z1w1.yyyy;
z1w0_hash_0 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.x ) );
z1w0_hash_1 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.y ) );
z1w0_hash_2 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.z ) );
z1w0_hash_3 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.w ) );
hashval = x0y0_x1y0_x0y1_x1y1 * z0w0_z1w0_z0w1_z1w1.zzzz;
z0w1_hash_0 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.x ) );
z0w1_hash_1 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.y ) );
z0w1_hash_2 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.z ) );
z0w1_hash_3 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.w ) );
hashval = x0y0_x1y0_x0y1_x1y1 * z0w0_z1w0_z0w1_z1w1.wwww;
z1w1_hash_0 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.x ) );
z1w1_hash_1 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.y ) );
z1w1_hash_2 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.z ) );
z1w1_hash_3 = fract( hashval * ( 1.0 / SOMELARGEFLOATS.w ) );
}
//
// Interpolation functions
// ( smoothly increase from 0.0 to 1.0 as x increases linearly from 0.0 to 1.0 )
// http://briansharpe.wordpress.com/2011/11/14/two-useful-interpolation-functions-for-noise-development/
//
float Interpolation_C1( float x ) { return x * x * (3.0 - 2.0 * x); } // 3x^2-2x^3 ( Hermine Curve. Same as SmoothStep(). As used by Perlin in Original Noise. )
vec2 Interpolation_C1( vec2 x ) { return x * x * (3.0 - 2.0 * x); }
vec3 Interpolation_C1( vec3 x ) { return x * x * (3.0 - 2.0 * x); }
vec4 Interpolation_C1( vec4 x ) { return x * x * (3.0 - 2.0 * x); }
float Interpolation_C2( float x ) { return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); } // 6x^5-15x^4+10x^3 ( Quintic Curve. As used by Perlin in Improved Noise. http://mrl.nyu.edu/~perlin/paper445.pdf )
vec2 Interpolation_C2( vec2 x ) { return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); }
vec3 Interpolation_C2( vec3 x ) { return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); }
vec4 Interpolation_C2( vec4 x ) { return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); }
vec4 Interpolation_C2_InterpAndDeriv( vec2 x ) { return x.xyxy * x.xyxy * ( x.xyxy * ( x.xyxy * ( x.xyxy * vec2( 6.0, 0.0 ).xxyy + vec2( -15.0, 30.0 ).xxyy ) + vec2( 10.0, -60.0 ).xxyy ) + vec2( 0.0, 30.0 ).xxyy ); }
vec3 Interpolation_C2_Deriv( vec3 x ) { return x * x * (x * (x * 30.0 - 60.0) + 30.0); }
float Interpolation_C2_Fast( float x ) { float x3 = x*x*x; return ( 7.0 + ( x3 - 7.0 ) * x ) * x3; } // 7x^3-7x^4+x^7 ( Faster than Perlin Quintic. Not quite as good shape. )
vec2 Interpolation_C2_Fast( vec2 x ) { vec2 x3 = x*x*x; return ( 7.0 + ( x3 - 7.0 ) * x ) * x3; }
vec3 Interpolation_C2_Fast( vec3 x ) { vec3 x3 = x*x*x; return ( 7.0 + ( x3 - 7.0 ) * x ) * x3; }
vec4 Interpolation_C2_Fast( vec4 x ) { vec4 x3 = x*x*x; return ( 7.0 + ( x3 - 7.0 ) * x ) * x3; }
float Interpolation_C3( float x ) { float xsq = x*x; float xsqsq = xsq*xsq; return xsqsq * ( 25.0 - 48.0 * x + xsq * ( 25.0 - xsqsq ) ); } // 25x^4-48x^5+25x^6-x^10 ( C3 Interpolation function. If anyone ever needs it... :) )
vec2 Interpolation_C3( vec2 x ) { vec2 xsq = x*x; vec2 xsqsq = xsq*xsq; return xsqsq * ( 25.0 - 48.0 * x + xsq * ( 25.0 - xsqsq ) ); }
vec3 Interpolation_C3( vec3 x ) { vec3 xsq = x*x; vec3 xsqsq = xsq*xsq; return xsqsq * ( 25.0 - 48.0 * x + xsq * ( 25.0 - xsqsq ) ); }
vec4 Interpolation_C3( vec4 x ) { vec4 xsq = x*x; vec4 xsqsq = xsq*xsq; return xsqsq * ( 25.0 - 48.0 * x + xsq * ( 25.0 - xsqsq ) ); }
//
// Falloff defined in XSquared
// ( smoothly decrease from 1.0 to 0.0 as xsq increases from 0.0 to 1.0 )
// http://briansharpe.wordpress.com/2011/11/14/two-useful-interpolation-functions-for-noise-development/
//
float Falloff_Xsq_C1( float xsq ) { xsq = 1.0 - xsq; return xsq*xsq; } // ( 1.0 - x*x )^2 ( Used by Humus for lighting falloff in Just Cause 2. GPUPro 1 )
float Falloff_Xsq_C2( float xsq ) { xsq = 1.0 - xsq; return xsq*xsq*xsq; } // ( 1.0 - x*x )^3. NOTE: 2nd derivative is 0.0 at x=1.0, but non-zero at x=0.0
vec4 Falloff_Xsq_C2( vec4 xsq ) { xsq = 1.0 - xsq; return xsq*xsq*xsq; }
//
// Value Noise 2D
// Return value range of 0.0->1.0
// http://briansharpe.files.wordpress.com/2011/11/valuesample1.jpg
//
float Value2D( vec2 P )
{
// establish our grid cell and unit position
vec2 Pi = floor(P);
vec2 Pf = P - Pi;
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hash = FAST32_hash_2D( Pi );
//vec4 hash = FAST32_2_hash_2D( Pi );
//vec4 hash = BBS_hash_2D( Pi );
//vec4 hash = SGPP_hash_2D( Pi );
//vec4 hash = BBS_hash_hq_2D( Pi );
// blend the results and return
vec2 blend = Interpolation_C2( Pf );
vec4 blend2 = vec4( blend, vec2( 1.0 - blend ) );
return dot( hash, blend2.zxzx * blend2.wwyy );
}
//
// Value Noise 3D
// Return value range of 0.0->1.0
// http://briansharpe.files.wordpress.com/2011/11/valuesample1.jpg
//
float Value3D( vec3 P )
{
// establish our grid cell and unit position
vec3 Pi = floor(P);
vec3 Pf = P - Pi;
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hash_lowz, hash_highz;
FAST32_hash_3D( Pi, hash_lowz, hash_highz );
//FAST32_2_hash_3D( Pi, hash_lowz, hash_highz );
//BBS_hash_3D( Pi, hash_lowz, hash_highz );
//SGPP_hash_3D( Pi, hash_lowz, hash_highz );
// blend the results and return
vec3 blend = Interpolation_C2( Pf );
vec4 res0 = mix( hash_lowz, hash_highz, blend.z );
vec4 blend2 = vec4( blend.xy, vec2( 1.0 - blend.xy ) );
return dot( res0, blend2.zxzx * blend2.wwyy );
}
//
// Value Noise 4D
// Return value range of 0.0->1.0
//
float Value4D( vec4 P )
{
// establish our grid cell and unit position
vec4 Pi = floor(P);
vec4 Pf = P - Pi;
// calculate the hash.
vec4 z0w0_hash; // vec4 == ( x0y0, x1y0, x0y1, x1y1 )
vec4 z1w0_hash;
vec4 z0w1_hash;
vec4 z1w1_hash;
FAST32_2_hash_4D( Pi, z0w0_hash, z1w0_hash, z0w1_hash, z1w1_hash );
// blend the results and return
vec4 blend = Interpolation_C2( Pf );
vec4 res0 = z0w0_hash + ( z0w1_hash - z0w0_hash ) * blend.wwww;
vec4 res1 = z1w0_hash + ( z1w1_hash - z1w0_hash ) * blend.wwww;
res0 = res0 + ( res1 - res0 ) * blend.zzzz;
blend.zw = vec2( 1.0 - blend.xy );
return dot( res0, blend.zxzx * blend.wwyy );
}
//
// Perlin Noise 2D ( gradient noise )
// Return value range of -1.0->1.0
// http://briansharpe.files.wordpress.com/2011/11/perlinsample.jpg
//
float Perlin2D( vec2 P )
{
// establish our grid cell and unit position
vec2 Pi = floor(P);
vec4 Pf_Pfmin1 = P.xyxy - vec4( Pi, Pi + 1.0 );
#if 1
//
// classic noise looks much better than improved noise in 2D, and with an efficent hash function runs at about the same speed.
// requires 2 random numbers per point.
//
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hash_x, hash_y;
FAST32_hash_2D( Pi, hash_x, hash_y );
//SGPP_hash_2D( Pi, hash_x, hash_y );
// calculate the gradient results
vec4 grad_x = hash_x - 0.49999;
vec4 grad_y = hash_y - 0.49999;
vec4 grad_results = inversesqrt( grad_x * grad_x + grad_y * grad_y ) * ( grad_x * Pf_Pfmin1.xzxz + grad_y * Pf_Pfmin1.yyww );
#if 1
// Classic Perlin Interpolation
grad_results *= 1.4142135623730950488016887242097; // (optionally) scale things to a strict -1.0->1.0 range *= 1.0/sqrt(0.5)
vec2 blend = Interpolation_C2( Pf_Pfmin1.xy );
vec4 blend2 = vec4( blend, vec2( 1.0 - blend ) );
return dot( grad_results, blend2.zxzx * blend2.wwyy );
#else
// Classic Perlin Surflet
// http://briansharpe.wordpress.com/2012/03/09/modifications-to-classic-perlin-noise/
grad_results *= 2.3703703703703703703703703703704; // (optionally) scale things to a strict -1.0->1.0 range *= 1.0/cube(0.75)
vec4 vecs_len_sq = Pf_Pfmin1 * Pf_Pfmin1;
vecs_len_sq = vecs_len_sq.xzxz + vecs_len_sq.yyww;
return dot( Falloff_Xsq_C2( min( vec4( 1.0 ), vecs_len_sq ) ), grad_results );
#endif
#else
//
// 2D improved perlin noise.
// requires 1 random value per point.
// does not look as good as classic in 2D due to only a small number of possible cell types. But can run a lot faster than classic perlin noise if the hash function is slow
//
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hash = FAST32_hash_2D( Pi );
//vec4 hash = BBS_hash_2D( Pi );
//vec4 hash = SGPP_hash_2D( Pi );
//vec4 hash = BBS_hash_hq_2D( Pi );
//
// evaulate the gradients
// choose between the 4 diagonal gradients. ( slightly slower than choosing the axis gradients, but shows less grid artifacts )
// NOTE: diagonals give us a nice strict -1.0->1.0 range without additional scaling
// [1.0,1.0] [-1.0,1.0] [1.0,-1.0] [-1.0,-1.0]
//
hash -= 0.5;
vec4 grad_results = Pf_Pfmin1.xzxz * sign( hash ) + Pf_Pfmin1.yyww * sign( abs( hash ) - 0.25 );
// blend the results and return
vec2 blend = Interpolation_C2( Pf_Pfmin1.xy );
vec4 blend2 = vec4( blend, vec2( 1.0 - blend ) );
return dot( grad_results, blend2.zxzx * blend2.wwyy );
#endif
}
//
// Perlin Noise 3D ( gradient noise )
// Return value range of -1.0->1.0
// http://briansharpe.files.wordpress.com/2011/11/perlinsample.jpg
//
float Perlin3D( vec3 P )
{
// establish our grid cell and unit position
vec3 Pi = floor(P);
vec3 Pf = P - Pi;
vec3 Pf_min1 = Pf - 1.0;
#if 1
//
// classic noise.
// requires 3 random values per point. with an efficent hash function will run faster than improved noise
//
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hashx0, hashy0, hashz0, hashx1, hashy1, hashz1;
FAST32_hash_3D( Pi, hashx0, hashy0, hashz0, hashx1, hashy1, hashz1 );
//SGPP_hash_3D( Pi, hashx0, hashy0, hashz0, hashx1, hashy1, hashz1 );
// calculate the gradients
vec4 grad_x0 = hashx0 - 0.49999;
vec4 grad_y0 = hashy0 - 0.49999;
vec4 grad_z0 = hashz0 - 0.49999;
vec4 grad_x1 = hashx1 - 0.49999;
vec4 grad_y1 = hashy1 - 0.49999;
vec4 grad_z1 = hashz1 - 0.49999;
vec4 grad_results_0 = inversesqrt( grad_x0 * grad_x0 + grad_y0 * grad_y0 + grad_z0 * grad_z0 ) * ( vec2( Pf.x, Pf_min1.x ).xyxy * grad_x0 + vec2( Pf.y, Pf_min1.y ).xxyy * grad_y0 + Pf.zzzz * grad_z0 );
vec4 grad_results_1 = inversesqrt( grad_x1 * grad_x1 + grad_y1 * grad_y1 + grad_z1 * grad_z1 ) * ( vec2( Pf.x, Pf_min1.x ).xyxy * grad_x1 + vec2( Pf.y, Pf_min1.y ).xxyy * grad_y1 + Pf_min1.zzzz * grad_z1 );
#if 1
// Classic Perlin Interpolation
vec3 blend = Interpolation_C2( Pf );
vec4 res0 = mix( grad_results_0, grad_results_1, blend.z );
vec4 blend2 = vec4( blend.xy, vec2( 1.0 - blend.xy ) );
float final = dot( res0, blend2.zxzx * blend2.wwyy );
final *= 1.1547005383792515290182975610039; // (optionally) scale things to a strict -1.0->1.0 range *= 1.0/sqrt(0.75)
return final;
#else
// Classic Perlin Surflet
// http://briansharpe.wordpress.com/2012/03/09/modifications-to-classic-perlin-noise/
Pf *= Pf;
Pf_min1 *= Pf_min1;
vec4 vecs_len_sq = vec4( Pf.x, Pf_min1.x, Pf.x, Pf_min1.x ) + vec4( Pf.yy, Pf_min1.yy );
float final = dot( Falloff_Xsq_C2( min( vec4( 1.0 ), vecs_len_sq + Pf.zzzz ) ), grad_results_0 ) + dot( Falloff_Xsq_C2( min( vec4( 1.0 ), vecs_len_sq + Pf_min1.zzzz ) ), grad_results_1 );
final *= 2.3703703703703703703703703703704; // (optionally) scale things to a strict -1.0->1.0 range *= 1.0/cube(0.75)
return final;
#endif
#else
//
// improved noise.
// requires 1 random value per point. Will run faster than classic noise if a slow hashing function is used
//
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hash_lowz, hash_highz;
FAST32_hash_3D( Pi, hash_lowz, hash_highz );
//BBS_hash_3D( Pi, hash_lowz, hash_highz );
//SGPP_hash_3D( Pi, hash_lowz, hash_highz );
//
// "improved" noise using 8 corner gradients. Faster than the 12 mid-edge point method.
// Ken mentions using diagonals like this can cause "clumping", but we'll live with that.
// [1,1,1] [-1,1,1] [1,-1,1] [-1,-1,1]
// [1,1,-1] [-1,1,-1] [1,-1,-1] [-1,-1,-1]
//
hash_lowz -= 0.5;
vec4 grad_results_0_0 = vec2( Pf.x, Pf_min1.x ).xyxy * sign( hash_lowz );
hash_lowz = abs( hash_lowz ) - 0.25;
vec4 grad_results_0_1 = vec2( Pf.y, Pf_min1.y ).xxyy * sign( hash_lowz );
vec4 grad_results_0_2 = Pf.zzzz * sign( abs( hash_lowz ) - 0.125 );
vec4 grad_results_0 = grad_results_0_0 + grad_results_0_1 + grad_results_0_2;
hash_highz -= 0.5;
vec4 grad_results_1_0 = vec2( Pf.x, Pf_min1.x ).xyxy * sign( hash_highz );
hash_highz = abs( hash_highz ) - 0.25;
vec4 grad_results_1_1 = vec2( Pf.y, Pf_min1.y ).xxyy * sign( hash_highz );
vec4 grad_results_1_2 = Pf_min1.zzzz * sign( abs( hash_highz ) - 0.125 );
vec4 grad_results_1 = grad_results_1_0 + grad_results_1_1 + grad_results_1_2;
// blend the gradients and return
vec3 blend = Interpolation_C2( Pf );
vec4 res0 = mix( grad_results_0, grad_results_1, blend.z );
vec4 blend2 = vec4( blend.xy, vec2( 1.0 - blend.xy ) );
return dot( res0, blend2.zxzx * blend2.wwyy ) * (2.0 / 3.0); // (optionally) mult by (2.0/3.0) to scale to a strict -1.0->1.0 range
#endif
}
//
// Perlin Noise 4D ( gradient noise )
// Return value range of -1.0->1.0
//
float Perlin4D( vec4 P )
{
// establish our grid cell and unit position
vec4 Pi = floor(P);
vec4 Pf = P - Pi;
vec4 Pf_min1 = Pf - 1.0;
// calculate the hash.
vec4 lowz_loww_hash_0, lowz_loww_hash_1, lowz_loww_hash_2, lowz_loww_hash_3;
vec4 highz_loww_hash_0, highz_loww_hash_1, highz_loww_hash_2, highz_loww_hash_3;
vec4 lowz_highw_hash_0, lowz_highw_hash_1, lowz_highw_hash_2, lowz_highw_hash_3;
vec4 highz_highw_hash_0, highz_highw_hash_1, highz_highw_hash_2, highz_highw_hash_3;
FAST32_2_hash_4D(
Pi,
lowz_loww_hash_0, lowz_loww_hash_1, lowz_loww_hash_2, lowz_loww_hash_3,
highz_loww_hash_0, highz_loww_hash_1, highz_loww_hash_2, highz_loww_hash_3,
lowz_highw_hash_0, lowz_highw_hash_1, lowz_highw_hash_2, lowz_highw_hash_3,
highz_highw_hash_0, highz_highw_hash_1, highz_highw_hash_2, highz_highw_hash_3 );
// calculate the gradients
lowz_loww_hash_0 -= 0.49999;
lowz_loww_hash_1 -= 0.49999;
lowz_loww_hash_2 -= 0.49999;
lowz_loww_hash_3 -= 0.49999;
highz_loww_hash_0 -= 0.49999;
highz_loww_hash_1 -= 0.49999;
highz_loww_hash_2 -= 0.49999;
highz_loww_hash_3 -= 0.49999;
lowz_highw_hash_0 -= 0.49999;
lowz_highw_hash_1 -= 0.49999;
lowz_highw_hash_2 -= 0.49999;
lowz_highw_hash_3 -= 0.49999;
highz_highw_hash_0 -= 0.49999;
highz_highw_hash_1 -= 0.49999;
highz_highw_hash_2 -= 0.49999;
highz_highw_hash_3 -= 0.49999;
vec4 grad_results_lowz_loww = inversesqrt( lowz_loww_hash_0 * lowz_loww_hash_0 + lowz_loww_hash_1 * lowz_loww_hash_1 + lowz_loww_hash_2 * lowz_loww_hash_2 + lowz_loww_hash_3 * lowz_loww_hash_3 );
grad_results_lowz_loww *= ( vec2( Pf.x, Pf_min1.x ).xyxy * lowz_loww_hash_0 + vec2( Pf.y, Pf_min1.y ).xxyy * lowz_loww_hash_1 + Pf.zzzz * lowz_loww_hash_2 + Pf.wwww * lowz_loww_hash_3 );
vec4 grad_results_highz_loww = inversesqrt( highz_loww_hash_0 * highz_loww_hash_0 + highz_loww_hash_1 * highz_loww_hash_1 + highz_loww_hash_2 * highz_loww_hash_2 + highz_loww_hash_3 * highz_loww_hash_3 );
grad_results_highz_loww *= ( vec2( Pf.x, Pf_min1.x ).xyxy * highz_loww_hash_0 + vec2( Pf.y, Pf_min1.y ).xxyy * highz_loww_hash_1 + Pf_min1.zzzz * highz_loww_hash_2 + Pf.wwww * highz_loww_hash_3 );
vec4 grad_results_lowz_highw = inversesqrt( lowz_highw_hash_0 * lowz_highw_hash_0 + lowz_highw_hash_1 * lowz_highw_hash_1 + lowz_highw_hash_2 * lowz_highw_hash_2 + lowz_highw_hash_3 * lowz_highw_hash_3 );
grad_results_lowz_highw *= ( vec2( Pf.x, Pf_min1.x ).xyxy * lowz_highw_hash_0 + vec2( Pf.y, Pf_min1.y ).xxyy * lowz_highw_hash_1 + Pf.zzzz * lowz_highw_hash_2 + Pf_min1.wwww * lowz_highw_hash_3 );
vec4 grad_results_highz_highw = inversesqrt( highz_highw_hash_0 * highz_highw_hash_0 + highz_highw_hash_1 * highz_highw_hash_1 + highz_highw_hash_2 * highz_highw_hash_2 + highz_highw_hash_3 * highz_highw_hash_3 );
grad_results_highz_highw *= ( vec2( Pf.x, Pf_min1.x ).xyxy * highz_highw_hash_0 + vec2( Pf.y, Pf_min1.y ).xxyy * highz_highw_hash_1 + Pf_min1.zzzz * highz_highw_hash_2 + Pf_min1.wwww * highz_highw_hash_3 );
// Classic Perlin Interpolation
vec4 blend = Interpolation_C2( Pf );
vec4 res0 = grad_results_lowz_loww + ( grad_results_lowz_highw - grad_results_lowz_loww ) * blend.wwww;
vec4 res1 = grad_results_highz_loww + ( grad_results_highz_highw - grad_results_highz_loww ) * blend.wwww;
res0 = res0 + ( res1 - res0 ) * blend.zzzz;
blend.zw = vec2( 1.0 ) - blend.xy;
return dot( res0, blend.zxzx * blend.wwyy );
}
//
// ValuePerlin Noise 2D ( value gradient noise )
// A uniform blend between value and perlin noise
// Return value range of -1.0->1.0
// http://briansharpe.files.wordpress.com/2011/11/valueperlinsample.jpg
//
float ValuePerlin2D( vec2 P, float blend_val )
{
// establish our grid cell and unit position
vec2 Pi = floor(P);
vec4 Pf_Pfmin1 = P.xyxy - vec4( Pi, Pi + 1.0 );
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hash_value, hash_x, hash_y;
FAST32_hash_2D( Pi, hash_value, hash_x, hash_y );
// calculate the gradient results
vec4 grad_x = hash_x - 0.49999;
vec4 grad_y = hash_y - 0.49999;
vec4 grad_results = inversesqrt( grad_x * grad_x + grad_y * grad_y ) * ( grad_x * Pf_Pfmin1.xzxz + grad_y * Pf_Pfmin1.yyww );
grad_results *= 1.4142135623730950488016887242097; // scale the perlin component to a -1.0->1.0 range *= 1.0/sqrt(0.5)
grad_results = mix( (hash_value * 2.0 - 1.0), grad_results, blend_val );
// blend the results and return
vec2 blend = Interpolation_C2( Pf_Pfmin1.xy );
vec4 blend2 = vec4( blend, vec2( 1.0 - blend ) );
return dot( grad_results, blend2.zxzx * blend2.wwyy );
}
//
// ValuePerlin Noise 3D ( value gradient noise )
// A uniform blend between value and perlin noise
// Return value range of -1.0->1.0
// http://briansharpe.files.wordpress.com/2011/11/valueperlinsample.jpg
//
float ValuePerlin3D( vec3 P, float blend_val )
{
// establish our grid cell and unit position
vec3 Pi = floor(P);
vec3 Pf = P - Pi;
vec3 Pf_min1 = Pf - 1.0;
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hash_value0, hashx0, hashy0, hashz0, hash_value1, hashx1, hashy1, hashz1;
FAST32_hash_3D( Pi, hash_value0, hashx0, hashy0, hashz0, hash_value1, hashx1, hashy1, hashz1 );
// calculate the gradients
vec4 grad_x0 = hashx0 - 0.49999;
vec4 grad_y0 = hashy0 - 0.49999;
vec4 grad_z0 = hashz0 - 0.49999;
vec4 grad_x1 = hashx1 - 0.49999;
vec4 grad_y1 = hashy1 - 0.49999;
vec4 grad_z1 = hashz1 - 0.49999;
vec4 grad_results_0 = inversesqrt( grad_x0 * grad_x0 + grad_y0 * grad_y0 + grad_z0 * grad_z0 ) * ( vec2( Pf.x, Pf_min1.x ).xyxy * grad_x0 + vec2( Pf.y, Pf_min1.y ).xxyy * grad_y0 + Pf.zzzz * grad_z0 );
vec4 grad_results_1 = inversesqrt( grad_x1 * grad_x1 + grad_y1 * grad_y1 + grad_z1 * grad_z1 ) * ( vec2( Pf.x, Pf_min1.x ).xyxy * grad_x1 + vec2( Pf.y, Pf_min1.y ).xxyy * grad_y1 + Pf_min1.zzzz * grad_z1 );
grad_results_0 *= 1.1547005383792515290182975610039; // scale the perlin component to a -1.0->1.0 range *= 1.0/sqrt(0.75)
grad_results_1 *= 1.1547005383792515290182975610039;
grad_results_0 = mix( (hash_value0 * 2.0 - 1.0), grad_results_0, blend_val );
grad_results_1 = mix( (hash_value1 * 2.0 - 1.0), grad_results_1, blend_val );
// blend the gradients and return
vec3 blend = Interpolation_C2( Pf );
vec4 res0 = mix( grad_results_0, grad_results_1, blend.z );
vec4 blend2 = vec4( blend.xy, vec2( 1.0 - blend.xy ) );
return dot( res0, blend2.zxzx * blend2.wwyy );
}
//
// Cubist Noise 2D
// http://briansharpe.files.wordpress.com/2011/12/cubistsample.jpg
//
// Generates a noise which resembles a cubist-style painting pattern. Final Range 0.0->1.0
// NOTE: contains discontinuities. best used only for texturing.
// NOTE: Any serious game implementation should hard-code these parameter values for efficiency.
//
float Cubist2D( vec2 P, vec2 range_clamp ) // range_clamp.x = low, range_clamp.y = 1.0/(high-low). suggest value low=-2.0 high=1.0
{
// establish our grid cell and unit position
vec2 Pi = floor(P);
vec4 Pf_Pfmin1 = P.xyxy - vec4( Pi, Pi + 1.0 );
// calculate the hash.
// ( various hashing methods listed in order of speed )
vec4 hash_x, hash_y, hash_value;
FAST32_hash_2D( Pi, hash_x, hash_y, hash_value );
// calculate the gradient results
vec4 grad_x = hash_x - 0.49999;
vec4 grad_y = hash_y - 0.49999;
vec4 grad_results = inversesqrt( grad_x * grad_x + grad_y * grad_y ) * ( grad_x * Pf_Pfmin1.xzxz + grad_y * Pf_Pfmin1.yyww );
// invert the gradient to convert from perlin to cubist
grad_results = ( hash_value - 0.5 ) * ( 1.0 / grad_results );
// blend the results and return
vec2 blend = Interpolation_C2( Pf_Pfmin1.xy );
vec4 blend2 = vec4( blend, vec2( 1.0 - blend ) );
float final = dot( grad_results, blend2.zxzx * blend2.wwyy );
// the 1.0/grad calculation pushes the result to a possible to +-infinity. Need to clamp to keep things sane
return clamp( ( final - range_clamp.x ) * range_clamp.y, 0.0, 1.0 );
//return smoothstep( 0.0, 1.0, ( final - range_clamp.x ) * range_clamp.y ); // experiments. smoothstep doesn't look as good, but does remove some discontinuities....
}
//
// Cubist Noise 3D
// http://briansharpe.files.wordpress.com/2011/12/cubistsample.jpg
//
// Generates a noise which resembles a cubist-style painting pattern. Final Range 0.0->1.0
// NOTE: contains discontinuities. best used only for texturing.