84944be0c380a0ccb1e74feff30133bdc0856916
[pwl6.git] / src / data / shaders / noise.glsl
1 //
2 // Description : Array and textureless GLSL 2D/3D/4D simplex
3 // noise functions.
4 // Author : Ian McEwan, Ashima Arts.
5 // Maintainer : ijm
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 //
11 /*
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in
20 all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 THE SOFTWARE.
29 */
30
31 precision highp float;
32
33 vec2 mod289(vec2 x) {
34 return x - floor(x * (1.0 / 289.0)) * 289.0;
35 }
36
37 vec3 mod289(vec3 x) {
38 return x - floor(x * (1.0 / 289.0)) * 289.0;
39 }
40
41 vec4 mod289(vec4 x) {
42 return x - floor(x * (1.0 / 289.0)) * 289.0;
43 }
44
45 vec3 permute(vec3 x) {
46 return mod289(((x*34.0)+1.0)*x);
47 }
48
49 vec4 permute(vec4 x) {
50 return mod289(((x*34.0)+1.0)*x);
51 }
52
53 vec4 taylorInvSqrt(vec4 r)
54 {
55 return 1.79284291400159 - 0.85373472095314 * r;
56 }
57
58 float snoise(vec3 v)
59 {
60 const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
61 const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
62
63 // First corner
64 vec3 i = floor(v + dot(v, C.yyy) );
65 vec3 x0 = v - i + dot(i, C.xxx) ;
66
67 // Other corners
68 vec3 g = step(x0.yzx, x0.xyz);
69 vec3 l = 1.0 - g;
70 vec3 i1 = min( g.xyz, l.zxy );
71 vec3 i2 = max( g.xyz, l.zxy );
72
73 // x0 = x0 - 0.0 + 0.0 * C.xxx;
74 // x1 = x0 - i1 + 1.0 * C.xxx;
75 // x2 = x0 - i2 + 2.0 * C.xxx;
76 // x3 = x0 - 1.0 + 3.0 * C.xxx;
77 vec3 x1 = x0 - i1 + C.xxx;
78 vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
79 vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
80
81 // Permutations
82 i = mod289(i);
83 vec4 p = permute( permute( permute(
84 i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
85 + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
86 + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
87
88 // Gradients: 7x7 points over a square, mapped onto an octahedron.
89 // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
90 float n_ = 0.142857142857; // 1.0/7.0
91 vec3 ns = n_ * D.wyz - D.xzx;
92
93 vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
94
95 vec4 x_ = floor(j * ns.z);
96 vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
97
98 vec4 x = x_ *ns.x + ns.yyyy;
99 vec4 y = y_ *ns.x + ns.yyyy;
100 vec4 h = 1.0 - abs(x) - abs(y);
101
102 vec4 b0 = vec4( x.xy, y.xy );
103 vec4 b1 = vec4( x.zw, y.zw );
104
105 //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
106 //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
107 vec4 s0 = floor(b0)*2.0 + 1.0;
108 vec4 s1 = floor(b1)*2.0 + 1.0;
109 vec4 sh = -step(h, vec4(0.0));
110
111 vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
112 vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
113
114 vec3 p0 = vec3(a0.xy,h.x);
115 vec3 p1 = vec3(a0.zw,h.y);
116 vec3 p2 = vec3(a1.xy,h.z);
117 vec3 p3 = vec3(a1.zw,h.w);
118
119 //Normalise gradients
120 vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
121 p0 *= norm.x;
122 p1 *= norm.y;
123 p2 *= norm.z;
124 p3 *= norm.w;
125
126 // Mix final noise value
127 vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
128 m = m * m;
129 return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
130 dot(p2,x2), dot(p3,x3) ) );
131 }
132
133 float snoise(vec2 v)
134 {
135 const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
136 0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
137 -0.577350269189626, // -1.0 + 2.0 * C.x
138 0.024390243902439); // 1.0 / 41.0
139 // First corner
140 vec2 i = floor(v + dot(v, C.yy) );
141 vec2 x0 = v - i + dot(i, C.xx);
142
143 // Other corners
144 vec2 i1;
145 //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
146 //i1.y = 1.0 - i1.x;
147 i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
148 // x0 = x0 - 0.0 + 0.0 * C.xx ;
149 // x1 = x0 - i1 + 1.0 * C.xx ;
150 // x2 = x0 - 1.0 + 2.0 * C.xx ;
151 vec4 x12 = x0.xyxy + C.xxzz;
152 x12.xy -= i1;
153
154 // Permutations
155 i = mod289(i); // Avoid truncation effects in permutation
156 vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
157 + i.x + vec3(0.0, i1.x, 1.0 ));
158
159 vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
160 m = m*m ;
161 m = m*m ;
162
163 // Gradients: 41 points uniformly over a line, mapped onto a diamond.
164 // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
165
166 vec3 x = 2.0 * fract(p * C.www) - 1.0;
167 vec3 h = abs(x) - 0.5;
168 vec3 ox = floor(x + 0.5);
169 vec3 a0 = x - ox;
170
171 // Normalise gradients implicitly by scaling m
172 // Approximation of: m *= inversesqrt( a0*a0 + h*h );
173 m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
174
175 // Compute final noise value at P
176 vec3 g;
177 g.x = a0.x * x0.x + h.x * x0.y;
178 g.yz = a0.yz * x12.xz + h.yz * x12.yw;
179 return 130.0 * dot(m, g);
180 }