Advertisement
remo9211

noiseSimplex

May 22nd, 2024
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.57 KB | None | 0 0
  1. #ifndef NOISE_SIMPLEX_FUNC
  2. #define NOISE_SIMPLEX_FUNC
  3. /*
  4.  
  5. Description:
  6. Array- and textureless CgFx/HLSL 2D, 3D and 4D simplex noise functions.
  7. a.k.a. simplified and optimized Perlin noise.
  8.  
  9. The functions have very good performance
  10. and no dependencies on external data.
  11.  
  12. 2D - Very fast, very compact code.
  13. 3D - Fast, compact code.
  14. 4D - Reasonably fast, reasonably compact code.
  15.  
  16. ------------------------------------------------------------------
  17.  
  18. Ported by:
  19. Lex-DRL
  20. I've ported the code from GLSL to CgFx/HLSL for Unity,
  21. added a couple more optimisations (to speed it up even further)
  22. and slightly reformatted the code to make it more readable.
  23.  
  24. Original GLSL functions:
  25. https://github.com/ashima/webgl-noise
  26. Credits from original glsl file are at the end of this cginc.
  27.  
  28. ------------------------------------------------------------------
  29.  
  30. Usage:
  31.  
  32. float ns = snoise(v);
  33. // v is any of: float2, float3, float4
  34.  
  35. Return type is float.
  36. To generate 2 or more components of noise (colorful noise),
  37. call these functions several times with different
  38. constant offsets for the arguments.
  39. E.g.:
  40.  
  41. float3 colorNs = float3(
  42. snoise(v),
  43. snoise(v + 17.0),
  44. snoise(v - 43.0),
  45. );
  46.  
  47.  
  48. Remark about those offsets from the original author:
  49.  
  50. People have different opinions on whether these offsets should be integers
  51. for the classic noise functions to match the spacing of the zeroes,
  52. so we have left that for you to decide for yourself.
  53. For most applications, the exact offsets don't really matter as long
  54. as they are not too small or too close to the noise lattice period
  55. (289 in this implementation).
  56.  
  57. */
  58.  
  59. // 1 / 289
  60. #define NOISE_SIMPLEX_1_DIV_289 0.00346020761245674740484429065744f
  61.  
  62. float mod289(float x) {
  63. return x - floor(x * NOISE_SIMPLEX_1_DIV_289) * 289.0;
  64. }
  65.  
  66. float2 mod289(float2 x) {
  67. return x - floor(x * NOISE_SIMPLEX_1_DIV_289) * 289.0;
  68. }
  69.  
  70. float3 mod289(float3 x) {
  71. return x - floor(x * NOISE_SIMPLEX_1_DIV_289) * 289.0;
  72. }
  73.  
  74. float4 mod289(float4 x) {
  75. return x - floor(x * NOISE_SIMPLEX_1_DIV_289) * 289.0;
  76. }
  77.  
  78.  
  79. // ( x*34.0 + 1.0 )*x =
  80. // x*x*34.0 + x
  81. float permute(float x) {
  82. return mod289(
  83. x*x*34.0 + x
  84. );
  85. }
  86.  
  87. float3 permute(float3 x) {
  88. return mod289(
  89. x*x*34.0 + x
  90. );
  91. }
  92.  
  93. float4 permute(float4 x) {
  94. return mod289(
  95. x*x*34.0 + x
  96. );
  97. }
  98.  
  99.  
  100.  
  101. float taylorInvSqrt(float r) {
  102. return 1.79284291400159 - 0.85373472095314 * r;
  103. }
  104.  
  105. float4 taylorInvSqrt(float4 r) {
  106. return 1.79284291400159 - 0.85373472095314 * r;
  107. }
  108.  
  109.  
  110.  
  111. float4 grad4(float j, float4 ip)
  112. {
  113. const float4 ones = float4(1.0, 1.0, 1.0, -1.0);
  114. float4 p, s;
  115. p.xyz = floor( frac(j * ip.xyz) * 7.0) * ip.z - 1.0;
  116. p.w = 1.5 - dot( abs(p.xyz), ones.xyz );
  117.  
  118. // GLSL: lessThan(x, y) = x < y
  119. // HLSL: 1 - step(y, x) = x < y
  120. s = float4(
  121. 1 - step(0.0, p)
  122. );
  123. p.xyz = p.xyz + (s.xyz * 2 - 1) * s.www;
  124.  
  125. return p;
  126. }
  127.  
  128.  
  129.  
  130. // ----------------------------------- 2D -------------------------------------
  131.  
  132. float snoise(float2 v)
  133. {
  134. const float4 C = float4(
  135. 0.211324865405187, // (3.0-sqrt(3.0))/6.0
  136. 0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
  137. -0.577350269189626, // -1.0 + 2.0 * C.x
  138. 0.024390243902439 // 1.0 / 41.0
  139. );
  140.  
  141. // First corner
  142. float2 i = floor( v + dot(v, C.yy) );
  143. float2 x0 = v - i + dot(i, C.xx);
  144.  
  145. // Other corners
  146. // float2 i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0);
  147. // Lex-DRL: afaik, step() in GPU is faster than if(), so:
  148. // step(x, y) = x <= y
  149. int xLessEqual = step(x0.x, x0.y); // x <= y ?
  150. int2 i1 =
  151. int2(1, 0) * (1 - xLessEqual) // x > y
  152. + int2(0, 1) * xLessEqual // x <= y
  153. ;
  154. float4 x12 = x0.xyxy + C.xxzz;
  155. x12.xy -= i1;
  156.  
  157. // Permutations
  158. i = mod289(i); // Avoid truncation effects in permutation
  159. float3 p = permute(
  160. permute(
  161. i.y + float3(0.0, i1.y, 1.0 )
  162. ) + i.x + float3(0.0, i1.x, 1.0 )
  163. );
  164.  
  165. float3 m = max(
  166. 0.5 - float3(
  167. dot(x0, x0),
  168. dot(x12.xy, x12.xy),
  169. dot(x12.zw, x12.zw)
  170. ),
  171. 0.0
  172. );
  173. m = m*m ;
  174. m = m*m ;
  175.  
  176. // Gradients: 41 points uniformly over a line, mapped onto a diamond.
  177. // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
  178.  
  179. float3 x = 2.0 * frac(p * C.www) - 1.0;
  180. float3 h = abs(x) - 0.5;
  181. float3 ox = floor(x + 0.5);
  182. float3 a0 = x - ox;
  183.  
  184. // Normalise gradients implicitly by scaling m
  185. // Approximation of: m *= inversesqrt( a0*a0 + h*h );
  186. m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
  187.  
  188. // Compute final noise value at P
  189. float3 g;
  190. g.x = a0.x * x0.x + h.x * x0.y;
  191. g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  192. return 130.0 * dot(m, g);
  193. }
  194.  
  195. // ----------------------------------- 3D -------------------------------------
  196.  
  197. float snoise(float3 v)
  198. {
  199. const float2 C = float2(
  200. 0.166666666666666667, // 1/6
  201. 0.333333333333333333 // 1/3
  202. );
  203. const float4 D = float4(0.0, 0.5, 1.0, 2.0);
  204.  
  205. // First corner
  206. float3 i = floor( v + dot(v, C.yyy) );
  207. float3 x0 = v - i + dot(i, C.xxx);
  208.  
  209. // Other corners
  210. float3 g = step(x0.yzx, x0.xyz);
  211. float3 l = 1 - g;
  212. float3 i1 = min(g.xyz, l.zxy);
  213. float3 i2 = max(g.xyz, l.zxy);
  214.  
  215. float3 x1 = x0 - i1 + C.xxx;
  216. float3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
  217. float3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
  218.  
  219. // Permutations
  220. i = mod289(i);
  221. float4 p = permute(
  222. permute(
  223. permute(
  224. i.z + float4(0.0, i1.z, i2.z, 1.0 )
  225. ) + i.y + float4(0.0, i1.y, i2.y, 1.0 )
  226. ) + i.x + float4(0.0, i1.x, i2.x, 1.0 )
  227. );
  228.  
  229. // Gradients: 7x7 points over a square, mapped onto an octahedron.
  230. // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
  231. float n_ = 0.142857142857; // 1/7
  232. float3 ns = n_ * D.wyz - D.xzx;
  233.  
  234. float4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
  235.  
  236. float4 x_ = floor(j * ns.z);
  237. float4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
  238.  
  239. float4 x = x_ *ns.x + ns.yyyy;
  240. float4 y = y_ *ns.x + ns.yyyy;
  241. float4 h = 1.0 - abs(x) - abs(y);
  242.  
  243. float4 b0 = float4( x.xy, y.xy );
  244. float4 b1 = float4( x.zw, y.zw );
  245.  
  246. //float4 s0 = float4(lessThan(b0,0.0))*2.0 - 1.0;
  247. //float4 s1 = float4(lessThan(b1,0.0))*2.0 - 1.0;
  248. float4 s0 = floor(b0)*2.0 + 1.0;
  249. float4 s1 = floor(b1)*2.0 + 1.0;
  250. float4 sh = -step(h, 0.0);
  251.  
  252. float4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
  253. float4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
  254.  
  255. float3 p0 = float3(a0.xy,h.x);
  256. float3 p1 = float3(a0.zw,h.y);
  257. float3 p2 = float3(a1.xy,h.z);
  258. float3 p3 = float3(a1.zw,h.w);
  259.  
  260. //Normalise gradients
  261. float4 norm = taylorInvSqrt(float4(
  262. dot(p0, p0),
  263. dot(p1, p1),
  264. dot(p2, p2),
  265. dot(p3, p3)
  266. ));
  267. p0 *= norm.x;
  268. p1 *= norm.y;
  269. p2 *= norm.z;
  270. p3 *= norm.w;
  271.  
  272. // Mix final noise value
  273. float4 m = max(
  274. 0.6 - float4(
  275. dot(x0, x0),
  276. dot(x1, x1),
  277. dot(x2, x2),
  278. dot(x3, x3)
  279. ),
  280. 0.0
  281. );
  282. m = m * m;
  283. return 42.0 * dot(
  284. m*m,
  285. float4(
  286. dot(p0, x0),
  287. dot(p1, x1),
  288. dot(p2, x2),
  289. dot(p3, x3)
  290. )
  291. );
  292. }
  293.  
  294. // ----------------------------------- 4D -------------------------------------
  295.  
  296. float snoise(float4 v)
  297. {
  298. const float4 C = float4(
  299. 0.138196601125011, // (5 - sqrt(5))/20 G4
  300. 0.276393202250021, // 2 * G4
  301. 0.414589803375032, // 3 * G4
  302. -0.447213595499958 // -1 + 4 * G4
  303. );
  304.  
  305. // First corner
  306. float4 i = floor(
  307. v +
  308. dot(
  309. v,
  310. 0.309016994374947451 // (sqrt(5) - 1) / 4
  311. )
  312. );
  313. float4 x0 = v - i + dot(i, C.xxxx);
  314.  
  315. // Other corners
  316.  
  317. // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
  318. float4 i0;
  319. float3 isX = step( x0.yzw, x0.xxx );
  320. float3 isYZ = step( x0.zww, x0.yyz );
  321. i0.x = isX.x + isX.y + isX.z;
  322. i0.yzw = 1.0 - isX;
  323. i0.y += isYZ.x + isYZ.y;
  324. i0.zw += 1.0 - isYZ.xy;
  325. i0.z += isYZ.z;
  326. i0.w += 1.0 - isYZ.z;
  327.  
  328. // i0 now contains the unique values 0,1,2,3 in each channel
  329. float4 i3 = saturate(i0);
  330. float4 i2 = saturate(i0-1.0);
  331. float4 i1 = saturate(i0-2.0);
  332.  
  333. // x0 = x0 - 0.0 + 0.0 * C.xxxx
  334. // x1 = x0 - i1 + 1.0 * C.xxxx
  335. // x2 = x0 - i2 + 2.0 * C.xxxx
  336. // x3 = x0 - i3 + 3.0 * C.xxxx
  337. // x4 = x0 - 1.0 + 4.0 * C.xxxx
  338. float4 x1 = x0 - i1 + C.xxxx;
  339. float4 x2 = x0 - i2 + C.yyyy;
  340. float4 x3 = x0 - i3 + C.zzzz;
  341. float4 x4 = x0 + C.wwww;
  342.  
  343. // Permutations
  344. i = mod289(i);
  345. float j0 = permute(
  346. permute(
  347. permute(
  348. permute(i.w) + i.z
  349. ) + i.y
  350. ) + i.x
  351. );
  352. float4 j1 = permute(
  353. permute(
  354. permute(
  355. permute (
  356. i.w + float4(i1.w, i2.w, i3.w, 1.0 )
  357. ) + i.z + float4(i1.z, i2.z, i3.z, 1.0 )
  358. ) + i.y + float4(i1.y, i2.y, i3.y, 1.0 )
  359. ) + i.x + float4(i1.x, i2.x, i3.x, 1.0 )
  360. );
  361.  
  362. // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
  363. // 7*7*6 = 294, which is close to the ring size 17*17 = 289.
  364. const float4 ip = float4(
  365. 0.003401360544217687075, // 1/294
  366. 0.020408163265306122449, // 1/49
  367. 0.142857142857142857143, // 1/7
  368. 0.0
  369. );
  370.  
  371. float4 p0 = grad4(j0, ip);
  372. float4 p1 = grad4(j1.x, ip);
  373. float4 p2 = grad4(j1.y, ip);
  374. float4 p3 = grad4(j1.z, ip);
  375. float4 p4 = grad4(j1.w, ip);
  376.  
  377. // Normalise gradients
  378. float4 norm = taylorInvSqrt(float4(
  379. dot(p0, p0),
  380. dot(p1, p1),
  381. dot(p2, p2),
  382. dot(p3, p3)
  383. ));
  384. p0 *= norm.x;
  385. p1 *= norm.y;
  386. p2 *= norm.z;
  387. p3 *= norm.w;
  388. p4 *= taylorInvSqrt( dot(p4, p4) );
  389.  
  390. // Mix contributions from the five corners
  391. float3 m0 = max(
  392. 0.6 - float3(
  393. dot(x0, x0),
  394. dot(x1, x1),
  395. dot(x2, x2)
  396. ),
  397. 0.0
  398. );
  399. float2 m1 = max(
  400. 0.6 - float2(
  401. dot(x3, x3),
  402. dot(x4, x4)
  403. ),
  404. 0.0
  405. );
  406. m0 = m0 * m0;
  407. m1 = m1 * m1;
  408.  
  409. return 49.0 * (
  410. dot(
  411. m0*m0,
  412. float3(
  413. dot(p0, x0),
  414. dot(p1, x1),
  415. dot(p2, x2)
  416. )
  417. ) + dot(
  418. m1*m1,
  419. float2(
  420. dot(p3, x3),
  421. dot(p4, x4)
  422. )
  423. )
  424. );
  425. }
  426.  
  427. float hash(float x, float y) {
  428. return frac(abs(sin(sin(123.321 + x) * (y + 321.123)) * 456.654));
  429. }
  430.  
  431. float perlin(float x, float y){
  432. float col = 0.0;
  433. for (int i = 0; i < 8; i++)
  434. {
  435. float fx = floor(x);
  436. float fy = floor(y);
  437. float cx = ceil(x);
  438. float cy = ceil(y);
  439. float a = hash(fx, fy);
  440. float b = hash(fx, cy);
  441. float c = hash(cx, fy);
  442. float d = hash(cx, cy);
  443. col += lerp(lerp(a, b, frac(y)), lerp(c, d, frac(y)), frac(x));
  444. col /= 2.0;
  445. x /= 2.0;
  446. y /= 2.0;
  447. }
  448. return col;
  449. }
  450.  
  451. float2 fade(float2 t) {
  452. return t*t*t*(t*(t*6.0-15.0)+10.0);
  453. }
  454.  
  455. float3 fade(float3 t) {
  456. return t*t*t*(t*(t*6.0-15.0)+10.0);
  457. }
  458.  
  459. // Classic Perlin noise, periodic variant
  460. float pnoise(float3 P, float3 rep)
  461. {
  462. float3 Pi0 = fmod(floor(P), rep); // Integer part, modulo period
  463. float3 Pi1 = fmod(Pi0 + float3(1,1,1), rep); // Integer part + 1, mod period
  464. Pi0 = mod289(Pi0);
  465. Pi1 = mod289(Pi1);
  466. float3 Pf0 = frac(P); // Fractional part for interpolation
  467. float3 Pf1 = Pf0 - float3(1,1,1); // Fractional part - 1.0
  468. float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  469. float4 iy = float4(Pi0.yy, Pi1.yy);
  470. float4 iz0 = Pi0.zzzz;
  471. float4 iz1 = Pi1.zzzz;
  472.  
  473. float4 ixy = permute(permute(ix) + iy);
  474. float4 ixy0 = permute(ixy + iz0);
  475. float4 ixy1 = permute(ixy + iz1);
  476.  
  477. float4 gx0 = ixy0 * (1.0 / 7.0);
  478. float4 gy0 = frac(floor(gx0) * (1.0 / 7.0)) - 0.5;
  479. gx0 = frac(gx0);
  480.  
  481. float4 pointFive4 = 0.5;
  482. float4 zero4 = 0;
  483. float4 gz0 = pointFive4 - abs(gx0) - abs(gy0);
  484. float4 sz0 = step(gz0, zero4);
  485. gx0 -= sz0 * (step(0.0, gx0) - 0.5);
  486. gy0 -= sz0 * (step(0.0, gy0) - 0.5);
  487.  
  488. float4 gx1 = ixy1 * (1.0 / 7.0);
  489. float4 gy1 = frac(floor(gx1) * (1.0 / 7.0)) - 0.5;
  490. gx1 = frac(gx1);
  491. float4 gz1 = pointFive4 - abs(gx1) - abs(gy1);
  492. float4 sz1 = step(gz1, zero4);
  493. gx1 -= sz1 * (step(0.0, gx1) - 0.5);
  494. gy1 -= sz1 * (step(0.0, gy1) - 0.5);
  495.  
  496. float3 g000 = float3(gx0.x,gy0.x,gz0.x);
  497. float3 g100 = float3(gx0.y,gy0.y,gz0.y);
  498. float3 g010 = float3(gx0.z,gy0.z,gz0.z);
  499. float3 g110 = float3(gx0.w,gy0.w,gz0.w);
  500. float3 g001 = float3(gx1.x,gy1.x,gz1.x);
  501. float3 g101 = float3(gx1.y,gy1.y,gz1.y);
  502. float3 g011 = float3(gx1.z,gy1.z,gz1.z);
  503. float3 g111 = float3(gx1.w,gy1.w,gz1.w);
  504.  
  505. float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  506. g000 *= norm0.x;
  507. g010 *= norm0.y;
  508. g100 *= norm0.z;
  509. g110 *= norm0.w;
  510. float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  511. g001 *= norm1.x;
  512. g011 *= norm1.y;
  513. g101 *= norm1.z;
  514. g111 *= norm1.w;
  515.  
  516. float n000 = dot(g000, Pf0);
  517. float n100 = dot(g100, float3(Pf1.x, Pf0.yz));
  518. float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z));
  519. float n110 = dot(g110, float3(Pf1.xy, Pf0.z));
  520. float n001 = dot(g001, float3(Pf0.xy, Pf1.z));
  521. float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z));
  522. float n011 = dot(g011, float3(Pf0.x, Pf1.yz));
  523. float n111 = dot(g111, Pf1);
  524.  
  525. float3 fade_xyz = fade(Pf0);
  526. float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z);
  527. float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y);
  528. float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x);
  529.  
  530. return 2.2 * n_xyz;
  531. }
  532.  
  533. float turbulence( float3 p ) {
  534. float w = 100.0;
  535. float t = -.5;
  536.  
  537. for (float f = 1.0 ; f <= 10.0 ; f++ ){
  538. float power = pow( 2.0, f );
  539. t += abs( pnoise( power * p, float3( 10.0, 10.0, 10.0 ) ) / power );
  540. }
  541.  
  542. return t;
  543. }
  544. // Credits from source glsl file:
  545. //
  546. // Description : Array and textureless GLSL 2D/3D/4D simplex
  547. // noise functions.
  548. // Author : Ian McEwan, Ashima Arts.
  549. // Maintainer : ijm
  550. // Lastmod : 20110822 (ijm)
  551. // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  552. // Distributed under the MIT License. See LICENSE file.
  553. // https://github.com/ashima/webgl-noise
  554. //
  555. //
  556. // The text from LICENSE file:
  557. //
  558. //
  559. // Copyright (C) 2011 by Ashima Arts (Simplex noise)
  560. // Copyright (C) 2011 by Stefan Gustavson (Classic noise)
  561. //
  562. // Permission is hereby granted, free of charge, to any person obtaining a copy
  563. // of this software and associated documentation files (the "Software"), to deal
  564. // in the Software without restriction, including without limitation the rights
  565. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  566. // copies of the Software, and to permit persons to whom the Software is
  567. // furnished to do so, subject to the following conditions:
  568. //
  569. // The above copyright notice and this permission notice shall be included in
  570. // all copies or substantial portions of the Software.
  571. //
  572. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  573. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  574. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  575. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  576. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  577. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  578. // THE SOFTWARE.
  579. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement