@xort/cga 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/THIRD_PARTY_NOTICES.md +26 -39
  2. package/build/@xort_cga.js +10096 -9226
  3. package/build/cga.d.ts +1286 -465
  4. package/build/cga.esm.js +10062 -9228
  5. package/build/shaders/cga.glsl +204 -0
  6. package/build/shaders/common.glsl +36 -0
  7. package/build/shaders/noise/GLSL-Noise.md +1108 -0
  8. package/build/shaders/noise/LICENSE +20 -0
  9. package/build/shaders/noise/cellular2D.glsl +69 -0
  10. package/build/shaders/noise/cellular2x2.glsl +64 -0
  11. package/build/shaders/noise/cellular2x2x2.glsl +88 -0
  12. package/build/shaders/noise/cellular3D.glsl +187 -0
  13. package/build/shaders/noise/classicnoise2D.glsl +113 -0
  14. package/build/shaders/noise/classicnoise3D.glsl +177 -0
  15. package/build/shaders/noise/classicnoise4D.glsl +302 -0
  16. package/build/shaders/noise/noise2D.glsl +71 -0
  17. package/build/shaders/noise/noise3D.glsl +103 -0
  18. package/build/shaders/noise/noise3Dgrad.glsl +111 -0
  19. package/build/shaders/noise/noise4D.glsl +129 -0
  20. package/build/shaders/noise/psrdnoise2D.glsl +463 -0
  21. package/dist/THIRD_PARTY_NOTICES.md +26 -39
  22. package/dist/index.cjs +10096 -9226
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +1286 -465
  25. package/dist/index.d.ts +1286 -465
  26. package/dist/index.js +10062 -9228
  27. package/dist/index.js.map +1 -1
  28. package/dist/shaders.cjs +21 -0
  29. package/dist/shaders.d.cts +20 -0
  30. package/dist/shaders.d.ts +20 -0
  31. package/dist/shaders.js +20 -0
  32. package/dist/three.cjs +3035 -0
  33. package/dist/three.cjs.map +1 -0
  34. package/dist/three.d.cts +311 -0
  35. package/dist/three.d.ts +311 -0
  36. package/dist/three.js +3008 -0
  37. package/dist/three.js.map +1 -0
  38. package/package.json +123 -83
  39. package/readme.md +74 -119
