2 // GLSL textureless classic 2D noise "cnoise",
3 // with an RSL-style periodic variant "pnoise".
4 // Author: Stefan Gustavson (stefan.gustavson@liu.se)
7 // Many thanks to Ian McEwan of Ashima Arts for the
8 // ideas for permutation and gradient selection.
10 // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
11 // Distributed under the MIT license. See LICENSE file.
12 // https://github.com/ashima/webgl-noise
17 return x - floor(x * (1.0 / 289.0)) * 289.0;
22 return mod289(((x*34.0)+1.0)*x);
25 vec4 taylorInvSqrt(vec4 r)
27 return 1.79284291400159 - 0.85373472095314 * r;
31 return t*t*t*(t*(t*6.0-15.0)+10.0);
34 // Classic Perlin noise, periodic variant
35 float pnoise(vec2 P, vec2 rep)
37 vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
38 vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
39 Pi = mod(Pi, rep.xyxy); // To create noise with explicit period
40 Pi = mod289(Pi); // To avoid truncation effects in permutation
46 vec4 i = permute(permute(ix) + iy);
48 vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
49 vec4 gy = abs(gx) - 0.5 ;
50 vec4 tx = floor(gx + 0.5);
53 vec2 g00 = vec2(gx.x,gy.x);
54 vec2 g10 = vec2(gx.y,gy.y);
55 vec2 g01 = vec2(gx.z,gy.z);
56 vec2 g11 = vec2(gx.w,gy.w);
58 vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
64 float n00 = dot(g00, vec2(fx.x, fy.x));
65 float n10 = dot(g10, vec2(fx.y, fy.y));
66 float n01 = dot(g01, vec2(fx.z, fy.z));
67 float n11 = dot(g11, vec2(fx.w, fy.w));
69 vec2 fade_xy = fade(Pf.xy);
70 vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
71 float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
75 #pragma glslify: export(pnoise)