noise.inl 32 KB

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