Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.25 KB | None | 0 0
  1. /* sdnoise1234, Simplex noise with true analytic
  2. * derivative in 1D to 4D.
  3. *
  4. * Copyright © 2003-2008, Stefan Gustavson
  5. *
  6. * Contact: stefan.gustavson@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22.  
  23. /** \file
  24. \brief C implementation file for Perlin simplex noise with analytic
  25. derivative over 1, 2, 3 and 4 dimensions.
  26. \author Stefan Gustavson (stefan.gustavson@gmail.com)
  27. \author Charl van Deventer (landon.skyfire@gmail.com)
  28. */
  29.  
  30. /*
  31. * This is an implementation of Perlin "simplex noise" over one
  32. * dimension (x), two dimensions (x,y), three dimensions (x,y,z)
  33. * and four dimensions (x,y,z,w). The analytic derivative is
  34. * returned, to make it possible to do lots of fun stuff like
  35. * flow animations, curl noise, analytic antialiasing and such.
  36. *
  37. * Visually, this noise is exactly the same as the plain version of
  38. * simplex noise provided in the file "snoise1234.c". It just returns
  39. * all partial derivatives in addition to the scalar noise value.
  40. *
  41. */
  42.  
  43. /*
  44. * 23 June 2010: Modified by Charl van Deventer to allow periodic arguments
  45. * Note: It doesn't check for bounds over 255 (wont work) and might fail with
  46. * negative coords.
  47. */
  48.  
  49. #include <math.h>
  50.  
  51. #include "sdnoise1234.h" /* We strictly don't need this, but play nice. */
  52.  
  53. #define FASTFLOOR(x) ( ((x)>0) ? ((int)x) : (((int)x)-1) )
  54.  
  55. /* Static data ---------------------- */
  56.  
  57. /*
  58. * Permutation table. This is just a random jumble of all numbers 0-255,
  59. * repeated twice to avoid wrapping the index at 255 for each lookup.
  60. */
  61. unsigned char perm[512] = {151,160,137,91,90,15,
  62. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  63. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  64. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  65. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  66. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  67. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  68. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  69. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  70. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  71. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  72. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  73. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
  74. 151,160,137,91,90,15,
  75. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  76. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  77. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  78. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  79. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  80. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  81. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  82. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  83. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  84. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  85. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  86. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
  87. };
  88.  
  89. /*
  90. * Gradient tables. These could be programmed the Ken Perlin way with
  91. * some clever bit-twiddling, but this is more clear, and not really slower.
  92. */
  93. static float grad2lut[8][2] = {
  94. { -1.0f, -1.0f }, { 1.0f, 0.0f } , { -1.0f, 0.0f } , { 1.0f, 1.0f } ,
  95. { -1.0f, 1.0f } , { 0.0f, -1.0f } , { 0.0f, 1.0f } , { 1.0f, -1.0f }
  96. };
  97.  
  98. /*
  99. * Gradient directions for 3D.
  100. * These vectors are based on the midpoints of the 12 edges of a cube.
  101. * A larger array of random unit length vectors would also do the job,
  102. * but these 12 (including 4 repeats to make the array length a power
  103. * of two) work better. They are not random, they are carefully chosen
  104. * to represent a small, isotropic set of directions.
  105. */
  106.  
  107. static float grad3lut[16][3] = {
  108. { 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f }, // 12 cube edges
  109. { -1.0f, 0.0f, 1.0f }, { 0.0f, -1.0f, 1.0f },
  110. { 1.0f, 0.0f, -1.0f }, { 0.0f, 1.0f, -1.0f },
  111. { -1.0f, 0.0f, -1.0f }, { 0.0f, -1.0f, -1.0f },
  112. { 1.0f, -1.0f, 0.0f }, { 1.0f, 1.0f, 0.0f },
  113. { -1.0f, 1.0f, 0.0f }, { -1.0f, -1.0f, 0.0f },
  114. { 1.0f, 0.0f, 1.0f }, { -1.0f, 0.0f, 1.0f }, // 4 repeats to make 16
  115. { 0.0f, 1.0f, -1.0f }, { 0.0f, -1.0f, -1.0f }
  116. };
  117.  
  118. static float grad4lut[32][4] = {
  119. { 0.0f, 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, -1.0f }, { 0.0f, 1.0f, -1.0f, 1.0f }, { 0.0f, 1.0f, -1.0f, -1.0f }, // 32 tesseract edges
  120. { 0.0f, -1.0f, 1.0f, 1.0f }, { 0.0f, -1.0f, 1.0f, -1.0f }, { 0.0f, -1.0f, -1.0f, 1.0f }, { 0.0f, -1.0f, -1.0f, -1.0f },
  121. { 1.0f, 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f, -1.0f }, { 1.0f, 0.0f, -1.0f, 1.0f }, { 1.0f, 0.0f, -1.0f, -1.0f },
  122. { -1.0f, 0.0f, 1.0f, 1.0f }, { -1.0f, 0.0f, 1.0f, -1.0f }, { -1.0f, 0.0f, -1.0f, 1.0f }, { -1.0f, 0.0f, -1.0f, -1.0f },
  123. { 1.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 0.0f, -1.0f }, { 1.0f, -1.0f, 0.0f, 1.0f }, { 1.0f, -1.0f, 0.0f, -1.0f },
  124. { -1.0f, 1.0f, 0.0f, 1.0f }, { -1.0f, 1.0f, 0.0f, -1.0f }, { -1.0f, -1.0f, 0.0f, 1.0f }, { -1.0f, -1.0f, 0.0f, -1.0f },
  125. { 1.0f, 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, -1.0f, 0.0f }, { 1.0f, -1.0f, 1.0f, 0.0f }, { 1.0f, -1.0f, -1.0f, 0.0f },
  126. { -1.0f, 1.0f, 1.0f, 0.0f }, { -1.0f, 1.0f, -1.0f, 0.0f }, { -1.0f, -1.0f, 1.0f, 0.0f }, { -1.0f, -1.0f, -1.0f, 0.0f }
  127. };
  128.  
  129. // A lookup table to traverse the simplex around a given point in 4D.
  130. // Details can be found where this table is used, in the 4D noise method.
  131. /* TODO: This should not be required, backport it from Bill's GLSL code! */
  132. static unsigned char simplex[64][4] = {
  133. {0,1,2,3},{0,1,3,2},{0,0,0,0},{0,2,3,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,2,3,0},
  134. {0,2,1,3},{0,0,0,0},{0,3,1,2},{0,3,2,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,3,2,0},
  135. {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
  136. {1,2,0,3},{0,0,0,0},{1,3,0,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,3,0,1},{2,3,1,0},
  137. {1,0,2,3},{1,0,3,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,0,3,1},{0,0,0,0},{2,1,3,0},
  138. {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
  139. {2,0,1,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,0,1,2},{3,0,2,1},{0,0,0,0},{3,1,2,0},
  140. {2,1,0,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,1,0,2},{0,0,0,0},{3,2,0,1},{3,2,1,0}};
  141.  
  142. /* --------------------------------------------------------------------- */
  143.  
  144. /*
  145. * Helper functions to compute gradients in 1D to 4D
  146. * and gradients-dot-residualvectors in 2D to 4D.
  147. */
  148.  
  149. float grad1( int hash, float *gx ) {
  150. int h = hash & 15;
  151. *gx = 1.0f + (h & 7); // Gradient value is one of 1.0, 2.0, ..., 8.0
  152. if (h&8) *gx = - *gx; // Make half of the gradients negative
  153. }
  154.  
  155. void grad2( int hash, float *gx, float *gy ) {
  156. int h = hash & 7;
  157. *gx = grad2lut[h][0];
  158. *gy = grad2lut[h][1];
  159. return;
  160. }
  161.  
  162. void grad3( int hash, float *gx, float *gy, float *gz ) {
  163. int h = hash & 15;
  164. *gx = grad3lut[h][0];
  165. *gy = grad3lut[h][1];
  166. *gz = grad3lut[h][2];
  167. return;
  168. }
  169.  
  170. void grad4( int hash, float *gx, float *gy, float *gz, float *gw) {
  171. int h = hash & 31;
  172. *gx = grad4lut[h][0];
  173. *gy = grad4lut[h][1];
  174. *gz = grad4lut[h][2];
  175. *gw = grad4lut[h][3];
  176. return;
  177. }
  178.  
  179. /** 1D simplex noise with derivative.
  180. * If the last argument is not null, the analytic derivative
  181. * is also calculated.
  182. */
  183. float sdnoise1( float x, int px, float *dnoise_dx)
  184. {
  185. int i0 = FASTFLOOR(x);
  186. int i1 = i0 + 1;
  187. float x0 = x - i0;
  188. float x1 = x0 - 1.0f;
  189.  
  190. float gx0, gx1;
  191. float n0, n1;
  192. float t20, t40, t21, t41;
  193.  
  194. float x20 = x0*x0;
  195. float t0 = 1.0f - x20;
  196. // if(t0 < 0.0f) t0 = 0.0f; // Never happens for 1D: x0<=1 always
  197. t20 = t0 * t0;
  198. t40 = t20 * t20;
  199. grad1(perm[i0 % px], &gx0);
  200. n0 = t40 * gx0 * x0;
  201.  
  202. float x21 = x1*x1;
  203. float t1 = 1.0f - x21;
  204. // if(t1 < 0.0f) t1 = 0.0f; // Never happens for 1D: |x1|<=1 always
  205. t21 = t1 * t1;
  206. t41 = t21 * t21;
  207. grad1(perm[i1 % px], &gx1);
  208. n1 = t41 * gx1 * x1;
  209.  
  210. /* Compute derivative according to:
  211. * *dnoise_dx = -8.0f * t20 * t0 * x0 * (gx0 * x0) + t40 * gx0;
  212. * *dnoise_dx += -8.0f * t21 * t1 * x1 * (gx1 * x1) + t41 * gx1;
  213. */
  214. *dnoise_dx = t20 * t0 * gx0 * x20;
  215. *dnoise_dx += t21 * t1 * gx1 * x21;
  216. *dnoise_dx *= -8.0f;
  217. *dnoise_dx += t40 * gx0 + t41 * gx1;
  218. *dnoise_dx *= 0.25f; /* Scale derivative to match the noise scaling */
  219.  
  220. // The maximum value of this noise is 8*(3/4)^4 = 2.53125
  221. // A factor of 0.395 would scale to fit exactly within [-1,1], but
  222. // to better match classic Perlin noise, we scale it down some more.
  223. return 0.25f * (n0 + n1);
  224. }
  225.  
  226. float sdnoise1( float x, float *dnoise_dx)
  227. {
  228. return sdnoise1(x, 256, dnoise_dx);
  229. }
  230.  
  231. /* Skewing factors for 2D simplex grid:
  232. * F2 = 0.5*(sqrt(3.0)-1.0)
  233. * G2 = (3.0-Math.sqrt(3.0))/6.0
  234. */
  235. #define F2 0.366025403
  236. #define G2 0.211324865
  237.  
  238. /** 2D simplex noise with derivatives.
  239. * If the last two arguments are not null, the analytic derivative
  240. * (the 2D gradient of the scalar noise field) is also calculated.
  241. */
  242. float sdnoise2( float x, float y, int px, int py, float *dnoise_dx, float *dnoise_dy )
  243. {
  244. float n0, n1, n2; /* Noise contributions from the three simplex corners */
  245. float gx0, gy0, gx1, gy1, gx2, gy2; /* Gradients at simplex corners */
  246.  
  247. /* Skew the input space to determine which simplex cell we're in */
  248. float s = ( x + y ) * F2; /* Hairy factor for 2D */
  249. float xs = x + s;
  250. float ys = y + s;
  251. int i = FASTFLOOR( xs );
  252. int j = FASTFLOOR( ys );
  253.  
  254. float t = ( float ) ( i + j ) * G2;
  255. float X0 = i - t; /* Unskew the cell origin back to (x,y) space */
  256. float Y0 = j - t;
  257. float x0 = x - X0; /* The x,y distances from the cell origin */
  258. float y0 = y - Y0;
  259.  
  260. /* For the 2D case, the simplex shape is an equilateral triangle.
  261. * Determine which simplex we are in. */
  262. int i1, j1; /* Offsets for second (middle) corner of simplex in (i,j) coords */
  263. if( x0 > y0 ) { i1 = 1; j1 = 0; } /* lower triangle, XY order: (0,0)->(1,0)->(1,1) */
  264. else { i1 = 0; j1 = 1; } /* upper triangle, YX order: (0,0)->(0,1)->(1,1) */
  265.  
  266. /* A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  267. * a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  268. * c = (3-sqrt(3))/6 */
  269. float x1 = x0 - i1 + G2; /* Offsets for middle corner in (x,y) unskewed coords */
  270. float y1 = y0 - j1 + G2;
  271. float x2 = x0 - 1.0f + 2.0f * G2; /* Offsets for last corner in (x,y) unskewed coords */
  272. float y2 = y0 - 1.0f + 2.0f * G2;
  273.  
  274. /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */
  275. int ii = i % px;
  276. int jj = j % py;
  277.  
  278. /* Calculate the contribution from the three corners */
  279. float t0 = 0.5f - x0 * x0 - y0 * y0;
  280. float t20, t40;
  281. if( t0 < 0.0f ) t40 = t20 = t0 = n0 = gx0 = gy0 = 0.0f; /* No influence */
  282. else {
  283. grad2( perm[ii + perm[jj]], &gx0, &gy0 );
  284. t20 = t0 * t0;
  285. t40 = t20 * t20;
  286. n0 = t40 * ( gx0 * x0 + gy0 * y0 );
  287. }
  288.  
  289. float t1 = 0.5f - x1 * x1 - y1 * y1;
  290. float t21, t41;
  291. if( t1 < 0.0f ) t21 = t41 = t1 = n1 = gx1 = gy1 = 0.0f; /* No influence */
  292. else {
  293. grad2( perm[ii + i1 + perm[jj + j1]], &gx1, &gy1 );
  294. t21 = t1 * t1;
  295. t41 = t21 * t21;
  296. n1 = t41 * ( gx1 * x1 + gy1 * y1 );
  297. }
  298.  
  299. float t2 = 0.5f - x2 * x2 - y2 * y2;
  300. float t22, t42;
  301. if( t2 < 0.0f ) t42 = t22 = t2 = n2 = gx2 = gy2 = 0.0f; /* No influence */
  302. else {
  303. grad2( perm[ii + 1 + perm[jj + 1]], &gx2, &gy2 );
  304. t22 = t2 * t2;
  305. t42 = t22 * t22;
  306. n2 = t42 * ( gx2 * x2 + gy2 * y2 );
  307. }
  308.  
  309. /* Add contributions from each corner to get the final noise value.
  310. * The result is scaled to return values in the interval [-1,1]. */
  311. float noise = 40.0f * ( n0 + n1 + n2 );
  312.  
  313. /* Compute derivative, if requested by supplying non-null pointers
  314. * for the last two arguments */
  315. if( ( dnoise_dx != 0 ) && ( dnoise_dy != 0 ) )
  316. {
  317. /* A straight, unoptimised calculation would be like:
  318. * *dnoise_dx = -8.0f * t20 * t0 * x0 * ( gx0 * x0 + gy0 * y0 ) + t40 * gx0;
  319. * *dnoise_dy = -8.0f * t20 * t0 * y0 * ( gx0 * x0 + gy0 * y0 ) + t40 * gy0;
  320. * *dnoise_dx += -8.0f * t21 * t1 * x1 * ( gx1 * x1 + gy1 * y1 ) + t41 * gx1;
  321. * *dnoise_dy += -8.0f * t21 * t1 * y1 * ( gx1 * x1 + gy1 * y1 ) + t41 * gy1;
  322. * *dnoise_dx += -8.0f * t22 * t2 * x2 * ( gx2 * x2 + gy2 * y2 ) + t42 * gx2;
  323. * *dnoise_dy += -8.0f * t22 * t2 * y2 * ( gx2 * x2 + gy2 * y2 ) + t42 * gy2;
  324. */
  325. float temp0 = t20 * t0 * ( gx0* x0 + gy0 * y0 );
  326. *dnoise_dx = temp0 * x0;
  327. *dnoise_dy = temp0 * y0;
  328. float temp1 = t21 * t1 * ( gx1 * x1 + gy1 * y1 );
  329. *dnoise_dx += temp1 * x1;
  330. *dnoise_dy += temp1 * y1;
  331. float temp2 = t22 * t2 * ( gx2* x2 + gy2 * y2 );
  332. *dnoise_dx += temp2 * x2;
  333. *dnoise_dy += temp2 * y2;
  334. *dnoise_dx *= -8.0f;
  335. *dnoise_dy *= -8.0f;
  336. *dnoise_dx += t40 * gx0 + t41 * gx1 + t42 * gx2;
  337. *dnoise_dy += t40 * gy0 + t41 * gy1 + t42 * gy2;
  338. *dnoise_dx *= 40.0f; /* Scale derivative to match the noise scaling */
  339. *dnoise_dy *= 40.0f;
  340. }
  341. return noise;
  342. }
  343.  
  344. float sdnoise2( float x, float y, float *dnoise_dx, float *dnoise_dy )
  345. {
  346. return sdnoise2( x, y, 256, 256, dnoise_dx, dnoise_dy );
  347. }
  348.  
  349. /* Skewing factors for 3D simplex grid:
  350. * F3 = 1/3
  351. * G3 = 1/6 */
  352. #define F3 0.333333333
  353. #define G3 0.166666667
  354.  
  355.  
  356. /** 3D simplex noise with derivatives.
  357. * If the last tthree arguments are not null, the analytic derivative
  358. * (the 3D gradient of the scalar noise field) is also calculated.
  359. */
  360. float sdnoise3( float x, float y, float z, int px, int py, int pz,
  361. float *dnoise_dx, float *dnoise_dy, float *dnoise_dz )
  362. {
  363. float n0, n1, n2, n3; /* Noise contributions from the four simplex corners */
  364. float noise; /* Return value */
  365. float gx0, gy0, gz0, gx1, gy1, gz1; /* Gradients at simplex corners */
  366. float gx2, gy2, gz2, gx3, gy3, gz3;
  367.  
  368. /* Skew the input space to determine which simplex cell we're in */
  369. float s = (x+y+z)*F3; /* Very nice and simple skew factor for 3D */
  370. float xs = x+s;
  371. float ys = y+s;
  372. float zs = z+s;
  373. int i = FASTFLOOR(xs);
  374. int j = FASTFLOOR(ys);
  375. int k = FASTFLOOR(zs);
  376.  
  377. float t = (float)(i+j+k)*G3;
  378. float X0 = i-t; /* Unskew the cell origin back to (x,y,z) space */
  379. float Y0 = j-t;
  380. float Z0 = k-t;
  381. float x0 = x-X0; /* The x,y,z distances from the cell origin */
  382. float y0 = y-Y0;
  383. float z0 = z-Z0;
  384.  
  385. /* For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  386. * Determine which simplex we are in. */
  387. int i1, j1, k1; /* Offsets for second corner of simplex in (i,j,k) coords */
  388. int i2, j2, k2; /* Offsets for third corner of simplex in (i,j,k) coords */
  389.  
  390. /* TODO: This code would benefit from a backport from the GLSL version! */
  391. if(x0>=y0) {
  392. if(y0>=z0)
  393. { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; } /* X Y Z order */
  394. else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; } /* X Z Y order */
  395. else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; } /* Z X Y order */
  396. }
  397. else { // x0<y0
  398. if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; } /* Z Y X order */
  399. else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; } /* Y Z X order */
  400. else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; } /* Y X Z order */
  401. }
  402.  
  403. /* A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  404. * a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  405. * a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  406. * c = 1/6. */
  407.  
  408. float x1 = x0 - i1 + G3; /* Offsets for second corner in (x,y,z) coords */
  409. float y1 = y0 - j1 + G3;
  410. float z1 = z0 - k1 + G3;
  411. float x2 = x0 - i2 + 2.0f * G3; /* Offsets for third corner in (x,y,z) coords */
  412. float y2 = y0 - j2 + 2.0f * G3;
  413. float z2 = z0 - k2 + 2.0f * G3;
  414. float x3 = x0 - 1.0f + 3.0f * G3; /* Offsets for last corner in (x,y,z) coords */
  415. float y3 = y0 - 1.0f + 3.0f * G3;
  416. float z3 = z0 - 1.0f + 3.0f * G3;
  417.  
  418. /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */
  419. int ii = i % px;
  420. int jj = j % py;
  421. int kk = k % pz;
  422.  
  423. /* Calculate the contribution from the four corners */
  424. float t0 = 0.6f - x0*x0 - y0*y0 - z0*z0;
  425. float t20, t40;
  426. if(t0 < 0.0f) n0 = t0 = t20 = t40 = gx0 = gy0 = gz0 = 0.0f;
  427. else {
  428. grad3( perm[ii + perm[jj + perm[kk]]], &gx0, &gy0, &gz0 );
  429. t20 = t0 * t0;
  430. t40 = t20 * t20;
  431. n0 = t40 * ( gx0 * x0 + gy0 * y0 + gz0 * z0 );
  432. }
  433.  
  434. float t1 = 0.6f - x1*x1 - y1*y1 - z1*z1;
  435. float t21, t41;
  436. if(t1 < 0.0f) n1 = t1 = t21 = t41 = gx1 = gy1 = gz1 = 0.0f;
  437. else {
  438. grad3( perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]], &gx1, &gy1, &gz1 );
  439. t21 = t1 * t1;
  440. t41 = t21 * t21;
  441. n1 = t41 * ( gx1 * x1 + gy1 * y1 + gz1 * z1 );
  442. }
  443.  
  444. float t2 = 0.6f - x2*x2 - y2*y2 - z2*z2;
  445. float t22, t42;
  446. if(t2 < 0.0f) n2 = t2 = t22 = t42 = gx2 = gy2 = gz2 = 0.0f;
  447. else {
  448. grad3( perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]], &gx2, &gy2, &gz2 );
  449. t22 = t2 * t2;
  450. t42 = t22 * t22;
  451. n2 = t42 * ( gx2 * x2 + gy2 * y2 + gz2 * z2 );
  452. }
  453.  
  454. float t3 = 0.6f - x3*x3 - y3*y3 - z3*z3;
  455. float t23, t43;
  456. if(t3 < 0.0f) n3 = t3 = t23 = t43 = gx3 = gy3 = gz3 = 0.0f;
  457. else {
  458. grad3( perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]], &gx3, &gy3, &gz3 );
  459. t23 = t3 * t3;
  460. t43 = t23 * t23;
  461. n3 = t43 * ( gx3 * x3 + gy3 * y3 + gz3 * z3 );
  462. }
  463.  
  464. /* Add contributions from each corner to get the final noise value.
  465. * The result is scaled to return values in the range [-1,1] */
  466. noise = 28.0f * (n0 + n1 + n2 + n3);
  467.  
  468. /* Compute derivative, if requested by supplying non-null pointers
  469. * for the last three arguments */
  470. if( ( dnoise_dx != 0 ) && ( dnoise_dy != 0 ) && ( dnoise_dz != 0 ))
  471. {
  472. /* A straight, unoptimised calculation would be like:
  473. * *dnoise_dx = -8.0f * t20 * t0 * x0 * dot(gx0, gy0, gz0, x0, y0, z0) + t40 * gx0;
  474. * *dnoise_dy = -8.0f * t20 * t0 * y0 * dot(gx0, gy0, gz0, x0, y0, z0) + t40 * gy0;
  475. * *dnoise_dz = -8.0f * t20 * t0 * z0 * dot(gx0, gy0, gz0, x0, y0, z0) + t40 * gz0;
  476. * *dnoise_dx += -8.0f * t21 * t1 * x1 * dot(gx1, gy1, gz1, x1, y1, z1) + t41 * gx1;
  477. * *dnoise_dy += -8.0f * t21 * t1 * y1 * dot(gx1, gy1, gz1, x1, y1, z1) + t41 * gy1;
  478. * *dnoise_dz += -8.0f * t21 * t1 * z1 * dot(gx1, gy1, gz1, x1, y1, z1) + t41 * gz1;
  479. * *dnoise_dx += -8.0f * t22 * t2 * x2 * dot(gx2, gy2, gz2, x2, y2, z2) + t42 * gx2;
  480. * *dnoise_dy += -8.0f * t22 * t2 * y2 * dot(gx2, gy2, gz2, x2, y2, z2) + t42 * gy2;
  481. * *dnoise_dz += -8.0f * t22 * t2 * z2 * dot(gx2, gy2, gz2, x2, y2, z2) + t42 * gz2;
  482. * *dnoise_dx += -8.0f * t23 * t3 * x3 * dot(gx3, gy3, gz3, x3, y3, z3) + t43 * gx3;
  483. * *dnoise_dy += -8.0f * t23 * t3 * y3 * dot(gx3, gy3, gz3, x3, y3, z3) + t43 * gy3;
  484. * *dnoise_dz += -8.0f * t23 * t3 * z3 * dot(gx3, gy3, gz3, x3, y3, z3) + t43 * gz3;
  485. */
  486. float temp0 = t20 * t0 * ( gx0 * x0 + gy0 * y0 + gz0 * z0 );
  487. *dnoise_dx = temp0 * x0;
  488. *dnoise_dy = temp0 * y0;
  489. *dnoise_dz = temp0 * z0;
  490. float temp1 = t21 * t1 * ( gx1 * x1 + gy1 * y1 + gz1 * z1 );
  491. *dnoise_dx += temp1 * x1;
  492. *dnoise_dy += temp1 * y1;
  493. *dnoise_dz += temp1 * z1;
  494. float temp2 = t22 * t2 * ( gx2 * x2 + gy2 * y2 + gz2 * z2 );
  495. *dnoise_dx += temp2 * x2;
  496. *dnoise_dy += temp2 * y2;
  497. *dnoise_dz += temp2 * z2;
  498. float temp3 = t23 * t3 * ( gx3 * x3 + gy3 * y3 + gz3 * z3 );
  499. *dnoise_dx += temp3 * x3;
  500. *dnoise_dy += temp3 * y3;
  501. *dnoise_dz += temp3 * z3;
  502. *dnoise_dx *= -8.0f;
  503. *dnoise_dy *= -8.0f;
  504. *dnoise_dz *= -8.0f;
  505. *dnoise_dx += t40 * gx0 + t41 * gx1 + t42 * gx2 + t43 * gx3;
  506. *dnoise_dy += t40 * gy0 + t41 * gy1 + t42 * gy2 + t43 * gy3;
  507. *dnoise_dz += t40 * gz0 + t41 * gz1 + t42 * gz2 + t43 * gz3;
  508. *dnoise_dx *= 28.0f; /* Scale derivative to match the noise scaling */
  509. *dnoise_dy *= 28.0f;
  510. *dnoise_dz *= 28.0f;
  511. }
  512. return noise;
  513. }
  514.  
  515. float sdnoise3( float x, float y, float z, float *dnoise_dx, float *dnoise_dy, float *dnoise_dz )
  516. {
  517. return sdnoise3(x, y, z, 256, 256, 256, dnoise_dx, dnoise_dy, dnoise_dz);
  518. }
  519.  
  520. // The skewing and unskewing factors are hairy again for the 4D case
  521. #define F4 0.309016994 // F4 = (Math.sqrt(5.0)-1.0)/4.0
  522. #define G4 0.138196601 // G4 = (5.0-Math.sqrt(5.0))/20.0
  523.  
  524. /** 4D simplex noise with derivatives.
  525. * If the last four arguments are not null, the analytic derivative
  526. * (the 4D gradient of the scalar noise field) is also calculated.
  527. */
  528. float sdnoise4( float x, float y, float z, float w,
  529. int px, int py, int pz, int pw,
  530. float *dnoise_dx, float *dnoise_dy,
  531. float *dnoise_dz, float *dnoise_dw)
  532. {
  533. float n0, n1, n2, n3, n4; // Noise contributions from the five corners
  534. float noise; // Return value
  535. float gx0, gy0, gz0, gw0, gx1, gy1, gz1, gw1; /* Gradients at simplex corners */
  536. float gx2, gy2, gz2, gw2, gx3, gy3, gz3, gw3, gx4, gy4, gz4, gw4;
  537. float t20, t21, t22, t23, t24;
  538. float t40, t41, t42, t43, t44;
  539.  
  540. // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
  541. float s = (x + y + z + w) * F4; // Factor for 4D skewing
  542. float xs = x + s;
  543. float ys = y + s;
  544. float zs = z + s;
  545. float ws = w + s;
  546. int i = FASTFLOOR(xs);
  547. int j = FASTFLOOR(ys);
  548. int k = FASTFLOOR(zs);
  549. int l = FASTFLOOR(ws);
  550.  
  551. float t = (i + j + k + l) * G4; // Factor for 4D unskewing
  552. float X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
  553. float Y0 = j - t;
  554. float Z0 = k - t;
  555. float W0 = l - t;
  556.  
  557. float x0 = x - X0; // The x,y,z,w distances from the cell origin
  558. float y0 = y - Y0;
  559. float z0 = z - Z0;
  560. float w0 = w - W0;
  561.  
  562. // For the 4D case, the simplex is a 4D shape I won't even try to describe.
  563. // To find out which of the 24 possible simplices we're in, we need to
  564. // determine the magnitude ordering of x0, y0, z0 and w0.
  565. // The method below is a reasonable way of finding the ordering of x,y,z,w
  566. // and then find the correct traversal order for the simplex we’re in.
  567. // First, six pair-wise comparisons are performed between each possible pair
  568. // of the four coordinates, and then the results are used to add up binary
  569. // bits for an integer index into a precomputed lookup table, simplex[].
  570. int c1 = (x0 > y0) ? 32 : 0;
  571. int c2 = (x0 > z0) ? 16 : 0;
  572. int c3 = (y0 > z0) ? 8 : 0;
  573. int c4 = (x0 > w0) ? 4 : 0;
  574. int c5 = (y0 > w0) ? 2 : 0;
  575. int c6 = (z0 > w0) ? 1 : 0;
  576. int c = c1 | c2 | c3 | c4 | c5 | c6; // '|' is mostly faster than '+'
  577.  
  578. int i1, j1, k1, l1; // The integer offsets for the second simplex corner
  579. int i2, j2, k2, l2; // The integer offsets for the third simplex corner
  580. int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
  581.  
  582. // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
  583. // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
  584. // impossible. Only the 24 indices which have non-zero entries make any sense.
  585. // We use a thresholding to set the coordinates in turn from the largest magnitude.
  586. // The number 3 in the "simplex" array is at the position of the largest coordinate.
  587. i1 = simplex[c][0]>=3 ? 1 : 0;
  588. j1 = simplex[c][1]>=3 ? 1 : 0;
  589. k1 = simplex[c][2]>=3 ? 1 : 0;
  590. l1 = simplex[c][3]>=3 ? 1 : 0;
  591. // The number 2 in the "simplex" array is at the second largest coordinate.
  592. i2 = simplex[c][0]>=2 ? 1 : 0;
  593. j2 = simplex[c][1]>=2 ? 1 : 0;
  594. k2 = simplex[c][2]>=2 ? 1 : 0;
  595. l2 = simplex[c][3]>=2 ? 1 : 0;
  596. // The number 1 in the "simplex" array is at the second smallest coordinate.
  597. i3 = simplex[c][0]>=1 ? 1 : 0;
  598. j3 = simplex[c][1]>=1 ? 1 : 0;
  599. k3 = simplex[c][2]>=1 ? 1 : 0;
  600. l3 = simplex[c][3]>=1 ? 1 : 0;
  601. // The fifth corner has all coordinate offsets = 1, so no need to look that up.
  602.  
  603. float x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
  604. float y1 = y0 - j1 + G4;
  605. float z1 = z0 - k1 + G4;
  606. float w1 = w0 - l1 + G4;
  607. float x2 = x0 - i2 + 2.0f * G4; // Offsets for third corner in (x,y,z,w) coords
  608. float y2 = y0 - j2 + 2.0f * G4;
  609. float z2 = z0 - k2 + 2.0f * G4;
  610. float w2 = w0 - l2 + 2.0f * G4;
  611. float x3 = x0 - i3 + 3.0f * G4; // Offsets for fourth corner in (x,y,z,w) coords
  612. float y3 = y0 - j3 + 3.0f * G4;
  613. float z3 = z0 - k3 + 3.0f * G4;
  614. float w3 = w0 - l3 + 3.0f * G4;
  615. float x4 = x0 - 1.0f + 4.0f * G4; // Offsets for last corner in (x,y,z,w) coords
  616. float y4 = y0 - 1.0f + 4.0f * G4;
  617. float z4 = z0 - 1.0f + 4.0f * G4;
  618. float w4 = w0 - 1.0f + 4.0f * G4;
  619.  
  620. // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
  621. int ii = i % px;
  622. int jj = j % py;
  623. int kk = k % pz;
  624. int ll = l % pw;
  625.  
  626. // Calculate the contribution from the five corners
  627. float t0 = 0.6f - x0*x0 - y0*y0 - z0*z0 - w0*w0;
  628. if(t0 < 0.0f) n0 = t0 = t20 = t40 = gx0 = gy0 = gz0 = gw0 = 0.0f;
  629. else {
  630. t20 = t0 * t0;
  631. t40 = t20 * t20;
  632. grad4(perm[ii+perm[jj+perm[kk+perm[ll]]]], &gx0, &gy0, &gz0, &gw0);
  633. n0 = t40 * ( gx0 * x0 + gy0 * y0 + gz0 * z0 + gw0 * w0 );
  634. }
  635.  
  636. float t1 = 0.6f - x1*x1 - y1*y1 - z1*z1 - w1*w1;
  637. if(t1 < 0.0f) n1 = t1 = t21 = t41 = gx1 = gy1 = gz1 = gw1 = 0.0f;
  638. else {
  639. t21 = t1 * t1;
  640. t41 = t21 * t21;
  641. grad4(perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]], &gx1, &gy1, &gz1, &gw1);
  642. n1 = t41 * ( gx1 * x1 + gy1 * y1 + gz1 * z1 + gw1 * w1 );
  643. }
  644.  
  645. float t2 = 0.6f - x2*x2 - y2*y2 - z2*z2 - w2*w2;
  646. if(t2 < 0.0f) n2 = t2 = t22 = t42 = gx2 = gy2 = gz2 = gw2 = 0.0f;
  647. else {
  648. t22 = t2 * t2;
  649. t42 = t22 * t22;
  650. grad4(perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]], &gx2, &gy2, &gz2, &gw2);
  651. n2 = t42 * ( gx2 * x2 + gy2 * y2 + gz2 * z2 + gw2 * w2 );
  652. }
  653.  
  654. float t3 = 0.6f - x3*x3 - y3*y3 - z3*z3 - w3*w3;
  655. if(t3 < 0.0f) n3 = t3 = t23 = t43 = gx3 = gy3 = gz3 = gw3 = 0.0f;
  656. else {
  657. t23 = t3 * t3;
  658. t43 = t23 * t23;
  659. grad4(perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]], &gx3, &gy3, &gz3, &gw3);
  660. n3 = t43 * ( gx3 * x3 + gy3 * y3 + gz3 * z3 + gw3 * w3 );
  661. }
  662.  
  663. float t4 = 0.6f - x4*x4 - y4*y4 - z4*z4 - w4*w4;
  664. if(t4 < 0.0f) n4 = t4 = t24 = t44 = gx4 = gy4 = gz4 = gw4 = 0.0f;
  665. else {
  666. t24 = t4 * t4;
  667. t44 = t24 * t24;
  668. grad4(perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]], &gx4, &gy4, &gz4, &gw4);
  669. n4 = t44 * ( gx4 * x4 + gy4 * y4 + gz4 * z4 + gw4 * w4 );
  670. }
  671.  
  672. // Sum up and scale the result to cover the range [-1,1]
  673. noise = 27.0f * (n0 + n1 + n2 + n3 + n4); // TODO: The scale factor is preliminary!
  674.  
  675. /* Compute derivative, if requested by supplying non-null pointers
  676. * for the last four arguments */
  677. if( ( dnoise_dx != 0 ) && ( dnoise_dy != 0 ) && ( dnoise_dz != 0 ) && ( dnoise_dw != 0 ) )
  678. {
  679. /* A straight, unoptimised calculation would be like:
  680. * *dnoise_dx = -8.0f * t20 * t0 * x0 * dot(gx0, gy0, gz0, gw0, x0, y0, z0, w0) + t40 * gx0;
  681. * *dnoise_dy = -8.0f * t20 * t0 * y0 * dot(gx0, gy0, gz0, gw0, x0, y0, z0, w0) + t40 * gy0;
  682. * *dnoise_dz = -8.0f * t20 * t0 * z0 * dot(gx0, gy0, gz0, gw0, x0, y0, z0, w0) + t40 * gz0;
  683. * *dnoise_dw = -8.0f * t20 * t0 * w0 * dot(gx0, gy0, gz0, gw0, x0, y0, z0, w0) + t40 * gw0;
  684. * *dnoise_dx += -8.0f * t21 * t1 * x1 * dot(gx1, gy1, gz1, gw1, x1, y1, z1, w1) + t41 * gx1;
  685. * *dnoise_dy += -8.0f * t21 * t1 * y1 * dot(gx1, gy1, gz1, gw1, x1, y1, z1, w1) + t41 * gy1;
  686. * *dnoise_dz += -8.0f * t21 * t1 * z1 * dot(gx1, gy1, gz1, gw1, x1, y1, z1, w1) + t41 * gz1;
  687. * *dnoise_dw = -8.0f * t21 * t1 * w1 * dot(gx1, gy1, gz1, gw1, x1, y1, z1, w1) + t41 * gw1;
  688. * *dnoise_dx += -8.0f * t22 * t2 * x2 * dot(gx2, gy2, gz2, gw2, x2, y2, z2, w2) + t42 * gx2;
  689. * *dnoise_dy += -8.0f * t22 * t2 * y2 * dot(gx2, gy2, gz2, gw2, x2, y2, z2, w2) + t42 * gy2;
  690. * *dnoise_dz += -8.0f * t22 * t2 * z2 * dot(gx2, gy2, gz2, gw2, x2, y2, z2, w2) + t42 * gz2;
  691. * *dnoise_dw += -8.0f * t22 * t2 * w2 * dot(gx2, gy2, gz2, gw2, x2, y2, z2, w2) + t42 * gw2;
  692. * *dnoise_dx += -8.0f * t23 * t3 * x3 * dot(gx3, gy3, gz3, gw3, x3, y3, z3, w3) + t43 * gx3;
  693. * *dnoise_dy += -8.0f * t23 * t3 * y3 * dot(gx3, gy3, gz3, gw3, x3, y3, z3, w3) + t43 * gy3;
  694. * *dnoise_dz += -8.0f * t23 * t3 * z3 * dot(gx3, gy3, gz3, gw3, x3, y3, z3, w3) + t43 * gz3;
  695. * *dnoise_dw += -8.0f * t23 * t3 * w3 * dot(gx3, gy3, gz3, gw3, x3, y3, z3, w3) + t43 * gw3;
  696. * *dnoise_dx += -8.0f * t24 * t4 * x4 * dot(gx4, gy4, gz4, gw4, x4, y4, z4, w4) + t44 * gx4;
  697. * *dnoise_dy += -8.0f * t24 * t4 * y4 * dot(gx4, gy4, gz4, gw4, x4, y4, z4, w4) + t44 * gy4;
  698. * *dnoise_dz += -8.0f * t24 * t4 * z4 * dot(gx4, gy4, gz4, gw4, x4, y4, z4, w4) + t44 * gz4;
  699. * *dnoise_dw += -8.0f * t24 * t4 * w4 * dot(gx4, gy4, gz4, gw4, x4, y4, z4, w4) + t44 * gw4;
  700. */
  701. float temp0 = t20 * t0 * ( gx0 * x0 + gy0 * y0 + gz0 * z0 + gw0 * w0 );
  702. *dnoise_dx = temp0 * x0;
  703. *dnoise_dy = temp0 * y0;
  704. *dnoise_dz = temp0 * z0;
  705. *dnoise_dw = temp0 * w0;
  706. float temp1 = t21 * t1 * ( gx1 * x1 + gy1 * y1 + gz1 * z1 + gw1 * w1 );
  707. *dnoise_dx += temp1 * x1;
  708. *dnoise_dy += temp1 * y1;
  709. *dnoise_dz += temp1 * z1;
  710. *dnoise_dw += temp1 * w1;
  711. float temp2 = t22 * t2 * ( gx2 * x2 + gy2 * y2 + gz2 * z2 + gw2 * w2 );
  712. *dnoise_dx += temp2 * x2;
  713. *dnoise_dy += temp2 * y2;
  714. *dnoise_dz += temp2 * z2;
  715. *dnoise_dw += temp2 * w2;
  716. float temp3 = t23 * t3 * ( gx3 * x3 + gy3 * y3 + gz3 * z3 + gw3 * w3 );
  717. *dnoise_dx += temp3 * x3;
  718. *dnoise_dy += temp3 * y3;
  719. *dnoise_dz += temp3 * z3;
  720. *dnoise_dw += temp3 * w3;
  721. float temp4 = t24 * t4 * ( gx4 * x4 + gy4 * y4 + gz4 * z4 + gw4 * w4 );
  722. *dnoise_dx += temp4 * x4;
  723. *dnoise_dy += temp4 * y4;
  724. *dnoise_dz += temp4 * z4;
  725. *dnoise_dw += temp4 * w4;
  726. *dnoise_dx *= -8.0f;
  727. *dnoise_dy *= -8.0f;
  728. *dnoise_dz *= -8.0f;
  729. *dnoise_dw *= -8.0f;
  730. *dnoise_dx += t40 * gx0 + t41 * gx1 + t42 * gx2 + t43 * gx3 + t44 * gx4;
  731. *dnoise_dy += t40 * gy0 + t41 * gy1 + t42 * gy2 + t43 * gy3 + t44 * gy4;
  732. *dnoise_dz += t40 * gz0 + t41 * gz1 + t42 * gz2 + t43 * gz3 + t44 * gz4;
  733. *dnoise_dw += t40 * gw0 + t41 * gw1 + t42 * gw2 + t43 * gw3 + t44 * gw4;
  734.  
  735. *dnoise_dx *= 28.0f; /* Scale derivative to match the noise scaling */
  736. *dnoise_dy *= 28.0f;
  737. *dnoise_dz *= 28.0f;
  738. *dnoise_dw *= 28.0f;
  739. }
  740.  
  741. return noise;
  742. }
  743.  
  744. float sdnoise4( float x, float y, float z, float w,
  745. float *dnoise_dx, float *dnoise_dy,
  746. float *dnoise_dz, float *dnoise_dw)
  747. return sdnoise4( x, y, z, w,
  748. 256, 256, 256, 256,
  749. *dnoise_dx, *dnoise_dy,
  750. *dnoise_dz, *dnoise_dw);
  751. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement