1/// @ref gtc_noise 2/// @file glm/gtc/noise.inl 3/// 4// Based on the work of Stefan Gustavson and Ashima Arts on "webgl-noise": 5// https://github.com/ashima/webgl-noise 6// Following Stefan Gustavson's paper "Simplex noise demystified": 7// http://www.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf 8 9namespace glm{ 10namespace gtc 11{ 12 template <typename T, precision P> 13 GLM_FUNC_QUALIFIER tvec4<T, P> grad4(T const & j, tvec4<T, P> const & ip) 14 { 15 tvec3<T, P> pXYZ = floor(fract(tvec3<T, P>(j) * tvec3<T, P>(ip)) * T(7)) * ip[2] - T(1); 16 T pW = static_cast<T>(1.5) - dot(abs(pXYZ), tvec3<T, P>(1)); 17 tvec4<T, P> s = tvec4<T, P>(lessThan(tvec4<T, P>(pXYZ, pW), tvec4<T, P>(0.0))); 18 pXYZ = pXYZ + (tvec3<T, P>(s) * T(2) - T(1)) * s.w; 19 return tvec4<T, P>(pXYZ, pW); 20 } 21}//namespace gtc 22 23 // Classic Perlin noise 24 template <typename T, precision P> 25 GLM_FUNC_QUALIFIER T perlin(tvec2<T, P> const & Position) 26 { 27 tvec4<T, P> Pi = glm::floor(tvec4<T, P>(Position.x, Position.y, Position.x, Position.y)) + tvec4<T, P>(0.0, 0.0, 1.0, 1.0); 28 tvec4<T, P> Pf = glm::fract(tvec4<T, P>(Position.x, Position.y, Position.x, Position.y)) - tvec4<T, P>(0.0, 0.0, 1.0, 1.0); 29 Pi = mod(Pi, tvec4<T, P>(289)); // To avoid truncation effects in permutation 30 tvec4<T, P> ix(Pi.x, Pi.z, Pi.x, Pi.z); 31 tvec4<T, P> iy(Pi.y, Pi.y, Pi.w, Pi.w); 32 tvec4<T, P> fx(Pf.x, Pf.z, Pf.x, Pf.z); 33 tvec4<T, P> fy(Pf.y, Pf.y, Pf.w, Pf.w); 34 35 tvec4<T, P> i = detail::permute(detail::permute(ix) + iy); 36 37 tvec4<T, P> gx = static_cast<T>(2) * glm::fract(i / T(41)) - T(1); 38 tvec4<T, P> gy = glm::abs(gx) - T(0.5); 39 tvec4<T, P> tx = glm::floor(gx + T(0.5)); 40 gx = gx - tx; 41 42 tvec2<T, P> g00(gx.x, gy.x); 43 tvec2<T, P> g10(gx.y, gy.y); 44 tvec2<T, P> g01(gx.z, gy.z); 45 tvec2<T, P> g11(gx.w, gy.w); 46 47 tvec4<T, P> norm = detail::taylorInvSqrt(tvec4<T, P>(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11))); 48 g00 *= norm.x; 49 g01 *= norm.y; 50 g10 *= norm.z; 51 g11 *= norm.w; 52 53 T n00 = dot(g00, tvec2<T, P>(fx.x, fy.x)); 54 T n10 = dot(g10, tvec2<T, P>(fx.y, fy.y)); 55 T n01 = dot(g01, tvec2<T, P>(fx.z, fy.z)); 56 T n11 = dot(g11, tvec2<T, P>(fx.w, fy.w)); 57 58 tvec2<T, P> fade_xy = detail::fade(tvec2<T, P>(Pf.x, Pf.y)); 59 tvec2<T, P> n_x = mix(tvec2<T, P>(n00, n01), tvec2<T, P>(n10, n11), fade_xy.x); 60 T n_xy = mix(n_x.x, n_x.y, fade_xy.y); 61 return T(2.3) * n_xy; 62 } 63 64 // Classic Perlin noise 65 template <typename T, precision P> 66 GLM_FUNC_QUALIFIER T perlin(tvec3<T, P> const & Position) 67 { 68 tvec3<T, P> Pi0 = floor(Position); // Integer part for indexing 69 tvec3<T, P> Pi1 = Pi0 + T(1); // Integer part + 1 70 Pi0 = detail::mod289(Pi0); 71 Pi1 = detail::mod289(Pi1); 72 tvec3<T, P> Pf0 = fract(Position); // Fractional part for interpolation 73 tvec3<T, P> Pf1 = Pf0 - T(1); // Fractional part - 1.0 74 tvec4<T, P> ix(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 75 tvec4<T, P> iy = tvec4<T, P>(tvec2<T, P>(Pi0.y), tvec2<T, P>(Pi1.y)); 76 tvec4<T, P> iz0(Pi0.z); 77 tvec4<T, P> iz1(Pi1.z); 78 79 tvec4<T, P> ixy = detail::permute(detail::permute(ix) + iy); 80 tvec4<T, P> ixy0 = detail::permute(ixy + iz0); 81 tvec4<T, P> ixy1 = detail::permute(ixy + iz1); 82 83 tvec4<T, P> gx0 = ixy0 * T(1.0 / 7.0); 84 tvec4<T, P> gy0 = fract(floor(gx0) * T(1.0 / 7.0)) - T(0.5); 85 gx0 = fract(gx0); 86 tvec4<T, P> gz0 = tvec4<T, P>(0.5) - abs(gx0) - abs(gy0); 87 tvec4<T, P> sz0 = step(gz0, tvec4<T, P>(0.0)); 88 gx0 -= sz0 * (step(T(0), gx0) - T(0.5)); 89 gy0 -= sz0 * (step(T(0), gy0) - T(0.5)); 90 91 tvec4<T, P> gx1 = ixy1 * T(1.0 / 7.0); 92 tvec4<T, P> gy1 = fract(floor(gx1) * T(1.0 / 7.0)) - T(0.5); 93 gx1 = fract(gx1); 94 tvec4<T, P> gz1 = tvec4<T, P>(0.5) - abs(gx1) - abs(gy1); 95 tvec4<T, P> sz1 = step(gz1, tvec4<T, P>(0.0)); 96 gx1 -= sz1 * (step(T(0), gx1) - T(0.5)); 97 gy1 -= sz1 * (step(T(0), gy1) - T(0.5)); 98 99 tvec3<T, P> g000(gx0.x, gy0.x, gz0.x); 100 tvec3<T, P> g100(gx0.y, gy0.y, gz0.y); 101 tvec3<T, P> g010(gx0.z, gy0.z, gz0.z); 102 tvec3<T, P> g110(gx0.w, gy0.w, gz0.w); 103 tvec3<T, P> g001(gx1.x, gy1.x, gz1.x); 104 tvec3<T, P> g101(gx1.y, gy1.y, gz1.y); 105 tvec3<T, P> g011(gx1.z, gy1.z, gz1.z); 106 tvec3<T, P> g111(gx1.w, gy1.w, gz1.w); 107 108 tvec4<T, P> norm0 = detail::taylorInvSqrt(tvec4<T, P>(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 109 g000 *= norm0.x; 110 g010 *= norm0.y; 111 g100 *= norm0.z; 112 g110 *= norm0.w; 113 tvec4<T, P> norm1 = detail::taylorInvSqrt(tvec4<T, P>(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 114 g001 *= norm1.x; 115 g011 *= norm1.y; 116 g101 *= norm1.z; 117 g111 *= norm1.w; 118 119 T n000 = dot(g000, Pf0); 120 T n100 = dot(g100, tvec3<T, P>(Pf1.x, Pf0.y, Pf0.z)); 121 T n010 = dot(g010, tvec3<T, P>(Pf0.x, Pf1.y, Pf0.z)); 122 T n110 = dot(g110, tvec3<T, P>(Pf1.x, Pf1.y, Pf0.z)); 123 T n001 = dot(g001, tvec3<T, P>(Pf0.x, Pf0.y, Pf1.z)); 124 T n101 = dot(g101, tvec3<T, P>(Pf1.x, Pf0.y, Pf1.z)); 125 T n011 = dot(g011, tvec3<T, P>(Pf0.x, Pf1.y, Pf1.z)); 126 T n111 = dot(g111, Pf1); 127 128 tvec3<T, P> fade_xyz = detail::fade(Pf0); 129 tvec4<T, P> n_z = mix(tvec4<T, P>(n000, n100, n010, n110), tvec4<T, P>(n001, n101, n011, n111), fade_xyz.z); 130 tvec2<T, P> n_yz = mix(tvec2<T, P>(n_z.x, n_z.y), tvec2<T, P>(n_z.z, n_z.w), fade_xyz.y); 131 T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); 132 return T(2.2) * n_xyz; 133 } 134 /* 135 // Classic Perlin noise 136 template <typename T, precision P> 137 GLM_FUNC_QUALIFIER T perlin(tvec3<T, P> const & P) 138 { 139 tvec3<T, P> Pi0 = floor(P); // Integer part for indexing 140 tvec3<T, P> Pi1 = Pi0 + T(1); // Integer part + 1 141 Pi0 = mod(Pi0, T(289)); 142 Pi1 = mod(Pi1, T(289)); 143 tvec3<T, P> Pf0 = fract(P); // Fractional part for interpolation 144 tvec3<T, P> Pf1 = Pf0 - T(1); // Fractional part - 1.0 145 tvec4<T, P> ix(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 146 tvec4<T, P> iy(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 147 tvec4<T, P> iz0(Pi0.z); 148 tvec4<T, P> iz1(Pi1.z); 149 150 tvec4<T, P> ixy = permute(permute(ix) + iy); 151 tvec4<T, P> ixy0 = permute(ixy + iz0); 152 tvec4<T, P> ixy1 = permute(ixy + iz1); 153 154 tvec4<T, P> gx0 = ixy0 / T(7); 155 tvec4<T, P> gy0 = fract(floor(gx0) / T(7)) - T(0.5); 156 gx0 = fract(gx0); 157 tvec4<T, P> gz0 = tvec4<T, P>(0.5) - abs(gx0) - abs(gy0); 158 tvec4<T, P> sz0 = step(gz0, tvec4<T, P>(0.0)); 159 gx0 -= sz0 * (step(0.0, gx0) - T(0.5)); 160 gy0 -= sz0 * (step(0.0, gy0) - T(0.5)); 161 162 tvec4<T, P> gx1 = ixy1 / T(7); 163 tvec4<T, P> gy1 = fract(floor(gx1) / T(7)) - T(0.5); 164 gx1 = fract(gx1); 165 tvec4<T, P> gz1 = tvec4<T, P>(0.5) - abs(gx1) - abs(gy1); 166 tvec4<T, P> sz1 = step(gz1, tvec4<T, P>(0.0)); 167 gx1 -= sz1 * (step(T(0), gx1) - T(0.5)); 168 gy1 -= sz1 * (step(T(0), gy1) - T(0.5)); 169 170 tvec3<T, P> g000(gx0.x, gy0.x, gz0.x); 171 tvec3<T, P> g100(gx0.y, gy0.y, gz0.y); 172 tvec3<T, P> g010(gx0.z, gy0.z, gz0.z); 173 tvec3<T, P> g110(gx0.w, gy0.w, gz0.w); 174 tvec3<T, P> g001(gx1.x, gy1.x, gz1.x); 175 tvec3<T, P> g101(gx1.y, gy1.y, gz1.y); 176 tvec3<T, P> g011(gx1.z, gy1.z, gz1.z); 177 tvec3<T, P> g111(gx1.w, gy1.w, gz1.w); 178 179 tvec4<T, P> norm0 = taylorInvSqrt(tvec4<T, P>(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 180 g000 *= norm0.x; 181 g010 *= norm0.y; 182 g100 *= norm0.z; 183 g110 *= norm0.w; 184 tvec4<T, P> norm1 = taylorInvSqrt(tvec4<T, P>(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 185 g001 *= norm1.x; 186 g011 *= norm1.y; 187 g101 *= norm1.z; 188 g111 *= norm1.w; 189 190 T n000 = dot(g000, Pf0); 191 T n100 = dot(g100, tvec3<T, P>(Pf1.x, Pf0.y, Pf0.z)); 192 T n010 = dot(g010, tvec3<T, P>(Pf0.x, Pf1.y, Pf0.z)); 193 T n110 = dot(g110, tvec3<T, P>(Pf1.x, Pf1.y, Pf0.z)); 194 T n001 = dot(g001, tvec3<T, P>(Pf0.x, Pf0.y, Pf1.z)); 195 T n101 = dot(g101, tvec3<T, P>(Pf1.x, Pf0.y, Pf1.z)); 196 T n011 = dot(g011, tvec3<T, P>(Pf0.x, Pf1.y, Pf1.z)); 197 T n111 = dot(g111, Pf1); 198 199 tvec3<T, P> fade_xyz = fade(Pf0); 200 tvec4<T, P> n_z = mix(tvec4<T, P>(n000, n100, n010, n110), tvec4<T, P>(n001, n101, n011, n111), fade_xyz.z); 201 tvec2<T, P> n_yz = mix( 202 tvec2<T, P>(n_z.x, n_z.y), 203 tvec2<T, P>(n_z.z, n_z.w), fade_xyz.y); 204 T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); 205 return T(2.2) * n_xyz; 206 } 207 */ 208 // Classic Perlin noise 209 template <typename T, precision P> 210 GLM_FUNC_QUALIFIER T perlin(tvec4<T, P> const & Position) 211 { 212 tvec4<T, P> Pi0 = floor(Position); // Integer part for indexing 213 tvec4<T, P> Pi1 = Pi0 + T(1); // Integer part + 1 214 Pi0 = mod(Pi0, tvec4<T, P>(289)); 215 Pi1 = mod(Pi1, tvec4<T, P>(289)); 216 tvec4<T, P> Pf0 = fract(Position); // Fractional part for interpolation 217 tvec4<T, P> Pf1 = Pf0 - T(1); // Fractional part - 1.0 218 tvec4<T, P> ix(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 219 tvec4<T, P> iy(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 220 tvec4<T, P> iz0(Pi0.z); 221 tvec4<T, P> iz1(Pi1.z); 222 tvec4<T, P> iw0(Pi0.w); 223 tvec4<T, P> iw1(Pi1.w); 224 225 tvec4<T, P> ixy = detail::permute(detail::permute(ix) + iy); 226 tvec4<T, P> ixy0 = detail::permute(ixy + iz0); 227 tvec4<T, P> ixy1 = detail::permute(ixy + iz1); 228 tvec4<T, P> ixy00 = detail::permute(ixy0 + iw0); 229 tvec4<T, P> ixy01 = detail::permute(ixy0 + iw1); 230 tvec4<T, P> ixy10 = detail::permute(ixy1 + iw0); 231 tvec4<T, P> ixy11 = detail::permute(ixy1 + iw1); 232 233 tvec4<T, P> gx00 = ixy00 / T(7); 234 tvec4<T, P> gy00 = floor(gx00) / T(7); 235 tvec4<T, P> gz00 = floor(gy00) / T(6); 236 gx00 = fract(gx00) - T(0.5); 237 gy00 = fract(gy00) - T(0.5); 238 gz00 = fract(gz00) - T(0.5); 239 tvec4<T, P> gw00 = tvec4<T, P>(0.75) - abs(gx00) - abs(gy00) - abs(gz00); 240 tvec4<T, P> sw00 = step(gw00, tvec4<T, P>(0.0)); 241 gx00 -= sw00 * (step(T(0), gx00) - T(0.5)); 242 gy00 -= sw00 * (step(T(0), gy00) - T(0.5)); 243 244 tvec4<T, P> gx01 = ixy01 / T(7); 245 tvec4<T, P> gy01 = floor(gx01) / T(7); 246 tvec4<T, P> gz01 = floor(gy01) / T(6); 247 gx01 = fract(gx01) - T(0.5); 248 gy01 = fract(gy01) - T(0.5); 249 gz01 = fract(gz01) - T(0.5); 250 tvec4<T, P> gw01 = tvec4<T, P>(0.75) - abs(gx01) - abs(gy01) - abs(gz01); 251 tvec4<T, P> sw01 = step(gw01, tvec4<T, P>(0.0)); 252 gx01 -= sw01 * (step(T(0), gx01) - T(0.5)); 253 gy01 -= sw01 * (step(T(0), gy01) - T(0.5)); 254 255 tvec4<T, P> gx10 = ixy10 / T(7); 256 tvec4<T, P> gy10 = floor(gx10) / T(7); 257 tvec4<T, P> gz10 = floor(gy10) / T(6); 258 gx10 = fract(gx10) - T(0.5); 259 gy10 = fract(gy10) - T(0.5); 260 gz10 = fract(gz10) - T(0.5); 261 tvec4<T, P> gw10 = tvec4<T, P>(0.75) - abs(gx10) - abs(gy10) - abs(gz10); 262 tvec4<T, P> sw10 = step(gw10, tvec4<T, P>(0)); 263 gx10 -= sw10 * (step(T(0), gx10) - T(0.5)); 264 gy10 -= sw10 * (step(T(0), gy10) - T(0.5)); 265 266 tvec4<T, P> gx11 = ixy11 / T(7); 267 tvec4<T, P> gy11 = floor(gx11) / T(7); 268 tvec4<T, P> gz11 = floor(gy11) / T(6); 269 gx11 = fract(gx11) - T(0.5); 270 gy11 = fract(gy11) - T(0.5); 271 gz11 = fract(gz11) - T(0.5); 272 tvec4<T, P> gw11 = tvec4<T, P>(0.75) - abs(gx11) - abs(gy11) - abs(gz11); 273 tvec4<T, P> sw11 = step(gw11, tvec4<T, P>(0.0)); 274 gx11 -= sw11 * (step(T(0), gx11) - T(0.5)); 275 gy11 -= sw11 * (step(T(0), gy11) - T(0.5)); 276 277 tvec4<T, P> g0000(gx00.x, gy00.x, gz00.x, gw00.x); 278 tvec4<T, P> g1000(gx00.y, gy00.y, gz00.y, gw00.y); 279 tvec4<T, P> g0100(gx00.z, gy00.z, gz00.z, gw00.z); 280 tvec4<T, P> g1100(gx00.w, gy00.w, gz00.w, gw00.w); 281 tvec4<T, P> g0010(gx10.x, gy10.x, gz10.x, gw10.x); 282 tvec4<T, P> g1010(gx10.y, gy10.y, gz10.y, gw10.y); 283 tvec4<T, P> g0110(gx10.z, gy10.z, gz10.z, gw10.z); 284 tvec4<T, P> g1110(gx10.w, gy10.w, gz10.w, gw10.w); 285 tvec4<T, P> g0001(gx01.x, gy01.x, gz01.x, gw01.x); 286 tvec4<T, P> g1001(gx01.y, gy01.y, gz01.y, gw01.y); 287 tvec4<T, P> g0101(gx01.z, gy01.z, gz01.z, gw01.z); 288 tvec4<T, P> g1101(gx01.w, gy01.w, gz01.w, gw01.w); 289 tvec4<T, P> g0011(gx11.x, gy11.x, gz11.x, gw11.x); 290 tvec4<T, P> g1011(gx11.y, gy11.y, gz11.y, gw11.y); 291 tvec4<T, P> g0111(gx11.z, gy11.z, gz11.z, gw11.z); 292 tvec4<T, P> g1111(gx11.w, gy11.w, gz11.w, gw11.w); 293 294 tvec4<T, P> norm00 = detail::taylorInvSqrt(tvec4<T, P>(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100))); 295 g0000 *= norm00.x; 296 g0100 *= norm00.y; 297 g1000 *= norm00.z; 298 g1100 *= norm00.w; 299 300 tvec4<T, P> norm01 = detail::taylorInvSqrt(tvec4<T, P>(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101))); 301 g0001 *= norm01.x; 302 g0101 *= norm01.y; 303 g1001 *= norm01.z; 304 g1101 *= norm01.w; 305 306 tvec4<T, P> norm10 = detail::taylorInvSqrt(tvec4<T, P>(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110))); 307 g0010 *= norm10.x; 308 g0110 *= norm10.y; 309 g1010 *= norm10.z; 310 g1110 *= norm10.w; 311 312 tvec4<T, P> norm11 = detail::taylorInvSqrt(tvec4<T, P>(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111))); 313 g0011 *= norm11.x; 314 g0111 *= norm11.y; 315 g1011 *= norm11.z; 316 g1111 *= norm11.w; 317 318 T n0000 = dot(g0000, Pf0); 319 T n1000 = dot(g1000, tvec4<T, P>(Pf1.x, Pf0.y, Pf0.z, Pf0.w)); 320 T n0100 = dot(g0100, tvec4<T, P>(Pf0.x, Pf1.y, Pf0.z, Pf0.w)); 321 T n1100 = dot(g1100, tvec4<T, P>(Pf1.x, Pf1.y, Pf0.z, Pf0.w)); 322 T n0010 = dot(g0010, tvec4<T, P>(Pf0.x, Pf0.y, Pf1.z, Pf0.w)); 323 T n1010 = dot(g1010, tvec4<T, P>(Pf1.x, Pf0.y, Pf1.z, Pf0.w)); 324 T n0110 = dot(g0110, tvec4<T, P>(Pf0.x, Pf1.y, Pf1.z, Pf0.w)); 325 T n1110 = dot(g1110, tvec4<T, P>(Pf1.x, Pf1.y, Pf1.z, Pf0.w)); 326 T n0001 = dot(g0001, tvec4<T, P>(Pf0.x, Pf0.y, Pf0.z, Pf1.w)); 327 T n1001 = dot(g1001, tvec4<T, P>(Pf1.x, Pf0.y, Pf0.z, Pf1.w)); 328 T n0101 = dot(g0101, tvec4<T, P>(Pf0.x, Pf1.y, Pf0.z, Pf1.w)); 329 T n1101 = dot(g1101, tvec4<T, P>(Pf1.x, Pf1.y, Pf0.z, Pf1.w)); 330 T n0011 = dot(g0011, tvec4<T, P>(Pf0.x, Pf0.y, Pf1.z, Pf1.w)); 331 T n1011 = dot(g1011, tvec4<T, P>(Pf1.x, Pf0.y, Pf1.z, Pf1.w)); 332 T n0111 = dot(g0111, tvec4<T, P>(Pf0.x, Pf1.y, Pf1.z, Pf1.w)); 333 T n1111 = dot(g1111, Pf1); 334 335 tvec4<T, P> fade_xyzw = detail::fade(Pf0); 336 tvec4<T, P> n_0w = mix(tvec4<T, P>(n0000, n1000, n0100, n1100), tvec4<T, P>(n0001, n1001, n0101, n1101), fade_xyzw.w); 337 tvec4<T, P> n_1w = mix(tvec4<T, P>(n0010, n1010, n0110, n1110), tvec4<T, P>(n0011, n1011, n0111, n1111), fade_xyzw.w); 338 tvec4<T, P> n_zw = mix(n_0w, n_1w, fade_xyzw.z); 339 tvec2<T, P> n_yzw = mix(tvec2<T, P>(n_zw.x, n_zw.y), tvec2<T, P>(n_zw.z, n_zw.w), fade_xyzw.y); 340 T n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x); 341 return T(2.2) * n_xyzw; 342 } 343 344 // Classic Perlin noise, periodic variant 345 template <typename T, precision P> 346 GLM_FUNC_QUALIFIER T perlin(tvec2<T, P> const & Position, tvec2<T, P> const & rep) 347 { 348 tvec4<T, P> Pi = floor(tvec4<T, P>(Position.x, Position.y, Position.x, Position.y)) + tvec4<T, P>(0.0, 0.0, 1.0, 1.0); 349 tvec4<T, P> Pf = fract(tvec4<T, P>(Position.x, Position.y, Position.x, Position.y)) - tvec4<T, P>(0.0, 0.0, 1.0, 1.0); 350 Pi = mod(Pi, tvec4<T, P>(rep.x, rep.y, rep.x, rep.y)); // To create noise with explicit period 351 Pi = mod(Pi, tvec4<T, P>(289)); // To avoid truncation effects in permutation 352 tvec4<T, P> ix(Pi.x, Pi.z, Pi.x, Pi.z); 353 tvec4<T, P> iy(Pi.y, Pi.y, Pi.w, Pi.w); 354 tvec4<T, P> fx(Pf.x, Pf.z, Pf.x, Pf.z); 355 tvec4<T, P> fy(Pf.y, Pf.y, Pf.w, Pf.w); 356 357 tvec4<T, P> i = detail::permute(detail::permute(ix) + iy); 358 359 tvec4<T, P> gx = static_cast<T>(2) * fract(i / T(41)) - T(1); 360 tvec4<T, P> gy = abs(gx) - T(0.5); 361 tvec4<T, P> tx = floor(gx + T(0.5)); 362 gx = gx - tx; 363 364 tvec2<T, P> g00(gx.x, gy.x); 365 tvec2<T, P> g10(gx.y, gy.y); 366 tvec2<T, P> g01(gx.z, gy.z); 367 tvec2<T, P> g11(gx.w, gy.w); 368 369 tvec4<T, P> norm = detail::taylorInvSqrt(tvec4<T, P>(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11))); 370 g00 *= norm.x; 371 g01 *= norm.y; 372 g10 *= norm.z; 373 g11 *= norm.w; 374 375 T n00 = dot(g00, tvec2<T, P>(fx.x, fy.x)); 376 T n10 = dot(g10, tvec2<T, P>(fx.y, fy.y)); 377 T n01 = dot(g01, tvec2<T, P>(fx.z, fy.z)); 378 T n11 = dot(g11, tvec2<T, P>(fx.w, fy.w)); 379 380 tvec2<T, P> fade_xy = detail::fade(tvec2<T, P>(Pf.x, Pf.y)); 381 tvec2<T, P> n_x = mix(tvec2<T, P>(n00, n01), tvec2<T, P>(n10, n11), fade_xy.x); 382 T n_xy = mix(n_x.x, n_x.y, fade_xy.y); 383 return T(2.3) * n_xy; 384 } 385 386 // Classic Perlin noise, periodic variant 387 template <typename T, precision P> 388 GLM_FUNC_QUALIFIER T perlin(tvec3<T, P> const & Position, tvec3<T, P> const & rep) 389 { 390 tvec3<T, P> Pi0 = mod(floor(Position), rep); // Integer part, modulo period 391 tvec3<T, P> Pi1 = mod(Pi0 + tvec3<T, P>(T(1)), rep); // Integer part + 1, mod period 392 Pi0 = mod(Pi0, tvec3<T, P>(289)); 393 Pi1 = mod(Pi1, tvec3<T, P>(289)); 394 tvec3<T, P> Pf0 = fract(Position); // Fractional part for interpolation 395 tvec3<T, P> Pf1 = Pf0 - tvec3<T, P>(T(1)); // Fractional part - 1.0 396 tvec4<T, P> ix = tvec4<T, P>(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 397 tvec4<T, P> iy = tvec4<T, P>(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 398 tvec4<T, P> iz0(Pi0.z); 399 tvec4<T, P> iz1(Pi1.z); 400 401 tvec4<T, P> ixy = detail::permute(detail::permute(ix) + iy); 402 tvec4<T, P> ixy0 = detail::permute(ixy + iz0); 403 tvec4<T, P> ixy1 = detail::permute(ixy + iz1); 404 405 tvec4<T, P> gx0 = ixy0 / T(7); 406 tvec4<T, P> gy0 = fract(floor(gx0) / T(7)) - T(0.5); 407 gx0 = fract(gx0); 408 tvec4<T, P> gz0 = tvec4<T, P>(0.5) - abs(gx0) - abs(gy0); 409 tvec4<T, P> sz0 = step(gz0, tvec4<T, P>(0)); 410 gx0 -= sz0 * (step(T(0), gx0) - T(0.5)); 411 gy0 -= sz0 * (step(T(0), gy0) - T(0.5)); 412 413 tvec4<T, P> gx1 = ixy1 / T(7); 414 tvec4<T, P> gy1 = fract(floor(gx1) / T(7)) - T(0.5); 415 gx1 = fract(gx1); 416 tvec4<T, P> gz1 = tvec4<T, P>(0.5) - abs(gx1) - abs(gy1); 417 tvec4<T, P> sz1 = step(gz1, tvec4<T, P>(T(0))); 418 gx1 -= sz1 * (step(T(0), gx1) - T(0.5)); 419 gy1 -= sz1 * (step(T(0), gy1) - T(0.5)); 420 421 tvec3<T, P> g000 = tvec3<T, P>(gx0.x, gy0.x, gz0.x); 422 tvec3<T, P> g100 = tvec3<T, P>(gx0.y, gy0.y, gz0.y); 423 tvec3<T, P> g010 = tvec3<T, P>(gx0.z, gy0.z, gz0.z); 424 tvec3<T, P> g110 = tvec3<T, P>(gx0.w, gy0.w, gz0.w); 425 tvec3<T, P> g001 = tvec3<T, P>(gx1.x, gy1.x, gz1.x); 426 tvec3<T, P> g101 = tvec3<T, P>(gx1.y, gy1.y, gz1.y); 427 tvec3<T, P> g011 = tvec3<T, P>(gx1.z, gy1.z, gz1.z); 428 tvec3<T, P> g111 = tvec3<T, P>(gx1.w, gy1.w, gz1.w); 429 430 tvec4<T, P> norm0 = detail::taylorInvSqrt(tvec4<T, P>(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); 431 g000 *= norm0.x; 432 g010 *= norm0.y; 433 g100 *= norm0.z; 434 g110 *= norm0.w; 435 tvec4<T, P> norm1 = detail::taylorInvSqrt(tvec4<T, P>(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); 436 g001 *= norm1.x; 437 g011 *= norm1.y; 438 g101 *= norm1.z; 439 g111 *= norm1.w; 440 441 T n000 = dot(g000, Pf0); 442 T n100 = dot(g100, tvec3<T, P>(Pf1.x, Pf0.y, Pf0.z)); 443 T n010 = dot(g010, tvec3<T, P>(Pf0.x, Pf1.y, Pf0.z)); 444 T n110 = dot(g110, tvec3<T, P>(Pf1.x, Pf1.y, Pf0.z)); 445 T n001 = dot(g001, tvec3<T, P>(Pf0.x, Pf0.y, Pf1.z)); 446 T n101 = dot(g101, tvec3<T, P>(Pf1.x, Pf0.y, Pf1.z)); 447 T n011 = dot(g011, tvec3<T, P>(Pf0.x, Pf1.y, Pf1.z)); 448 T n111 = dot(g111, Pf1); 449 450 tvec3<T, P> fade_xyz = detail::fade(Pf0); 451 tvec4<T, P> n_z = mix(tvec4<T, P>(n000, n100, n010, n110), tvec4<T, P>(n001, n101, n011, n111), fade_xyz.z); 452 tvec2<T, P> n_yz = mix(tvec2<T, P>(n_z.x, n_z.y), tvec2<T, P>(n_z.z, n_z.w), fade_xyz.y); 453 T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); 454 return T(2.2) * n_xyz; 455 } 456 457 // Classic Perlin noise, periodic version 458 template <typename T, precision P> 459 GLM_FUNC_QUALIFIER T perlin(tvec4<T, P> const & Position, tvec4<T, P> const & rep) 460 { 461 tvec4<T, P> Pi0 = mod(floor(Position), rep); // Integer part modulo rep 462 tvec4<T, P> Pi1 = mod(Pi0 + T(1), rep); // Integer part + 1 mod rep 463 tvec4<T, P> Pf0 = fract(Position); // Fractional part for interpolation 464 tvec4<T, P> Pf1 = Pf0 - T(1); // Fractional part - 1.0 465 tvec4<T, P> ix = tvec4<T, P>(Pi0.x, Pi1.x, Pi0.x, Pi1.x); 466 tvec4<T, P> iy = tvec4<T, P>(Pi0.y, Pi0.y, Pi1.y, Pi1.y); 467 tvec4<T, P> iz0(Pi0.z); 468 tvec4<T, P> iz1(Pi1.z); 469 tvec4<T, P> iw0(Pi0.w); 470 tvec4<T, P> iw1(Pi1.w); 471 472 tvec4<T, P> ixy = detail::permute(detail::permute(ix) + iy); 473 tvec4<T, P> ixy0 = detail::permute(ixy + iz0); 474 tvec4<T, P> ixy1 = detail::permute(ixy + iz1); 475 tvec4<T, P> ixy00 = detail::permute(ixy0 + iw0); 476 tvec4<T, P> ixy01 = detail::permute(ixy0 + iw1); 477 tvec4<T, P> ixy10 = detail::permute(ixy1 + iw0); 478 tvec4<T, P> ixy11 = detail::permute(ixy1 + iw1); 479 480 tvec4<T, P> gx00 = ixy00 / T(7); 481 tvec4<T, P> gy00 = floor(gx00) / T(7); 482 tvec4<T, P> gz00 = floor(gy00) / T(6); 483 gx00 = fract(gx00) - T(0.5); 484 gy00 = fract(gy00) - T(0.5); 485 gz00 = fract(gz00) - T(0.5); 486 tvec4<T, P> gw00 = tvec4<T, P>(0.75) - abs(gx00) - abs(gy00) - abs(gz00); 487 tvec4<T, P> sw00 = step(gw00, tvec4<T, P>(0)); 488 gx00 -= sw00 * (step(T(0), gx00) - T(0.5)); 489 gy00 -= sw00 * (step(T(0), gy00) - T(0.5)); 490 491 tvec4<T, P> gx01 = ixy01 / T(7); 492 tvec4<T, P> gy01 = floor(gx01) / T(7); 493 tvec4<T, P> gz01 = floor(gy01) / T(6); 494 gx01 = fract(gx01) - T(0.5); 495 gy01 = fract(gy01) - T(0.5); 496 gz01 = fract(gz01) - T(0.5); 497 tvec4<T, P> gw01 = tvec4<T, P>(0.75) - abs(gx01) - abs(gy01) - abs(gz01); 498 tvec4<T, P> sw01 = step(gw01, tvec4<T, P>(0.0)); 499 gx01 -= sw01 * (step(T(0), gx01) - T(0.5)); 500 gy01 -= sw01 * (step(T(0), gy01) - T(0.5)); 501 502 tvec4<T, P> gx10 = ixy10 / T(7); 503 tvec4<T, P> gy10 = floor(gx10) / T(7); 504 tvec4<T, P> gz10 = floor(gy10) / T(6); 505 gx10 = fract(gx10) - T(0.5); 506 gy10 = fract(gy10) - T(0.5); 507 gz10 = fract(gz10) - T(0.5); 508 tvec4<T, P> gw10 = tvec4<T, P>(0.75) - abs(gx10) - abs(gy10) - abs(gz10); 509 tvec4<T, P> sw10 = step(gw10, tvec4<T, P>(0.0)); 510 gx10 -= sw10 * (step(T(0), gx10) - T(0.5)); 511 gy10 -= sw10 * (step(T(0), gy10) - T(0.5)); 512 513 tvec4<T, P> gx11 = ixy11 / T(7); 514 tvec4<T, P> gy11 = floor(gx11) / T(7); 515 tvec4<T, P> gz11 = floor(gy11) / T(6); 516 gx11 = fract(gx11) - T(0.5); 517 gy11 = fract(gy11) - T(0.5); 518 gz11 = fract(gz11) - T(0.5); 519 tvec4<T, P> gw11 = tvec4<T, P>(0.75) - abs(gx11) - abs(gy11) - abs(gz11); 520 tvec4<T, P> sw11 = step(gw11, tvec4<T, P>(T(0))); 521 gx11 -= sw11 * (step(T(0), gx11) - T(0.5)); 522 gy11 -= sw11 * (step(T(0), gy11) - T(0.5)); 523 524 tvec4<T, P> g0000(gx00.x, gy00.x, gz00.x, gw00.x); 525 tvec4<T, P> g1000(gx00.y, gy00.y, gz00.y, gw00.y); 526 tvec4<T, P> g0100(gx00.z, gy00.z, gz00.z, gw00.z); 527 tvec4<T, P> g1100(gx00.w, gy00.w, gz00.w, gw00.w); 528 tvec4<T, P> g0010(gx10.x, gy10.x, gz10.x, gw10.x); 529 tvec4<T, P> g1010(gx10.y, gy10.y, gz10.y, gw10.y); 530 tvec4<T, P> g0110(gx10.z, gy10.z, gz10.z, gw10.z); 531 tvec4<T, P> g1110(gx10.w, gy10.w, gz10.w, gw10.w); 532 tvec4<T, P> g0001(gx01.x, gy01.x, gz01.x, gw01.x); 533 tvec4<T, P> g1001(gx01.y, gy01.y, gz01.y, gw01.y); 534 tvec4<T, P> g0101(gx01.z, gy01.z, gz01.z, gw01.z); 535 tvec4<T, P> g1101(gx01.w, gy01.w, gz01.w, gw01.w); 536 tvec4<T, P> g0011(gx11.x, gy11.x, gz11.x, gw11.x); 537 tvec4<T, P> g1011(gx11.y, gy11.y, gz11.y, gw11.y); 538 tvec4<T, P> g0111(gx11.z, gy11.z, gz11.z, gw11.z); 539 tvec4<T, P> g1111(gx11.w, gy11.w, gz11.w, gw11.w); 540 541 tvec4<T, P> norm00 = detail::taylorInvSqrt(tvec4<T, P>(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100))); 542 g0000 *= norm00.x; 543 g0100 *= norm00.y; 544 g1000 *= norm00.z; 545 g1100 *= norm00.w; 546 547 tvec4<T, P> norm01 = detail::taylorInvSqrt(tvec4<T, P>(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101))); 548 g0001 *= norm01.x; 549 g0101 *= norm01.y; 550 g1001 *= norm01.z; 551 g1101 *= norm01.w; 552 553 tvec4<T, P> norm10 = detail::taylorInvSqrt(tvec4<T, P>(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110))); 554 g0010 *= norm10.x; 555 g0110 *= norm10.y; 556 g1010 *= norm10.z; 557 g1110 *= norm10.w; 558 559 tvec4<T, P> norm11 = detail::taylorInvSqrt(tvec4<T, P>(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111))); 560 g0011 *= norm11.x; 561 g0111 *= norm11.y; 562 g1011 *= norm11.z; 563 g1111 *= norm11.w; 564 565 T n0000 = dot(g0000, Pf0); 566 T n1000 = dot(g1000, tvec4<T, P>(Pf1.x, Pf0.y, Pf0.z, Pf0.w)); 567 T n0100 = dot(g0100, tvec4<T, P>(Pf0.x, Pf1.y, Pf0.z, Pf0.w)); 568 T n1100 = dot(g1100, tvec4<T, P>(Pf1.x, Pf1.y, Pf0.z, Pf0.w)); 569 T n0010 = dot(g0010, tvec4<T, P>(Pf0.x, Pf0.y, Pf1.z, Pf0.w)); 570 T n1010 = dot(g1010, tvec4<T, P>(Pf1.x, Pf0.y, Pf1.z, Pf0.w)); 571 T n0110 = dot(g0110, tvec4<T, P>(Pf0.x, Pf1.y, Pf1.z, Pf0.w)); 572 T n1110 = dot(g1110, tvec4<T, P>(Pf1.x, Pf1.y, Pf1.z, Pf0.w)); 573 T n0001 = dot(g0001, tvec4<T, P>(Pf0.x, Pf0.y, Pf0.z, Pf1.w)); 574 T n1001 = dot(g1001, tvec4<T, P>(Pf1.x, Pf0.y, Pf0.z, Pf1.w)); 575 T n0101 = dot(g0101, tvec4<T, P>(Pf0.x, Pf1.y, Pf0.z, Pf1.w)); 576 T n1101 = dot(g1101, tvec4<T, P>(Pf1.x, Pf1.y, Pf0.z, Pf1.w)); 577 T n0011 = dot(g0011, tvec4<T, P>(Pf0.x, Pf0.y, Pf1.z, Pf1.w)); 578 T n1011 = dot(g1011, tvec4<T, P>(Pf1.x, Pf0.y, Pf1.z, Pf1.w)); 579 T n0111 = dot(g0111, tvec4<T, P>(Pf0.x, Pf1.y, Pf1.z, Pf1.w)); 580 T n1111 = dot(g1111, Pf1); 581 582 tvec4<T, P> fade_xyzw = detail::fade(Pf0); 583 tvec4<T, P> n_0w = mix(tvec4<T, P>(n0000, n1000, n0100, n1100), tvec4<T, P>(n0001, n1001, n0101, n1101), fade_xyzw.w); 584 tvec4<T, P> n_1w = mix(tvec4<T, P>(n0010, n1010, n0110, n1110), tvec4<T, P>(n0011, n1011, n0111, n1111), fade_xyzw.w); 585 tvec4<T, P> n_zw = mix(n_0w, n_1w, fade_xyzw.z); 586 tvec2<T, P> n_yzw = mix(tvec2<T, P>(n_zw.x, n_zw.y), tvec2<T, P>(n_zw.z, n_zw.w), fade_xyzw.y); 587 T n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x); 588 return T(2.2) * n_xyzw; 589 } 590 591 template <typename T, precision P> 592 GLM_FUNC_QUALIFIER T simplex(glm::tvec2<T, P> const & v) 593 { 594 tvec4<T, P> const C = tvec4<T, P>( 595 T( 0.211324865405187), // (3.0 - sqrt(3.0)) / 6.0 596 T( 0.366025403784439), // 0.5 * (sqrt(3.0) - 1.0) 597 T(-0.577350269189626), // -1.0 + 2.0 * C.x 598 T( 0.024390243902439)); // 1.0 / 41.0 599 600 // First corner 601 tvec2<T, P> i = floor(v + dot(v, tvec2<T, P>(C[1]))); 602 tvec2<T, P> x0 = v - i + dot(i, tvec2<T, P>(C[0])); 603 604 // Other corners 605 //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0 606 //i1.y = 1.0 - i1.x; 607 tvec2<T, P> i1 = (x0.x > x0.y) ? tvec2<T, P>(1, 0) : tvec2<T, P>(0, 1); 608 // x0 = x0 - 0.0 + 0.0 * C.xx ; 609 // x1 = x0 - i1 + 1.0 * C.xx ; 610 // x2 = x0 - 1.0 + 2.0 * C.xx ; 611 tvec4<T, P> x12 = tvec4<T, P>(x0.x, x0.y, x0.x, x0.y) + tvec4<T, P>(C.x, C.x, C.z, C.z); 612 x12 = tvec4<T, P>(tvec2<T, P>(x12) - i1, x12.z, x12.w); 613 614 // Permutations 615 i = mod(i, tvec2<T, P>(289)); // Avoid truncation effects in permutation 616 tvec3<T, P> p = detail::permute( 617 detail::permute(i.y + tvec3<T, P>(T(0), i1.y, T(1))) 618 + i.x + tvec3<T, P>(T(0), i1.x, T(1))); 619 620 tvec3<T, P> m = max(tvec3<T, P>(0.5) - tvec3<T, P>( 621 dot(x0, x0), 622 dot(tvec2<T, P>(x12.x, x12.y), tvec2<T, P>(x12.x, x12.y)), 623 dot(tvec2<T, P>(x12.z, x12.w), tvec2<T, P>(x12.z, x12.w))), tvec3<T, P>(0)); 624 m = m * m ; 625 m = m * m ; 626 627 // Gradients: 41 points uniformly over a line, mapped onto a diamond. 628 // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287) 629 630 tvec3<T, P> x = static_cast<T>(2) * fract(p * C.w) - T(1); 631 tvec3<T, P> h = abs(x) - T(0.5); 632 tvec3<T, P> ox = floor(x + T(0.5)); 633 tvec3<T, P> a0 = x - ox; 634 635 // Normalise gradients implicitly by scaling m 636 // Inlined for speed: m *= taylorInvSqrt( a0*a0 + h*h ); 637 m *= static_cast<T>(1.79284291400159) - T(0.85373472095314) * (a0 * a0 + h * h); 638 639 // Compute final noise value at P 640 tvec3<T, P> g; 641 g.x = a0.x * x0.x + h.x * x0.y; 642 //g.yz = a0.yz * x12.xz + h.yz * x12.yw; 643 g.y = a0.y * x12.x + h.y * x12.y; 644 g.z = a0.z * x12.z + h.z * x12.w; 645 return T(130) * dot(m, g); 646 } 647 648 template <typename T, precision P> 649 GLM_FUNC_QUALIFIER T simplex(tvec3<T, P> const & v) 650 { 651 tvec2<T, P> const C(1.0 / 6.0, 1.0 / 3.0); 652 tvec4<T, P> const D(0.0, 0.5, 1.0, 2.0); 653 654 // First corner 655 tvec3<T, P> i(floor(v + dot(v, tvec3<T, P>(C.y)))); 656 tvec3<T, P> x0(v - i + dot(i, tvec3<T, P>(C.x))); 657 658 // Other corners 659 tvec3<T, P> g(step(tvec3<T, P>(x0.y, x0.z, x0.x), x0)); 660 tvec3<T, P> l(T(1) - g); 661 tvec3<T, P> i1(min(g, tvec3<T, P>(l.z, l.x, l.y))); 662 tvec3<T, P> i2(max(g, tvec3<T, P>(l.z, l.x, l.y))); 663 664 // x0 = x0 - 0.0 + 0.0 * C.xxx; 665 // x1 = x0 - i1 + 1.0 * C.xxx; 666 // x2 = x0 - i2 + 2.0 * C.xxx; 667 // x3 = x0 - 1.0 + 3.0 * C.xxx; 668 tvec3<T, P> x1(x0 - i1 + C.x); 669 tvec3<T, P> x2(x0 - i2 + C.y); // 2.0*C.x = 1/3 = C.y 670 tvec3<T, P> x3(x0 - D.y); // -1.0+3.0*C.x = -0.5 = -D.y 671 672 // Permutations 673 i = detail::mod289(i); 674 tvec4<T, P> p(detail::permute(detail::permute(detail::permute( 675 i.z + tvec4<T, P>(T(0), i1.z, i2.z, T(1))) + 676 i.y + tvec4<T, P>(T(0), i1.y, i2.y, T(1))) + 677 i.x + tvec4<T, P>(T(0), i1.x, i2.x, T(1)))); 678 679 // Gradients: 7x7 points over a square, mapped onto an octahedron. 680 // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 681 T n_ = static_cast<T>(0.142857142857); // 1.0/7.0 682 tvec3<T, P> ns(n_ * tvec3<T, P>(D.w, D.y, D.z) - tvec3<T, P>(D.x, D.z, D.x)); 683 684 tvec4<T, P> j(p - T(49) * floor(p * ns.z * ns.z)); // mod(p,7*7) 685 686 tvec4<T, P> x_(floor(j * ns.z)); 687 tvec4<T, P> y_(floor(j - T(7) * x_)); // mod(j,N) 688 689 tvec4<T, P> x(x_ * ns.x + ns.y); 690 tvec4<T, P> y(y_ * ns.x + ns.y); 691 tvec4<T, P> h(T(1) - abs(x) - abs(y)); 692 693 tvec4<T, P> b0(x.x, x.y, y.x, y.y); 694 tvec4<T, P> b1(x.z, x.w, y.z, y.w); 695 696 // vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; 697 // vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; 698 tvec4<T, P> s0(floor(b0) * T(2) + T(1)); 699 tvec4<T, P> s1(floor(b1) * T(2) + T(1)); 700 tvec4<T, P> sh(-step(h, tvec4<T, P>(0.0))); 701 702 tvec4<T, P> a0 = tvec4<T, P>(b0.x, b0.z, b0.y, b0.w) + tvec4<T, P>(s0.x, s0.z, s0.y, s0.w) * tvec4<T, P>(sh.x, sh.x, sh.y, sh.y); 703 tvec4<T, P> a1 = tvec4<T, P>(b1.x, b1.z, b1.y, b1.w) + tvec4<T, P>(s1.x, s1.z, s1.y, s1.w) * tvec4<T, P>(sh.z, sh.z, sh.w, sh.w); 704 705 tvec3<T, P> p0(a0.x, a0.y, h.x); 706 tvec3<T, P> p1(a0.z, a0.w, h.y); 707 tvec3<T, P> p2(a1.x, a1.y, h.z); 708 tvec3<T, P> p3(a1.z, a1.w, h.w); 709 710 // Normalise gradients 711 tvec4<T, P> norm = detail::taylorInvSqrt(tvec4<T, P>(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3))); 712 p0 *= norm.x; 713 p1 *= norm.y; 714 p2 *= norm.z; 715 p3 *= norm.w; 716 717 // Mix final noise value 718 tvec4<T, P> m = max(T(0.6) - tvec4<T, P>(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), tvec4<T, P>(0)); 719 m = m * m; 720 return T(42) * dot(m * m, tvec4<T, P>(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3))); 721 } 722 723 template <typename T, precision P> 724 GLM_FUNC_QUALIFIER T simplex(tvec4<T, P> const & v) 725 { 726 tvec4<T, P> const C( 727 0.138196601125011, // (5 - sqrt(5))/20 G4 728 0.276393202250021, // 2 * G4 729 0.414589803375032, // 3 * G4 730 -0.447213595499958); // -1 + 4 * G4 731 732 // (sqrt(5) - 1)/4 = F4, used once below 733 T const F4 = static_cast<T>(0.309016994374947451); 734 735 // First corner 736 tvec4<T, P> i = floor(v + dot(v, vec4(F4))); 737 tvec4<T, P> x0 = v - i + dot(i, vec4(C.x)); 738 739 // Other corners 740 741 // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI) 742 tvec4<T, P> i0; 743 tvec3<T, P> isX = step(tvec3<T, P>(x0.y, x0.z, x0.w), tvec3<T, P>(x0.x)); 744 tvec3<T, P> isYZ = step(tvec3<T, P>(x0.z, x0.w, x0.w), tvec3<T, P>(x0.y, x0.y, x0.z)); 745 // i0.x = dot(isX, vec3(1.0)); 746 //i0.x = isX.x + isX.y + isX.z; 747 //i0.yzw = static_cast<T>(1) - isX; 748 i0 = tvec4<T, P>(isX.x + isX.y + isX.z, T(1) - isX); 749 // i0.y += dot(isYZ.xy, vec2(1.0)); 750 i0.y += isYZ.x + isYZ.y; 751 //i0.zw += 1.0 - tvec2<T, P>(isYZ.x, isYZ.y); 752 i0.z += static_cast<T>(1) - isYZ.x; 753 i0.w += static_cast<T>(1) - isYZ.y; 754 i0.z += isYZ.z; 755 i0.w += static_cast<T>(1) - isYZ.z; 756 757 // i0 now contains the unique values 0,1,2,3 in each channel 758 tvec4<T, P> i3 = clamp(i0, T(0), T(1)); 759 tvec4<T, P> i2 = clamp(i0 - T(1), T(0), T(1)); 760 tvec4<T, P> i1 = clamp(i0 - T(2), T(0), T(1)); 761 762 // x0 = x0 - 0.0 + 0.0 * C.xxxx 763 // x1 = x0 - i1 + 0.0 * C.xxxx 764 // x2 = x0 - i2 + 0.0 * C.xxxx 765 // x3 = x0 - i3 + 0.0 * C.xxxx 766 // x4 = x0 - 1.0 + 4.0 * C.xxxx 767 tvec4<T, P> x1 = x0 - i1 + C.x; 768 tvec4<T, P> x2 = x0 - i2 + C.y; 769 tvec4<T, P> x3 = x0 - i3 + C.z; 770 tvec4<T, P> x4 = x0 + C.w; 771 772 // Permutations 773 i = mod(i, tvec4<T, P>(289)); 774 T j0 = detail::permute(detail::permute(detail::permute(detail::permute(i.w) + i.z) + i.y) + i.x); 775 tvec4<T, P> j1 = detail::permute(detail::permute(detail::permute(detail::permute( 776 i.w + tvec4<T, P>(i1.w, i2.w, i3.w, T(1))) + 777 i.z + tvec4<T, P>(i1.z, i2.z, i3.z, T(1))) + 778 i.y + tvec4<T, P>(i1.y, i2.y, i3.y, T(1))) + 779 i.x + tvec4<T, P>(i1.x, i2.x, i3.x, T(1))); 780 781 // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope 782 // 7*7*6 = 294, which is close to the ring size 17*17 = 289. 783 tvec4<T, P> ip = tvec4<T, P>(T(1) / T(294), T(1) / T(49), T(1) / T(7), T(0)); 784 785 tvec4<T, P> p0 = gtc::grad4(j0, ip); 786 tvec4<T, P> p1 = gtc::grad4(j1.x, ip); 787 tvec4<T, P> p2 = gtc::grad4(j1.y, ip); 788 tvec4<T, P> p3 = gtc::grad4(j1.z, ip); 789 tvec4<T, P> p4 = gtc::grad4(j1.w, ip); 790 791 // Normalise gradients 792 tvec4<T, P> norm = detail::taylorInvSqrt(tvec4<T, P>(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3))); 793 p0 *= norm.x; 794 p1 *= norm.y; 795 p2 *= norm.z; 796 p3 *= norm.w; 797 p4 *= detail::taylorInvSqrt(dot(p4, p4)); 798 799 // Mix contributions from the five corners 800 tvec3<T, P> m0 = max(T(0.6) - tvec3<T, P>(dot(x0, x0), dot(x1, x1), dot(x2, x2)), tvec3<T, P>(0)); 801 tvec2<T, P> m1 = max(T(0.6) - tvec2<T, P>(dot(x3, x3), dot(x4, x4) ), tvec2<T, P>(0)); 802 m0 = m0 * m0; 803 m1 = m1 * m1; 804 return T(49) * 805 (dot(m0 * m0, tvec3<T, P>(dot(p0, x0), dot(p1, x1), dot(p2, x2))) + 806 dot(m1 * m1, tvec2<T, P>(dot(p3, x3), dot(p4, x4)))); 807 } 808}//namespace glm 809