@@ -0,0 +1,111 @@
1
+ //
2
+ // Description : Array and textureless GLSL 2D/3D/4D simplex
3
+ // noise functions.
4
+ // Author : Ian McEwan, Ashima Arts.
5
+ // Maintainer : stegu
6
+ // Lastmod : 20110822 (ijm)
7
+ // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
8
+ // Distributed under the MIT License. See LICENSE file.
9
+ // https://github.com/ashima/webgl-noise
10
+ // https://github.com/stegu/webgl-noise
11
+ //
12
+
13
+ vec3 mod289(vec3 x) {
14
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
15
+ }
16
+
17
+ vec4 mod289(vec4 x) {
18
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
19
+ }
20
+
21
+ vec4 permute(vec4 x) {
22
+ return mod289(((x*34.0)+1.0)*x);
23
+ }
24
+
25
+ vec4 taylorInvSqrt(vec4 r)
26
+ {
27
+ return 1.79284291400159 - 0.85373472095314 * r;
28
+ }
29
+
30
+ float snoise(vec3 v, out vec3 gradient)
31
+ {
32
+ const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
33
+ const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
34
+
35
+ // First corner
36
+ vec3 i = floor(v + dot(v, C.yyy) );
37
+ vec3 x0 = v - i + dot(i, C.xxx) ;
38
+
39
+ // Other corners
40
+ vec3 g = step(x0.yzx, x0.xyz);
41
+ vec3 l = 1.0 - g;
42
+ vec3 i1 = min( g.xyz, l.zxy );
43
+ vec3 i2 = max( g.xyz, l.zxy );
44
+
45
+ // x0 = x0 - 0.0 + 0.0 * C.xxx;
46
+ // x1 = x0 - i1 + 1.0 * C.xxx;
47
+ // x2 = x0 - i2 + 2.0 * C.xxx;
48
+ // x3 = x0 - 1.0 + 3.0 * C.xxx;
49
+ vec3 x1 = x0 - i1 + C.xxx;
50
+ vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
51
+ vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
52
+
53
+ // Permutations
54
+ i = mod289(i);
55
+ vec4 p = permute( permute( permute(
56
+ i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
57
+ + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
58
+ + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
59
+
60
+ // Gradients: 7x7 points over a square, mapped onto an octahedron.
61
+ // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
62
+ float n_ = 0.142857142857; // 1.0/7.0
63
+ vec3 ns = n_ * D.wyz - D.xzx;
64
+
65
+ vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
66
+
67
+ vec4 x_ = floor(j * ns.z);
68
+ vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
69
+
70
+ vec4 x = x_ *ns.x + ns.yyyy;
71
+ vec4 y = y_ *ns.x + ns.yyyy;
72
+ vec4 h = 1.0 - abs(x) - abs(y);
73
+
74
+ vec4 b0 = vec4( x.xy, y.xy );
75
+ vec4 b1 = vec4( x.zw, y.zw );
76
+
77
+ //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
78
+ //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
79
+ vec4 s0 = floor(b0)*2.0 + 1.0;
80
+ vec4 s1 = floor(b1)*2.0 + 1.0;
81
+ vec4 sh = -step(h, vec4(0.0));
82
+
83
+ vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
84
+ vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
85
+
86
+ vec3 p0 = vec3(a0.xy,h.x);
87
+ vec3 p1 = vec3(a0.zw,h.y);
88
+ vec3 p2 = vec3(a1.xy,h.z);
89
+ vec3 p3 = vec3(a1.zw,h.w);
90
+
91
+ //Normalise gradients
92
+ vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
93
+ p0 *= norm.x;
94
+ p1 *= norm.y;
95
+ p2 *= norm.z;
96
+ p3 *= norm.w;
97
+
98
+ // Mix final noise value
99
+ vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
100
+ vec4 m2 = m * m;
101
+ vec4 m4 = m2 * m2;
102
+ vec4 pdotx = vec4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3));
103
+
104
+ // Determine noise gradient
105
+ vec4 temp = m2 * m * pdotx;
106
+ gradient = -8.0 * (temp.x * x0 + temp.y * x1 + temp.z * x2 + temp.w * x3);
107
+ gradient += m4.x * p0 + m4.y * p1 + m4.z * p2 + m4.w * p3;
108
+ gradient *= 42.0;
109
+
110
+ return 42.0 * dot(m4, pdotx);
111
+ }
@@ -0,0 +1,129 @@
1
+ //
2
+ // Description : Array and textureless GLSL 2D/3D/4D simplex
3
+ // noise functions.
4
+ // Author : Ian McEwan, Ashima Arts.
5
+ // Maintainer : stegu
6
+ // Lastmod : 20110822 (ijm)
7
+ // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
8
+ // Distributed under the MIT License. See LICENSE file.
9
+ // https://github.com/ashima/webgl-noise
10
+ // https://github.com/stegu/webgl-noise
11
+ //
12
+
13
+ vec4 mod289(vec4 x) {
14
+ return x - floor(x * (1.0 / 289.0)) * 289.0; }
15
+
16
+ float mod289(float x) {
17
+ return x - floor(x * (1.0 / 289.0)) * 289.0; }
18
+
19
+ vec4 permute(vec4 x) {
20
+ return mod289(((x*34.0)+1.0)*x);
21
+ }
22
+
23
+ float permute(float x) {
24
+ return mod289(((x*34.0)+1.0)*x);
25
+ }
26
+
27
+ vec4 taylorInvSqrt(vec4 r)
28
+ {
29
+ return 1.79284291400159 - 0.85373472095314 * r;
30
+ }
31
+
32
+ float taylorInvSqrt(float r)
33
+ {
34
+ return 1.79284291400159 - 0.85373472095314 * r;
35
+ }
36
+
37
+ vec4 grad4(float j, vec4 ip)
38
+ {
39
+ const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0);
40
+ vec4 p,s;
41
+
42
+ p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0;
43
+ p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
44
+ s = vec4(lessThan(p, vec4(0.0)));
45
+ p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
46
+
47
+ return p;
48
+ }
49
+
50
+ // (sqrt(5) - 1)/4 = F4, used once below
51
+ #define F4 0.309016994374947451
52
+
53
+ float snoise(vec4 v)
54
+ {
55
+ const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5))/20 G4
56
+ 0.276393202250021, // 2 * G4
57
+ 0.414589803375032, // 3 * G4
58
+ -0.447213595499958); // -1 + 4 * G4
59
+
60
+ // First corner
61
+ vec4 i = floor(v + dot(v, vec4(F4)) );
62
+ vec4 x0 = v - i + dot(i, C.xxxx);
63
+
64
+ // Other corners
65
+
66
+ // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
67
+ vec4 i0;
68
+ vec3 isX = step( x0.yzw, x0.xxx );
69
+ vec3 isYZ = step( x0.zww, x0.yyz );
70
+ // i0.x = dot( isX, vec3( 1.0 ) );
71
+ i0.x = isX.x + isX.y + isX.z;
72
+ i0.yzw = 1.0 - isX;
73
+ // i0.y += dot( isYZ.xy, vec2( 1.0 ) );
74
+ i0.y += isYZ.x + isYZ.y;
75
+ i0.zw += 1.0 - isYZ.xy;
76
+ i0.z += isYZ.z;
77
+ i0.w += 1.0 - isYZ.z;
78
+
79
+ // i0 now contains the unique values 0,1,2,3 in each channel
80
+ vec4 i3 = clamp( i0, 0.0, 1.0 );
81
+ vec4 i2 = clamp( i0-1.0, 0.0, 1.0 );
82
+ vec4 i1 = clamp( i0-2.0, 0.0, 1.0 );
83
+
84
+ // x0 = x0 - 0.0 + 0.0 * C.xxxx
85
+ // x1 = x0 - i1 + 1.0 * C.xxxx
86
+ // x2 = x0 - i2 + 2.0 * C.xxxx
87
+ // x3 = x0 - i3 + 3.0 * C.xxxx
88
+ // x4 = x0 - 1.0 + 4.0 * C.xxxx
89
+ vec4 x1 = x0 - i1 + C.xxxx;
90
+ vec4 x2 = x0 - i2 + C.yyyy;
91
+ vec4 x3 = x0 - i3 + C.zzzz;
92
+ vec4 x4 = x0 + C.wwww;
93
+
94
+ // Permutations
95
+ i = mod289(i);
96
+ float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
97
+ vec4 j1 = permute( permute( permute( permute (
98
+ i.w + vec4(i1.w, i2.w, i3.w, 1.0 ))
99
+ + i.z + vec4(i1.z, i2.z, i3.z, 1.0 ))
100
+ + i.y + vec4(i1.y, i2.y, i3.y, 1.0 ))
101
+ + i.x + vec4(i1.x, i2.x, i3.x, 1.0 ));
102
+
103
+ // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
104
+ // 7*7*6 = 294, which is close to the ring size 17*17 = 289.
105
+ vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;
106
+
107
+ vec4 p0 = grad4(j0, ip);
108
+ vec4 p1 = grad4(j1.x, ip);
109
+ vec4 p2 = grad4(j1.y, ip);
110
+ vec4 p3 = grad4(j1.z, ip);
111
+ vec4 p4 = grad4(j1.w, ip);
112
+
113
+ // Normalise gradients
114
+ vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
115
+ p0 *= norm.x;
116
+ p1 *= norm.y;
117
+ p2 *= norm.z;
118
+ p3 *= norm.w;
119
+ p4 *= taylorInvSqrt(dot(p4,p4));
120
+
121
+ // Mix contributions from the five corners
122
+ vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0);
123
+ vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0);
124
+ m0 = m0 * m0;
125
+ m1 = m1 * m1;
126
+ return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
127
+ + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;
128
+
129
+ }
@@ -0,0 +1,463 @@
1
+ //
2
+ // vec3 psrdnoise(vec2 pos, vec2 per, float rot)
3
+ // vec3 psdnoise(vec2 pos, vec2 per)
4
+ // float psrnoise(vec2 pos, vec2 per, float rot)
5
+ // float psnoise(vec2 pos, vec2 per)
6
+ // vec3 srdnoise(vec2 pos, float rot)
7
+ // vec3 sdnoise(vec2 pos)
8
+ // float srnoise(vec2 pos, float rot)
9
+ // float snoise(vec2 pos)
10
+ //
11
+ // Periodic (tiling) 2-D simplex noise (hexagonal lattice gradient noise)
12
+ // with rotating gradients and analytic derivatives.
13
+ // Variants also without the derivative (no "d" in the name), without
14
+ // the tiling property (no "p" in the name) and without the rotating
15
+ // gradients (no "r" in the name).
16
+ //
17
+ // This is (yet) another variation on simplex noise. It's similar to the
18
+ // version presented by Ken Perlin, but the grid is axis-aligned and
19
+ // slightly stretched in the y direction to permit rectangular tiling.
20
+ //
21
+ // The noise can be made to tile seamlessly to any integer period in x and
22
+ // any even integer period in y. Odd periods may be specified for y, but
23
+ // then the actual tiling period will be twice that number.
24
+ //
25
+ // The rotating gradients give the appearance of a swirling motion, and can
26
+ // serve a similar purpose for animation as motion along z in 3-D noise.
27
+ // The rotating gradients in conjunction with the analytic derivatives
28
+ // can make "flow noise" effects as presented by Perlin and Neyret.
29
+ //
30
+ // vec3 {p}s{r}dnoise(vec2 pos {, vec2 per} {, float rot})
31
+ // "pos" is the input (x,y) coordinate
32
+ // "per" is the x and y period, where per.x is a positive integer
33
+ // and per.y is a positive even integer
34
+ // "rot" is the angle to rotate the gradients (any float value,
35
+ // where 0.0 is no rotation and 1.0 is one full turn)
36
+ // The first component of the 3-element return vector is the noise value.
37
+ // The second and third components are the x and y partial derivatives.
38
+ //
39
+ // float {p}s{r}noise(vec2 pos {, vec2 per} {, float rot})
40
+ // "pos" is the input (x,y) coordinate
41
+ // "per" is the x and y period, where per.x is a positive integer
42
+ // and per.y is a positive even integer
43
+ // "rot" is the angle to rotate the gradients (any float value,
44
+ // where 0.0 is no rotation and 1.0 is one full turn)
45
+ // The return value is the noise value.
46
+ // Partial derivatives are not computed, making these functions faster.
47
+ //
48
+ // Author: Stefan Gustavson (stefan.gustavson@gmail.com)
49
+ // Version 2016-05-10.
50
+ //
51
+ // Many thanks to Ian McEwan of Ashima Arts for the
52
+ // idea of using a permutation polynomial.
53
+ //
54
+ // Copyright (c) 2016 Stefan Gustavson. All rights reserved.
55
+ // Distributed under the MIT license. See LICENSE file.
56
+ // https://github.com/stegu/webgl-noise
57
+ //
58
+
59
+ //
60
+ // TODO: One-pixel wide artefacts used to occur due to precision issues with
61
+ // the gradient indexing. This is specific to this variant of noise, because
62
+ // one axis of the simplex grid is perfectly aligned with the input x axis.
63
+ // The errors were rare, and they are now very unlikely to ever be visible
64
+ // after a quick fix was introduced: a small offset is added to the y coordinate.
65
+ // A proper fix would involve using round() instead of floor() in selected
66
+ // places, but the quick fix works fine.
67
+ // (If you run into problems with this, please let me know.)
68
+ //
69
+
70
+ // Modulo 289, optimizes to code without divisions
71
+ vec3 mod289(vec3 x) {
72
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
73
+ }
74
+
75
+ // Permutation polynomial (ring size 289 = 17*17)
76
+ float permute(float x) { return mod(((x * 34.0) + 1.0) * x, 289.0); }
77
+ vec3 permute(vec3 x) {
78
+ return mod289(((x*34.0)+1.0)*x);
79
+ }
80
+
81
+ // Hashed 2-D gradients with an extra rotation.
82
+ // (The constant 0.0243902439 is 1/41)
83
+ vec2 rgrad2(vec2 p, float rot) {
84
+ #if 0
85
+ // Map from a line to a diamond such that a shift maps to a rotation.
86
+ float u = permute(permute(p.x) + p.y) * 0.0243902439 + rot; // Rotate by shift
87
+ u = 4.0 * fract(u) - 2.0;
88
+ // (This vector could be normalized, exactly or approximately.)
89
+ return vec2(abs(u)-1.0, abs(abs(u+1.0)-2.0)-1.0);
90
+ #else
91
+ // For more isotropic gradients, sin/cos can be used instead.
92
+ float u = permute(permute(p.x) + p.y) * 0.0243902439 + rot; // Rotate by shift
93
+ u = fract(u) * 6.28318530718; // 2*pi
94
+ return vec2(cos(u), sin(u));
95
+ #endif
96
+ }
97
+
98
+ //
99
+ // 2-D tiling simplex noise with rotating gradients and analytical derivative.
100
+ // The first component of the 3-element return vector is the noise value,
101
+ // and the second and third components are the x and y partial derivatives.
102
+ //
103
+ vec3 psrdnoise(vec2 pos, vec2 per, float rot) {
104
+ // Hack: offset y slightly to hide some rare artifacts
105
+ pos.y += 0.01;
106
+ // Skew to hexagonal grid
107
+ vec2 uv = vec2(pos.x + pos.y*0.5, pos.y);
108
+
109
+ vec2 i0 = floor(uv);
110
+ vec2 f0 = fract(uv);
111
+ // Traversal order
112
+ vec2 i1 = (f0.x > f0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
113
+
114
+ // Unskewed grid points in (x,y) space
115
+ vec2 p0 = vec2(i0.x - i0.y * 0.5, i0.y);
116
+ vec2 p1 = vec2(p0.x + i1.x - i1.y * 0.5, p0.y + i1.y);
117
+ vec2 p2 = vec2(p0.x + 0.5, p0.y + 1.0);
118
+
119
+ // Integer grid point indices in (u,v) space
120
+ i1 = i0 + i1;
121
+ vec2 i2 = i0 + vec2(1.0, 1.0);
122
+
123
+ // Vectors in unskewed (x,y) coordinates from
124
+ // each of the simplex corners to the evaluation point
125
+ vec2 d0 = pos - p0;
126
+ vec2 d1 = pos - p1;
127
+ vec2 d2 = pos - p2;
128
+
129
+ // Wrap i0, i1 and i2 to the desired period before gradient hashing:
130
+ // wrap points in (x,y), map to (u,v)
131
+ vec3 xw = mod(vec3(p0.x, p1.x, p2.x), per.x);
132
+ vec3 yw = mod(vec3(p0.y, p1.y, p2.y), per.y);
133
+ vec3 iuw = xw + 0.5 * yw;
134
+ vec3 ivw = yw;
135
+
136
+ // Create gradients from indices
137
+ vec2 g0 = rgrad2(vec2(iuw.x, ivw.x), rot);
138
+ vec2 g1 = rgrad2(vec2(iuw.y, ivw.y), rot);
139
+ vec2 g2 = rgrad2(vec2(iuw.z, ivw.z), rot);
140
+
141
+ // Gradients dot vectors to corresponding corners
142
+ // (The derivatives of this are simply the gradients)
143
+ vec3 w = vec3(dot(g0, d0), dot(g1, d1), dot(g2, d2));
144
+
145
+ // Radial weights from corners
146
+ // 0.8 is the square of 2/sqrt(5), the distance from
147
+ // a grid point to the nearest simplex boundary
148
+ vec3 t = 0.8 - vec3(dot(d0, d0), dot(d1, d1), dot(d2, d2));
149
+
150
+ // Partial derivatives for analytical gradient computation
151
+ vec3 dtdx = -2.0 * vec3(d0.x, d1.x, d2.x);
152
+ vec3 dtdy = -2.0 * vec3(d0.y, d1.y, d2.y);
153
+
154
+ // Set influence of each surflet to zero outside radius sqrt(0.8)
155
+ if (t.x < 0.0) {
156
+ dtdx.x = 0.0;
157
+ dtdy.x = 0.0;
158
+ t.x = 0.0;
159
+ }
160
+ if (t.y < 0.0) {
161
+ dtdx.y = 0.0;
162
+ dtdy.y = 0.0;
163
+ t.y = 0.0;
164
+ }
165
+ if (t.z < 0.0) {
166
+ dtdx.z = 0.0;
167
+ dtdy.z = 0.0;
168
+ t.z = 0.0;
169
+ }
170
+
171
+ // Fourth power of t (and third power for derivative)
172
+ vec3 t2 = t * t;
173
+ vec3 t4 = t2 * t2;
174
+ vec3 t3 = t2 * t;
175
+
176
+ // Final noise value is:
177
+ // sum of ((radial weights) times (gradient dot vector from corner))
178
+ float n = dot(t4, w);
179
+
180
+ // Final analytical derivative (gradient of a sum of scalar products)
181
+ vec2 dt0 = vec2(dtdx.x, dtdy.x) * 4.0 * t3.x;
182
+ vec2 dn0 = t4.x * g0 + dt0 * w.x;
183
+ vec2 dt1 = vec2(dtdx.y, dtdy.y) * 4.0 * t3.y;
184
+ vec2 dn1 = t4.y * g1 + dt1 * w.y;
185
+ vec2 dt2 = vec2(dtdx.z, dtdy.z) * 4.0 * t3.z;
186
+ vec2 dn2 = t4.z * g2 + dt2 * w.z;
187
+
188
+ return 11.0*vec3(n, dn0 + dn1 + dn2);
189
+ }
190
+
191
+ //
192
+ // 2-D tiling simplex noise with fixed gradients
193
+ // and analytical derivative.
194
+ // This function is implemented as a wrapper to "psrdnoise",
195
+ // at the minimal cost of three extra additions.
196
+ //
197
+ vec3 psdnoise(vec2 pos, vec2 per) {
198
+ return psrdnoise(pos, per, 0.0);
199
+ }
200
+
201
+ //
202
+ // 2-D tiling simplex noise with rotating gradients,
203
+ // but without the analytical derivative.
204
+ //
205
+ float psrnoise(vec2 pos, vec2 per, float rot) {
206
+ // Offset y slightly to hide some rare artifacts
207
+ pos.y += 0.001;
208
+ // Skew to hexagonal grid
209
+ vec2 uv = vec2(pos.x + pos.y*0.5, pos.y);
210
+
211
+ vec2 i0 = floor(uv);
212
+ vec2 f0 = fract(uv);
213
+ // Traversal order
214
+ vec2 i1 = (f0.x > f0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
215
+
216
+ // Unskewed grid points in (x,y) space
217
+ vec2 p0 = vec2(i0.x - i0.y * 0.5, i0.y);
218
+ vec2 p1 = vec2(p0.x + i1.x - i1.y * 0.5, p0.y + i1.y);
219
+ vec2 p2 = vec2(p0.x + 0.5, p0.y + 1.0);
220
+
221
+ // Integer grid point indices in (u,v) space
222
+ i1 = i0 + i1;
223
+ vec2 i2 = i0 + vec2(1.0, 1.0);
224
+
225
+ // Vectors in unskewed (x,y) coordinates from
226
+ // each of the simplex corners to the evaluation point
227
+ vec2 d0 = pos - p0;
228
+ vec2 d1 = pos - p1;
229
+ vec2 d2 = pos - p2;
230
+
231
+ // Wrap i0, i1 and i2 to the desired period before gradient hashing:
232
+ // wrap points in (x,y), map to (u,v)
233
+ vec3 xw = mod(vec3(p0.x, p1.x, p2.x), per.x);
234
+ vec3 yw = mod(vec3(p0.y, p1.y, p2.y), per.y);
235
+ vec3 iuw = xw + 0.5 * yw;
236
+ vec3 ivw = yw;
237
+
238
+ // Create gradients from indices
239
+ vec2 g0 = rgrad2(vec2(iuw.x, ivw.x), rot);
240
+ vec2 g1 = rgrad2(vec2(iuw.y, ivw.y), rot);
241
+ vec2 g2 = rgrad2(vec2(iuw.z, ivw.z), rot);
242
+
243
+ // Gradients dot vectors to corresponding corners
244
+ // (The derivatives of this are simply the gradients)
245
+ vec3 w = vec3(dot(g0, d0), dot(g1, d1), dot(g2, d2));
246
+
247
+ // Radial weights from corners
248
+ // 0.8 is the square of 2/sqrt(5), the distance from
249
+ // a grid point to the nearest simplex boundary
250
+ vec3 t = 0.8 - vec3(dot(d0, d0), dot(d1, d1), dot(d2, d2));
251
+
252
+ // Set influence of each surflet to zero outside radius sqrt(0.8)
253
+ t = max(t, 0.0);
254
+
255
+ // Fourth power of t
256
+ vec3 t2 = t * t;
257
+ vec3 t4 = t2 * t2;
258
+
259
+ // Final noise value is:
260
+ // sum of ((radial weights) times (gradient dot vector from corner))
261
+ float n = dot(t4, w);
262
+
263
+ // Rescale to cover the range [-1,1] reasonably well
264
+ return 11.0*n;
265
+ }
266
+
267
+ //
268
+ // 2-D tiling simplex noise with fixed gradients,
269
+ // without the analytical derivative.
270
+ // This function is implemented as a wrapper to "psrnoise",
271
+ // at the minimal cost of three extra additions.
272
+ //
273
+ float psnoise(vec2 pos, vec2 per) {
274
+ return psrnoise(pos, per, 0.0);
275
+ }
276
+
277
+ //
278
+ // 2-D non-tiling simplex noise with rotating gradients and analytical derivative.
279
+ // The first component of the 3-element return vector is the noise value,
280
+ // and the second and third components are the x and y partial derivatives.
281
+ //
282
+ vec3 srdnoise(vec2 pos, float rot) {
283
+ // Offset y slightly to hide some rare artifacts
284
+ pos.y += 0.001;
285
+ // Skew to hexagonal grid
286
+ vec2 uv = vec2(pos.x + pos.y*0.5, pos.y);
287
+
288
+ vec2 i0 = floor(uv);
289
+ vec2 f0 = fract(uv);
290
+ // Traversal order
291
+ vec2 i1 = (f0.x > f0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
292
+
293
+ // Unskewed grid points in (x,y) space
294
+ vec2 p0 = vec2(i0.x - i0.y * 0.5, i0.y);
295
+ vec2 p1 = vec2(p0.x + i1.x - i1.y * 0.5, p0.y + i1.y);
296
+ vec2 p2 = vec2(p0.x + 0.5, p0.y + 1.0);
297
+
298
+ // Integer grid point indices in (u,v) space
299
+ i1 = i0 + i1;
300
+ vec2 i2 = i0 + vec2(1.0, 1.0);
301
+
302
+ // Vectors in unskewed (x,y) coordinates from
303
+ // each of the simplex corners to the evaluation point
304
+ vec2 d0 = pos - p0;
305
+ vec2 d1 = pos - p1;
306
+ vec2 d2 = pos - p2;
307
+
308
+ vec3 x = vec3(p0.x, p1.x, p2.x);
309
+ vec3 y = vec3(p0.y, p1.y, p2.y);
310
+ vec3 iuw = x + 0.5 * y;
311
+ vec3 ivw = y;
312
+
313
+ // Avoid precision issues in permutation
314
+ iuw = mod289(iuw);
315
+ ivw = mod289(ivw);
316
+
317
+ // Create gradients from indices
318
+ vec2 g0 = rgrad2(vec2(iuw.x, ivw.x), rot);
319
+ vec2 g1 = rgrad2(vec2(iuw.y, ivw.y), rot);
320
+ vec2 g2 = rgrad2(vec2(iuw.z, ivw.z), rot);
321
+
322
+ // Gradients dot vectors to corresponding corners
323
+ // (The derivatives of this are simply the gradients)
324
+ vec3 w = vec3(dot(g0, d0), dot(g1, d1), dot(g2, d2));
325
+
326
+ // Radial weights from corners
327
+ // 0.8 is the square of 2/sqrt(5), the distance from
328
+ // a grid point to the nearest simplex boundary
329
+ vec3 t = 0.8 - vec3(dot(d0, d0), dot(d1, d1), dot(d2, d2));
330
+
331
+ // Partial derivatives for analytical gradient computation
332
+ vec3 dtdx = -2.0 * vec3(d0.x, d1.x, d2.x);
333
+ vec3 dtdy = -2.0 * vec3(d0.y, d1.y, d2.y);
334
+
335
+ // Set influence of each surflet to zero outside radius sqrt(0.8)
336
+ if (t.x < 0.0) {
337
+ dtdx.x = 0.0;
338
+ dtdy.x = 0.0;
339
+ t.x = 0.0;
340
+ }
341
+ if (t.y < 0.0) {
342
+ dtdx.y = 0.0;
343
+ dtdy.y = 0.0;
344
+ t.y = 0.0;
345
+ }
346
+ if (t.z < 0.0) {
347
+ dtdx.z = 0.0;
348
+ dtdy.z = 0.0;
349
+ t.z = 0.0;
350
+ }
351
+
352
+ // Fourth power of t (and third power for derivative)
353
+ vec3 t2 = t * t;
354
+ vec3 t4 = t2 * t2;
355
+ vec3 t3 = t2 * t;
356
+
357
+ // Final noise value is:
358
+ // sum of ((radial weights) times (gradient dot vector from corner))
359
+ float n = dot(t4, w);
360
+
361
+ // Final analytical derivative (gradient of a sum of scalar products)
362
+ vec2 dt0 = vec2(dtdx.x, dtdy.x) * 4.0 * t3.x;
363
+ vec2 dn0 = t4.x * g0 + dt0 * w.x;
364
+ vec2 dt1 = vec2(dtdx.y, dtdy.y) * 4.0 * t3.y;
365
+ vec2 dn1 = t4.y * g1 + dt1 * w.y;
366
+ vec2 dt2 = vec2(dtdx.z, dtdy.z) * 4.0 * t3.z;
367
+ vec2 dn2 = t4.z * g2 + dt2 * w.z;
368
+
369
+ return 11.0*vec3(n, dn0 + dn1 + dn2);
370
+ }
371
+
372
+ //
373
+ // 2-D non-tiling simplex noise with fixed gradients and analytical derivative.
374
+ // This function is implemented as a wrapper to "srdnoise",
375
+ // at the minimal cost of three extra additions.
376
+ //
377
+ vec3 sdnoise(vec2 pos) {
378
+ return srdnoise(pos, 0.0);
379
+ }
380
+
381
+ //
382
+ // 2-D non-tiling simplex noise with rotating gradients,
383
+ // without the analytical derivative.
384
+ //
385
+ float srnoise(vec2 pos, float rot) {
386
+ // Offset y slightly to hide some rare artifacts
387
+ pos.y += 0.001;
388
+ // Skew to hexagonal grid
389
+ vec2 uv = vec2(pos.x + pos.y*0.5, pos.y);
390
+
391
+ vec2 i0 = floor(uv);
392
+ vec2 f0 = fract(uv);
393
+ // Traversal order
394
+ vec2 i1 = (f0.x > f0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
395
+
396
+ // Unskewed grid points in (x,y) space
397
+ vec2 p0 = vec2(i0.x - i0.y * 0.5, i0.y);
398
+ vec2 p1 = vec2(p0.x + i1.x - i1.y * 0.5, p0.y + i1.y);
399
+ vec2 p2 = vec2(p0.x + 0.5, p0.y + 1.0);
400
+
401
+ // Integer grid point indices in (u,v) space
402
+ i1 = i0 + i1;
403
+ vec2 i2 = i0 + vec2(1.0, 1.0);
404
+
405
+ // Vectors in unskewed (x,y) coordinates from
406
+ // each of the simplex corners to the evaluation point
407
+ vec2 d0 = pos - p0;
408
+ vec2 d1 = pos - p1;
409
+ vec2 d2 = pos - p2;
410
+
411
+ // Wrap i0, i1 and i2 to the desired period before gradient hashing:
412
+ // wrap points in (x,y), map to (u,v)
413
+ vec3 x = vec3(p0.x, p1.x, p2.x);
414
+ vec3 y = vec3(p0.y, p1.y, p2.y);
415
+ vec3 iuw = x + 0.5 * y;
416
+ vec3 ivw = y;
417
+
418
+ // Avoid precision issues in permutation
419
+ iuw = mod289(iuw);
420
+ ivw = mod289(ivw);
421
+
422
+ // Create gradients from indices
423
+ vec2 g0 = rgrad2(vec2(iuw.x, ivw.x), rot);
424
+ vec2 g1 = rgrad2(vec2(iuw.y, ivw.y), rot);
425
+ vec2 g2 = rgrad2(vec2(iuw.z, ivw.z), rot);
426
+
427
+ // Gradients dot vectors to corresponding corners
428
+ // (The derivatives of this are simply the gradients)
429
+ vec3 w = vec3(dot(g0, d0), dot(g1, d1), dot(g2, d2));
430
+
431
+ // Radial weights from corners
432
+ // 0.8 is the square of 2/sqrt(5), the distance from
433
+ // a grid point to the nearest simplex boundary
434
+ vec3 t = 0.8 - vec3(dot(d0, d0), dot(d1, d1), dot(d2, d2));
435
+
436
+ // Set influence of each surflet to zero outside radius sqrt(0.8)
437
+ t = max(t, 0.0);
438
+
439
+ // Fourth power of t
440
+ vec3 t2 = t * t;
441
+ vec3 t4 = t2 * t2;
442
+
443
+ // Final noise value is:
444
+ // sum of ((radial weights) times (gradient dot vector from corner))
445
+ float n = dot(t4, w);
446
+
447
+ // Rescale to cover the range [-1,1] reasonably well
448
+ return 11.0*n;
449
+ }
450
+
451
+ //
452
+ // 2-D non-tiling simplex noise with fixed gradients,
453
+ // without the analytical derivative.
454
+ // This function is implemented as a wrapper to "srnoise",
455
+ // at the minimal cost of three extra additions.
456
+ // Note: if this kind of noise is all you want, there are faster
457
+ // GLSL implementations of non-tiling simplex noise out there.
458
+ // This one is included mainly for completeness and compatibility
459
+ // with the other functions in the file.
460
+ //
461
+ float snoise(vec2 pos) {
462
+ return srnoise(pos, 0.0);
463
+ }