jax 0.0.0.7 → 0.0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/CHANGELOG +21 -0
  2. data/Rakefile +13 -1
  3. data/builtin/shaders/functions/noise.ejs +780 -7
  4. data/guides/partials/_top_nav.html.erb +17 -0
  5. data/guides/source/index.html.erb +7 -0
  6. data/guides/source/layout.html.erb +1 -12
  7. data/lib/jax/generators/app/templates/public/javascripts/jax.js +4 -1
  8. data/lib/jax/version.rb +1 -1
  9. data/spec/example_app/app/controllers/lighting_controller.js +0 -2
  10. data/spec/javascripts/shaders/preprocessor_spec.js +19 -4
  11. data/src/constants.yml +1 -1
  12. data/src/jax/anim_frame.js +1 -1
  13. data/src/jax/core/util.js +5 -0
  14. data/src/jax/webgl/shader.js +2 -0
  15. data/vendor/pdoc_template/html/assets/images/pdoc/alias.png +0 -0
  16. data/vendor/pdoc_template/html/assets/images/pdoc/class.png +0 -0
  17. data/vendor/pdoc_template/html/assets/images/pdoc/class_deprecated.png +0 -0
  18. data/vendor/pdoc_template/html/assets/images/pdoc/class_method.png +0 -0
  19. data/vendor/pdoc_template/html/assets/images/pdoc/class_property.png +0 -0
  20. data/vendor/pdoc_template/html/assets/images/pdoc/constant.png +0 -0
  21. data/vendor/pdoc_template/html/assets/images/pdoc/constructor.png +0 -0
  22. data/vendor/pdoc_template/html/assets/images/pdoc/deprecated.png +0 -0
  23. data/vendor/pdoc_template/html/assets/images/pdoc/description.png +0 -0
  24. data/vendor/pdoc_template/html/assets/images/pdoc/information.png +0 -0
  25. data/vendor/pdoc_template/html/assets/images/pdoc/instance_method.png +0 -0
  26. data/vendor/pdoc_template/html/assets/images/pdoc/instance_property.png +0 -0
  27. data/vendor/pdoc_template/html/assets/images/pdoc/method.png +0 -0
  28. data/vendor/pdoc_template/html/assets/images/pdoc/method_deprecated.png +0 -0
  29. data/vendor/pdoc_template/html/assets/images/pdoc/mixin.png +0 -0
  30. data/vendor/pdoc_template/html/assets/images/pdoc/namespace.png +0 -0
  31. data/vendor/pdoc_template/html/assets/images/pdoc/property.png +0 -0
  32. data/vendor/pdoc_template/html/assets/images/pdoc/related_to.png +0 -0
  33. data/vendor/pdoc_template/html/assets/images/pdoc/search-background.png +0 -0
  34. data/vendor/pdoc_template/html/assets/images/pdoc/section-background.png +0 -0
  35. data/vendor/pdoc_template/html/assets/images/pdoc/section.png +0 -0
  36. data/vendor/pdoc_template/html/assets/images/pdoc/selected-section-background.png +0 -0
  37. data/vendor/pdoc_template/html/assets/images/pdoc/subclass.png +0 -0
  38. data/vendor/pdoc_template/html/assets/images/pdoc/superclass.png +0 -0
  39. data/vendor/pdoc_template/html/assets/images/pdoc/utility.png +0 -0
  40. data/vendor/pdoc_template/html/assets/javascripts/pdoc/application.js +478 -0
  41. data/vendor/pdoc_template/html/assets/javascripts/pdoc/prototype.js +4874 -0
  42. data/vendor/pdoc_template/html/assets/javascripts/pdoc/tabs.js +506 -0
  43. data/vendor/pdoc_template/html/assets/stylesheets/jax.css +30 -0
  44. data/vendor/pdoc_template/html/assets/stylesheets/pdoc/api.css +681 -0
  45. data/vendor/pdoc_template/html/assets/stylesheets/pdoc/pygments.css +62 -0
  46. data/vendor/pdoc_template/html/helpers.rb +35 -0
  47. data/vendor/pdoc_template/html/index.erb +18 -0
  48. data/vendor/pdoc_template/html/item_index.js.erb +6 -0
  49. data/vendor/pdoc_template/html/layout.erb +67 -0
  50. data/vendor/pdoc_template/html/leaf.erb +22 -0
  51. data/vendor/pdoc_template/html/node.erb +30 -0
  52. data/vendor/pdoc_template/html/partials/class_relationships.erb +19 -0
  53. data/vendor/pdoc_template/html/partials/classes.erb +7 -0
  54. data/vendor/pdoc_template/html/partials/constructor.erb +5 -0
  55. data/vendor/pdoc_template/html/partials/description.erb +5 -0
  56. data/vendor/pdoc_template/html/partials/link_list.erb +1 -0
  57. data/vendor/pdoc_template/html/partials/method_signatures.erb +14 -0
  58. data/vendor/pdoc_template/html/partials/methodized_note.erb +9 -0
  59. data/vendor/pdoc_template/html/partials/mixins.erb +7 -0
  60. data/vendor/pdoc_template/html/partials/namespaces.erb +7 -0
  61. data/vendor/pdoc_template/html/partials/related_utilities.erb +5 -0
  62. data/vendor/pdoc_template/html/partials/relationships.erb +11 -0
  63. data/vendor/pdoc_template/html/partials/short_description_list.erb +7 -0
  64. data/vendor/pdoc_template/html/partials/title.erb +24 -0
  65. data/vendor/pdoc_template/html/section.erb +18 -0
  66. metadata +56 -4
data/CHANGELOG CHANGED
@@ -1,3 +1,24 @@
1
+ * 0.0.0.8 *
2
+
3
+ * Added +Jax.Shaders.max_vertex_textures+ which tracks the value of
4
+ GL_MAX_VERTEX_TEXTURE_UNITS so you can design shaders that Just Work.
5
+
6
+ * Exposed +shader_type+ to the EJS template so you can determine whether a shader's
7
+ source is for a vertex or fragment shader. (It's a string equal to either 'vertex'
8
+ or 'fragment'.) Useful for 'common' code, even more useful for reusable function
9
+ libraries. ;)
10
+
11
+ * Modified Jax built-in noise functions to utilize the above in order to quietly
12
+ fall back to the no-texture-lookup noise implementation by Ian McEwan:
13
+ https://github.com/ashima/webgl-noise . This implementation is considerably slower
14
+ than the VTL one (my FPS on Mac drops from ~100 to ~60 for the 'blob' demo), so
15
+ it is better used as a secondary noise than as a primary.
16
+
17
+ * Renamed the VTL 'noise' function to 'cnoise' to avoid ambiguity in programs. Use
18
+ 'cnoise' or 'snoise'.
19
+
20
+
21
+
1
22
  * 0.0.0.7 *
