faros initial commit
[faros-demo] / glsl-noise / simplex / 2d.glsl
1 //
2 // Description : Array and textureless GLSL 2D simplex noise function.
3 //      Author : Ian McEwan, Ashima Arts.
4 //  Maintainer : ijm
5 //     Lastmod : 20110822 (ijm)
6 //     License : Copyright (C) 2011 Ashima Arts. All rights reserved.
7 //               Distributed under the MIT License. See LICENSE file.
8 //               https://github.com/ashima/webgl-noise
9 //
10
11 vec3 mod289(vec3 x) {
12   return x - floor(x * (1.0 / 289.0)) * 289.0;
13 }
14
15 vec2 mod289(vec2 x) {
16   return x - floor(x * (1.0 / 289.0)) * 289.0;
17 }
18
19 vec3 permute(vec3 x) {
20   return mod289(((x*34.0)+1.0)*x);
21 }
22
23 float snoise(vec2 v)
24   {
25   const vec4 C = vec4(0.211324865405187,  // (3.0-sqrt(3.0))/6.0
26                       0.366025403784439,  // 0.5*(sqrt(3.0)-1.0)
27                      -0.577350269189626,  // -1.0 + 2.0 * C.x
28                       0.024390243902439); // 1.0 / 41.0
29 // First corner
30   vec2 i  = floor(v + dot(v, C.yy) );
31   vec2 x0 = v -   i + dot(i, C.xx);
32
33 // Other corners
34   vec2 i1;
35   //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
36   //i1.y = 1.0 - i1.x;
37   i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
38   // x0 = x0 - 0.0 + 0.0 * C.xx ;
39   // x1 = x0 - i1 + 1.0 * C.xx ;
40   // x2 = x0 - 1.0 + 2.0 * C.xx ;
41   vec4 x12 = x0.xyxy + C.xxzz;
42   x12.xy -= i1;
43
44 // Permutations
45   i = mod289(i); // Avoid truncation effects in permutation
46   vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
47     + i.x + vec3(0.0, i1.x, 1.0 ));
48
49   vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
50   m = m*m ;
51   m = m*m ;
52
53 // Gradients: 41 points uniformly over a line, mapped onto a diamond.
54 // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
55
56   vec3 x = 2.0 * fract(p * C.www) - 1.0;
57   vec3 h = abs(x) - 0.5;
58   vec3 ox = floor(x + 0.5);
59   vec3 a0 = x - ox;
60
61 // Normalise gradients implicitly by scaling m
62 // Approximation of: m *= inversesqrt( a0*a0 + h*h );
63   m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
64
65 // Compute final noise value at P
66   vec3 g;
67   g.x  = a0.x  * x0.x  + h.x  * x0.y;
68   g.yz = a0.yz * x12.xz + h.yz * x12.yw;
69   return 130.0 * dot(m, g);
70 }
71
72 #pragma glslify: export(snoise)