2 // Description : Array and textureless GLSL 2D simplex noise function.
3 // Author : Ian McEwan, Ashima Arts.
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
12 return x - floor(x * (1.0 / 289.0)) * 289.0;
16 return x - floor(x * (1.0 / 289.0)) * 289.0;
19 vec3 permute(vec3 x) {
20 return mod289(((x*34.0)+1.0)*x);
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
30 vec2 i = floor(v + dot(v, C.yy) );
31 vec2 x0 = v - i + dot(i, C.xx);
35 //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
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;
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 ));
49 vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
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)
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);
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 );
65 // Compute final noise value at P
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);
72 #pragma glslify: export(snoise)