Noise2.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. //using System.Collections;
  7. using System;
  8. // =================================
  9. // Define namespace.
  10. // =================================
  11. namespace MirzaBeig
  12. {
  13. namespace Scripting
  14. {
  15. namespace Effects
  16. {
  17. // =================================
  18. // Classes.
  19. // =================================
  20. //[ExecuteInEditMode]
  21. [System.Serializable]
  22. public static class Noise2
  23. {
  24. // =================================
  25. // Nested classes and structures.
  26. // =================================
  27. // ...
  28. // =================================
  29. // Variables.
  30. // =================================
  31. // ...
  32. // These variables are for simplex noise ->
  33. // based on Stefan Gustavson's C/C++ implementation.
  34. // Simple skewing factors for the 3D case.
  35. // Used for simplex noise.
  36. static float F3 = 0.333333333f;
  37. static float G3 = 0.166666667f;
  38. // =================================
  39. // Functions.
  40. // =================================
  41. // ...
  42. // Return -1.0 -> 1.0.
  43. static float smooth(float t)
  44. {
  45. return t * t * (3.0f - 2.0f * t);
  46. }
  47. static float fade(float t)
  48. {
  49. return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
  50. }
  51. static int floor(float x)
  52. {
  53. return x > 0 ? (int)x : (int)x - 1;
  54. }
  55. static float lerp(float from, float to, float t)
  56. {
  57. return from + (t * (to - from));
  58. }
  59. // Mathf is usually just System.Math with a float cast.
  60. // Saving the extra function call and casting manually
  61. // has a noticeable (good) impact on performance and FPS.
  62. // Returns a pseudo-random value based on the input seed (x).
  63. //static float hash(float x)
  64. //{
  65. // float sine = (float)(Math.Sin(x) * 43758.5453);
  66. // float fractionalSine = sine - floor(sine);
  67. // return fractionalSine;
  68. //}
  69. // ...
  70. static byte[] perm =
  71. {
  72. 151,160,137,91,90,15,
  73. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  74. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  75. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  76. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  77. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  78. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  79. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  80. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  81. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  82. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  83. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  84. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
  85. 151,160,137,91,90,15,
  86. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  87. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  88. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  89. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  90. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  91. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  92. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  93. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  94. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  95. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  96. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  97. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
  98. };
  99. // Instead of multiplying by gradient and
  100. // getting dot product, get the final value directly.
  101. static float grad(int hash, float x, float y, float z)
  102. {
  103. switch (hash & 0xF)
  104. {
  105. case 0x0: return x + y;
  106. case 0x1: return -x + y;
  107. case 0x2: return x - y;
  108. case 0x3: return -x - y;
  109. case 0x4: return x + x;
  110. case 0x5: return -x + x;
  111. case 0x6: return x - x;
  112. case 0x7: return -x - x;
  113. case 0x8: return y + x;
  114. case 0x9: return -y + x;
  115. case 0xA: return y - x;
  116. case 0xB: return -y - x;
  117. case 0xC: return y + z;
  118. case 0xD: return -y + x;
  119. case 0xE: return y - x;
  120. case 0xF: return -y - z;
  121. // Not executed.
  122. default: return 0.0f;
  123. }
  124. }
  125. // ...
  126. public static float perlin(float x, float y, float z)
  127. {
  128. // Integer part (floor).
  129. int ix0 = ((x) > 0) ? ((int)x) : ((int)x - 1);
  130. int iy0 = ((y) > 0) ? ((int)y) : ((int)y - 1);
  131. int iz0 = ((z) > 0) ? ((int)z) : ((int)z - 1);
  132. // Fractional part (v - floor).
  133. float fx0 = x - ix0;
  134. float fy0 = y - iy0;
  135. float fz0 = z - iz0;
  136. // Fractional part minus one.
  137. float fx1 = fx0 - 1.0f;
  138. float fy1 = fy0 - 1.0f;
  139. float fz1 = fz0 - 1.0f;
  140. // Wrap to 0...255.
  141. int ix1 = (ix0 + 1) & 255;
  142. int iy1 = (iy0 + 1) & 255;
  143. int iz1 = (iz0 + 1) & 255;
  144. ix0 &= 255;
  145. iy0 &= 255;
  146. iz0 &= 255;
  147. // Smooth / fade.
  148. float r = fz0 * fz0 * fz0 * (fz0 * (fz0 * 6.0f - 15.0f) + 10.0f);
  149. float t = fy0 * fy0 * fy0 * (fy0 * (fy0 * 6.0f - 15.0f) + 10.0f);
  150. float s = fx0 * fx0 * fx0 * (fx0 * (fx0 * 6.0f - 15.0f) + 10.0f);
  151. // Gradients.
  152. int hash;
  153. float gradient;
  154. float nxy0, nxy1;
  155. float nx0, nx1;
  156. float n0, n1;
  157. // --- 1
  158. hash = perm[ix0 + perm[iy0 + perm[iz0]]];
  159. switch (hash & 0xF)
  160. {
  161. case 0x0: gradient = fx0 + fy0; break;
  162. case 0x1: gradient = -fx0 + fy0; break;
  163. case 0x2: gradient = fx0 - fy0; break;
  164. case 0x3: gradient = -fx0 - fy0; break;
  165. case 0x4: gradient = fx0 + fx0; break;
  166. case 0x5: gradient = -fx0 + fx0; break;
  167. case 0x6: gradient = fx0 - fx0; break;
  168. case 0x7: gradient = -fx0 - fx0; break;
  169. case 0x8: gradient = fy0 + fx0; break;
  170. case 0x9: gradient = -fy0 + fx0; break;
  171. case 0xA: gradient = fy0 - fx0; break;
  172. case 0xB: gradient = -fy0 - fx0; break;
  173. case 0xC: gradient = fy0 + fz0; break;
  174. case 0xD: gradient = -fy0 + fx0; break;
  175. case 0xE: gradient = fy0 - fx0; break;
  176. case 0xF: gradient = -fy0 - fz0; break;
  177. default: gradient = 0.0f; break;
  178. }
  179. nxy0 = gradient;
  180. // --- 2
  181. hash = perm[ix0 + perm[iy0 + perm[iz1]]];
  182. switch (hash & 0xF)
  183. {
  184. case 0x0: gradient = fx0 + fy0; break;
  185. case 0x1: gradient = -fx0 + fy0; break;
  186. case 0x2: gradient = fx0 - fy0; break;
  187. case 0x3: gradient = -fx0 - fy0; break;
  188. case 0x4: gradient = fx0 + fx0; break;
  189. case 0x5: gradient = -fx0 + fx0; break;
  190. case 0x6: gradient = fx0 - fx0; break;
  191. case 0x7: gradient = -fx0 - fx0; break;
  192. case 0x8: gradient = fy0 + fx0; break;
  193. case 0x9: gradient = -fy0 + fx0; break;
  194. case 0xA: gradient = fy0 - fx0; break;
  195. case 0xB: gradient = -fy0 - fx0; break;
  196. case 0xC: gradient = fy0 + fz1; break;
  197. case 0xD: gradient = -fy0 + fx0; break;
  198. case 0xE: gradient = fy0 - fx0; break;
  199. case 0xF: gradient = -fy0 - fz1; break;
  200. default: gradient = 0.0f; break;
  201. }
  202. nxy1 = gradient;
  203. // ---
  204. nx0 = nxy0 + (r * (nxy1 - nxy0));
  205. // --- 3
  206. hash = perm[ix0 + perm[iy1 + perm[iz0]]];
  207. switch (hash & 0xF)
  208. {
  209. case 0x0: gradient = fx0 + fy1; break;
  210. case 0x1: gradient = -fx0 + fy1; break;
  211. case 0x2: gradient = fx0 - fy1; break;
  212. case 0x3: gradient = -fx0 - fy1; break;
  213. case 0x4: gradient = fx0 + fx0; break;
  214. case 0x5: gradient = -fx0 + fx0; break;
  215. case 0x6: gradient = fx0 - fx0; break;
  216. case 0x7: gradient = -fx0 - fx0; break;
  217. case 0x8: gradient = fy1 + fx0; break;
  218. case 0x9: gradient = -fy1 + fx0; break;
  219. case 0xA: gradient = fy1 - fx0; break;
  220. case 0xB: gradient = -fy1 - fx0; break;
  221. case 0xC: gradient = fy1 + fz0; break;
  222. case 0xD: gradient = -fy1 + fx0; break;
  223. case 0xE: gradient = fy1 - fx0; break;
  224. case 0xF: gradient = -fy1 - fz0; break;
  225. default: gradient = 0.0f; break;
  226. }
  227. nxy0 = gradient;
  228. // --- 4
  229. hash = perm[ix0 + perm[iy1 + perm[iz1]]];
  230. switch (hash & 0xF)
  231. {
  232. case 0x0: gradient = fx0 + fy1; break;
  233. case 0x1: gradient = -fx0 + fy1; break;
  234. case 0x2: gradient = fx0 - fy1; break;
  235. case 0x3: gradient = -fx0 - fy1; break;
  236. case 0x4: gradient = fx0 + fx0; break;
  237. case 0x5: gradient = -fx0 + fx0; break;
  238. case 0x6: gradient = fx0 - fx0; break;
  239. case 0x7: gradient = -fx0 - fx0; break;
  240. case 0x8: gradient = fy1 + fx0; break;
  241. case 0x9: gradient = -fy1 + fx0; break;
  242. case 0xA: gradient = fy1 - fx0; break;
  243. case 0xB: gradient = -fy1 - fx0; break;
  244. case 0xC: gradient = fy1 + fz1; break;
  245. case 0xD: gradient = -fy1 + fx0; break;
  246. case 0xE: gradient = fy1 - fx0; break;
  247. case 0xF: gradient = -fy1 - fz1; break;
  248. default: gradient = 0.0f; break;
  249. }
  250. nxy1 = gradient;
  251. // ---
  252. nx1 = nxy0 + (r * (nxy1 - nxy0));
  253. // ---
  254. n0 = nx0 + (t * (nx1 - nx0));
  255. // --- 5
  256. hash = perm[ix1 + perm[iy0 + perm[iz0]]];
  257. switch (hash & 0xF)
  258. {
  259. case 0x0: gradient = fx1 + fy0; break;
  260. case 0x1: gradient = -fx1 + fy0; break;
  261. case 0x2: gradient = fx1 - fy0; break;
  262. case 0x3: gradient = -fx1 - fy0; break;
  263. case 0x4: gradient = fx1 + fx1; break;
  264. case 0x5: gradient = -fx1 + fx1; break;
  265. case 0x6: gradient = fx1 - fx1; break;
  266. case 0x7: gradient = -fx1 - fx1; break;
  267. case 0x8: gradient = fy0 + fx1; break;
  268. case 0x9: gradient = -fy0 + fx1; break;
  269. case 0xA: gradient = fy0 - fx1; break;
  270. case 0xB: gradient = -fy0 - fx1; break;
  271. case 0xC: gradient = fy0 + fz0; break;
  272. case 0xD: gradient = -fy0 + fx1; break;
  273. case 0xE: gradient = fy0 - fx1; break;
  274. case 0xF: gradient = -fy0 - fz0; break;
  275. default: gradient = 0.0f; break;
  276. }
  277. nxy0 = gradient;
  278. // --- 6
  279. hash = perm[ix1 + perm[iy0 + perm[iz1]]];
  280. switch (hash & 0xF)
  281. {
  282. case 0x0: gradient = fx1 + fy0; break;
  283. case 0x1: gradient = -fx1 + fy0; break;
  284. case 0x2: gradient = fx1 - fy0; break;
  285. case 0x3: gradient = -fx1 - fy0; break;
  286. case 0x4: gradient = fx1 + fx1; break;
  287. case 0x5: gradient = -fx1 + fx1; break;
  288. case 0x6: gradient = fx1 - fx1; break;
  289. case 0x7: gradient = -fx1 - fx1; break;
  290. case 0x8: gradient = fy0 + fx1; break;
  291. case 0x9: gradient = -fy0 + fx1; break;
  292. case 0xA: gradient = fy0 - fx1; break;
  293. case 0xB: gradient = -fy0 - fx1; break;
  294. case 0xC: gradient = fy0 + fz1; break;
  295. case 0xD: gradient = -fy0 + fx1; break;
  296. case 0xE: gradient = fy0 - fx1; break;
  297. case 0xF: gradient = -fy0 - fz1; break;
  298. default: gradient = 0.0f; break;
  299. }
  300. nxy1 = gradient;
  301. // --- 7
  302. nx0 = nxy0 + (r * (nxy1 - nxy0));
  303. // ---
  304. hash = perm[ix1 + perm[iy1 + perm[iz0]]];
  305. switch (hash & 0xF)
  306. {
  307. case 0x0: gradient = fx1 + fy1; break;
  308. case 0x1: gradient = -fx1 + fy1; break;
  309. case 0x2: gradient = fx1 - fy1; break;
  310. case 0x3: gradient = -fx1 - fy1; break;
  311. case 0x4: gradient = fx1 + fx1; break;
  312. case 0x5: gradient = -fx1 + fx1; break;
  313. case 0x6: gradient = fx1 - fx1; break;
  314. case 0x7: gradient = -fx1 - fx1; break;
  315. case 0x8: gradient = fy1 + fx1; break;
  316. case 0x9: gradient = -fy1 + fx1; break;
  317. case 0xA: gradient = fy1 - fx1; break;
  318. case 0xB: gradient = -fy1 - fx1; break;
  319. case 0xC: gradient = fy1 + fz0; break;
  320. case 0xD: gradient = -fy1 + fx1; break;
  321. case 0xE: gradient = fy1 - fx1; break;
  322. case 0xF: gradient = -fy1 - fz0; break;
  323. default: gradient = 0.0f; break;
  324. }
  325. nxy0 = gradient;
  326. // --- 8
  327. hash = perm[ix1 + perm[iy1 + perm[iz1]]];
  328. switch (hash & 0xF)
  329. {
  330. case 0x0: gradient = fx1 + fy1; break;
  331. case 0x1: gradient = -fx1 + fy1; break;
  332. case 0x2: gradient = fx1 - fy1; break;
  333. case 0x3: gradient = -fx1 - fy1; break;
  334. case 0x4: gradient = fx1 + fx1; break;
  335. case 0x5: gradient = -fx1 + fx1; break;
  336. case 0x6: gradient = fx1 - fx1; break;
  337. case 0x7: gradient = -fx1 - fx1; break;
  338. case 0x8: gradient = fy1 + fx1; break;
  339. case 0x9: gradient = -fy1 + fx1; break;
  340. case 0xA: gradient = fy1 - fx1; break;
  341. case 0xB: gradient = -fy1 - fx1; break;
  342. case 0xC: gradient = fy1 + fz1; break;
  343. case 0xD: gradient = -fy1 + fx1; break;
  344. case 0xE: gradient = fy1 - fx1; break;
  345. case 0xF: gradient = -fy1 - fz1; break;
  346. default: gradient = 0.0f; break;
  347. }
  348. nxy1 = gradient;
  349. // ---
  350. nx1 = nxy0 + (r * (nxy1 - nxy0));
  351. // ---
  352. n1 = nx0 + (t * (nx1 - nx0));
  353. // ---
  354. return 0.936f * (n0 + (s * (n1 - n0)));
  355. }
  356. // Based on Stefan Gustavson's C/C++ implementation.
  357. public static float simplex(float x, float y, float z)
  358. {
  359. float n0, n1, n2, n3; // Noise contributions from the four corners
  360. // Skew the input space to determine which simplex cell we're in.
  361. float s = (x + y + z) * F3; // Very nice and simple skew factor for 3D.
  362. float xs = x + s;
  363. float ys = y + s;
  364. float zs = z + s;
  365. int i = xs > 0 ? (int)xs : (int)xs - 1;
  366. int j = ys > 0 ? (int)ys : (int)ys - 1;
  367. int k = zs > 0 ? (int)zs : (int)zs - 1;
  368. float t = (i + j + k) * G3;
  369. float X0 = i - t; // Unskew the cell origin back to (x, y, z) space
  370. float Y0 = j - t;
  371. float Z0 = k - t;
  372. float x0 = x - X0; // The x, y, z distances from the cell origin.
  373. float y0 = y - Y0;
  374. float z0 = z - Z0;
  375. // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  376. // Determine which simplex we are in.
  377. int i1, j1, k1; // Offsets for second corner of simplex in (i, j, k) coords.
  378. int i2, j2, k2; // Offsets for third corner of simplex in (i, j, k) coords.
  379. /* This code would benefit from a backport from the GLSL version! */
  380. if (x0 >= y0)
  381. {
  382. if (y0 >= z0) // X Y Z order
  383. {
  384. i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0;
  385. }
  386. else if (x0 >= z0) // X Z Y order
  387. {
  388. i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1;
  389. }
  390. else // Z X Y order
  391. {
  392. i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1;
  393. }
  394. }
  395. else
  396. {
  397. // x0 < y0.
  398. if (y0 < z0) // Z Y X order.
  399. {
  400. i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1;
  401. }
  402. else if (x0 < z0) // Y Z X order.
  403. {
  404. i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1;
  405. }
  406. else // Y X Z order.
  407. {
  408. i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0;
  409. }
  410. }
  411. // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  412. // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  413. // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  414. // c = 1/6.
  415. float x1 = x0 - i1 + G3; // Offsets for second corner in (x, y, z) coords.
  416. float y1 = y0 - j1 + G3;
  417. float z1 = z0 - k1 + G3;
  418. float x2 = x0 - i2 + 2.0f * G3; // Offsets for third corner in (x, y, z) coords.
  419. float y2 = y0 - j2 + 2.0f * G3;
  420. float z2 = z0 - k2 + 2.0f * G3;
  421. float x3 = x0 - 1.0f + 3.0f * G3; // Offsets for last corner in (x, y, z) coords.
  422. float y3 = y0 - 1.0f + 3.0f * G3;
  423. float z3 = z0 - 1.0f + 3.0f * G3;
  424. // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds.
  425. int ii = i & 0xff;
  426. int jj = j & 0xff;
  427. int kk = k & 0xff;
  428. // Calculate the contribution from the four corners.
  429. float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0;
  430. // Gradients.
  431. //int h;
  432. int hash;
  433. float gradient;
  434. if (t0 < 0.0f)
  435. {
  436. n0 = 0.0f;
  437. }
  438. else
  439. {
  440. t0 *= t0;
  441. // --- 1
  442. hash = perm[ii + perm[jj + perm[kk]]];
  443. switch (hash & 0xF)
  444. {
  445. case 0x0: gradient = x0 + y0; break;
  446. case 0x1: gradient = -x0 + y0; break;
  447. case 0x2: gradient = x0 - y0; break;
  448. case 0x3: gradient = -x0 - y0; break;
  449. case 0x4: gradient = x0 + x0; break;
  450. case 0x5: gradient = -x0 + x0; break;
  451. case 0x6: gradient = x0 - x0; break;
  452. case 0x7: gradient = -x0 - x0; break;
  453. case 0x8: gradient = y0 + x0; break;
  454. case 0x9: gradient = -y0 + x0; break;
  455. case 0xA: gradient = y0 - x0; break;
  456. case 0xB: gradient = -y0 - x0; break;
  457. case 0xC: gradient = y0 + z0; break;
  458. case 0xD: gradient = -y0 + x0; break;
  459. case 0xE: gradient = y0 - x0; break;
  460. case 0xF: gradient = -y0 - z0; break;
  461. default: gradient = 0.0f; break;
  462. }
  463. n0 = t0 * t0 * gradient;
  464. }
  465. float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1;
  466. if (t1 < 0.0f)
  467. {
  468. n1 = 0.0f;
  469. }
  470. else
  471. {
  472. t1 *= t1;
  473. hash = perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]];
  474. switch (hash & 0xF)
  475. {
  476. case 0x0: gradient = x1 + y1; break;
  477. case 0x1: gradient = -x1 + y1; break;
  478. case 0x2: gradient = x1 - y1; break;
  479. case 0x3: gradient = -x1 - y1; break;
  480. case 0x4: gradient = x1 + x1; break;
  481. case 0x5: gradient = -x1 + x1; break;
  482. case 0x6: gradient = x1 - x1; break;
  483. case 0x7: gradient = -x1 - x1; break;
  484. case 0x8: gradient = y1 + x1; break;
  485. case 0x9: gradient = -y1 + x1; break;
  486. case 0xA: gradient = y1 - x1; break;
  487. case 0xB: gradient = -y1 - x1; break;
  488. case 0xC: gradient = y1 + z1; break;
  489. case 0xD: gradient = -y1 + x1; break;
  490. case 0xE: gradient = y1 - x1; break;
  491. case 0xF: gradient = -y1 - z1; break;
  492. default: gradient = 0.0f; break;
  493. }
  494. n1 = t1 * t1 * gradient;
  495. }
  496. float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2;
  497. if (t2 < 0.0f)
  498. {
  499. n2 = 0.0f;
  500. }
  501. else
  502. {
  503. t2 *= t2;
  504. hash = perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]];
  505. switch (hash & 0xF)
  506. {
  507. case 0x0: gradient = x2 + y2; break;
  508. case 0x1: gradient = -x2 + y2; break;
  509. case 0x2: gradient = x2 - y2; break;
  510. case 0x3: gradient = -x2 - y2; break;
  511. case 0x4: gradient = x2 + x2; break;
  512. case 0x5: gradient = -x2 + x2; break;
  513. case 0x6: gradient = x2 - x2; break;
  514. case 0x7: gradient = -x2 - x2; break;
  515. case 0x8: gradient = y2 + x2; break;
  516. case 0x9: gradient = -y2 + x2; break;
  517. case 0xA: gradient = y2 - x2; break;
  518. case 0xB: gradient = -y2 - x2; break;
  519. case 0xC: gradient = y2 + z2; break;
  520. case 0xD: gradient = -y2 + x2; break;
  521. case 0xE: gradient = y2 - x2; break;
  522. case 0xF: gradient = -y2 - z2; break;
  523. default: gradient = 0.0f; break;
  524. }
  525. n2 = t2 * t2 * gradient;
  526. }
  527. float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3;
  528. if (t3 < 0.0f)
  529. {
  530. n3 = 0.0f;
  531. }
  532. else
  533. {
  534. t3 *= t3;
  535. hash = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]];
  536. switch (hash & 0xF)
  537. {
  538. case 0x0: gradient = x3 + y3; break;
  539. case 0x1: gradient = -x3 + y3; break;
  540. case 0x2: gradient = x3 - y3; break;
  541. case 0x3: gradient = -x3 - y3; break;
  542. case 0x4: gradient = x3 + x3; break;
  543. case 0x5: gradient = -x3 + x3; break;
  544. case 0x6: gradient = x3 - x3; break;
  545. case 0x7: gradient = -x3 - x3; break;
  546. case 0x8: gradient = y3 + x3; break;
  547. case 0x9: gradient = -y3 + x3; break;
  548. case 0xA: gradient = y3 - x3; break;
  549. case 0xB: gradient = -y3 - x3; break;
  550. case 0xC: gradient = y3 + z3; break;
  551. case 0xD: gradient = -y3 + x3; break;
  552. case 0xE: gradient = y3 - x3; break;
  553. case 0xF: gradient = -y3 - z3; break;
  554. default: gradient = 0.0f; break;
  555. }
  556. n3 = t3 * t3 * gradient;
  557. }
  558. // Add contributions from each corner to get the final noise value.
  559. // The result is scaled to stay just inside [-1, 1].
  560. return 32.0f * (n0 + n1 + n2 + n3);
  561. }
  562. // ...
  563. public static float octavePerlin(float x, float y, float z, float frequency, int octaves, float lacunarity, float persistence)
  564. {
  565. // 0 and 1 will do nothing.
  566. if (octaves < 2)
  567. {
  568. return perlin(x * frequency, y * frequency, z * frequency);
  569. }
  570. else
  571. {
  572. float total = 0.0f;
  573. float amplitude = 1.0f;
  574. float max = 0.0f;
  575. for (int i = 0; i < octaves; i++)
  576. {
  577. total += perlin(x * frequency, y * frequency, z * frequency) * amplitude;
  578. max += amplitude;
  579. frequency *= lacunarity;
  580. amplitude *= persistence;
  581. }
  582. return total / max;
  583. }
  584. }
  585. // ...
  586. public static float octaveSimplex(float x, float y, float z, float frequency, int octaves, float lacunarity, float persistence)
  587. {
  588. // 0 and 1 will do nothing.
  589. if (octaves < 2)
  590. {
  591. return simplex(x * frequency, y * frequency, z * frequency);
  592. }
  593. else
  594. {
  595. float total = 0.0f;
  596. float amplitude = 1.0f;
  597. float max = 0.0f;
  598. for (int i = 0; i < octaves; i++)
  599. {
  600. total += simplex(x * frequency, y * frequency, z * frequency) * amplitude;
  601. max += amplitude;
  602. frequency *= lacunarity;
  603. amplitude *= persistence;
  604. }
  605. return total / max;
  606. }
  607. }
  608. // Based on Stefan Gustavson's C/C++ implementation.
  609. // Easier to understand, but not optimized (function calls aren't rolled out).
  610. public static float perlinUnoptimized(float x, float y, float z)
  611. {
  612. int ix0, iy0, iz0;
  613. int ix1, iy1, iz1;
  614. float fx0, fy0, fz0;
  615. float fx1, fy1, fz1;
  616. // Integer part (floor).
  617. ix0 = floor(x);
  618. iy0 = floor(y);
  619. iz0 = floor(z);
  620. // Fractional part (v - floor).
  621. fx0 = x - ix0;
  622. fy0 = y - iy0;
  623. fz0 = z - iz0;
  624. // Fractional part minus one.
  625. fx1 = fx0 - 1.0f;
  626. fy1 = fy0 - 1.0f;
  627. fz1 = fz0 - 1.0f;
  628. // Wrap to 0...255.
  629. ix1 = (ix0 + 1) & 255;
  630. iy1 = (iy0 + 1) & 255;
  631. iz1 = (iz0 + 1) & 255;
  632. ix0 &= 255;
  633. iy0 &= 255;
  634. iz0 &= 255;
  635. // Smooth / fade.
  636. float s, t, r;
  637. r = fade(fz0);
  638. t = fade(fy0);
  639. s = fade(fx0);
  640. // Gradients.
  641. float nxy0, nxy1;
  642. float nx0, nx1;
  643. float n0, n1;
  644. nxy0 = grad(perm[ix0 + perm[iy0 + perm[iz0]]], fx0, fy0, fz0);
  645. nxy1 = grad(perm[ix0 + perm[iy0 + perm[iz1]]], fx0, fy0, fz1);
  646. nx0 = lerp(nxy0, nxy1, r);
  647. nxy0 = grad(perm[ix0 + perm[iy1 + perm[iz0]]], fx0, fy1, fz0);
  648. nxy1 = grad(perm[ix0 + perm[iy1 + perm[iz1]]], fx0, fy1, fz1);
  649. nx1 = lerp(nxy0, nxy1, r);
  650. n0 = lerp(nx0, nx1, t);
  651. nxy0 = grad(perm[ix1 + perm[iy0 + perm[iz0]]], fx1, fy0, fz0);
  652. nxy1 = grad(perm[ix1 + perm[iy0 + perm[iz1]]], fx1, fy0, fz1);
  653. nx0 = lerp(nxy0, nxy1, r);
  654. nxy0 = grad(perm[ix1 + perm[iy1 + perm[iz0]]], fx1, fy1, fz0);
  655. nxy1 = grad(perm[ix1 + perm[iy1 + perm[iz1]]], fx1, fy1, fz1);
  656. nx1 = lerp(nxy0, nxy1, r);
  657. n1 = lerp(nx0, nx1, t);
  658. return 0.936f * (lerp(n0, n1, s));
  659. }
  660. // ...
  661. public static float simplexUnoptimized(float x, float y, float z)
  662. {
  663. float n0, n1, n2, n3; // Noise contributions from the four corners
  664. // Skew the input space to determine which simplex cell we're in.
  665. float s = (x + y + z) * F3; // Very nice and simple skew factor for 3D.
  666. float xs = x + s;
  667. float ys = y + s;
  668. float zs = z + s;
  669. int i = floor(xs);
  670. int j = floor(ys);
  671. int k = floor(zs);
  672. float t = (i + j + k) * G3;
  673. float X0 = i - t; // Unskew the cell origin back to (x, y, z) space
  674. float Y0 = j - t;
  675. float Z0 = k - t;
  676. float x0 = x - X0; // The x, y, z distances from the cell origin.
  677. float y0 = y - Y0;
  678. float z0 = z - Z0;
  679. // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  680. // Determine which simplex we are in.
  681. int i1, j1, k1; // Offsets for second corner of simplex in (i, j, k) coords.
  682. int i2, j2, k2; // Offsets for third corner of simplex in (i, j, k) coords.
  683. /* This code would benefit from a backport from the GLSL version! */
  684. if (x0 >= y0)
  685. {
  686. if (y0 >= z0) // X Y Z order
  687. {
  688. i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0;
  689. }
  690. else if (x0 >= z0) // X Z Y order
  691. {
  692. i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1;
  693. }
  694. else // Z X Y order
  695. {
  696. i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1;
  697. }
  698. }
  699. else
  700. {
  701. // x0 < y0.
  702. if (y0 < z0) // Z Y X order.
  703. {
  704. i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1;
  705. }
  706. else if (x0 < z0) // Y Z X order.
  707. {
  708. i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1;
  709. }
  710. else // Y X Z order.
  711. {
  712. i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0;
  713. }
  714. }
  715. // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  716. // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  717. // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  718. // c = 1/6.
  719. float x1 = x0 - i1 + G3; // Offsets for second corner in (x, y, z) coords.
  720. float y1 = y0 - j1 + G3;
  721. float z1 = z0 - k1 + G3;
  722. float x2 = x0 - i2 + 2.0f * G3; // Offsets for third corner in (x, y, z) coords.
  723. float y2 = y0 - j2 + 2.0f * G3;
  724. float z2 = z0 - k2 + 2.0f * G3;
  725. float x3 = x0 - 1.0f + 3.0f * G3; // Offsets for last corner in (x, y, z) coords.
  726. float y3 = y0 - 1.0f + 3.0f * G3;
  727. float z3 = z0 - 1.0f + 3.0f * G3;
  728. // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds.
  729. int ii = i & 0xff;
  730. int jj = j & 0xff;
  731. int kk = k & 0xff;
  732. // Calculate the contribution from the four corners.
  733. float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0;
  734. if (t0 < 0.0f)
  735. {
  736. n0 = 0.0f;
  737. }
  738. else
  739. {
  740. t0 *= t0;
  741. n0 = t0 * t0 * grad(perm[ii + perm[jj + perm[kk]]], x0, y0, z0);
  742. }
  743. float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1;
  744. if (t1 < 0.0f)
  745. {
  746. n1 = 0.0f;
  747. }
  748. else
  749. {
  750. t1 *= t1;
  751. n1 = t1 * t1 * grad(perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]], x1, y1, z1);
  752. }
  753. float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2;
  754. if (t2 < 0.0f)
  755. {
  756. n2 = 0.0f;
  757. }
  758. else
  759. {
  760. t2 *= t2;
  761. n2 = t2 * t2 * grad(perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]], x2, y2, z2);
  762. }
  763. float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3;
  764. if (t3 < 0.0f)
  765. {
  766. n3 = 0.0f;
  767. }
  768. else
  769. {
  770. t3 *= t3;
  771. n3 = t3 * t3 * grad(perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]], x3, y3, z3);
  772. }
  773. // Add contributions from each corner to get the final noise value.
  774. // The result is scaled to stay just inside [-1, 1].
  775. return 32.0f * (n0 + n1 + n2 + n3); // TODO: The scale factor is preliminary!
  776. }
  777. // =================================
  778. // End functions.
  779. // =================================
  780. }
  781. // =================================
  782. // End namespace.
  783. // =================================
  784. }
  785. }
  786. }
  787. // =================================
  788. // --END-- //
  789. // =================================