Advertisement
Guest User

Simplex noise

a guest
Jun 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.18 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.  
  428.  
  429. // Credits from source glsl file:
  430. //
  431. // Description : Array and textureless GLSL 2D/3D/4D simplex
  432. // noise functions.
  433. // Author : Ian McEwan, Ashima Arts.
  434. // Maintainer : ijm
  435. // Lastmod : 20110822 (ijm)
  436. // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  437. // Distributed under the MIT License. See LICENSE file.
  438. // https://github.com/ashima/webgl-noise
  439. //
  440. //
  441. // The text from LICENSE file:
  442. //
  443. //
  444. // Copyright (C) 2011 by Ashima Arts (Simplex noise)
  445. // Copyright (C) 2011 by Stefan Gustavson (Classic noise)
  446. //
  447. // Permission is hereby granted, free of charge, to any person obtaining a copy
  448. // of this software and associated documentation files (the "Software"), to deal
  449. // in the Software without restriction, including without limitation the rights
  450. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  451. // copies of the Software, and to permit persons to whom the Software is
  452. // furnished to do so, subject to the following conditions:
  453. //
  454. // The above copyright notice and this permission notice shall be included in
  455. // all copies or substantial portions of the Software.
  456. //
  457. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  458. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  459. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  460. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  461. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  462. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  463. // THE SOFTWARE.
  464. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement