├── LICENSE ├── README.md ├── classic ├── 2d.glsl ├── 3d.glsl └── 4d.glsl ├── package.json ├── periodic ├── 2d.glsl ├── 3d.glsl └── 4d.glsl └── simplex ├── 2d.glsl ├── 3d.glsl └── 4d.glsl /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2011 by Ashima Arts (Simplex noise) 2 | Copyright (C) 2011 by Stefan Gustavson (Classic noise) 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # glsl-noise [![frozen](http://hughsk.github.io/stability-badges/dist/frozen.svg)](http://github.com/hughsk/stability-badges) # 2 | 3 | [webgl-noise](http://github.com/ashima/webgl-noise) ported to an NPM package 4 | so that you can require it from 5 | [glslify](http://github.com/chrisdickinson/glslify). 6 | 7 | [![glsl-noise](https://nodei.co/npm/glsl-noise.png?mini=true)](https://nodei.co/npm/glsl-noise) 8 | 9 | ## Usage ## 10 | 11 | ``` glsl 12 | // Require as many or as little as you need: 13 | #pragma glslify: snoise2 = require(glsl-noise/simplex/2d) 14 | #pragma glslify: snoise3 = require(glsl-noise/simplex/3d) 15 | #pragma glslify: snoise4 = require(glsl-noise/simplex/4d) 16 | #pragma glslify: cnoise2 = require(glsl-noise/classic/2d) 17 | #pragma glslify: cnoise3 = require(glsl-noise/classic/3d) 18 | #pragma glslify: cnoise4 = require(glsl-noise/classic/4d) 19 | #pragma glslify: pnoise2 = require(glsl-noise/periodic/2d) 20 | #pragma glslify: pnoise3 = require(glsl-noise/periodic/3d) 21 | #pragma glslify: pnoise4 = require(glsl-noise/periodic/4d) 22 | 23 | attribute vec3 position; 24 | 25 | // And just treat them as functions like 26 | // you normally would: 27 | void main() { 28 | gl_FragColor = vec4(snoise3(position), 1.0); 29 | } 30 | ``` 31 | -------------------------------------------------------------------------------- /classic/2d.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // GLSL textureless classic 2D noise "cnoise", 3 | // with an RSL-style periodic variant "pnoise". 4 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 5 | // Version: 2011-08-22 6 | // 7 | // Many thanks to Ian McEwan of Ashima Arts for the 8 | // ideas for permutation and gradient selection. 9 | // 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 13 | // 14 | 15 | vec4 mod289(vec4 x) 16 | { 17 | return x - floor(x * (1.0 / 289.0)) * 289.0; 18 | } 19 | 20 | vec4 permute(vec4 x) 21 | { 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 | vec2 fade(vec2 t) { 31 | return t*t*t*(t*(t*6.0-15.0)+10.0); 32 | } 33 | 34 | // Classic Perlin noise 35 | float cnoise(vec2 P) 36 | { 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 = mod289(Pi); // To avoid truncation effects in permutation 40 | vec4 ix = Pi.xzxz; 41 | vec4 iy = Pi.yyww; 42 | vec4 fx = Pf.xzxz; 43 | vec4 fy = Pf.yyww; 44 | 45 | vec4 i = permute(permute(ix) + iy); 46 | 47 | vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ; 48 | vec4 gy = abs(gx) - 0.5 ; 49 | vec4 tx = floor(gx + 0.5); 50 | gx = gx - tx; 51 | 52 | vec2 g00 = vec2(gx.x,gy.x); 53 | vec2 g10 = vec2(gx.y,gy.y); 54 | vec2 g01 = vec2(gx.z,gy.z); 55 | vec2 g11 = vec2(gx.w,gy.w); 56 | 57 | vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11))); 58 | g00 *= norm.x; 59 | g01 *= norm.y; 60 | g10 *= norm.z; 61 | g11 *= norm.w; 62 | 63 | float n00 = dot(g00, vec2(fx.x, fy.x)); 64 | float n10 = dot(g10, vec2(fx.y, fy.y)); 65 | float n01 = dot(g01, vec2(fx.z, fy.z)); 66 | float n11 = dot(g11, vec2(fx.w, fy.w)); 67 | 68 | vec2 fade_xy = fade(Pf.xy); 69 | vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x); 70 | float n_xy = mix(n_x.x, n_x.y, fade_xy.y); 71 | return 2.3 * n_xy; 72 | } 73 | 74 | #pragma glslify: export(cnoise) 75 | -------------------------------------------------------------------------------- /classic/3d.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // GLSL textureless classic 3D noise "cnoise", 3 | // with an RSL-style periodic variant "pnoise". 4 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 5 | // Version: 2011-10-11 6 | // 7 | // Many thanks to Ian McEwan of Ashima Arts for the 8 | // ideas for permutation and gradient selection. 9 | // 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 13 | // 14 | 15 | vec3 mod289(vec3 x) 16 | { 17 | return x - floor(x * (1.0 / 289.0)) * 289.0; 18 | } 19 | 20 | vec4 mod289(vec4 x) 21 | { 22 | return x - floor(x * (1.0 / 289.0)) * 289.0; 23 | } 24 | 25 | vec4 permute(vec4 x) 26 | { 27 | return mod289(((x*34.0)+1.0)*x); 28 | } 29 | 30 | vec4 taylorInvSqrt(vec4 r) 31 | { 32 | return 1.79284291400159 - 0.85373472095314 * r; 33 | } 34 | 35 | vec3 fade(vec3 t) { 36 | return t*t*t*(t*(t*6.0-15.0)+10.0); 37 | } 38 | 39 | // Classic Perlin noise 40 | float cnoise(vec3 P) 41 | { 42 | vec3 Pi0 = floor(P); // Integer part for indexing 43 | vec3 Pi1 = Pi0 + vec3(1.0); // Integer part + 1 44 | Pi0 = mod289(Pi0); 45 | Pi1 = mod289(Pi1); 46 | vec3 Pf0 = fract(P); // Fractional part for interpolation 47 | vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0 48 | vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 49 | vec4 iy = vec4(Pi0.yy, Pi1.yy); 50 | vec4 iz0 = Pi0.zzzz; 51 | vec4 iz1 = Pi1.zzzz; 52 | 53 | vec4 ixy = permute(permute(ix) + iy); 54 | vec4 ixy0 = permute(ixy + iz0); 55 | vec4 ixy1 = permute(ixy + iz1); 56 | 57 | vec4 gx0 = ixy0 * (1.0 / 7.0); 58 | vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5; 59 | gx0 = fract(gx0); 60 | vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0); 61 | vec4 sz0 = step(gz0, vec4(0.0)); 62 | gx0 -= sz0 * (step(0.0, gx0) - 0.5); 63 | gy0 -= sz0 * (step(0.0, gy0) - 0.5); 64 | 65 | vec4 gx1 = ixy1 * (1.0 / 7.0); 66 | vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5; 67 | gx1 = fract(gx1); 68 | vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1); 69 | vec4 sz1 = step(gz1, vec4(0.0)); 70 | gx1 -= sz1 * (step(0.0, gx1) - 0.5); 71 | gy1 -= sz1 * (step(0.0, gy1) - 0.5); 72 | 73 | vec3 g000 = vec3(gx0.x,gy0.x,gz0.x); 74 | vec3 g100 = vec3(gx0.y,gy0.y,gz0.y); 75 | vec3 g010 = vec3(gx0.z,gy0.z,gz0.z); 76 | vec3 g110 = vec3(gx0.w,gy0.w,gz0.w); 77 | vec3 g001 = vec3(gx1.x,gy1.x,gz1.x); 78 | vec3 g101 = vec3(gx1.y,gy1.y,gz1.y); 79 | vec3 g011 = vec3(gx1.z,gy1.z,gz1.z); 80 | vec3 g111 = vec3(gx1.w,gy1.w,gz1.w); 81 | 82 | vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 83 | g000 *= norm0.x; 84 | g010 *= norm0.y; 85 | g100 *= norm0.z; 86 | g110 *= norm0.w; 87 | vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 88 | g001 *= norm1.x; 89 | g011 *= norm1.y; 90 | g101 *= norm1.z; 91 | g111 *= norm1.w; 92 | 93 | float n000 = dot(g000, Pf0); 94 | float n100 = dot(g100, vec3(Pf1.x, Pf0.yz)); 95 | float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z)); 96 | float n110 = dot(g110, vec3(Pf1.xy, Pf0.z)); 97 | float n001 = dot(g001, vec3(Pf0.xy, Pf1.z)); 98 | float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z)); 99 | float n011 = dot(g011, vec3(Pf0.x, Pf1.yz)); 100 | float n111 = dot(g111, Pf1); 101 | 102 | vec3 fade_xyz = fade(Pf0); 103 | vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z); 104 | vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y); 105 | float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); 106 | return 2.2 * n_xyz; 107 | } 108 | 109 | #pragma glslify: export(cnoise) 110 | -------------------------------------------------------------------------------- /classic/4d.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // GLSL textureless classic 4D noise "cnoise", 3 | // with an RSL-style periodic variant "pnoise". 4 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 5 | // Version: 2011-08-22 6 | // 7 | // Many thanks to Ian McEwan of Ashima Arts for the 8 | // ideas for permutation and gradient selection. 9 | // 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 13 | // 14 | 15 | vec4 mod289(vec4 x) 16 | { 17 | return x - floor(x * (1.0 / 289.0)) * 289.0; 18 | } 19 | 20 | vec4 permute(vec4 x) 21 | { 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 | vec4 fade(vec4 t) { 31 | return t*t*t*(t*(t*6.0-15.0)+10.0); 32 | } 33 | 34 | // Classic Perlin noise 35 | float cnoise(vec4 P) 36 | { 37 | vec4 Pi0 = floor(P); // Integer part for indexing 38 | vec4 Pi1 = Pi0 + 1.0; // Integer part + 1 39 | Pi0 = mod289(Pi0); 40 | Pi1 = mod289(Pi1); 41 | vec4 Pf0 = fract(P); // Fractional part for interpolation 42 | vec4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0 43 | vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 44 | vec4 iy = vec4(Pi0.yy, Pi1.yy); 45 | vec4 iz0 = vec4(Pi0.zzzz); 46 | vec4 iz1 = vec4(Pi1.zzzz); 47 | vec4 iw0 = vec4(Pi0.wwww); 48 | vec4 iw1 = vec4(Pi1.wwww); 49 | 50 | vec4 ixy = permute(permute(ix) + iy); 51 | vec4 ixy0 = permute(ixy + iz0); 52 | vec4 ixy1 = permute(ixy + iz1); 53 | vec4 ixy00 = permute(ixy0 + iw0); 54 | vec4 ixy01 = permute(ixy0 + iw1); 55 | vec4 ixy10 = permute(ixy1 + iw0); 56 | vec4 ixy11 = permute(ixy1 + iw1); 57 | 58 | vec4 gx00 = ixy00 * (1.0 / 7.0); 59 | vec4 gy00 = floor(gx00) * (1.0 / 7.0); 60 | vec4 gz00 = floor(gy00) * (1.0 / 6.0); 61 | gx00 = fract(gx00) - 0.5; 62 | gy00 = fract(gy00) - 0.5; 63 | gz00 = fract(gz00) - 0.5; 64 | vec4 gw00 = vec4(0.75) - abs(gx00) - abs(gy00) - abs(gz00); 65 | vec4 sw00 = step(gw00, vec4(0.0)); 66 | gx00 -= sw00 * (step(0.0, gx00) - 0.5); 67 | gy00 -= sw00 * (step(0.0, gy00) - 0.5); 68 | 69 | vec4 gx01 = ixy01 * (1.0 / 7.0); 70 | vec4 gy01 = floor(gx01) * (1.0 / 7.0); 71 | vec4 gz01 = floor(gy01) * (1.0 / 6.0); 72 | gx01 = fract(gx01) - 0.5; 73 | gy01 = fract(gy01) - 0.5; 74 | gz01 = fract(gz01) - 0.5; 75 | vec4 gw01 = vec4(0.75) - abs(gx01) - abs(gy01) - abs(gz01); 76 | vec4 sw01 = step(gw01, vec4(0.0)); 77 | gx01 -= sw01 * (step(0.0, gx01) - 0.5); 78 | gy01 -= sw01 * (step(0.0, gy01) - 0.5); 79 | 80 | vec4 gx10 = ixy10 * (1.0 / 7.0); 81 | vec4 gy10 = floor(gx10) * (1.0 / 7.0); 82 | vec4 gz10 = floor(gy10) * (1.0 / 6.0); 83 | gx10 = fract(gx10) - 0.5; 84 | gy10 = fract(gy10) - 0.5; 85 | gz10 = fract(gz10) - 0.5; 86 | vec4 gw10 = vec4(0.75) - abs(gx10) - abs(gy10) - abs(gz10); 87 | vec4 sw10 = step(gw10, vec4(0.0)); 88 | gx10 -= sw10 * (step(0.0, gx10) - 0.5); 89 | gy10 -= sw10 * (step(0.0, gy10) - 0.5); 90 | 91 | vec4 gx11 = ixy11 * (1.0 / 7.0); 92 | vec4 gy11 = floor(gx11) * (1.0 / 7.0); 93 | vec4 gz11 = floor(gy11) * (1.0 / 6.0); 94 | gx11 = fract(gx11) - 0.5; 95 | gy11 = fract(gy11) - 0.5; 96 | gz11 = fract(gz11) - 0.5; 97 | vec4 gw11 = vec4(0.75) - abs(gx11) - abs(gy11) - abs(gz11); 98 | vec4 sw11 = step(gw11, vec4(0.0)); 99 | gx11 -= sw11 * (step(0.0, gx11) - 0.5); 100 | gy11 -= sw11 * (step(0.0, gy11) - 0.5); 101 | 102 | vec4 g0000 = vec4(gx00.x,gy00.x,gz00.x,gw00.x); 103 | vec4 g1000 = vec4(gx00.y,gy00.y,gz00.y,gw00.y); 104 | vec4 g0100 = vec4(gx00.z,gy00.z,gz00.z,gw00.z); 105 | vec4 g1100 = vec4(gx00.w,gy00.w,gz00.w,gw00.w); 106 | vec4 g0010 = vec4(gx10.x,gy10.x,gz10.x,gw10.x); 107 | vec4 g1010 = vec4(gx10.y,gy10.y,gz10.y,gw10.y); 108 | vec4 g0110 = vec4(gx10.z,gy10.z,gz10.z,gw10.z); 109 | vec4 g1110 = vec4(gx10.w,gy10.w,gz10.w,gw10.w); 110 | vec4 g0001 = vec4(gx01.x,gy01.x,gz01.x,gw01.x); 111 | vec4 g1001 = vec4(gx01.y,gy01.y,gz01.y,gw01.y); 112 | vec4 g0101 = vec4(gx01.z,gy01.z,gz01.z,gw01.z); 113 | vec4 g1101 = vec4(gx01.w,gy01.w,gz01.w,gw01.w); 114 | vec4 g0011 = vec4(gx11.x,gy11.x,gz11.x,gw11.x); 115 | vec4 g1011 = vec4(gx11.y,gy11.y,gz11.y,gw11.y); 116 | vec4 g0111 = vec4(gx11.z,gy11.z,gz11.z,gw11.z); 117 | vec4 g1111 = vec4(gx11.w,gy11.w,gz11.w,gw11.w); 118 | 119 | vec4 norm00 = taylorInvSqrt(vec4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100))); 120 | g0000 *= norm00.x; 121 | g0100 *= norm00.y; 122 | g1000 *= norm00.z; 123 | g1100 *= norm00.w; 124 | 125 | vec4 norm01 = taylorInvSqrt(vec4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101))); 126 | g0001 *= norm01.x; 127 | g0101 *= norm01.y; 128 | g1001 *= norm01.z; 129 | g1101 *= norm01.w; 130 | 131 | vec4 norm10 = taylorInvSqrt(vec4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110))); 132 | g0010 *= norm10.x; 133 | g0110 *= norm10.y; 134 | g1010 *= norm10.z; 135 | g1110 *= norm10.w; 136 | 137 | vec4 norm11 = taylorInvSqrt(vec4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111))); 138 | g0011 *= norm11.x; 139 | g0111 *= norm11.y; 140 | g1011 *= norm11.z; 141 | g1111 *= norm11.w; 142 | 143 | float n0000 = dot(g0000, Pf0); 144 | float n1000 = dot(g1000, vec4(Pf1.x, Pf0.yzw)); 145 | float n0100 = dot(g0100, vec4(Pf0.x, Pf1.y, Pf0.zw)); 146 | float n1100 = dot(g1100, vec4(Pf1.xy, Pf0.zw)); 147 | float n0010 = dot(g0010, vec4(Pf0.xy, Pf1.z, Pf0.w)); 148 | float n1010 = dot(g1010, vec4(Pf1.x, Pf0.y, Pf1.z, Pf0.w)); 149 | float n0110 = dot(g0110, vec4(Pf0.x, Pf1.yz, Pf0.w)); 150 | float n1110 = dot(g1110, vec4(Pf1.xyz, Pf0.w)); 151 | float n0001 = dot(g0001, vec4(Pf0.xyz, Pf1.w)); 152 | float n1001 = dot(g1001, vec4(Pf1.x, Pf0.yz, Pf1.w)); 153 | float n0101 = dot(g0101, vec4(Pf0.x, Pf1.y, Pf0.z, Pf1.w)); 154 | float n1101 = dot(g1101, vec4(Pf1.xy, Pf0.z, Pf1.w)); 155 | float n0011 = dot(g0011, vec4(Pf0.xy, Pf1.zw)); 156 | float n1011 = dot(g1011, vec4(Pf1.x, Pf0.y, Pf1.zw)); 157 | float n0111 = dot(g0111, vec4(Pf0.x, Pf1.yzw)); 158 | float n1111 = dot(g1111, Pf1); 159 | 160 | vec4 fade_xyzw = fade(Pf0); 161 | vec4 n_0w = mix(vec4(n0000, n1000, n0100, n1100), vec4(n0001, n1001, n0101, n1101), fade_xyzw.w); 162 | vec4 n_1w = mix(vec4(n0010, n1010, n0110, n1110), vec4(n0011, n1011, n0111, n1111), fade_xyzw.w); 163 | vec4 n_zw = mix(n_0w, n_1w, fade_xyzw.z); 164 | vec2 n_yzw = mix(n_zw.xy, n_zw.zw, fade_xyzw.y); 165 | float n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x); 166 | return 2.2 * n_xyzw; 167 | } 168 | 169 | #pragma glslify: export(cnoise) 170 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glsl-noise", 3 | "version": "0.0.0", 4 | "description": "webgl-noise shaders ported to work with glslify", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/hughsk/glsl-noise.git" 12 | }, 13 | "keywords": [ 14 | "glsl", 15 | "noise", 16 | "shader", 17 | "perlin", 18 | "simplex", 19 | "webgl", 20 | "glslify" 21 | ], 22 | "author": "Ian McEwan, Ashima Arts, Stefan Gustavson", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/hughsk/glsl-noise/issues" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /periodic/2d.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // GLSL textureless classic 2D noise "cnoise", 3 | // with an RSL-style periodic variant "pnoise". 4 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 5 | // Version: 2011-08-22 6 | // 7 | // Many thanks to Ian McEwan of Ashima Arts for the 8 | // ideas for permutation and gradient selection. 9 | // 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 13 | // 14 | 15 | vec4 mod289(vec4 x) 16 | { 17 | return x - floor(x * (1.0 / 289.0)) * 289.0; 18 | } 19 | 20 | vec4 permute(vec4 x) 21 | { 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 | vec2 fade(vec2 t) { 31 | return t*t*t*(t*(t*6.0-15.0)+10.0); 32 | } 33 | 34 | // Classic Perlin noise, periodic variant 35 | float pnoise(vec2 P, vec2 rep) 36 | { 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 41 | vec4 ix = Pi.xzxz; 42 | vec4 iy = Pi.yyww; 43 | vec4 fx = Pf.xzxz; 44 | vec4 fy = Pf.yyww; 45 | 46 | vec4 i = permute(permute(ix) + iy); 47 | 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); 51 | gx = gx - tx; 52 | 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); 57 | 58 | vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11))); 59 | g00 *= norm.x; 60 | g01 *= norm.y; 61 | g10 *= norm.z; 62 | g11 *= norm.w; 63 | 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)); 68 | 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); 72 | return 2.3 * n_xy; 73 | } 74 | 75 | #pragma glslify: export(pnoise) 76 | -------------------------------------------------------------------------------- /periodic/3d.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // GLSL textureless classic 3D noise "cnoise", 3 | // with an RSL-style periodic variant "pnoise". 4 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 5 | // Version: 2011-10-11 6 | // 7 | // Many thanks to Ian McEwan of Ashima Arts for the 8 | // ideas for permutation and gradient selection. 9 | // 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 13 | // 14 | 15 | vec3 mod289(vec3 x) 16 | { 17 | return x - floor(x * (1.0 / 289.0)) * 289.0; 18 | } 19 | 20 | vec4 mod289(vec4 x) 21 | { 22 | return x - floor(x * (1.0 / 289.0)) * 289.0; 23 | } 24 | 25 | vec4 permute(vec4 x) 26 | { 27 | return mod289(((x*34.0)+1.0)*x); 28 | } 29 | 30 | vec4 taylorInvSqrt(vec4 r) 31 | { 32 | return 1.79284291400159 - 0.85373472095314 * r; 33 | } 34 | 35 | vec3 fade(vec3 t) { 36 | return t*t*t*(t*(t*6.0-15.0)+10.0); 37 | } 38 | 39 | // Classic Perlin noise, periodic variant 40 | float pnoise(vec3 P, vec3 rep) 41 | { 42 | vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period 43 | vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period 44 | Pi0 = mod289(Pi0); 45 | Pi1 = mod289(Pi1); 46 | vec3 Pf0 = fract(P); // Fractional part for interpolation 47 | vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0 48 | vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 49 | vec4 iy = vec4(Pi0.yy, Pi1.yy); 50 | vec4 iz0 = Pi0.zzzz; 51 | vec4 iz1 = Pi1.zzzz; 52 | 53 | vec4 ixy = permute(permute(ix) + iy); 54 | vec4 ixy0 = permute(ixy + iz0); 55 | vec4 ixy1 = permute(ixy + iz1); 56 | 57 | vec4 gx0 = ixy0 * (1.0 / 7.0); 58 | vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5; 59 | gx0 = fract(gx0); 60 | vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0); 61 | vec4 sz0 = step(gz0, vec4(0.0)); 62 | gx0 -= sz0 * (step(0.0, gx0) - 0.5); 63 | gy0 -= sz0 * (step(0.0, gy0) - 0.5); 64 | 65 | vec4 gx1 = ixy1 * (1.0 / 7.0); 66 | vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5; 67 | gx1 = fract(gx1); 68 | vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1); 69 | vec4 sz1 = step(gz1, vec4(0.0)); 70 | gx1 -= sz1 * (step(0.0, gx1) - 0.5); 71 | gy1 -= sz1 * (step(0.0, gy1) - 0.5); 72 | 73 | vec3 g000 = vec3(gx0.x,gy0.x,gz0.x); 74 | vec3 g100 = vec3(gx0.y,gy0.y,gz0.y); 75 | vec3 g010 = vec3(gx0.z,gy0.z,gz0.z); 76 | vec3 g110 = vec3(gx0.w,gy0.w,gz0.w); 77 | vec3 g001 = vec3(gx1.x,gy1.x,gz1.x); 78 | vec3 g101 = vec3(gx1.y,gy1.y,gz1.y); 79 | vec3 g011 = vec3(gx1.z,gy1.z,gz1.z); 80 | vec3 g111 = vec3(gx1.w,gy1.w,gz1.w); 81 | 82 | vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 83 | g000 *= norm0.x; 84 | g010 *= norm0.y; 85 | g100 *= norm0.z; 86 | g110 *= norm0.w; 87 | vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 88 | g001 *= norm1.x; 89 | g011 *= norm1.y; 90 | g101 *= norm1.z; 91 | g111 *= norm1.w; 92 | 93 | float n000 = dot(g000, Pf0); 94 | float n100 = dot(g100, vec3(Pf1.x, Pf0.yz)); 95 | float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z)); 96 | float n110 = dot(g110, vec3(Pf1.xy, Pf0.z)); 97 | float n001 = dot(g001, vec3(Pf0.xy, Pf1.z)); 98 | float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z)); 99 | float n011 = dot(g011, vec3(Pf0.x, Pf1.yz)); 100 | float n111 = dot(g111, Pf1); 101 | 102 | vec3 fade_xyz = fade(Pf0); 103 | vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z); 104 | vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y); 105 | float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); 106 | return 2.2 * n_xyz; 107 | } 108 | 109 | #pragma glslify: export(pnoise) 110 | -------------------------------------------------------------------------------- /periodic/4d.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // GLSL textureless classic 4D noise "cnoise", 3 | // with an RSL-style periodic variant "pnoise". 4 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 5 | // Version: 2011-08-22 6 | // 7 | // Many thanks to Ian McEwan of Ashima Arts for the 8 | // ideas for permutation and gradient selection. 9 | // 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 13 | // 14 | 15 | vec4 mod289(vec4 x) 16 | { 17 | return x - floor(x * (1.0 / 289.0)) * 289.0; 18 | } 19 | 20 | vec4 permute(vec4 x) 21 | { 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 | vec4 fade(vec4 t) { 31 | return t*t*t*(t*(t*6.0-15.0)+10.0); 32 | } 33 | 34 | // Classic Perlin noise, periodic version 35 | float pnoise(vec4 P, vec4 rep) 36 | { 37 | vec4 Pi0 = mod(floor(P), rep); // Integer part modulo rep 38 | vec4 Pi1 = mod(Pi0 + 1.0, rep); // Integer part + 1 mod rep 39 | Pi0 = mod289(Pi0); 40 | Pi1 = mod289(Pi1); 41 | vec4 Pf0 = fract(P); // Fractional part for interpolation 42 | vec4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0 43 | vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 44 | vec4 iy = vec4(Pi0.yy, Pi1.yy); 45 | vec4 iz0 = vec4(Pi0.zzzz); 46 | vec4 iz1 = vec4(Pi1.zzzz); 47 | vec4 iw0 = vec4(Pi0.wwww); 48 | vec4 iw1 = vec4(Pi1.wwww); 49 | 50 | vec4 ixy = permute(permute(ix) + iy); 51 | vec4 ixy0 = permute(ixy + iz0); 52 | vec4 ixy1 = permute(ixy + iz1); 53 | vec4 ixy00 = permute(ixy0 + iw0); 54 | vec4 ixy01 = permute(ixy0 + iw1); 55 | vec4 ixy10 = permute(ixy1 + iw0); 56 | vec4 ixy11 = permute(ixy1 + iw1); 57 | 58 | vec4 gx00 = ixy00 * (1.0 / 7.0); 59 | vec4 gy00 = floor(gx00) * (1.0 / 7.0); 60 | vec4 gz00 = floor(gy00) * (1.0 / 6.0); 61 | gx00 = fract(gx00) - 0.5; 62 | gy00 = fract(gy00) - 0.5; 63 | gz00 = fract(gz00) - 0.5; 64 | vec4 gw00 = vec4(0.75) - abs(gx00) - abs(gy00) - abs(gz00); 65 | vec4 sw00 = step(gw00, vec4(0.0)); 66 | gx00 -= sw00 * (step(0.0, gx00) - 0.5); 67 | gy00 -= sw00 * (step(0.0, gy00) - 0.5); 68 | 69 | vec4 gx01 = ixy01 * (1.0 / 7.0); 70 | vec4 gy01 = floor(gx01) * (1.0 / 7.0); 71 | vec4 gz01 = floor(gy01) * (1.0 / 6.0); 72 | gx01 = fract(gx01) - 0.5; 73 | gy01 = fract(gy01) - 0.5; 74 | gz01 = fract(gz01) - 0.5; 75 | vec4 gw01 = vec4(0.75) - abs(gx01) - abs(gy01) - abs(gz01); 76 | vec4 sw01 = step(gw01, vec4(0.0)); 77 | gx01 -= sw01 * (step(0.0, gx01) - 0.5); 78 | gy01 -= sw01 * (step(0.0, gy01) - 0.5); 79 | 80 | vec4 gx10 = ixy10 * (1.0 / 7.0); 81 | vec4 gy10 = floor(gx10) * (1.0 / 7.0); 82 | vec4 gz10 = floor(gy10) * (1.0 / 6.0); 83 | gx10 = fract(gx10) - 0.5; 84 | gy10 = fract(gy10) - 0.5; 85 | gz10 = fract(gz10) - 0.5; 86 | vec4 gw10 = vec4(0.75) - abs(gx10) - abs(gy10) - abs(gz10); 87 | vec4 sw10 = step(gw10, vec4(0.0)); 88 | gx10 -= sw10 * (step(0.0, gx10) - 0.5); 89 | gy10 -= sw10 * (step(0.0, gy10) - 0.5); 90 | 91 | vec4 gx11 = ixy11 * (1.0 / 7.0); 92 | vec4 gy11 = floor(gx11) * (1.0 / 7.0); 93 | vec4 gz11 = floor(gy11) * (1.0 / 6.0); 94 | gx11 = fract(gx11) - 0.5; 95 | gy11 = fract(gy11) - 0.5; 96 | gz11 = fract(gz11) - 0.5; 97 | vec4 gw11 = vec4(0.75) - abs(gx11) - abs(gy11) - abs(gz11); 98 | vec4 sw11 = step(gw11, vec4(0.0)); 99 | gx11 -= sw11 * (step(0.0, gx11) - 0.5); 100 | gy11 -= sw11 * (step(0.0, gy11) - 0.5); 101 | 102 | vec4 g0000 = vec4(gx00.x,gy00.x,gz00.x,gw00.x); 103 | vec4 g1000 = vec4(gx00.y,gy00.y,gz00.y,gw00.y); 104 | vec4 g0100 = vec4(gx00.z,gy00.z,gz00.z,gw00.z); 105 | vec4 g1100 = vec4(gx00.w,gy00.w,gz00.w,gw00.w); 106 | vec4 g0010 = vec4(gx10.x,gy10.x,gz10.x,gw10.x); 107 | vec4 g1010 = vec4(gx10.y,gy10.y,gz10.y,gw10.y); 108 | vec4 g0110 = vec4(gx10.z,gy10.z,gz10.z,gw10.z); 109 | vec4 g1110 = vec4(gx10.w,gy10.w,gz10.w,gw10.w); 110 | vec4 g0001 = vec4(gx01.x,gy01.x,gz01.x,gw01.x); 111 | vec4 g1001 = vec4(gx01.y,gy01.y,gz01.y,gw01.y); 112 | vec4 g0101 = vec4(gx01.z,gy01.z,gz01.z,gw01.z); 113 | vec4 g1101 = vec4(gx01.w,gy01.w,gz01.w,gw01.w); 114 | vec4 g0011 = vec4(gx11.x,gy11.x,gz11.x,gw11.x); 115 | vec4 g1011 = vec4(gx11.y,gy11.y,gz11.y,gw11.y); 116 | vec4 g0111 = vec4(gx11.z,gy11.z,gz11.z,gw11.z); 117 | vec4 g1111 = vec4(gx11.w,gy11.w,gz11.w,gw11.w); 118 | 119 | vec4 norm00 = taylorInvSqrt(vec4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100))); 120 | g0000 *= norm00.x; 121 | g0100 *= norm00.y; 122 | g1000 *= norm00.z; 123 | g1100 *= norm00.w; 124 | 125 | vec4 norm01 = taylorInvSqrt(vec4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101))); 126 | g0001 *= norm01.x; 127 | g0101 *= norm01.y; 128 | g1001 *= norm01.z; 129 | g1101 *= norm01.w; 130 | 131 | vec4 norm10 = taylorInvSqrt(vec4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110))); 132 | g0010 *= norm10.x; 133 | g0110 *= norm10.y; 134 | g1010 *= norm10.z; 135 | g1110 *= norm10.w; 136 | 137 | vec4 norm11 = taylorInvSqrt(vec4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111))); 138 | g0011 *= norm11.x; 139 | g0111 *= norm11.y; 140 | g1011 *= norm11.z; 141 | g1111 *= norm11.w; 142 | 143 | float n0000 = dot(g0000, Pf0); 144 | float n1000 = dot(g1000, vec4(Pf1.x, Pf0.yzw)); 145 | float n0100 = dot(g0100, vec4(Pf0.x, Pf1.y, Pf0.zw)); 146 | float n1100 = dot(g1100, vec4(Pf1.xy, Pf0.zw)); 147 | float n0010 = dot(g0010, vec4(Pf0.xy, Pf1.z, Pf0.w)); 148 | float n1010 = dot(g1010, vec4(Pf1.x, Pf0.y, Pf1.z, Pf0.w)); 149 | float n0110 = dot(g0110, vec4(Pf0.x, Pf1.yz, Pf0.w)); 150 | float n1110 = dot(g1110, vec4(Pf1.xyz, Pf0.w)); 151 | float n0001 = dot(g0001, vec4(Pf0.xyz, Pf1.w)); 152 | float n1001 = dot(g1001, vec4(Pf1.x, Pf0.yz, Pf1.w)); 153 | float n0101 = dot(g0101, vec4(Pf0.x, Pf1.y, Pf0.z, Pf1.w)); 154 | float n1101 = dot(g1101, vec4(Pf1.xy, Pf0.z, Pf1.w)); 155 | float n0011 = dot(g0011, vec4(Pf0.xy, Pf1.zw)); 156 | float n1011 = dot(g1011, vec4(Pf1.x, Pf0.y, Pf1.zw)); 157 | float n0111 = dot(g0111, vec4(Pf0.x, Pf1.yzw)); 158 | float n1111 = dot(g1111, Pf1); 159 | 160 | vec4 fade_xyzw = fade(Pf0); 161 | vec4 n_0w = mix(vec4(n0000, n1000, n0100, n1100), vec4(n0001, n1001, n0101, n1101), fade_xyzw.w); 162 | vec4 n_1w = mix(vec4(n0010, n1010, n0110, n1110), vec4(n0011, n1011, n0111, n1111), fade_xyzw.w); 163 | vec4 n_zw = mix(n_0w, n_1w, fade_xyzw.z); 164 | vec2 n_yzw = mix(n_zw.xy, n_zw.zw, fade_xyzw.y); 165 | float n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x); 166 | return 2.2 * n_xyzw; 167 | } 168 | 169 | #pragma glslify: export(pnoise) 170 | -------------------------------------------------------------------------------- /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) 73 | -------------------------------------------------------------------------------- /simplex/3d.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 | vec3 mod289(vec3 x) { 13 | return x - floor(x * (1.0 / 289.0)) * 289.0; 14 | } 15 | 16 | vec4 mod289(vec4 x) { 17 | return x - floor(x * (1.0 / 289.0)) * 289.0; 18 | } 19 | 20 | vec4 permute(vec4 x) { 21 | return mod289(((x*34.0)+1.0)*x); 22 | } 23 | 24 | vec4 taylorInvSqrt(vec4 r) 25 | { 26 | return 1.79284291400159 - 0.85373472095314 * r; 27 | } 28 | 29 | float snoise(vec3 v) 30 | { 31 | const vec2 C = vec2(1.0/6.0, 1.0/3.0) ; 32 | const vec4 D = vec4(0.0, 0.5, 1.0, 2.0); 33 | 34 | // First corner 35 | vec3 i = floor(v + dot(v, C.yyy) ); 36 | vec3 x0 = v - i + dot(i, C.xxx) ; 37 | 38 | // Other corners 39 | vec3 g = step(x0.yzx, x0.xyz); 40 | vec3 l = 1.0 - g; 41 | vec3 i1 = min( g.xyz, l.zxy ); 42 | vec3 i2 = max( g.xyz, l.zxy ); 43 | 44 | // x0 = x0 - 0.0 + 0.0 * C.xxx; 45 | // x1 = x0 - i1 + 1.0 * C.xxx; 46 | // x2 = x0 - i2 + 2.0 * C.xxx; 47 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 48 | vec3 x1 = x0 - i1 + C.xxx; 49 | vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y 50 | vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y 51 | 52 | // Permutations 53 | i = mod289(i); 54 | vec4 p = permute( permute( permute( 55 | i.z + vec4(0.0, i1.z, i2.z, 1.0 )) 56 | + i.y + vec4(0.0, i1.y, i2.y, 1.0 )) 57 | + i.x + vec4(0.0, i1.x, i2.x, 1.0 )); 58 | 59 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 60 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 61 | float n_ = 0.142857142857; // 1.0/7.0 62 | vec3 ns = n_ * D.wyz - D.xzx; 63 | 64 | vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7) 65 | 66 | vec4 x_ = floor(j * ns.z); 67 | vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N) 68 | 69 | vec4 x = x_ *ns.x + ns.yyyy; 70 | vec4 y = y_ *ns.x + ns.yyyy; 71 | vec4 h = 1.0 - abs(x) - abs(y); 72 | 73 | vec4 b0 = vec4( x.xy, y.xy ); 74 | vec4 b1 = vec4( x.zw, y.zw ); 75 | 76 | //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; 77 | //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; 78 | vec4 s0 = floor(b0)*2.0 + 1.0; 79 | vec4 s1 = floor(b1)*2.0 + 1.0; 80 | vec4 sh = -step(h, vec4(0.0)); 81 | 82 | vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ; 83 | vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ; 84 | 85 | vec3 p0 = vec3(a0.xy,h.x); 86 | vec3 p1 = vec3(a0.zw,h.y); 87 | vec3 p2 = vec3(a1.xy,h.z); 88 | vec3 p3 = vec3(a1.zw,h.w); 89 | 90 | //Normalise gradients 91 | vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); 92 | p0 *= norm.x; 93 | p1 *= norm.y; 94 | p2 *= norm.z; 95 | p3 *= norm.w; 96 | 97 | // Mix final noise value 98 | vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0); 99 | m = m * m; 100 | return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), 101 | dot(p2,x2), dot(p3,x3) ) ); 102 | } 103 | 104 | #pragma glslify: export(snoise) 105 | -------------------------------------------------------------------------------- /simplex/4d.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 | vec4 mod289(vec4 x) { 13 | return x - floor(x * (1.0 / 289.0)) * 289.0; } 14 | 15 | float mod289(float x) { 16 | return x - floor(x * (1.0 / 289.0)) * 289.0; } 17 | 18 | vec4 permute(vec4 x) { 19 | return mod289(((x*34.0)+1.0)*x); 20 | } 21 | 22 | float permute(float x) { 23 | return mod289(((x*34.0)+1.0)*x); 24 | } 25 | 26 | vec4 taylorInvSqrt(vec4 r) 27 | { 28 | return 1.79284291400159 - 0.85373472095314 * r; 29 | } 30 | 31 | float taylorInvSqrt(float r) 32 | { 33 | return 1.79284291400159 - 0.85373472095314 * r; 34 | } 35 | 36 | vec4 grad4(float j, vec4 ip) 37 | { 38 | const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0); 39 | vec4 p,s; 40 | 41 | p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0; 42 | p.w = 1.5 - dot(abs(p.xyz), ones.xyz); 43 | s = vec4(lessThan(p, vec4(0.0))); 44 | p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www; 45 | 46 | return p; 47 | } 48 | 49 | // (sqrt(5) - 1)/4 = F4, used once below 50 | #define F4 0.309016994374947451 51 | 52 | float snoise(vec4 v) 53 | { 54 | const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5))/20 G4 55 | 0.276393202250021, // 2 * G4 56 | 0.414589803375032, // 3 * G4 57 | -0.447213595499958); // -1 + 4 * G4 58 | 59 | // First corner 60 | vec4 i = floor(v + dot(v, vec4(F4)) ); 61 | vec4 x0 = v - i + dot(i, C.xxxx); 62 | 63 | // Other corners 64 | 65 | // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI) 66 | vec4 i0; 67 | vec3 isX = step( x0.yzw, x0.xxx ); 68 | vec3 isYZ = step( x0.zww, x0.yyz ); 69 | // i0.x = dot( isX, vec3( 1.0 ) ); 70 | i0.x = isX.x + isX.y + isX.z; 71 | i0.yzw = 1.0 - isX; 72 | // i0.y += dot( isYZ.xy, vec2( 1.0 ) ); 73 | i0.y += isYZ.x + isYZ.y; 74 | i0.zw += 1.0 - isYZ.xy; 75 | i0.z += isYZ.z; 76 | i0.w += 1.0 - isYZ.z; 77 | 78 | // i0 now contains the unique values 0,1,2,3 in each channel 79 | vec4 i3 = clamp( i0, 0.0, 1.0 ); 80 | vec4 i2 = clamp( i0-1.0, 0.0, 1.0 ); 81 | vec4 i1 = clamp( i0-2.0, 0.0, 1.0 ); 82 | 83 | // x0 = x0 - 0.0 + 0.0 * C.xxxx 84 | // x1 = x0 - i1 + 1.0 * C.xxxx 85 | // x2 = x0 - i2 + 2.0 * C.xxxx 86 | // x3 = x0 - i3 + 3.0 * C.xxxx 87 | // x4 = x0 - 1.0 + 4.0 * C.xxxx 88 | vec4 x1 = x0 - i1 + C.xxxx; 89 | vec4 x2 = x0 - i2 + C.yyyy; 90 | vec4 x3 = x0 - i3 + C.zzzz; 91 | vec4 x4 = x0 + C.wwww; 92 | 93 | // Permutations 94 | i = mod289(i); 95 | float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x); 96 | vec4 j1 = permute( permute( permute( permute ( 97 | i.w + vec4(i1.w, i2.w, i3.w, 1.0 )) 98 | + i.z + vec4(i1.z, i2.z, i3.z, 1.0 )) 99 | + i.y + vec4(i1.y, i2.y, i3.y, 1.0 )) 100 | + i.x + vec4(i1.x, i2.x, i3.x, 1.0 )); 101 | 102 | // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope 103 | // 7*7*6 = 294, which is close to the ring size 17*17 = 289. 104 | vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ; 105 | 106 | vec4 p0 = grad4(j0, ip); 107 | vec4 p1 = grad4(j1.x, ip); 108 | vec4 p2 = grad4(j1.y, ip); 109 | vec4 p3 = grad4(j1.z, ip); 110 | vec4 p4 = grad4(j1.w, ip); 111 | 112 | // Normalise gradients 113 | vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); 114 | p0 *= norm.x; 115 | p1 *= norm.y; 116 | p2 *= norm.z; 117 | p3 *= norm.w; 118 | p4 *= taylorInvSqrt(dot(p4,p4)); 119 | 120 | // Mix contributions from the five corners 121 | vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0); 122 | vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0); 123 | m0 = m0 * m0; 124 | m1 = m1 * m1; 125 | return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 ))) 126 | + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ; 127 | 128 | } 129 | 130 | #pragma glslify: export(snoise) 131 | --------------------------------------------------------------------------------