2
23
 
3
24
  * Fixed a bug which could cause applications to fail in production (after running
data/Rakefile CHANGED
@@ -92,8 +92,12 @@ end
92
92
  namespace :doc do
93
93
  desc "build the Jax JavaScript documentation"
94
94
  task :js do
95
+ require 'erb'
95
96
  FileUtils.rm_rf 'doc'
96
97
 
98
+ @link_to_guides = true
99
+ @hide_links_to_api_docs = true
100
+ header = ERB.new(File.read(File.expand_path("guides/partials/_top_nav.html.erb", File.dirname(__FILE__)))).result(binding)
97
101
  PDoc.run({
98
102
  :source_files => (['src/jax.js', 'vendor/glmatrix/glMatrix.js'] +
99
103
  # Dir['vendor/ejs/src/**/*.js'] +
@@ -112,6 +116,9 @@ namespace :doc do
112
116
  :short_name => 'Jax',
113
117
  :home_url => 'http://jaxgl.com',
114
118
  :version => Jax::VERSION,
119
+ :templates => "vendor/pdoc_template/html",
120
+ :header => header,
121
+ :index_header => header,
115
122
  # :index_header => "",
116
123
  # :footer => '',
117
124
  # :assets => 'doc_assets'
@@ -122,8 +129,13 @@ namespace :doc do
122
129
  end
123
130
 
124
131
  namespace :guides do
125
- task :generate do
132
+ # gen doc:js first because we're going to include a direct link to the JS API dox
133
+ task :generate => 'doc:js' do
126
134
  rm_rf "guides/output"
135
+ if !ENV["SKIP_API"]
136
+ mkdir_p "guides/output/api"
137
+ cp_r "doc", "guides/output/api/js"
138
+ end
127
139
  ENV["WARN_BROKEN_LINKS"] = "1" # authors can't disable this
128
140
  ruby "guides/jax_guides.rb"
129
141
  end
@@ -1,10 +1,30 @@
1
+ /**
2
+ * Classic and 'improved' (simplex) Perlin noise.
3
+ *
4
+ * This implementation attempts to use texture-based lookups if the client
5
+ * hardware can support it. This is no problem in fragment shaders but can
6
+ * be an issue in vertex shaders, where VTL is not supported by about 20%
7
+ * of clients.
8
+ *
9
+ * In the event this is a vertex shader *and* the client doesn't support
10
+ * VTL, the functions will fall back to 'ashima' noise
11
+ * (https://github.com/ashima/webgl-noise) for a slower, non-texture-based
12
+ * implementation.
13
+ **/
14
+
15
+ uniform float time; // Used for texture animation
16
+
17
+
18
+ <%if (shader_type != 'vertex' || Jax.Shader.max_vertex_textures > 0) {%>
19
+
20
+
1
21
  /*
2
22
  * 2D, 3D and 4D Perlin noise, classic and simplex, in a GLSL fragment shader.
3
23
  *
4
24
  * Classic noise is implemented by the functions:
5
- * float noise(vec2 P)
6
- * float noise(vec3 P)
7
- * float noise(vec4 P)
25
+ * float cnoise(vec2 P)
26
+ * float cnoise(vec3 P)
27
+ * float cnoise(vec4 P)
8
28
  *
9
29
  * Simplex noise is implemented by the functions:
10
30
  * float snoise(vec2 P)
@@ -53,7 +73,6 @@ uniform sampler2D permTexture;
53
73
  //uniform sampler1D simplexTexture;
54
74
  uniform sampler2D simplexTexture;
55
75
  uniform sampler2D gradTexture;
56
- uniform float time; // Used for texture animation
57
76
 
58
77
  /*
59
78
  * Both 2D and 3D texture coordinates are defined, for testing purposes.
@@ -85,7 +104,7 @@ float fade(float t) {
85
104
  /*
86
105
  * 2D classic Perlin noise. Fast, but less useful than 3D noise.
87
106
  */
88
- float noise(vec2 P)
107
+ float cnoise(vec2 P)
89
108
  {
90
109
  vec2 Pi = ONE*floor(P)+ONEHALF; // Integer part, scaled and offset for texture lookup
91
110
  vec2 Pf = fract(P); // Fractional part for interpolation
@@ -120,7 +139,7 @@ float noise(vec2 P)
120
139
  /*
121
140
  * 3D classic noise. Slower, but a lot more useful than 2D noise.
122
141
  */
123
- float noise(vec3 P)
142
+ float cnoise(vec3 P)
124
143
  {
125
144
  vec3 Pi = ONE*floor(P)+ONEHALF; // Integer part, scaled so +1 moves one texel
126
145
  // and offset 1/2 texel to sample texel centers
@@ -176,7 +195,7 @@ float noise(vec3 P)
176
195
  * 16 dot products, 4 mix operations and a lot of additions and multiplications.
177
196
  * Needless to say, it's not super fast. But it's not dead slow either.
178
197
  */
179
- float noise(vec4 P)
198
+ float cnoise(vec4 P)
180
199
  {
181
200
  vec4 Pi = ONE*floor(P)+ONEHALF; // Integer part, scaled so +1 moves one texel
182
201
  // and offset 1/2 texel to sample texel centers
@@ -521,3 +540,757 @@ float snoise(vec4 P) {
521
540
  // Sum up and scale the result to cover the range [-1,1]
522
541
  return 27.0 * (n0 + n1 + n2 + n3 + n4);
523
542
  }
543
+
544
+ <%
545
+ } else {
546
+ // non-texture-based implementation:
547
+ // Ian McEwan, Ashima Arts.
548
+ // Copyright (C) 2011 Ashima Arts. All rights reserved.
549
+ // Distributed under the MIT License. See LICENSE file.
550
+ %>
551
+
552
+ vec4 permute(vec4 x)
553
+ {
554
+ return mod(((x*34.0)+1.0)*x, 289.0);
555
+ }
556
+
557
+ vec3 permute(vec3 x)
558
+ {
559
+ return mod(((x*34.0)+1.0)*x, 289.0);
560
+ }
561
+
562
+ float permute(float x)
563
+ {
564
+ return floor(mod(((x*34.0)+1.0)*x, 289.0));
565
+ }
566
+
567
+ vec4 taylorInvSqrt(vec4 r)
568
+ {
569
+ return 1.79284291400159 - 0.85373472095314 * r;
570
+ }
571
+
572
+ float taylorInvSqrt(float r)
573
+ {
574
+ return 1.79284291400159 - 0.85373472095314 * r;
575
+ }
576
+
577
+ vec4 grad4(float j, vec4 ip)
578
+ {
579
+ const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0);
580
+ vec4 p,s;
581
+
582
+ p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0;
583
+ p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
584
+ s = vec4(lessThan(p, vec4(0.0)));
585
+ p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
586
+
587
+ return p;
588
+ }
589
+
590
+ vec4 fade(vec4 t) {
591
+ return t*t*t*(t*(t*6.0-15.0)+10.0);
592
+ }
593
+
594
+ vec3 fade(vec3 t) {
595
+ return t*t*t*(t*(t*6.0-15.0)+10.0);
596
+ }
597
+
598
+ vec2 fade(vec2 t) {
599
+ return t*t*t*(t*(t*6.0-15.0)+10.0);
600
+ }
601
+
602
+ // Classic Perlin noise
603
+ float cnoise(vec2 P)
604
+ {
605
+ vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
606
+ vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
607
+ Pi = mod(Pi, 289.0); // To avoid truncation effects in permutation
608
+ vec4 ix = Pi.xzxz;
609
+ vec4 iy = Pi.yyww;
610
+ vec4 fx = Pf.xzxz;
611
+ vec4 fy = Pf.yyww;
612
+
613
+ vec4 i = permute(permute(ix) + iy);
614
+
615
+ vec4 gx = 2.0 * fract(i / 41.0) - 1.0 ;
616
+ vec4 gy = abs(gx) - 0.5 ;
617
+ vec4 tx = floor(gx + 0.5);
618
+ gx = gx - tx;
619
+
620
+ vec2 g00 = vec2(gx.x,gy.x);
621
+ vec2 g10 = vec2(gx.y,gy.y);
622
+ vec2 g01 = vec2(gx.z,gy.z);
623
+ vec2 g11 = vec2(gx.w,gy.w);
624
+
625
+ vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
626
+ g00 *= norm.x;
627
+ g01 *= norm.y;
628
+ g10 *= norm.z;
629
+ g11 *= norm.w;
630
+
631
+ float n00 = dot(g00, vec2(fx.x, fy.x));
632
+ float n10 = dot(g10, vec2(fx.y, fy.y));
633
+ float n01 = dot(g01, vec2(fx.z, fy.z));
634
+ float n11 = dot(g11, vec2(fx.w, fy.w));
635
+
636
+ vec2 fade_xy = fade(Pf.xy);
637
+ vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
638
+ float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
639
+ return 2.3 * n_xy;
640
+ }
641
+
642
+ // Classic Perlin noise, periodic variant
643
+ float pnoise(vec2 P, vec2 rep)
644
+ {
645
+ vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
646
+ vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
647
+ Pi = mod(Pi, rep.xyxy); // To create noise with explicit period
648
+ Pi = mod(Pi, 289.0); // To avoid truncation effects in permutation
649
+ vec4 ix = Pi.xzxz;
650
+ vec4 iy = Pi.yyww;
651
+ vec4 fx = Pf.xzxz;
652
+ vec4 fy = Pf.yyww;
653
+
654
+ vec4 i = permute(permute(ix) + iy);
655
+
656
+ vec4 gx = 2.0 * fract(i / 41.0) - 1.0 ;
657
+ vec4 gy = abs(gx) - 0.5 ;
658
+ vec4 tx = floor(gx + 0.5);
659
+ gx = gx - tx;
660
+
661
+ vec2 g00 = vec2(gx.x,gy.x);
662
+ vec2 g10 = vec2(gx.y,gy.y);
663
+ vec2 g01 = vec2(gx.z,gy.z);
664
+ vec2 g11 = vec2(gx.w,gy.w);
665
+
666
+ vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
667
+ g00 *= norm.x;
668
+ g01 *= norm.y;
669
+ g10 *= norm.z;
670
+ g11 *= norm.w;
671
+
672
+ float n00 = dot(g00, vec2(fx.x, fy.x));
673
+ float n10 = dot(g10, vec2(fx.y, fy.y));
674
+ float n01 = dot(g01, vec2(fx.z, fy.z));
675
+ float n11 = dot(g11, vec2(fx.w, fy.w));
676
+
677
+ vec2 fade_xy = fade(Pf.xy);
678
+ vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
679
+ float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
680
+ return 2.3 * n_xy;
681
+ }
682
+
683
+ // Classic Perlin noise
684
+ float cnoise(vec3 P)
685
+ {
686
+ vec3 Pi0 = floor(P); // Integer part for indexing
687
+ vec3 Pi1 = Pi0 + vec3(1.0); // Integer part + 1
688
+ Pi0 = mod(Pi0, 289.0);
689
+ Pi1 = mod(Pi1, 289.0);
690
+ vec3 Pf0 = fract(P); // Fractional part for interpolation
691
+ vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
692
+ vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
693
+ vec4 iy = vec4(Pi0.yy, Pi1.yy);
694
+ vec4 iz0 = Pi0.zzzz;
695
+ vec4 iz1 = Pi1.zzzz;
696
+
697
+ vec4 ixy = permute(permute(ix) + iy);
698
+ vec4 ixy0 = permute(ixy + iz0);
699
+ vec4 ixy1 = permute(ixy + iz1);
700
+
701
+ vec4 gx0 = ixy0 / 7.0;
702
+ vec4 gy0 = fract(floor(gx0) / 7.0) - 0.5;
703
+ gx0 = fract(gx0);
704
+ vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
705
+ vec4 sz0 = step(gz0, vec4(0.0));
706
+ gx0 -= sz0 * (step(0.0, gx0) - 0.5);
707
+ gy0 -= sz0 * (step(0.0, gy0) - 0.5);
708
+
709
+ vec4 gx1 = ixy1 / 7.0;
710
+ vec4 gy1 = fract(floor(gx1) / 7.0) - 0.5;
711
+ gx1 = fract(gx1);
712
+ vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
713
+ vec4 sz1 = step(gz1, vec4(0.0));
714
+ gx1 -= sz1 * (step(0.0, gx1) - 0.5);
715
+ gy1 -= sz1 * (step(0.0, gy1) - 0.5);
716
+
717
+ vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
718
+ vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
719
+ vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
720
+ vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
721
+ vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
722
+ vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
723
+ vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
724
+ vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);
725
+
726
+ vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
727
+ g000 *= norm0.x;
728
+ g010 *= norm0.y;
729
+ g100 *= norm0.z;
730
+ g110 *= norm0.w;
731
+ vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
732
+ g001 *= norm1.x;
733
+ g011 *= norm1.y;
734
+ g101 *= norm1.z;
735
+ g111 *= norm1.w;
736
+
737
+ float n000 = dot(g000, Pf0);
738
+ float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
739
+ float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
740
+ float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
741
+ float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
742
+ float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
743
+ float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
744
+ float n111 = dot(g111, Pf1);
745
+
746
+ vec3 fade_xyz = fade(Pf0);
747
+ vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
748
+ vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
749
+ float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
750
+ return 2.2 * n_xyz;
751
+ }
752
+
753
+ // Classic Perlin noise, periodic variant
754
+ float pnoise(vec3 P, vec3 rep)
755
+ {
756
+ vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period
757
+ vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period
758
+ Pi0 = mod(Pi0, 289.0);
759
+ Pi1 = mod(Pi1, 289.0);
760
+ vec3 Pf0 = fract(P); // Fractional part for interpolation
761
+ vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
762
+ vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
763
+ vec4 iy = vec4(Pi0.yy, Pi1.yy);
764
+ vec4 iz0 = Pi0.zzzz;
765
+ vec4 iz1 = Pi1.zzzz;
766
+
767
+ vec4 ixy = permute(permute(ix) + iy);
768
+ vec4 ixy0 = permute(ixy + iz0);
769
+ vec4 ixy1 = permute(ixy + iz1);
770
+
771
+ vec4 gx0 = ixy0 / 7.0;
772
+ vec4 gy0 = fract(floor(gx0) / 7.0) - 0.5;
773
+ gx0 = fract(gx0);
774
+ vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
775
+ vec4 sz0 = step(gz0, vec4(0.0));
776
+ gx0 -= sz0 * (step(0.0, gx0) - 0.5);
777
+ gy0 -= sz0 * (step(0.0, gy0) - 0.5);
778
+
779
+ vec4 gx1 = ixy1 / 7.0;
780
+ vec4 gy1 = fract(floor(gx1) / 7.0) - 0.5;
781
+ gx1 = fract(gx1);
782
+ vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
783
+ vec4 sz1 = step(gz1, vec4(0.0));
784
+ gx1 -= sz1 * (step(0.0, gx1) - 0.5);
785
+ gy1 -= sz1 * (step(0.0, gy1) - 0.5);
786
+
787
+ vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
788
+ vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
789
+ vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
790
+ vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
791
+ vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
792
+ vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
793
+ vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
794
+ vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);
795
+
796
+ vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
797
+ g000 *= norm0.x;
798
+ g010 *= norm0.y;
799
+ g100 *= norm0.z;
800
+ g110 *= norm0.w;
801
+ vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
802
+ g001 *= norm1.x;
803
+ g011 *= norm1.y;
804
+ g101 *= norm1.z;
805
+ g111 *= norm1.w;
806
+
807
+ float n000 = dot(g000, Pf0);
808
+ float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
809
+ float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
810
+ float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
811
+ float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
812
+ float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
813
+ float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
814
+ float n111 = dot(g111, Pf1);
815
+
816
+ vec3 fade_xyz = fade(Pf0);
817
+ vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
818
+ vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
819
+ float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
820
+ return 2.2 * n_xyz;
821
+ }
822
+
823
+ // Classic Perlin noise
824
+ float cnoise(vec4 P)
825
+ {
826
+ vec4 Pi0 = floor(P); // Integer part for indexing
827
+ vec4 Pi1 = Pi0 + 1.0; // Integer part + 1
828
+ Pi0 = mod(Pi0, 289.0);
829
+ Pi1 = mod(Pi1, 289.0);
830
+ vec4 Pf0 = fract(P); // Fractional part for interpolation
831
+ vec4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0
832
+ vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
833
+ vec4 iy = vec4(Pi0.yy, Pi1.yy);
834
+ vec4 iz0 = vec4(Pi0.zzzz);
835
+ vec4 iz1 = vec4(Pi1.zzzz);
836
+ vec4 iw0 = vec4(Pi0.wwww);
837
+ vec4 iw1 = vec4(Pi1.wwww);
838
+
839
+ vec4 ixy = permute(permute(ix) + iy);
840
+ vec4 ixy0 = permute(ixy + iz0);
841
+ vec4 ixy1 = permute(ixy + iz1);
842
+ vec4 ixy00 = permute(ixy0 + iw0);
843
+ vec4 ixy01 = permute(ixy0 + iw1);
844
+ vec4 ixy10 = permute(ixy1 + iw0);
845
+ vec4 ixy11 = permute(ixy1 + iw1);
846
+
847
+ vec4 gx00 = ixy00 / 7.0;
848
+ vec4 gy00 = floor(gx00) / 7.0;
849
+ vec4 gz00 = floor(gy00) / 6.0;
850
+ gx00 = fract(gx00) - 0.5;
851
+ gy00 = fract(gy00) - 0.5;
852
+ gz00 = fract(gz00) - 0.5;
853
+ vec4 gw00 = vec4(0.75) - abs(gx00) - abs(gy00) - abs(gz00);
854
+ vec4 sw00 = step(gw00, vec4(0.0));
855
+ gx00 -= sw00 * (step(0.0, gx00) - 0.5);
856
+ gy00 -= sw00 * (step(0.0, gy00) - 0.5);
857
+
858
+ vec4 gx01 = ixy01 / 7.0;
859
+ vec4 gy01 = floor(gx01) / 7.0;
860
+ vec4 gz01 = floor(gy01) / 6.0;
861
+ gx01 = fract(gx01) - 0.5;
862
+ gy01 = fract(gy01) - 0.5;
863
+ gz01 = fract(gz01) - 0.5;
864
+ vec4 gw01 = vec4(0.75) - abs(gx01) - abs(gy01) - abs(gz01);
865
+ vec4 sw01 = step(gw01, vec4(0.0));
866
+ gx01 -= sw01 * (step(0.0, gx01) - 0.5);
867
+ gy01 -= sw01 * (step(0.0, gy01) - 0.5);
868
+
869
+ vec4 gx10 = ixy10 / 7.0;
870
+ vec4 gy10 = floor(gx10) / 7.0;
871
+ vec4 gz10 = floor(gy10) / 6.0;
872
+ gx10 = fract(gx10) - 0.5;
873
+ gy10 = fract(gy10) - 0.5;
874
+ gz10 = fract(gz10) - 0.5;
875
+ vec4 gw10 = vec4(0.75) - abs(gx10) - abs(gy10) - abs(gz10);
876
+ vec4 sw10 = step(gw10, vec4(0.0));
877
+ gx10 -= sw10 * (step(0.0, gx10) - 0.5);
878
+ gy10 -= sw10 * (step(0.0, gy10) - 0.5);
879
+
880
+ vec4 gx11 = ixy11 / 7.0;
881
+ vec4 gy11 = floor(gx11) / 7.0;
882
+ vec4 gz11 = floor(gy11) / 6.0;
883
+ gx11 = fract(gx11) - 0.5;
884
+ gy11 = fract(gy11) - 0.5;
885
+ gz11 = fract(gz11) - 0.5;
886
+ vec4 gw11 = vec4(0.75) - abs(gx11) - abs(gy11) - abs(gz11);
887
+ vec4 sw11 = step(gw11, vec4(0.0));
888
+ gx11 -= sw11 * (step(0.0, gx11) - 0.5);
889
+ gy11 -= sw11 * (step(0.0, gy11) - 0.5);
890
+
891
+ vec4 g0000 = vec4(gx00.x,gy00.x,gz00.x,gw00.x);
892
+ vec4 g1000 = vec4(gx00.y,gy00.y,gz00.y,gw00.y);
893
+ vec4 g0100 = vec4(gx00.z,gy00.z,gz00.z,gw00.z);
894
+ vec4 g1100 = vec4(gx00.w,gy00.w,gz00.w,gw00.w);
895
+ vec4 g0010 = vec4(gx10.x,gy10.x,gz10.x,gw10.x);
896
+ vec4 g1010 = vec4(gx10.y,gy10.y,gz10.y,gw10.y);
897
+ vec4 g0110 = vec4(gx10.z,gy10.z,gz10.z,gw10.z);
898
+ vec4 g1110 = vec4(gx10.w,gy10.w,gz10.w,gw10.w);
899
+ vec4 g0001 = vec4(gx01.x,gy01.x,gz01.x,gw01.x);
900
+ vec4 g1001 = vec4(gx01.y,gy01.y,gz01.y,gw01.y);
901
+ vec4 g0101 = vec4(gx01.z,gy01.z,gz01.z,gw01.z);
902
+ vec4 g1101 = vec4(gx01.w,gy01.w,gz01.w,gw01.w);
903
+ vec4 g0011 = vec4(gx11.x,gy11.x,gz11.x,gw11.x);
904
+ vec4 g1011 = vec4(gx11.y,gy11.y,gz11.y,gw11.y);
905
+ vec4 g0111 = vec4(gx11.z,gy11.z,gz11.z,gw11.z);
906
+ vec4 g1111 = vec4(gx11.w,gy11.w,gz11.w,gw11.w);
907
+
908
+ vec4 norm00 = taylorInvSqrt(vec4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
909
+ g0000 *= norm00.x;
910
+ g0100 *= norm00.y;
911
+ g1000 *= norm00.z;
912
+ g1100 *= norm00.w;
913
+
914
+ vec4 norm01 = taylorInvSqrt(vec4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
915
+ g0001 *= norm01.x;
916
+ g0101 *= norm01.y;
917
+ g1001 *= norm01.z;
918
+ g1101 *= norm01.w;
919
+
920
+ vec4 norm10 = taylorInvSqrt(vec4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
921
+ g0010 *= norm10.x;
922
+ g0110 *= norm10.y;
923
+ g1010 *= norm10.z;
924
+ g1110 *= norm10.w;
925
+
926
+ vec4 norm11 = taylorInvSqrt(vec4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
927
+ g0011 *= norm11.x;
928
+ g0111 *= norm11.y;
929
+ g1011 *= norm11.z;
930
+ g1111 *= norm11.w;
931
+
932
+ float n0000 = dot(g0000, Pf0);
933
+ float n1000 = dot(g1000, vec4(Pf1.x, Pf0.yzw));
934
+ float n0100 = dot(g0100, vec4(Pf0.x, Pf1.y, Pf0.zw));
935
+ float n1100 = dot(g1100, vec4(Pf1.xy, Pf0.zw));
936
+ float n0010 = dot(g0010, vec4(Pf0.xy, Pf1.z, Pf0.w));
937
+ float n1010 = dot(g1010, vec4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
938
+ float n0110 = dot(g0110, vec4(Pf0.x, Pf1.yz, Pf0.w));
939
+ float n1110 = dot(g1110, vec4(Pf1.xyz, Pf0.w));
940
+ float n0001 = dot(g0001, vec4(Pf0.xyz, Pf1.w));
941
+ float n1001 = dot(g1001, vec4(Pf1.x, Pf0.yz, Pf1.w));
942
+ float n0101 = dot(g0101, vec4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
943
+ float n1101 = dot(g1101, vec4(Pf1.xy, Pf0.z, Pf1.w));
944
+ float n0011 = dot(g0011, vec4(Pf0.xy, Pf1.zw));
945
+ float n1011 = dot(g1011, vec4(Pf1.x, Pf0.y, Pf1.zw));
946
+ float n0111 = dot(g0111, vec4(Pf0.x, Pf1.yzw));
947
+ float n1111 = dot(g1111, Pf1);
948
+
949
+ vec4 fade_xyzw = fade(Pf0);
950
+ vec4 n_0w = mix(vec4(n0000, n1000, n0100, n1100), vec4(n0001, n1001, n0101, n1101), fade_xyzw.w);
951
+ vec4 n_1w = mix(vec4(n0010, n1010, n0110, n1110), vec4(n0011, n1011, n0111, n1111), fade_xyzw.w);
952
+ vec4 n_zw = mix(n_0w, n_1w, fade_xyzw.z);
953
+ vec2 n_yzw = mix(n_zw.xy, n_zw.zw, fade_xyzw.y);
954
+ float n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x);
955
+ return 2.2 * n_xyzw;
956
+ }
957
+
958
+ // Classic Perlin noise, periodic version
959
+ float cnoise(vec4 P, vec4 rep)
960
+ {
961
+ vec4 Pi0 = mod(floor(P), rep); // Integer part modulo rep
962
+ vec4 Pi1 = mod(Pi0 + 1.0, rep); // Integer part + 1 mod rep
963
+ vec4 Pf0 = fract(P); // Fractional part for interpolation
964
+ vec4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0
965
+ vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
966
+ vec4 iy = vec4(Pi0.yy, Pi1.yy);
967
+ vec4 iz0 = vec4(Pi0.zzzz);
968
+ vec4 iz1 = vec4(Pi1.zzzz);
969
+ vec4 iw0 = vec4(Pi0.wwww);
970
+ vec4 iw1 = vec4(Pi1.wwww);
971
+
972
+ vec4 ixy = permute(permute(ix) + iy);
973
+ vec4 ixy0 = permute(ixy + iz0);
974
+ vec4 ixy1 = permute(ixy + iz1);
975
+ vec4 ixy00 = permute(ixy0 + iw0);
976
+ vec4 ixy01 = permute(ixy0 + iw1);
977
+ vec4 ixy10 = permute(ixy1 + iw0);
978
+ vec4 ixy11 = permute(ixy1 + iw1);
979
+
980
+ vec4 gx00 = ixy00 / 7.0;
981
+ vec4 gy00 = floor(gx00) / 7.0;
982
+ vec4 gz00 = floor(gy00) / 6.0;
983
+ gx00 = fract(gx00) - 0.5;
984
+ gy00 = fract(gy00) - 0.5;
985
+ gz00 = fract(gz00) - 0.5;
986
+ vec4 gw00 = vec4(0.75) - abs(gx00) - abs(gy00) - abs(gz00);
987
+ vec4 sw00 = step(gw00, vec4(0.0));
988
+ gx00 -= sw00 * (step(0.0, gx00) - 0.5);
989
+ gy00 -= sw00 * (step(0.0, gy00) - 0.5);
990
+
991
+ vec4 gx01 = ixy01 / 7.0;
992
+ vec4 gy01 = floor(gx01) / 7.0;
993
+ vec4 gz01 = floor(gy01) / 6.0;
994
+ gx01 = fract(gx01) - 0.5;
995
+ gy01 = fract(gy01) - 0.5;
996
+ gz01 = fract(gz01) - 0.5;
997
+ vec4 gw01 = vec4(0.75) - abs(gx01) - abs(gy01) - abs(gz01);
998
+ vec4 sw01 = step(gw01, vec4(0.0));
999
+ gx01 -= sw01 * (step(0.0, gx01) - 0.5);
1000
+ gy01 -= sw01 * (step(0.0, gy01) - 0.5);
1001
+
1002
+ vec4 gx10 = ixy10 / 7.0;
1003
+ vec4 gy10 = floor(gx10) / 7.0;
1004
+ vec4 gz10 = floor(gy10) / 6.0;
1005
+ gx10 = fract(gx10) - 0.5;
1006
+ gy10 = fract(gy10) - 0.5;
1007
+ gz10 = fract(gz10) - 0.5;
1008
+ vec4 gw10 = vec4(0.75) - abs(gx10) - abs(gy10) - abs(gz10);
1009
+ vec4 sw10 = step(gw10, vec4(0.0));
1010
+ gx10 -= sw10 * (step(0.0, gx10) - 0.5);
1011
+ gy10 -= sw10 * (step(0.0, gy10) - 0.5);
1012
+
1013
+ vec4 gx11 = ixy11 / 7.0;
1014
+ vec4 gy11 = floor(gx11) / 7.0;
1015
+ vec4 gz11 = floor(gy11) / 6.0;
1016
+ gx11 = fract(gx11) - 0.5;
1017
+ gy11 = fract(gy11) - 0.5;
1018
+ gz11 = fract(gz11) - 0.5;
1019
+ vec4 gw11 = vec4(0.75) - abs(gx11) - abs(gy11) - abs(gz11);
1020
+ vec4 sw11 = step(gw11, vec4(0.0));
1021
+ gx11 -= sw11 * (step(0.0, gx11) - 0.5);
1022
+ gy11 -= sw11 * (step(0.0, gy11) - 0.5);
1023
+
1024
+ vec4 g0000 = vec4(gx00.x,gy00.x,gz00.x,gw00.x);
1025
+ vec4 g1000 = vec4(gx00.y,gy00.y,gz00.y,gw00.y);
1026
+ vec4 g0100 = vec4(gx00.z,gy00.z,gz00.z,gw00.z);
1027
+ vec4 g1100 = vec4(gx00.w,gy00.w,gz00.w,gw00.w);
1028
+ vec4 g0010 = vec4(gx10.x,gy10.x,gz10.x,gw10.x);
1029
+ vec4 g1010 = vec4(gx10.y,gy10.y,gz10.y,gw10.y);
1030
+ vec4 g0110 = vec4(gx10.z,gy10.z,gz10.z,gw10.z);
1031
+ vec4 g1110 = vec4(gx10.w,gy10.w,gz10.w,gw10.w);
1032
+ vec4 g0001 = vec4(gx01.x,gy01.x,gz01.x,gw01.x);
1033
+ vec4 g1001 = vec4(gx01.y,gy01.y,gz01.y,gw01.y);
1034
+ vec4 g0101 = vec4(gx01.z,gy01.z,gz01.z,gw01.z);
1035
+ vec4 g1101 = vec4(gx01.w,gy01.w,gz01.w,gw01.w);
1036
+ vec4 g0011 = vec4(gx11.x,gy11.x,gz11.x,gw11.x);
1037
+ vec4 g1011 = vec4(gx11.y,gy11.y,gz11.y,gw11.y);
1038
+ vec4 g0111 = vec4(gx11.z,gy11.z,gz11.z,gw11.z);
1039
+ vec4 g1111 = vec4(gx11.w,gy11.w,gz11.w,gw11.w);
1040
+
1041
+ vec4 norm00 = taylorInvSqrt(vec4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
1042
+ g0000 *= norm00.x;
1043
+ g0100 *= norm00.y;
1044
+ g1000 *= norm00.z;
1045
+ g1100 *= norm00.w;
1046
+
1047
+ vec4 norm01 = taylorInvSqrt(vec4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
1048
+ g0001 *= norm01.x;
1049
+ g0101 *= norm01.y;
1050
+ g1001 *= norm01.z;
1051
+ g1101 *= norm01.w;
1052
+
1053
+ vec4 norm10 = taylorInvSqrt(vec4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
1054
+ g0010 *= norm10.x;
1055
+ g0110 *= norm10.y;
1056
+ g1010 *= norm10.z;
1057
+ g1110 *= norm10.w;
1058
+
1059
+ vec4 norm11 = taylorInvSqrt(vec4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
1060
+ g0011 *= norm11.x;
1061
+ g0111 *= norm11.y;
1062
+ g1011 *= norm11.z;
1063
+ g1111 *= norm11.w;
1064
+
1065
+ float n0000 = dot(g0000, Pf0);
1066
+ float n1000 = dot(g1000, vec4(Pf1.x, Pf0.yzw));
1067
+ float n0100 = dot(g0100, vec4(Pf0.x, Pf1.y, Pf0.zw));
1068
+ float n1100 = dot(g1100, vec4(Pf1.xy, Pf0.zw));
1069
+ float n0010 = dot(g0010, vec4(Pf0.xy, Pf1.z, Pf0.w));
1070
+ float n1010 = dot(g1010, vec4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
1071
+ float n0110 = dot(g0110, vec4(Pf0.x, Pf1.yz, Pf0.w));
1072
+ float n1110 = dot(g1110, vec4(Pf1.xyz, Pf0.w));
1073
+ float n0001 = dot(g0001, vec4(Pf0.xyz, Pf1.w));
1074
+ float n1001 = dot(g1001, vec4(Pf1.x, Pf0.yz, Pf1.w));
1075
+ float n0101 = dot(g0101, vec4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
1076
+ float n1101 = dot(g1101, vec4(Pf1.xy, Pf0.z, Pf1.w));
1077
+ float n0011 = dot(g0011, vec4(Pf0.xy, Pf1.zw));
1078
+ float n1011 = dot(g1011, vec4(Pf1.x, Pf0.y, Pf1.zw));
1079
+ float n0111 = dot(g0111, vec4(Pf0.x, Pf1.yzw));
1080
+ float n1111 = dot(g1111, Pf1);
1081
+
1082
+ vec4 fade_xyzw = fade(Pf0);
1083
+ vec4 n_0w = mix(vec4(n0000, n1000, n0100, n1100), vec4(n0001, n1001, n0101, n1101), fade_xyzw.w);
1084
+ vec4 n_1w = mix(vec4(n0010, n1010, n0110, n1110), vec4(n0011, n1011, n0111, n1111), fade_xyzw.w);
1085
+ vec4 n_zw = mix(n_0w, n_1w, fade_xyzw.z);
1086
+ vec2 n_yzw = mix(n_zw.xy, n_zw.zw, fade_xyzw.y);
1087
+ float n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x);
1088
+ return 2.2 * n_xyzw;
1089
+ }
1090
+
1091
+ float snoise(vec2 v)
1092
+ {
1093
+ const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
1094
+ 0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
1095
+ -0.577350269189626, // -1.0 + 2.0 * C.x
1096
+ 0.024390243902439); // 1.0 / 41.0
1097
+ // First corner
1098
+ vec2 i = floor(v + dot(v, C.yy) );
1099
+ vec2 x0 = v - i + dot(i, C.xx);
1100
+
1101
+ // Other corners
1102
+ vec2 i1;
1103
+ //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
1104
+ //i1.y = 1.0 - i1.x;
1105
+ i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
1106
+ // x0 = x0 - 0.0 + 0.0 * C.xx ;
1107
+ // x1 = x0 - i1 + 1.0 * C.xx ;
1108
+ // x2 = x0 - 1.0 + 2.0 * C.xx ;
1109
+ vec4 x12 = x0.xyxy + C.xxzz;
1110
+ x12.xy -= i1;
1111
+
1112
+ // Permutations
1113
+ i = mod(i, 289.0); // Avoid truncation effects in permutation
1114
+ vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
1115
+ + i.x + vec3(0.0, i1.x, 1.0 ));
1116
+
1117
+ vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
1118
+ m = m*m ;
1119
+ m = m*m ;
1120
+
1121
+ // Gradients: 41 points uniformly over a line, mapped onto a diamond.
1122
+ // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
1123
+
1124
+ vec3 x = 2.0 * fract(p * C.www) - 1.0;
1125
+ vec3 h = abs(x) - 0.5;
1126
+ vec3 ox = floor(x + 0.5);
1127
+ vec3 a0 = x - ox;
1128
+
1129
+ // Normalise gradients implicitly by scaling m
1130
+ // Inlined for speed: m *= taylorInvSqrt( a0*a0 + h*h );
1131
+ m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
1132
+
1133
+ // Compute final noise value at P
1134
+ vec3 g;
1135
+ g.x = a0.x * x0.x + h.x * x0.y;
1136
+ g.yz = a0.yz * x12.xz + h.yz * x12.yw;
1137
+ return 130.0 * dot(m, g);
1138
+ }
1139
+
1140
+ float snoise(vec3 v)
1141
+ {
1142
+ const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
1143
+ const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
1144
+
1145
+ // First corner
1146
+ vec3 i = floor(v + dot(v, C.yyy) );
1147
+ vec3 x0 = v - i + dot(i, C.xxx) ;
1148
+
1149
+ // Other corners
1150
+ vec3 g = step(x0.yzx, x0.xyz);
1151
+ vec3 l = 1.0 - g;
1152
+ vec3 i1 = min( g.xyz, l.zxy );
1153
+ vec3 i2 = max( g.xyz, l.zxy );
1154
+
1155
+ // x0 = x0 - 0.0 + 0.0 * C.xxx;
1156
+ // x1 = x0 - i1 + 1.0 * C.xxx;
1157
+ // x2 = x0 - i2 + 2.0 * C.xxx;
1158
+ // x3 = x0 - 1.0 + 3.0 * C.xxx;
1159
+ vec3 x1 = x0 - i1 + C.xxx;
1160
+ vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
1161
+ vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
1162
+
1163
+ // Permutations
1164
+ i = mod(i, 289.0 );
1165
+ vec4 p = permute( permute( permute(
1166
+ i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
1167
+ + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
1168
+ + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
1169
+
1170
+ // Gradients: 7x7 points over a square, mapped onto an octahedron.
1171
+ // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
1172
+ float n_ = 0.142857142857; // 1.0/7.0
1173
+ vec3 ns = n_ * D.wyz - D.xzx;
1174
+
1175
+ vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
1176
+
1177
+ vec4 x_ = floor(j * ns.z);
1178
+ vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
1179
+
1180
+ vec4 x = x_ *ns.x + ns.yyyy;
1181
+ vec4 y = y_ *ns.x + ns.yyyy;
1182
+ vec4 h = 1.0 - abs(x) - abs(y);
1183
+
1184
+ vec4 b0 = vec4( x.xy, y.xy );
1185
+ vec4 b1 = vec4( x.zw, y.zw );
1186
+
1187
+ //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
1188
+ //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
1189
+ vec4 s0 = floor(b0)*2.0 + 1.0;
1190
+ vec4 s1 = floor(b1)*2.0 + 1.0;
1191
+ vec4 sh = -step(h, vec4(0.0));
1192
+
1193
+ vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
1194
+ vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
1195
+
1196
+ vec3 p0 = vec3(a0.xy,h.x);
1197
+ vec3 p1 = vec3(a0.zw,h.y);
1198
+ vec3 p2 = vec3(a1.xy,h.z);
1199
+ vec3 p3 = vec3(a1.zw,h.w);
1200
+
1201
+ //Normalise gradients
1202
+ vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
1203
+ p0 *= norm.x;
1204
+ p1 *= norm.y;
1205
+ p2 *= norm.z;
1206
+ p3 *= norm.w;
1207
+
1208
+ // Mix final noise value
1209
+ vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
1210
+ m = m * m;
1211
+ return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
1212
+ dot(p2,x2), dot(p3,x3) ) );
1213
+ }
1214
+
1215
+ float snoise(vec4 v)
1216
+ {
1217
+ const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5))/20 G4
1218
+ 0.276393202250021, // 2 * G4
1219
+ 0.414589803375032, // 3 * G4
1220
+ -0.447213595499958); // -1 + 4 * G4
1221
+
1222
+ // (sqrt(5) - 1)/4 = F4, used once below
1223
+ #define F4 0.309016994374947451
1224
+
1225
+ // First corner
1226
+ vec4 i = floor(v + dot(v, vec4(F4)) );
1227
+ vec4 x0 = v - i + dot(i, C.xxxx);
1228
+
1229
+ // Other corners
1230
+
1231
+ // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
1232
+ vec4 i0;
1233
+ vec3 isX = step( x0.yzw, x0.xxx );
1234
+ vec3 isYZ = step( x0.zww, x0.yyz );
1235
+ // i0.x = dot( isX, vec3( 1.0 ) );
1236
+ i0.x = isX.x + isX.y + isX.z;
1237
+ i0.yzw = 1.0 - isX;
1238
+ // i0.y += dot( isYZ.xy, vec2( 1.0 ) );
1239
+ i0.y += isYZ.x + isYZ.y;
1240
+ i0.zw += 1.0 - isYZ.xy;
1241
+ i0.z += isYZ.z;
1242
+ i0.w += 1.0 - isYZ.z;
1243
+
1244
+ // i0 now contains the unique values 0,1,2,3 in each channel
1245
+ vec4 i3 = clamp( i0, 0.0, 1.0 );
1246
+ vec4 i2 = clamp( i0-1.0, 0.0, 1.0 );
1247
+ vec4 i1 = clamp( i0-2.0, 0.0, 1.0 );
1248
+
1249
+ // x0 = x0 - 0.0 + 0.0 * C.xxxx
1250
+ // x1 = x0 - i1 + 0.0 * C.xxxx
1251
+ // x2 = x0 - i2 + 0.0 * C.xxxx
1252
+ // x3 = x0 - i3 + 0.0 * C.xxxx
1253
+ // x4 = x0 - 1.0 + 4.0 * C.xxxx
1254
+ vec4 x1 = x0 - i1 + C.xxxx;
1255
+ vec4 x2 = x0 - i2 + C.yyyy;
1256
+ vec4 x3 = x0 - i3 + C.zzzz;
1257
+ vec4 x4 = x0 + C.wwww;
1258
+
1259
+ // Permutations
1260
+ i = mod(i, 289.0);
1261
+ float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
1262
+ vec4 j1 = permute( permute( permute( permute (
1263
+ i.w + vec4(i1.w, i2.w, i3.w, 1.0 ))
1264
+ + i.z + vec4(i1.z, i2.z, i3.z, 1.0 ))
1265
+ + i.y + vec4(i1.y, i2.y, i3.y, 1.0 ))
1266
+ + i.x + vec4(i1.x, i2.x, i3.x, 1.0 ));
1267
+
1268
+ // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
1269
+ // 7*7*6 = 294, which is close to the ring size 17*17 = 289.
1270
+ vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;
1271
+
1272
+ vec4 p0 = grad4(j0, ip);
1273
+ vec4 p1 = grad4(j1.x, ip);
1274
+ vec4 p2 = grad4(j1.y, ip);
1275
+ vec4 p3 = grad4(j1.z, ip);
1276
+ vec4 p4 = grad4(j1.w, ip);
1277
+
1278
+ // Normalise gradients
1279
+ vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
1280
+ p0 *= norm.x;
1281
+ p1 *= norm.y;
1282
+ p2 *= norm.z;
1283
+ p3 *= norm.w;
1284
+ p4 *= taylorInvSqrt(dot(p4,p4));
1285
+
1286
+ // Mix contributions from the five corners
1287
+ vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0);
1288
+ vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0);
1289
+ m0 = m0 * m0;
1290
+ m1 = m1 * m1;
1291
+ return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
1292
+ + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;
1293
+
1294
+ }
1295
+
1296
+ <% } %>