Advertisement
Guest User

simplex hlsl

a guest
Jul 15th, 2013
2,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. int simplexSeed;
  2.  
  3. int hash(int a)
  4. {
  5. a = (a ^ 61) ^ (a >> 16);
  6. a = a + (a << 3);
  7. a = a ^ (a >> 4);
  8. a = a * simplexSeed;
  9. a = a ^ (a >> 15);
  10. return a;
  11. }
  12.  
  13. /*
  14. ** 1D
  15. */
  16.  
  17. void grad1( int hash, out float gx )
  18. {
  19. int h = hash & 15;
  20. gx = 1.0f + (h & 7);
  21. if (h&8) gx = -gx;
  22. }
  23.  
  24. float noise( float x )
  25. {
  26. int i0 = floor(x);
  27. int i1 = i0 + 1;
  28. float x0 = x - i0;
  29. float x1 = x0 - 1.0f;
  30.  
  31. float gx0, gx1;
  32. float n0, n1;
  33. float t20, t40, t21, t41;
  34.  
  35. float x20 = x0 * x0;
  36. float t0 = 1.0f - x20;
  37. t20 = t0 * t0;
  38. t40 = t20 * t20;
  39. grad1(hash(i0 & 0xff), gx0);
  40. n0 = t40 * gx0 * x0;
  41.  
  42. float x21 = x1*x1;
  43. float t1 = 1.0f - x21;
  44. t21 = t1 * t1;
  45. t41 = t21 * t21;
  46. grad1(hash(i1 & 0xff), gx1);
  47. n1 = t41 * gx1 * x1;
  48.  
  49. return 0.25f * (n0 + n1);
  50. }
  51.  
  52. float noise (float x, out float dnoise_dx)
  53. {
  54. int i0 = floor(x);
  55. int i1 = i0 + 1;
  56. float x0 = x - i0;
  57. float x1 = x0 - 1.0f;
  58.  
  59. float gx0, gx1;
  60. float n0, n1;
  61. float t20, t40, t21, t41;
  62.  
  63. float x20 = x0 * x0;
  64. float t0 = 1.0f - x20;
  65. t20 = t0 * t0;
  66. t40 = t20 * t20;
  67. grad1(hash(i0 & 0xff), gx0);
  68. n0 = t40 * gx0 * x0;
  69.  
  70. float x21 = x1*x1;
  71. float t1 = 1.0f - x21;
  72. t21 = t1 * t1;
  73. t41 = t21 * t21;
  74. grad1(hash(i1 & 0xff), gx1);
  75. n1 = t41 * gx1 * x1;
  76.  
  77. dnoise_dx = t20 * t0 * gx0 * x20;
  78. dnoise_dx += t21 * t1 * gx1 * x21;
  79. dnoise_dx *= -8.0f;
  80. dnoise_dx += t40 * gx0 + t41 * gx1;
  81. dnoise_dx *= 0.1;
  82. return 0.25f * (n0 + n1);
  83. }
  84.  
  85. /*
  86. ** 2D
  87. */
  88.  
  89. static float2 grad2lut[8] =
  90. {
  91. { -1.0f, -1.0f }, { 1.0f, 0.0f } , { -1.0f, 0.0f } , { 1.0f, 1.0f } ,
  92. { -1.0f, 1.0f } , { 0.0f, -1.0f } , { 0.0f, 1.0f } , { 1.0f, -1.0f }
  93. };
  94.  
  95. float2 grad2( int hash)
  96. {
  97. return grad2lut[hash & 7];
  98. }
  99.  
  100. float noise( float2 input)
  101. {
  102. float n0, n1, n2;
  103. float2 g0, g1, g2;
  104.  
  105. float s = ( input.x + input.y ) * 0.366025403f;
  106. float2 a = input + s;
  107. int2 ij = floor( a );
  108.  
  109. float t = ( float ) ( ij.x + ij.y ) * 0.211324865f;
  110. float2 b = ij - t;
  111. float2 c = input - b;
  112.  
  113. int2 ij1 = c.x > c.y ? float2(1,0) : float2(0,1);
  114.  
  115. float2 c1 = c - ij1 + 0.211324865f;
  116. float2 c2 = c - 1.0f + 2.0f * 0.211324865f;
  117.  
  118. int ii = ij.x & 0xff;
  119. int jj = ij.y & 0xff;
  120.  
  121. float t0 = 0.5f - c.x * c.x - c.y * c.y;
  122. float t20, t40;
  123. if( t0 < 0.0f ) t40 = t20 = t0 = n0 = g0.x = g0.y = 0.0f;
  124. else
  125. {
  126. g0 = grad2( hash(ii + hash(jj)));
  127. t20 = t0 * t0;
  128. t40 = t20 * t20;
  129. n0 = t40 * ( g0.x * c.x + g0.y * c.y );
  130. }
  131.  
  132. float t1 = 0.5f - c1.x * c1.x - c1.y * c1.y;
  133. float t21, t41;
  134. if( t1 < 0.0f ) t21 = t41 = t1 = n1 = g1.x = g1.y = 0.0f;
  135. else
  136. {
  137. g1 = grad2( hash(ii + ij1.x + hash(jj + ij1.y)));
  138. t21 = t1 * t1;
  139. t41 = t21 * t21;
  140. n1 = t41 * ( g1.x * c1.x + g1.y * c1.y );
  141. }
  142.  
  143. float t2 = 0.5f - c2.x * c2.x - c2.y * c2.y;
  144. float t22, t42;
  145. if( t2 < 0.0f ) t42 = t22 = t2 = n2 = g2.x = g2.y = 0.0f;
  146. else
  147. {
  148. g2 = grad2( hash(ii + 1 + hash(jj + 1)));
  149. t22 = t2 * t2;
  150. t42 = t22 * t22;
  151. n2 = t42 * ( g2.x * c2.x + g2.y * c2.y );
  152. }
  153.  
  154. float noise = 40.0f * ( n0 + n1 + n2 );
  155. return noise;
  156. }
  157.  
  158. float noise( float2 input, out float2 derivative)
  159. {
  160. float n0, n1, n2;
  161. float2 g0, g1, g2;
  162.  
  163. float s = ( input.x + input.y ) * 0.366025403f;
  164. float2 a = input + s;
  165. int2 ij = floor( a );
  166.  
  167. float t = ( float ) ( ij.x + ij.y ) * 0.211324865f;
  168. float2 b = ij - t;
  169. float2 c = input - b;
  170.  
  171. int2 ij1 = c.x > c.y ? float2(1,0) : float2(0,1);
  172.  
  173. float2 c1 = c - ij1 + 0.211324865f;
  174. float2 c2 = c - 1.0f + 2.0f * 0.211324865f;
  175.  
  176. int ii = ij.x & 0xff;
  177. int jj = ij.y & 0xff;
  178.  
  179. float t0 = 0.5f - c.x * c.x - c.y * c.y;
  180. float t20, t40;
  181. if( t0 < 0.0f ) t40 = t20 = t0 = n0 = g0.x = g0.y = 0.0f;
  182. else
  183. {
  184. g0 = grad2( hash(ii + hash(jj)));
  185. t20 = t0 * t0;
  186. t40 = t20 * t20;
  187. n0 = t40 * ( g0.x * c.x + g0.y * c.y );
  188. }
  189.  
  190. float t1 = 0.5f - c1.x * c1.x - c1.y * c1.y;
  191. float t21, t41;
  192. if( t1 < 0.0f ) t21 = t41 = t1 = n1 = g1.x = g1.y = 0.0f;
  193. else
  194. {
  195. g1 = grad2( hash(ii + ij1.x + hash(jj + ij1.y)));
  196. t21 = t1 * t1;
  197. t41 = t21 * t21;
  198. n1 = t41 * ( g1.x * c1.x + g1.y * c1.y );
  199. }
  200.  
  201. float t2 = 0.5f - c2.x * c2.x - c2.y * c2.y;
  202. float t22, t42;
  203. if( t2 < 0.0f ) t42 = t22 = t2 = n2 = g2.x = g2.y = 0.0f;
  204. else
  205. {
  206. g2 = grad2( hash(ii + 1 + hash(jj + 1)));
  207. t22 = t2 * t2;
  208. t42 = t22 * t22;
  209. n2 = t42 * ( g2.x * c2.x + g2.y * c2.y );
  210. }
  211.  
  212. float noise = 40.0f * ( n0 + n1 + n2 );
  213.  
  214. float temp0 = t20 * t0 * ( g0.x * c.x + g0.y * c.y );
  215. float temp1 = t21 * t1 * ( g1.x * c1.x + g1.y * c1.y );
  216. float temp2 = t22 * t2 * ( g2.x * c2.x + g2.y * c2.y );
  217. derivative = ((temp0 * c + temp1 * c1 + temp2 * c2) * -8 + (t40 * g0 + t41 * g1 + t42 * g2)) * 40;
  218.  
  219. return noise;
  220. }
  221.  
  222. /*
  223. ** 3D
  224. */
  225.  
  226. static float3 grad3lut[16] =
  227. {
  228. { 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f },
  229. { -1.0f, 0.0f, 1.0f }, { 0.0f, -1.0f, 1.0f },
  230. { 1.0f, 0.0f, -1.0f }, { 0.0f, 1.0f, -1.0f },
  231. { -1.0f, 0.0f, -1.0f }, { 0.0f, -1.0f, -1.0f },
  232. { 1.0f, -1.0f, 0.0f }, { 1.0f, 1.0f, 0.0f },
  233. { -1.0f, 1.0f, 0.0f }, { -1.0f, -1.0f, 0.0f },
  234. { 1.0f, 0.0f, 1.0f }, { -1.0f, 0.0f, 1.0f },
  235. { 0.0f, 1.0f, -1.0f }, { 0.0f, -1.0f, -1.0f }
  236. };
  237.  
  238. float3 grad3(int hash)
  239. {
  240. return grad3lut[hash & 15];
  241. }
  242.  
  243. float simplexNoise(float3 input)
  244. {
  245. float n0, n1, n2, n3;
  246. float noise;
  247. float3 g0, g1, g2, g3;
  248.  
  249. float s = (input.x + input.y + input.z) * 0.333333333;
  250. float3 a = input + s;
  251. int3 ijk = floor(a);
  252.  
  253. float t = (float)(ijk.x + ijk.y + ijk.z) * 0.166666667;
  254. float3 b = ijk - t;
  255. float3 c = input - b;
  256.  
  257. int3 ijk1;
  258. int3 ijk2;
  259.  
  260. if(c.x >= c.y) {
  261. if(c.y >= c.z)
  262. { ijk1 = int3(1, 0, 0); ijk2 = int3(1,1,0); }
  263. else if(c.x >= c.z) { ijk1 = int3(1, 0, 0); ijk2 = int3(1,0,1); }
  264. else { ijk1 = int3(0, 0, 1); ijk2 = int3(1,0,1); }
  265. }
  266. else {
  267. if(c.y < c.z) { ijk1 = int3(0, 0, 1); ijk2 = int3(0,1,1); }
  268. else if(c.x < c.z) { ijk1 = int3(0, 1, 0); ijk2 = int3(0,1,1); }
  269. else { ijk1 = int3(0, 1, 0); ijk2 = int3(1,1,0); }
  270. }
  271.  
  272. float3 c1 = c - ijk1 + 0.166666667;
  273. float3 c2 = c - ijk2 + 2.0f * 0.166666667;
  274. float3 c3 = c - 1.0f + 3.0f * 0.166666667;
  275.  
  276. int ii = ijk.x & 0xff;
  277. int jj = ijk.y & 0xff;
  278. int kk = ijk.z & 0xff;
  279.  
  280. float t0 = 0.6f - c.x * c.x - c.y * c.y - c.z * c.z;
  281. float t20, t40;
  282. if(t0 < 0.0f) n0 = t0 = t20 = t40 = g0.x = g0.y = g0.z = 0.0f;
  283. else {
  284. g0 = grad3( hash(ii + hash(jj + hash(kk))));
  285. t20 = t0 * t0;
  286. t40 = t20 * t20;
  287. n0 = t40 * ( g0.x * c.x + g0.y * c.y + g0.z * c.z );
  288. }
  289.  
  290. float t1 = 0.6f - c1.x * c1.x - c1.y * c1.y - c1.z * c1.z;
  291. float t21, t41;
  292. if(t1 < 0.0f) n1 = t1 = t21 = t41 = g1.x = g1.y = g1.z = 0.0f;
  293. else {
  294. g1 = grad3( hash(ii + ijk1.x + hash(jj + ijk1.y + hash(kk + ijk1.z))));
  295. t21 = t1 * t1;
  296. t41 = t21 * t21;
  297. n1 = t41 * ( g1.x * c1.x + g1.y * c1.y + g1.z * c1.z );
  298. }
  299.  
  300. float t2 = 0.6f - c2.x * c2.x - c2.y * c2.y - c2.z * c2.z;
  301. float t22, t42;
  302. if(t2 < 0.0f) n2 = t2 = t22 = t42 = g2.x = g2.y = g2.z = 0.0f;
  303. else {
  304. g2 = grad3( hash(ii + ijk2.x + hash(jj + ijk2.y + hash(kk + ijk2.z))));
  305. t22 = t2 * t2;
  306. t42 = t22 * t22;
  307. n2 = t42 * ( g2.x * c2.x + g2.y * c2.y + g2.z * c2.z );
  308. }
  309.  
  310. float t3 = 0.6f - c3.x * c3.x - c3.y * c3.y - c3.z * c3.z;
  311. float t23, t43;
  312. if(t3 < 0.0f) n3 = t3 = t23 = t43 = g3.x = g3.y = g3.z = 0.0f;
  313. else {
  314. g3 = grad3( hash(ii + 1 + hash(jj + 1 + hash(kk + 1))));
  315. t23 = t3 * t3;
  316. t43 = t23 * t23;
  317. n3 = t43 * ( g3.x * c3.x + g3.y * c3.y + g3.z * c3.z );
  318. }
  319.  
  320. noise = 20.0f * (n0 + n1 + n2 + n3);
  321. return noise;
  322. }
  323.  
  324. float simplexNoise( float3 input, out float3 derivative)
  325. {
  326. float n0, n1, n2, n3;
  327. float noise;
  328. float3 g0, g1, g2, g3;
  329.  
  330. float s = (input.x + input.y + input.z) * 0.333333333;
  331. float3 a = input + s;
  332. int3 ijk = floor(a);
  333.  
  334. float t = (float)(ijk.x + ijk.y + ijk.z) * 0.166666667;
  335. float3 b = ijk - t;
  336. float3 c = input - b;
  337.  
  338. int3 ijk1;
  339. int3 ijk2;
  340.  
  341. if(c.x >= c.y) {
  342. if(c.y >= c.z)
  343. { ijk1 = int3(1, 0, 0); ijk2 = int3(1,1,0); }
  344. else if(c.x >= c.z) { ijk1 = int3(1, 0, 0); ijk2 = int3(1,0,1); }
  345. else { ijk1 = int3(0, 0, 1); ijk2 = int3(1,0,1); }
  346. }
  347. else {
  348. if(c.y < c.z) { ijk1 = int3(0, 0, 1); ijk2 = int3(0,1,1); }
  349. else if(c.x < c.z) { ijk1 = int3(0, 1, 0); ijk2 = int3(0,1,1); }
  350. else { ijk1 = int3(0, 1, 0); ijk2 = int3(1,1,0); }
  351. }
  352.  
  353. float3 c1 = c - ijk1 + 0.166666667;
  354. float3 c2 = c - ijk2 + 2.0f * 0.166666667;
  355. float3 c3 = c - 1.0f + 3.0f * 0.166666667;
  356.  
  357. int ii = ijk.x & 0xff;
  358. int jj = ijk.y & 0xff;
  359. int kk = ijk.z & 0xff;
  360.  
  361. float t0 = 0.6f - c.x * c.x - c.y * c.y - c.z * c.z;
  362. float t20, t40;
  363. if(t0 < 0.0f) n0 = t0 = t20 = t40 = g0.x = g0.y = g0.z = 0.0f;
  364. else {
  365. g0 = grad3( hash(ii + hash(jj + hash(kk))));
  366. t20 = t0 * t0;
  367. t40 = t20 * t20;
  368. n0 = t40 * ( g0.x * c.x + g0.y * c.y + g0.z * c.z );
  369. }
  370.  
  371. float t1 = 0.6f - c1.x * c1.x - c1.y * c1.y - c1.z * c1.z;
  372. float t21, t41;
  373. if(t1 < 0.0f) n1 = t1 = t21 = t41 = g1.x = g1.y = g1.z = 0.0f;
  374. else {
  375. g1 = grad3( hash(ii + ijk1.x + hash(jj + ijk1.y + hash(kk + ijk1.z))));
  376. t21 = t1 * t1;
  377. t41 = t21 * t21;
  378. n1 = t41 * ( g1.x * c1.x + g1.y * c1.y + g1.z * c1.z );
  379. }
  380.  
  381. float t2 = 0.6f - c2.x * c2.x - c2.y * c2.y - c2.z * c2.z;
  382. float t22, t42;
  383. if(t2 < 0.0f) n2 = t2 = t22 = t42 = g2.x = g2.y = g2.z = 0.0f;
  384. else {
  385. g2 = grad3( hash(ii + ijk2.x + hash(jj + ijk2.y + hash(kk + ijk2.z))));
  386. t22 = t2 * t2;
  387. t42 = t22 * t22;
  388. n2 = t42 * ( g2.x * c2.x + g2.y * c2.y + g2.z * c2.z );
  389. }
  390.  
  391. float t3 = 0.6f - c3.x * c3.x - c3.y * c3.y - c3.z * c3.z;
  392. float t23, t43;
  393. if(t3 < 0.0f) n3 = t3 = t23 = t43 = g3.x = g3.y = g3.z = 0.0f;
  394. else {
  395. g3 = grad3( hash(ii + 1 + hash(jj + 1 + hash(kk + 1))));
  396. t23 = t3 * t3;
  397. t43 = t23 * t23;
  398. n3 = t43 * ( g3.x * c3.x + g3.y * c3.y + g3.z * c3.z );
  399. }
  400.  
  401. noise = 20.0f * (n0 + n1 + n2 + n3);
  402.  
  403. float temp0 = t20 * t0 * ( g0.x * c.x + g0.y * c.y + g0.z * c.z );
  404. derivative = temp0 * c;
  405. float temp1 = t21 * t1 * ( g1.x * c1.x + g1.y * c1.y + g1.z * c1.z );
  406. derivative += temp1 * c1;
  407. float temp2 = t22 * t2 * ( g2.x * c2.x + g2.y * c2.y + g2.z * c2.z );
  408. derivative += temp2 * c2;
  409. float temp3 = t23 * t3 * ( g3.x * c3.x + g3.y * c3.y + g3.z * c3.z );
  410. derivative += temp3 * c3;
  411. derivative *= -8.0f;
  412. derivative += t40 * g0 + t41 * g1 + t42 * g2 + t43 * g3;
  413. derivative *= 28.0f;
  414.  
  415. return noise;
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement