Author Topic: Simplex Noise Anyone?  (Read 6613 times)

0 Members and 1 Guest are viewing this topic.

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Simplex Noise Anyone?
« on: February 25, 2022, 12:49:00 pm »
Okay, I started trying to port a simplex noise function to QB64.  I have gotten some interesting results.  However, even though I believe the port of the function is right, I don't think I understand how to actually use it.  After working on it for several hours over several days, I am still at a loss.  If someone can enlighten me as to what I am doing wrong or not doing, I would appreciate the education.  NOTE: I have only ported the 2D section of the algorithm to this point.

Thank you!
~Phlashlite

Code: QB64: [Select]
  1. '$DEBUG
  2. _TITLE "Simplex noise port"
  3.  
  4. ' ported to QB64 by: Phlashlite, February 20, 2022
  5. '    from the paper: Simplex noise demystified
  6. '            author: Stefan Gustavson, Link”ping University, Sweden (stegu@itn.liu.se), 2005-03-22
  7. '               URL: https://weber.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  8. ' - Simplex noise has a lower computational complexity and requires fewer multiplications.
  9. ' - Simplex noise scales to higher dimensions (4D, 5D and up) with much less computational
  10. '   cost, the complexity is for dimensions instead of the of classic Noise.
  11. ' - Simplex noise has no noticeable directional artifacts.
  12. ' - Simplex noise has a well-defined and continuous gradient everywhere that can be computed
  13. '   quite cheaply.
  14. ' - Simplex noise is easy to implement in hardware
  15.  
  16.  
  17. SCREEN _NEWIMAGE(800, 600, 32)
  18.  
  19. DEFDBL A-Z
  20.  
  21. 'private static int grad3[][] = {{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}};
  22. g3:
  23. DATA 1,1,0
  24. DATA -1,1,0
  25. DATA 1,-1,0
  26. DATA -1,-1,0
  27. DATA 1,0,1
  28. DATA -1,0,1
  29. DATA 1,0,-1
  30. DATA -1,0,-1
  31. DATA 0,1,1
  32. DATA 0,-1,1
  33. DATA 0,1,-1
  34. DATA 0,-1,-1
  35.  
  36. TYPE TriBit
  37.     x AS INTEGER
  38.     y AS INTEGER
  39.     z AS INTEGER
  40. DIM SHARED grad3(12) AS TriBit
  41.  
  42. FOR g% = 0 TO 11
  43.  
  44.     READ grad3(g%).x
  45.     READ grad3(g%).y
  46.     READ grad3(g%).z
  47.  
  48.     'PRINT grad3(g%).x, grad3(g%).y, grad3(g%).z
  49.  
  50. NEXT g%
  51.  
  52.  
  53. 'private static int p[] = {151,160,137,91,90,15,
  54. '131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  55. '190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  56. '88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  57. '77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  58. '102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,196,
  59. '135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,250,124,123,
  60. '5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  61. '223,183,170,213,119,248,152,2,44,154,163,70,221,153,101,155,167,43,172,9,
  62. '129,22,39,253,19,98,108,110,79,113,224,232,178,185,112,104,218,246,97,228,
  63. '251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249,14,239,107,
  64. '49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,
  65. '138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
  66. p:
  67. DATA 151,160,137,091,090,015,131,013,201,095
  68. DATA 096,053,194,233,007,225,140,036,103,030
  69. DATA 069,142,008,099,037,240,021,010,023,190
  70. DATA 006,148,247,120,234,075,000,026,197,062
  71. DATA 094,252,219,203,117,035,011,032,057,177
  72. DATA 033,088,237,149,056,087,174,020,125,136
  73. DATA 171,168,068,175,074,165,071,134,139,048
  74. DATA 027,166,077,146,158,231,083,111,229,122
  75. DATA 060,211,133,230,220,105,092,041,055,046
  76. DATA 245,040,244,102,143,054,065,025,063,161
  77. DATA 001,216,080,073,209,076,132,187,208,089
  78. DATA 018,169,200,196,135,130,116,188,159,086
  79. DATA 164,100,109,198,173,186,003,064,052,217
  80. DATA 226,250,124,123,005,202,038,147,118,126
  81. DATA 255,082,085,212,207,206,059,227,047,016
  82. DATA 058,017,182,189,028,042,223,183,170,213
  83. DATA 119,248,152,002,044,154,163,070,221,153
  84. DATA 101,155,167,043,172,009,129,022,039,253
  85. DATA 019,098,108,110,079,113,224,232,178,185
  86. DATA 112,104,218,246,097,228,251,034,242,193
  87. DATA 238,210,144,012,191,179,162,241,081,051
  88. DATA 145,235,249,014,239,107,049,192,214,031
  89. DATA 181,199,106,157,184,084,204,176,115,121
  90. DATA 050,045,127,004,150,254,138,236,205,093
  91. DATA 222,114,067,029,024,072,243,141,128,195
  92. DATA 078,066,215,061,156,180
  93. DATA 151,160,137,091,090,015,131,013,201,095
  94. DATA 096,053,194,233,007,225,140,036,103,030
  95. DATA 069,142,008,099,037,240,021,010,023,190
  96. DATA 006,148,247,120,234,075,000,026,197,062
  97. DATA 094,252,219,203,117,035,011,032,057,177
  98. DATA 033,088,237,149,056,087,174,020,125,136
  99. DATA 171,168,068,175,074,165,071,134,139,048
  100. DATA 027,166,077,146,158,231,083,111,229,122
  101. DATA 060,211,133,230,220,105,092,041,055,046
  102. DATA 245,040,244,102,143,054,065,025,063,161
  103. DATA 001,216,080,073,209,076,132,187,208,089
  104. DATA 018,169,200,196,135,130,116,188,159,086
  105. DATA 164,100,109,198,173,186,003,064,052,217
  106. DATA 226,250,124,123,005,202,038,147,118,126
  107. DATA 255,082,085,212,207,206,059,227,047,016
  108. DATA 058,017,182,189,028,042,223,183,170,213
  109. DATA 119,248,152,002,044,154,163,070,221,153
  110. DATA 101,155,167,043,172,009,129,022,039,253
  111. DATA 019,098,108,110,079,113,224,232,178,185
  112. DATA 112,104,218,246,097,228,251,034,242,193
  113. DATA 238,210,144,012,191,179,162,241,081,051
  114. DATA 145,235,249,014,239,107,049,192,214,031
  115. DATA 181,199,106,157,184,084,204,176,115,121
  116. DATA 050,045,127,004,150,254,138,236,205,093
  117. DATA 222,114,067,029,024,072,243,141,128,195
  118. DATA 078,066,215,061,156,180
  119.  
  120.  
  121. 'To remove the need for index wrapping, double the permutation table length
  122. 'private static int perm[] = new int[512];
  123. DIM SHARED perm(512) AS INTEGER
  124.  
  125. 'static { for(int i=0; i<512; i++) perm[i]=p[i & 255]; }
  126. FOR i% = 0 TO 511
  127.     READ p(i%)
  128.     perm(i%) = p(i%) AND 255
  129.  
  130.     'PRINT perm(i%)
  131.  
  132.  
  133. '' 2D simplex noise************************************************************
  134.  
  135. ' final double F2 = 0.5*(Math.sqrt(3.0)-1.0);
  136. CONST F2 = .5 * (SQR(3) - 1)
  137. ' final double G2 = (3.0-Math.sqrt(3.0))/6.0;
  138. CONST G2 = (3 - SQR(3)) / 6
  139.  
  140. scale = .01
  141. '------------------------------------------------------------------------------
  142. FOR x% = 0 TO 800
  143.     FOR y% = 0 TO 600
  144.  
  145.         gs% = map!(noise2D(scale * x%, scale * y%), -1, 1, 0, 255)
  146.         c& = _RGB(gs%, gs%, gs%)
  147.         PSET (x%, y%), c&
  148.  
  149.         'PRINT INT(map!(noise2D(x%, y%), -1, 1, 0, 255));
  150.         'PRINT INT((noise2D(x%, y%) + 1) / 2.0 * 255.0);
  151.  
  152.     NEXT
  153. '------------------------------------------------------------------------------
  154.  
  155.  
  156. ' public static double noise(double xin, double yin) {
  157. FUNCTION noise2D (xin, yin)
  158.     ' double n0, n1, n2;' Noise contributions from the three corners
  159.     DIM n0, n1, n2
  160.  
  161.     '' Skew the input space to determine which simplex cell we're in
  162.     ' double s = (xin+yin)*F2;' Hairy factor for 2D
  163.     s = (xin + yin) * F2
  164.     ' int i = fastfloor(xin+s);
  165.     i% = INT(xin + s)
  166.     ' int j = fastfloor(yin+s);
  167.     j% = INT(yin + s)
  168.  
  169.  
  170.     ' double t = (i+j)*G2;
  171.     t = (i% + j%) * G2
  172.     ' double X0 = i-t;' Unskew the cell origin back to (x,y) space
  173.     BX0 = i% - t
  174.     ' double Y0 = j-t;
  175.     BY0 = j% - t
  176.     ' double x0 = xin-X0;' The x,y distances from the cell origin
  177.     x0 = xin - BX0
  178.     ' double y0 = yin-Y0;
  179.     y0 = yin - BY0
  180.  
  181.  
  182.     '' For the 2D case, the simplex shape is an equilateral triangle
  183.     '' Determine which simplex we are in.
  184.     ' int i1, j1;' Offsets for second (middle) corner of simplex in (i,j) coords
  185.     DIM AS INTEGER i1, j1
  186.     ' if(x0>y0) {i1=1; j1=0;}' lower triangle, XY order: (0,0)->(1,0)->(1,1)
  187.     IF x0 > y0 THEN
  188.         i1 = 1: j1 = 0
  189.         ' else {i1=0; j1=1;}' upper triangle, YX order: (0,0)->(0,1)->(1,1)
  190.     ELSE
  191.         i1 = 0: j1 = 1
  192.     END IF
  193.  
  194.  
  195.     '' A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  196.     '' a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  197.     '' c = (3-sqrt(3))/6 which is G2
  198.     '
  199.     ' double x1 = x0 - i1 + G2;' Offsets for middle corner in (x,y) unskewed coords
  200.     x1 = x0 - i1 + G2
  201.     ' double y1 = y0 - j1 + G2;
  202.     y1 = y0 - j1 + G2
  203.     ' double x2 = x0 - 1.0 + 2.0 * G2;' Offsets for last corner in (x,y) unskewed coords
  204.     x2 = x0 - 1 + 2 * G2
  205.     ' double y2 = y0 - 1.0 + 2.0 * G2;
  206.     y2 = y0 - 1 + 2 * G2
  207.  
  208.  
  209.     '' Work out the hashed gradient indices of the three simplex corners
  210.     ' int ii = i & 255;
  211.     ii% = i AND 255
  212.     ' int jj = j & 255;
  213.     jj% = j AND 255
  214.     ' int gi0 = perm[ii+perm[jj]] % 12;
  215.     gi0% = perm(ii + perm(jj + j1)) MOD 12
  216.     ' int gi1 = perm[ii+i1+perm[jj+j1]] % 12;
  217.     gi1% = perm(ii + i1 + perm(jj + j1)) MOD 12
  218.     ' int gi2 = perm[ii+1+perm[jj+1]] % 12;
  219.     gi2% = perm(ii + 1 + perm(jj + 1)) MOD 12
  220.  
  221.  
  222.     '' Calculate the contribution from the three corners
  223.     '
  224.     ' double t0 = 0.5 - x0*x0-y0*y0;
  225.     t0 = .5 - x0 * x0 - y0 * y0
  226.     ' if(t0<0) n0 = 0.0;
  227.     IF t0 < 0 THEN
  228.         n0 = 0
  229.         ' else {
  230.     ELSE
  231.         ' t0 *= t0;
  232.         t0 = t0 * t0
  233.         ' n0 = t0 * t0 * dot(grad3[gi0], x0, y0);' (x,y) of grad3 used for 2D gradient
  234.         n0 = t0 * t0 * DotP2(gi0%, x0, y0)
  235.         ' }
  236.     END IF
  237.  
  238.  
  239.     ' double t1 = 0.5 - x1*x1-y1*y1;
  240.     t1 = .5 - x1 * x1 - y1 * y1
  241.     ' if(t1<0) n1 = 0.0;
  242.     IF t1 < 0 THEN
  243.         n1 = 0
  244.         ' else {
  245.     ELSE
  246.         ' t1 *= t1;
  247.         t1 = t1 * t1
  248.         ' n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
  249.         n1 = t1 * t1 * DotP2(gi1%, x1, y1)
  250.         ' }
  251.     END IF
  252.  
  253.  
  254.     ' double t2 = 0.5 - x2*x2-y2*y2;
  255.     t2 = .5 - x2 * x2 - y2 * y2
  256.     ' if(t2<0) n2 = 0.0;
  257.     IF t2 < 0 THEN
  258.         n2 = 0
  259.         ' else {
  260.     ELSE
  261.         ' t2 *= t2;
  262.         t2 = t2 * t2
  263.         ' n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
  264.         n2 = t2 * t2 * DotP2(gi2%, x2, y2)
  265.         ' }
  266.     END IF
  267.  
  268.  
  269.     '' Add contributions from each corner to get the final noise value.
  270.     '' The result is scaled to return values in the interval [-1,1].
  271.     '
  272.     ' return 70.0 * (n0 + n1 + n2);
  273.     noise2D = 70 * (n0 + n1 + n2)
  274.  
  275.     ' }
  276.  
  277.  
  278.  
  279. ' This method is a *lot* faster than using (int)Math.floor(x)
  280. ' private static int fastfloor(double x) {
  281. ' return x>0 ? (int)x : (int)x-1;}
  282.  
  283. ' private static double dot(int g[], double x, double y) {
  284. ' return g[0]*x + g[1]*y; }
  285. FUNCTION DotP2 (g%, x, y)
  286.     DotP2 = grad3(g%).x * x + grad3(g%).y * y
  287.  
  288. ' Map function I found or translated from somewhere.
  289. ' The Coding Train" guy on youtube, where I translated the rain code from explained it in one of his videos.
  290. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  291.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  292.  
  293.  
  294. ' private static double dot(int g[], double x, double y, double z, double w) {
  295. ' private static double dot(int g[], double x, double y) {
  296. ' return g[0]*x + g[1]*y; }
  297.  
  298.  
  299. ' private static double dot(int g[], double x, double y, double z) {
  300. ' return g[0]*x + g[1]*y + g[2]*z; }
  301.  
  302.  
  303. ' private static double dot(int g[], double x, double y, double z, double w) {
  304. ' return g[0]*x + g[1]*y + g[2]*z + g[3]*w; }
  305. ' return g[0]*x + g[1]*y + g[2]*z + g[3]*w; }
  306.  
  307.  
  308.  
  309. ' ******* NOTE: 3D portion below not ported yet ~Phlashlite *******
  310.  
  311.  
  312.  
  313. '' 3D simplex noise
  314. ' public static double noise(double xin, double yin, double zin) {
  315. ' double n0, n1, n2, n3;' Noise contributions from the four corners
  316.  
  317.  
  318. '' Skew the input space to determine which simplex cell we're in
  319. ' final double F3 = 1.0/3.0;
  320. ' double s = (xin+yin+zin)*F3;' Very nice and simple skew factor for 3D
  321. ' int i = fastfloor(xin+s);
  322. ' int j = fastfloor(yin+s);
  323. ' int k = fastfloor(zin+s);
  324. ' final double G3 = 1.0/6.0;' Very nice and simple unskew factor, too
  325. ' double t = (i+j+k)*G3;
  326. ' double X0 = i-t;' Unskew the cell origin back to (x,y,z) space
  327. ' double Y0 = j-t;
  328. ' double Z0 = k-t;
  329. ' double x0 = xin-X0;' The x,y,z distances from the cell origin
  330. ' double y0 = yin-Y0;
  331. ' double z0 = zin-Z0;
  332.  
  333.  
  334. '' For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  335. '' Determine which simplex we are in.
  336. ' int i1, j1, k1;' Offsets for second corner of simplex in (i,j,k) coords
  337. ' int i2, j2, k2;' Offsets for third corner of simplex in (i,j,k) coords
  338. ' if(x0>=y0) {
  339. ' if(y0>=z0)
  340. ' { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; }' X Y Z order
  341. ' else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; }' X Z Y order
  342. ' else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; }' Z X Y order
  343. ' }
  344. ' else {' x0<y0
  345. ' if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; }' Z Y X order
  346. ' else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; }' Y Z X order
  347. ' else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; }' Y X Z order
  348. ' }
  349.  
  350.  
  351. '' A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  352. '' a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  353. '' a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  354. '' c = 1/6.
  355. ' double x1 = x0 - i1 + G3;' Offsets for second corner in (x,y,z) coords
  356. ' double y1 = y0 - j1 + G3;
  357. ' double z1 = z0 - k1 + G3;
  358. ' double x2 = x0 - i2 + 2.0*G3;' Offsets for third corner in (x,y,z) coords
  359. ' double y2 = y0 - j2 + 2.0*G3;
  360. ' double z2 = z0 - k2 + 2.0*G3;
  361. ' double x3 = x0 - 1.0 + 3.0*G3;' Offsets for last corner in (x,y,z) coords
  362. ' double y3 = y0 - 1.0 + 3.0*G3;
  363. ' double z3 = z0 - 1.0 + 3.0*G3;
  364.  
  365.  
  366. '' Work out the hashed gradient indices of the four simplex corners
  367. ' int ii = i & 255;
  368. ' int jj = j & 255;
  369. ' int kk = k & 255;
  370. ' int gi0 = perm[ii+perm[jj+perm[kk]]] % 12;
  371. ' int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1]]] % 12;
  372. ' int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2]]] % 12;
  373. ' int gi3 = perm[ii+1+perm[jj+1+perm[kk+1]]] % 12;
  374.  
  375.  
  376. '' Calculate the contribution from the four corners
  377. ' double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0;
  378. ' if(t0<0) n0 = 0.0;
  379. ' else {
  380. ' t0 *= t0;
  381. ' n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
  382. ' }
  383. ' double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1;
  384. ' if(t1<0) n1 = 0.0;
  385. ' else {
  386. ' t1 *= t1;
  387. ' n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
  388. ' }
  389. ' double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2;
  390. ' if(t2<0) n2 = 0.0;
  391. ' else {
  392. ' t2 *= t2;
  393. ' n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
  394. ' }
  395. ' double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3;
  396. ' if(t3<0) n3 = 0.0;
  397. ' else {
  398. ' t3 *= t3;
  399. ' n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
  400. ' }
  401.  
  402.  
  403. '' Add contributions from each corner to get the final noise value.
  404. '' The result is scaled to stay just inside [-1,1]
  405. ' return 32.0*(n0 + n1 + n2 + n3);
  406. ' }
  407.  
  408.  
  409. '' 4D simplex noise
  410. ' double noise(double x, double y, double z, double w) {
  411.  
  412. '' The skewing and unskewing factors are hairy again for the 4D case
  413. ' final double F4 = (Math.sqrt(5.0)-1.0)/4.0;
  414. ' final double G4 = (5.0-Math.sqrt(5.0))/20.0;
  415. ' double n0, n1, n2, n3, n4;' Noise contributions from the five corners
  416.  
  417.  
  418. '' Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
  419. '  double s = (x + y + z + w) * F4;' Factor for 4D skewing
  420. ' int i = fastfloor(x + s);
  421. ' int j = fastfloor(y + s);
  422. ' int k = fastfloor(z + s);
  423. ' int l = fastfloor(w + s);
  424. ' double t = (i + j + k + l) * G4;' Factor for 4D unskewing
  425. ' double X0 = i - t;' Unskew the cell origin back to (x,y,z,w) space
  426. ' double Y0 = j - t;
  427. ' double Z0 = k - t;
  428. ' double W0 = l - t;
  429. ' double x0 = x - X0;' The x,y,z,w distances from the cell origin
  430. ' double y0 = y - Y0;
  431. ' double z0 = z - Z0;
  432. ' double w0 = w - W0;
  433.  
  434.  
  435. '' For the 4D case, the simplex is a 4D shape I won't even try to describe.
  436. '' To find out which of the 24 possible simplices we're in, we need to
  437. '' determine the magnitude ordering of x0, y0, z0 and w0.
  438. '' The method below is a good way of finding the ordering of x,y,z,w and
  439. '' then find the correct traversal order for the simplex we’re in.
  440. '' First, six pair-wise comparisons are performed between each possible pair
  441. '' of the four coordinates, and the results are used to add up binary bits
  442. '' for an integer index.
  443. ' int c1 = (x0 > y0) ? 32 : 0;
  444. ' int c2 = (x0 > z0) ? 16 : 0;
  445. ' int c3 = (y0 > z0) ? 8 : 0;
  446. ' int c4 = (x0 > w0) ? 4 : 0;
  447. ' int c5 = (y0 > w0) ? 2 : 0;
  448. ' int c6 = (z0 > w0) ? 1 : 0;
  449. ' int c = c1 + c2 + c3 + c4 + c5 + c6;
  450. ' int i1, j1, k1, l1;' The integer offsets for the second simplex corner
  451. ' int i2, j2, k2, l2;' The integer offsets for the third simplex corner
  452. ' int i3, j3, k3, l3;' The integer offsets for the fourth simplex corner
  453.  
  454.  
  455. '' simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
  456. '' Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
  457. '' impossible. Only the 24 indices which have non-zero entries make any sense.
  458. '' We use a thresholding to set the coordinates in turn from the largest magnitude.
  459.  
  460.  
  461. '' The number 3 in the "simplex" array is at the position of the largest coordinate.
  462. ' i1 = simplex[c][0]>=3 ? 1 : 0;
  463. ' j1 = simplex[c][1]>=3 ? 1 : 0;
  464. ' k1 = simplex[c][2]>=3 ? 1 : 0;
  465. ' l1 = simplex[c][3]>=3 ? 1 : 0;
  466.  
  467.  
  468. '' The number 2 in the "simplex" array is at the second largest coordinate.
  469. ' i2 = simplex[c][0]>=2 ? 1 : 0;
  470. ' j2 = simplex[c][1]>=2 ? 1 : 0;
  471. ' k2 = simplex[c][2]>=2 ? 1 : 0;
  472. ' l2 = simplex[c][3]>=2 ? 1 : 0;
  473.  
  474.  
  475. '' The number 1 in the "simplex" array is at the second smallest coordinate.
  476. ' i3 = simplex[c][0]>=1 ? 1 : 0;
  477. ' j3 = simplex[c][1]>=1 ? 1 : 0;
  478. ' k3 = simplex[c][2]>=1 ? 1 : 0;
  479. ' l3 = simplex[c][3]>=1 ? 1 : 0;
  480.  
  481.  
  482. '' The fifth corner has all coordinate offsets = 1, so no need to look that up.
  483. ' double x1 = x0 - i1 + G4;' Offsets for second corner in (x,y,z,w) coords
  484. ' double y1 = y0 - j1 + G4;
  485. ' double z1 = z0 - k1 + G4;
  486. ' double w1 = w0 - l1 + G4;
  487. ' double x2 = x0 - i2 + 2.0*G4;' Offsets for third corner in (x,y,z,w) coords
  488. ' double y2 = y0 - j2 + 2.0*G4;
  489. ' double z2 = z0 - k2 + 2.0*G4;
  490. ' double w2 = w0 - l2 + 2.0*G4;
  491. ' double x3 = x0 - i3 + 3.0*G4;' Offsets for fourth corner in (x,y,z,w) coords
  492. ' double y3 = y0 - j3 + 3.0*G4;
  493. ' double z3 = z0 - k3 + 3.0*G4;
  494. ' double w3 = w0 - l3 + 3.0*G4;
  495. ' double x4 = x0 - 1.0 + 4.0*G4;' Offsets for last corner in (x,y,z,w) coords
  496. ' double y4 = y0 - 1.0 + 4.0*G4;
  497. ' double z4 = z0 - 1.0 + 4.0*G4;
  498. ' double w4 = w0 - 1.0 + 4.0*G4;
  499.  
  500.  
  501. '' Work out the hashed gradient indices of the five simplex corners
  502. ' int ii = i & 255;
  503. ' int jj = j & 255;
  504. ' int kk = k & 255;
  505. ' int ll = l & 255;
  506. ' int gi0 = perm[ii+perm[jj+perm[kk+perm[ll]]]] % 32;
  507. ' int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]] % 32;
  508. ' int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]] % 32;
  509. ' int gi3 = perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]] % 32;
  510. ' int gi4 = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32;
  511.  
  512.  
  513. '' Calculate the contribution from the five corners
  514. ' double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0;
  515. ' if(t0<0) n0 = 0.0;
  516. ' else {
  517. ' t0 *= t0;
  518. ' n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
  519. ' }
  520. ' double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1;
  521. ' if(t1<0) n1 = 0.0;
  522. ' else {
  523. ' t1 *= t1;
  524. ' n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
  525. ' }
  526. ' double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2;
  527. ' if(t2<0) n2 = 0.0;
  528. ' else {
  529. ' t2 *= t2;
  530. ' n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
  531. ' }
  532. ' double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3;
  533. ' if(t3<0) n3 = 0.0;
  534. ' else {
  535. ' t3 *= t3;
  536. ' n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
  537. ' }
  538. ' double t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4;
  539. ' if(t4<0) n4 = 0.0;
  540. ' else {
  541. ' t4 *= t4;
  542. ' n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
  543. ' }
  544.  
  545.  
  546. '' Sum up and scale the result to cover the range [-1,1]
  547. ' return 27.0 * (n0 + n1 + n2 + n3 + n4);
  548.  
  549. 'private static int grad4[][]= {{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1},
  550. '{0,-1,1,1}, {0,-1,1,-1}, {0,-1,-1,1}, {0,-1,-1,-1},
  551. '{1,0,1,1}, {1,0,1,-1}, {1,0,-1,1}, {1,0,-1,-1},
  552. '{-1,0,1,1}, {-1,0,1,-1}, {-1,0,-1,1}, {-1,0,-1,-1},
  553. '{1,1,0,1}, {1,1,0,-1}, {1,-1,0,1}, {1,-1,0,-1},
  554. '{-1,1,0,1}, {-1,1,0,-1}, {-1,-1,0,1}, {-1,-1,0,-1},
  555. '{1,1,1,0}, {1,1,-1,0}, {1,-1,1,0}, {1,-1,-1,0},
  556. '{-1,1,1,0}, {-1,1,-1,0}, {-1,-1,1,0}, {-1,-1,-1,0}};
  557.  
  558. '' A lookup table to traverse the simplex around a given point in 4D.
  559. '' Details can be found where this table is used, in the 4D noise method.
  560. ' private static int simplex[][] = {
  561. ' {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},
  562. ' {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},
  563. ' {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},
  564. ' {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},
  565. ' {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},
  566. ' {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},
  567. ' {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},
  568. ' {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}};
  569.  
  570.  
  571. '}
  572. '}
  573.  
  574.  
  575.  

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Simplex Noise Anyone?
« Reply #1 on: February 25, 2022, 12:59:08 pm »
Okay... So, I have found some obvious stuff... and it is looking closer.

Code: QB64: [Select]
  1. '$DEBUG
  2. _TITLE "Simplex noise port"
  3.  
  4. ' ported to QB64 by: Phlashlite, February 20, 2022
  5. '    from the paper: Simplex noise demystified
  6. '            author: Stefan Gustavson, Link%u201Dping University, Sweden (stegu@itn.liu.se), 2005-03-22
  7. '               URL: https://weber.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  8. ' - Simplex noise has a lower computational complexity and requires fewer multiplications.
  9. ' - Simplex noise scales to higher dimensions (4D, 5D and up) with much less computational
  10. '   cost, the complexity is for dimensions instead of the of classic Noise.
  11. ' - Simplex noise has no noticeable directional artifacts.
  12. ' - Simplex noise has a well-defined and continuous gradient everywhere that can be computed
  13. '   quite cheaply.
  14. ' - Simplex noise is easy to implement in hardware
  15.  
  16.  
  17. SCREEN _NEWIMAGE(800, 600, 32)
  18.  
  19. DEFDBL A-Z
  20.  
  21. 'private static int grad3[][] = {{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}};
  22. g3:
  23. DATA 1,1,0
  24. DATA -1,1,0
  25. DATA 1,-1,0
  26. DATA -1,-1,0
  27. DATA 1,0,1
  28. DATA -1,0,1
  29. DATA 1,0,-1
  30. DATA -1,0,-1
  31. DATA 0,1,1
  32. DATA 0,-1,1
  33. DATA 0,1,-1
  34. DATA 0,-1,-1
  35.  
  36. TYPE TriBit
  37.     x AS INTEGER
  38.     y AS INTEGER
  39.     z AS INTEGER
  40. DIM SHARED grad3(12) AS TriBit
  41.  
  42. FOR g% = 0 TO 11
  43.  
  44.     READ grad3(g%).x
  45.     READ grad3(g%).y
  46.     READ grad3(g%).z
  47.  
  48.     'PRINT grad3(g%).x, grad3(g%).y, grad3(g%).z
  49.  
  50. NEXT g%
  51.  
  52.  
  53. 'private static int p[] = {151,160,137,91,90,15,
  54. '131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  55. '190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  56. '88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  57. '77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  58. '102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,196,
  59. '135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,250,124,123,
  60. '5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  61. '223,183,170,213,119,248,152,2,44,154,163,70,221,153,101,155,167,43,172,9,
  62. '129,22,39,253,19,98,108,110,79,113,224,232,178,185,112,104,218,246,97,228,
  63. '251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249,14,239,107,
  64. '49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,
  65. '138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
  66. p:
  67. DATA 151,160,137,091,090,015,131,013,201,095
  68. DATA 096,053,194,233,007,225,140,036,103,030
  69. DATA 069,142,008,099,037,240,021,010,023,190
  70. DATA 006,148,247,120,234,075,000,026,197,062
  71. DATA 094,252,219,203,117,035,011,032,057,177
  72. DATA 033,088,237,149,056,087,174,020,125,136
  73. DATA 171,168,068,175,074,165,071,134,139,048
  74. DATA 027,166,077,146,158,231,083,111,229,122
  75. DATA 060,211,133,230,220,105,092,041,055,046
  76. DATA 245,040,244,102,143,054,065,025,063,161
  77. DATA 001,216,080,073,209,076,132,187,208,089
  78. DATA 018,169,200,196,135,130,116,188,159,086
  79. DATA 164,100,109,198,173,186,003,064,052,217
  80. DATA 226,250,124,123,005,202,038,147,118,126
  81. DATA 255,082,085,212,207,206,059,227,047,016
  82. DATA 058,017,182,189,028,042,223,183,170,213
  83. DATA 119,248,152,002,044,154,163,070,221,153
  84. DATA 101,155,167,043,172,009,129,022,039,253
  85. DATA 019,098,108,110,079,113,224,232,178,185
  86. DATA 112,104,218,246,097,228,251,034,242,193
  87. DATA 238,210,144,012,191,179,162,241,081,051
  88. DATA 145,235,249,014,239,107,049,192,214,031
  89. DATA 181,199,106,157,184,084,204,176,115,121
  90. DATA 050,045,127,004,150,254,138,236,205,093
  91. DATA 222,114,067,029,024,072,243,141,128,195
  92. DATA 078,066,215,061,156,180
  93. DATA 151,160,137,091,090,015,131,013,201,095
  94. DATA 096,053,194,233,007,225,140,036,103,030
  95. DATA 069,142,008,099,037,240,021,010,023,190
  96. DATA 006,148,247,120,234,075,000,026,197,062
  97. DATA 094,252,219,203,117,035,011,032,057,177
  98. DATA 033,088,237,149,056,087,174,020,125,136
  99. DATA 171,168,068,175,074,165,071,134,139,048
  100. DATA 027,166,077,146,158,231,083,111,229,122
  101. DATA 060,211,133,230,220,105,092,041,055,046
  102. DATA 245,040,244,102,143,054,065,025,063,161
  103. DATA 001,216,080,073,209,076,132,187,208,089
  104. DATA 018,169,200,196,135,130,116,188,159,086
  105. DATA 164,100,109,198,173,186,003,064,052,217
  106. DATA 226,250,124,123,005,202,038,147,118,126
  107. DATA 255,082,085,212,207,206,059,227,047,016
  108. DATA 058,017,182,189,028,042,223,183,170,213
  109. DATA 119,248,152,002,044,154,163,070,221,153
  110. DATA 101,155,167,043,172,009,129,022,039,253
  111. DATA 019,098,108,110,079,113,224,232,178,185
  112. DATA 112,104,218,246,097,228,251,034,242,193
  113. DATA 238,210,144,012,191,179,162,241,081,051
  114. DATA 145,235,249,014,239,107,049,192,214,031
  115. DATA 181,199,106,157,184,084,204,176,115,121
  116. DATA 050,045,127,004,150,254,138,236,205,093
  117. DATA 222,114,067,029,024,072,243,141,128,195
  118. DATA 078,066,215,061,156,180
  119.  
  120.  
  121. 'To remove the need for index wrapping, double the permutation table length
  122. 'private static int perm[] = new int[512];
  123. DIM SHARED perm(512) AS INTEGER
  124.  
  125. 'static { for(int i=0; i<512; i++) perm[i]=p[i & 255]; }
  126. FOR i% = 0 TO 511
  127.     READ p(i%)
  128.     perm(i%) = p(i%) AND 255
  129.  
  130.     'PRINT perm(i%)
  131.  
  132.  
  133. '' 2D simplex noise************************************************************
  134.  
  135. ' final double F2 = 0.5*(Math.sqrt(3.0)-1.0);
  136. CONST F2 = .5 * (SQR(3) - 1)
  137. ' final double G2 = (3.0-Math.sqrt(3.0))/6.0;
  138. CONST G2 = (3 - SQR(3)) / 6
  139.  
  140. scale = .01
  141. '------------------------------------------------------------------------------
  142. FOR x% = 0 TO 800
  143.     FOR y% = 0 TO 600
  144.  
  145.         gs% = map!(noise2D(scale * x%, scale * y%), -1, 1, 0, 255)
  146.         c& = _RGB(gs%, gs%, gs%)
  147.         PSET (x%, y%), c&
  148.  
  149.         'PRINT INT(map!(noise2D(x%, y%), -1, 1, 0, 255));
  150.         'PRINT INT((noise2D(x%, y%) + 1) / 2.0 * 255.0);
  151.  
  152.     NEXT
  153. '------------------------------------------------------------------------------
  154.  
  155.  
  156. ' public static double noise(double xin, double yin) {
  157. FUNCTION noise2D (xin, yin)
  158.     ' double n0, n1, n2;' Noise contributions from the three corners
  159.     DIM n0, n1, n2
  160.  
  161.     '' Skew the input space to determine which simplex cell we're in
  162.     ' double s = (xin+yin)*F2;' Hairy factor for 2D
  163.     s = (xin + yin) * F2
  164.     ' int i = fastfloor(xin+s);
  165.     i% = INT(xin + s)
  166.     ' int j = fastfloor(yin+s);
  167.     j% = INT(yin + s)
  168.  
  169.  
  170.     ' double t = (i+j)*G2;
  171.     t = (i% + j%) * G2
  172.     ' double X0 = i-t;' Unskew the cell origin back to (x,y) space
  173.     BX0 = i% - t
  174.     ' double Y0 = j-t;
  175.     BY0 = j% - t
  176.     ' double x0 = xin-X0;' The x,y distances from the cell origin
  177.     x0 = xin - BX0
  178.     ' double y0 = yin-Y0;
  179.     y0 = yin - BY0
  180.  
  181.  
  182.     '' For the 2D case, the simplex shape is an equilateral triangle
  183.     '' Determine which simplex we are in.
  184.     ' int i1, j1;' Offsets for second (middle) corner of simplex in (i,j) coords
  185.     DIM AS INTEGER i1, j1
  186.     ' if(x0>y0) {i1=1; j1=0;}' lower triangle, XY order: (0,0)->(1,0)->(1,1)
  187.     IF x0 > y0 THEN
  188.         i1 = 1: j1 = 0
  189.         ' else {i1=0; j1=1;}' upper triangle, YX order: (0,0)->(0,1)->(1,1)
  190.     ELSE
  191.         i1 = 0: j1 = 1
  192.     END IF
  193.  
  194.  
  195.     '' A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  196.     '' a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  197.     '' c = (3-sqrt(3))/6 which is G2
  198.     '
  199.     ' double x1 = x0 - i1 + G2;' Offsets for middle corner in (x,y) unskewed coords
  200.     x1 = x0 - i1 + G2
  201.     ' double y1 = y0 - j1 + G2;
  202.     y1 = y0 - j1 + G2
  203.     ' double x2 = x0 - 1.0 + 2.0 * G2;' Offsets for last corner in (x,y) unskewed coords
  204.     x2 = x0 - 1 + 2 * G2
  205.     ' double y2 = y0 - 1.0 + 2.0 * G2;
  206.     y2 = y0 - 1 + 2 * G2
  207.  
  208.  
  209.     '' Work out the hashed gradient indices of the three simplex corners
  210.     ' int ii = i & 255;
  211.     ii% = i% AND 255
  212.     ' int jj = j & 255;
  213.     jj% = j% AND 255
  214.     ' int gi0 = perm[ii+perm[jj]] % 12;
  215.     gi0% = perm(ii% + perm(jj% + j1)) MOD 12
  216.     ' int gi1 = perm[ii+i1+perm[jj+j1]] % 12;
  217.     gi1% = perm(ii% + i1% + perm(jj% + j1)) MOD 12
  218.     ' int gi2 = perm[ii+1+perm[jj+1]] % 12;
  219.     gi2% = perm(ii% + 1 + perm(jj% + 1)) MOD 12
  220.  
  221.  
  222.     '' Calculate the contribution from the three corners
  223.     '
  224.     ' double t0 = 0.5 - x0*x0-y0*y0;
  225.     t0 = .5 - x0 * x0 - y0 * y0
  226.     ' if(t0<0) n0 = 0.0;
  227.     IF t0 < 0 THEN
  228.         n0 = 0
  229.         ' else {
  230.     ELSE
  231.         ' t0 *= t0;
  232.         t0 = t0 * t0
  233.         ' n0 = t0 * t0 * dot(grad3[gi0], x0, y0);' (x,y) of grad3 used for 2D gradient
  234.         n0 = t0 * t0 * DotP2(gi0%, x0, y0)
  235.         ' }
  236.     END IF
  237.  
  238.  
  239.     ' double t1 = 0.5 - x1*x1-y1*y1;
  240.     t1 = .5 - x1 * x1 - y1 * y1
  241.     ' if(t1<0) n1 = 0.0;
  242.     IF t1 < 0 THEN
  243.         n1 = 0
  244.         ' else {
  245.     ELSE
  246.         ' t1 *= t1;
  247.         t1 = t1 * t1
  248.         ' n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
  249.         n1 = t1 * t1 * DotP2(gi1%, x1, y1)
  250.         ' }
  251.     END IF
  252.  
  253.  
  254.     ' double t2 = 0.5 - x2*x2-y2*y2;
  255.     t2 = .5 - x2 * x2 - y2 * y2
  256.     ' if(t2<0) n2 = 0.0;
  257.     IF t2 < 0 THEN
  258.         n2 = 0
  259.         ' else {
  260.     ELSE
  261.         ' t2 *= t2;
  262.         t2 = t2 * t2
  263.         ' n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
  264.         n2 = t2 * t2 * DotP2(gi2%, x2, y2)
  265.         ' }
  266.     END IF
  267.  
  268.  
  269.     '' Add contributions from each corner to get the final noise value.
  270.     '' The result is scaled to return values in the interval [-1,1].
  271.     '
  272.     ' return 70.0 * (n0 + n1 + n2);
  273.     noise2D = 70 * (n0 + n1 + n2)
  274.  
  275.     ' }
  276.  
  277.  
  278.  
  279. ' This method is a *lot* faster than using (int)Math.floor(x)
  280. ' private static int fastfloor(double x) {
  281. ' return x>0 ? (int)x : (int)x-1;}
  282.  
  283. ' private static double dot(int g[], double x, double y) {
  284. ' return g[0]*x + g[1]*y; }
  285. FUNCTION DotP2 (g%, x, y)
  286.     DotP2 = grad3(g%).x * x + grad3(g%).y * y
  287.  
  288. ' Map function I found or translated from somewhere.
  289. ' The Coding Train" guy on youtube, where I translated the rain code from explained it in one of his videos.
  290. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  291.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  292.  
  293.  
  294. ' private static double dot(int g[], double x, double y, double z, double w) {
  295. ' private static double dot(int g[], double x, double y) {
  296. ' return g[0]*x + g[1]*y; }
  297.  
  298.  
  299. ' private static double dot(int g[], double x, double y, double z) {
  300. ' return g[0]*x + g[1]*y + g[2]*z; }
  301.  
  302.  
  303. ' private static double dot(int g[], double x, double y, double z, double w) {
  304. ' return g[0]*x + g[1]*y + g[2]*z + g[3]*w; }
  305. ' return g[0]*x + g[1]*y + g[2]*z + g[3]*w; }
  306.  
  307.  
  308.  
  309. ' ******* NOTE: 3D portion below not ported yet ~Phlashlite *******
  310.  
  311.  
  312.  
  313. '' 3D simplex noise
  314. ' public static double noise(double xin, double yin, double zin) {
  315. ' double n0, n1, n2, n3;' Noise contributions from the four corners
  316.  
  317.  
  318. '' Skew the input space to determine which simplex cell we're in
  319. ' final double F3 = 1.0/3.0;
  320. ' double s = (xin+yin+zin)*F3;' Very nice and simple skew factor for 3D
  321. ' int i = fastfloor(xin+s);
  322. ' int j = fastfloor(yin+s);
  323. ' int k = fastfloor(zin+s);
  324. ' final double G3 = 1.0/6.0;' Very nice and simple unskew factor, too
  325. ' double t = (i+j+k)*G3;
  326. ' double X0 = i-t;' Unskew the cell origin back to (x,y,z) space
  327. ' double Y0 = j-t;
  328. ' double Z0 = k-t;
  329. ' double x0 = xin-X0;' The x,y,z distances from the cell origin
  330. ' double y0 = yin-Y0;
  331. ' double z0 = zin-Z0;
  332.  
  333.  
  334. '' For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  335. '' Determine which simplex we are in.
  336. ' int i1, j1, k1;' Offsets for second corner of simplex in (i,j,k) coords
  337. ' int i2, j2, k2;' Offsets for third corner of simplex in (i,j,k) coords
  338. ' if(x0>=y0) {
  339. ' if(y0>=z0)
  340. ' { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; }' X Y Z order
  341. ' else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; }' X Z Y order
  342. ' else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; }' Z X Y order
  343. ' }
  344. ' else {' x0<y0
  345. ' if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; }' Z Y X order
  346. ' else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; }' Y Z X order
  347. ' else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; }' Y X Z order
  348. ' }
  349.  
  350.  
  351. '' A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  352. '' a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  353. '' a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  354. '' c = 1/6.
  355. ' double x1 = x0 - i1 + G3;' Offsets for second corner in (x,y,z) coords
  356. ' double y1 = y0 - j1 + G3;
  357. ' double z1 = z0 - k1 + G3;
  358. ' double x2 = x0 - i2 + 2.0*G3;' Offsets for third corner in (x,y,z) coords
  359. ' double y2 = y0 - j2 + 2.0*G3;
  360. ' double z2 = z0 - k2 + 2.0*G3;
  361. ' double x3 = x0 - 1.0 + 3.0*G3;' Offsets for last corner in (x,y,z) coords
  362. ' double y3 = y0 - 1.0 + 3.0*G3;
  363. ' double z3 = z0 - 1.0 + 3.0*G3;
  364.  
  365.  
  366. '' Work out the hashed gradient indices of the four simplex corners
  367. ' int ii = i & 255;
  368. ' int jj = j & 255;
  369. ' int kk = k & 255;
  370. ' int gi0 = perm[ii+perm[jj+perm[kk]]] % 12;
  371. ' int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1]]] % 12;
  372. ' int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2]]] % 12;
  373. ' int gi3 = perm[ii+1+perm[jj+1+perm[kk+1]]] % 12;
  374.  
  375.  
  376. '' Calculate the contribution from the four corners
  377. ' double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0;
  378. ' if(t0<0) n0 = 0.0;
  379. ' else {
  380. ' t0 *= t0;
  381. ' n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
  382. ' }
  383. ' double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1;
  384. ' if(t1<0) n1 = 0.0;
  385. ' else {
  386. ' t1 *= t1;
  387. ' n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
  388. ' }
  389. ' double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2;
  390. ' if(t2<0) n2 = 0.0;
  391. ' else {
  392. ' t2 *= t2;
  393. ' n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
  394. ' }
  395. ' double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3;
  396. ' if(t3<0) n3 = 0.0;
  397. ' else {
  398. ' t3 *= t3;
  399. ' n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
  400. ' }
  401.  
  402.  
  403. '' Add contributions from each corner to get the final noise value.
  404. '' The result is scaled to stay just inside [-1,1]
  405. ' return 32.0*(n0 + n1 + n2 + n3);
  406. ' }
  407.  
  408.  
  409. '' 4D simplex noise
  410. ' double noise(double x, double y, double z, double w) {
  411.  
  412. '' The skewing and unskewing factors are hairy again for the 4D case
  413. ' final double F4 = (Math.sqrt(5.0)-1.0)/4.0;
  414. ' final double G4 = (5.0-Math.sqrt(5.0))/20.0;
  415. ' double n0, n1, n2, n3, n4;' Noise contributions from the five corners
  416.  
  417.  
  418. '' Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
  419. '  double s = (x + y + z + w) * F4;' Factor for 4D skewing
  420. ' int i = fastfloor(x + s);
  421. ' int j = fastfloor(y + s);
  422. ' int k = fastfloor(z + s);
  423. ' int l = fastfloor(w + s);
  424. ' double t = (i + j + k + l) * G4;' Factor for 4D unskewing
  425. ' double X0 = i - t;' Unskew the cell origin back to (x,y,z,w) space
  426. ' double Y0 = j - t;
  427. ' double Z0 = k - t;
  428. ' double W0 = l - t;
  429. ' double x0 = x - X0;' The x,y,z,w distances from the cell origin
  430. ' double y0 = y - Y0;
  431. ' double z0 = z - Z0;
  432. ' double w0 = w - W0;
  433.  
  434.  
  435. '' For the 4D case, the simplex is a 4D shape I won't even try to describe.
  436. '' To find out which of the 24 possible simplices we're in, we need to
  437. '' determine the magnitude ordering of x0, y0, z0 and w0.
  438. '' The method below is a good way of finding the ordering of x,y,z,w and
  439. '' then find the correct traversal order for the simplex we%u2019re in.
  440. '' First, six pair-wise comparisons are performed between each possible pair
  441. '' of the four coordinates, and the results are used to add up binary bits
  442. '' for an integer index.
  443. ' int c1 = (x0 > y0) ? 32 : 0;
  444. ' int c2 = (x0 > z0) ? 16 : 0;
  445. ' int c3 = (y0 > z0) ? 8 : 0;
  446. ' int c4 = (x0 > w0) ? 4 : 0;
  447. ' int c5 = (y0 > w0) ? 2 : 0;
  448. ' int c6 = (z0 > w0) ? 1 : 0;
  449. ' int c = c1 + c2 + c3 + c4 + c5 + c6;
  450. ' int i1, j1, k1, l1;' The integer offsets for the second simplex corner
  451. ' int i2, j2, k2, l2;' The integer offsets for the third simplex corner
  452. ' int i3, j3, k3, l3;' The integer offsets for the fourth simplex corner
  453.  
  454.  
  455. '' simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
  456. '' Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
  457. '' impossible. Only the 24 indices which have non-zero entries make any sense.
  458. '' We use a thresholding to set the coordinates in turn from the largest magnitude.
  459.  
  460.  
  461. '' The number 3 in the "simplex" array is at the position of the largest coordinate.
  462. ' i1 = simplex[c][0]>=3 ? 1 : 0;
  463. ' j1 = simplex[c][1]>=3 ? 1 : 0;
  464. ' k1 = simplex[c][2]>=3 ? 1 : 0;
  465. ' l1 = simplex[c][3]>=3 ? 1 : 0;
  466.  
  467.  
  468. '' The number 2 in the "simplex" array is at the second largest coordinate.
  469. ' i2 = simplex[c][0]>=2 ? 1 : 0;
  470. ' j2 = simplex[c][1]>=2 ? 1 : 0;
  471. ' k2 = simplex[c][2]>=2 ? 1 : 0;
  472. ' l2 = simplex[c][3]>=2 ? 1 : 0;
  473.  
  474.  
  475. '' The number 1 in the "simplex" array is at the second smallest coordinate.
  476. ' i3 = simplex[c][0]>=1 ? 1 : 0;
  477. ' j3 = simplex[c][1]>=1 ? 1 : 0;
  478. ' k3 = simplex[c][2]>=1 ? 1 : 0;
  479. ' l3 = simplex[c][3]>=1 ? 1 : 0;
  480.  
  481.  
  482. '' The fifth corner has all coordinate offsets = 1, so no need to look that up.
  483. ' double x1 = x0 - i1 + G4;' Offsets for second corner in (x,y,z,w) coords
  484. ' double y1 = y0 - j1 + G4;
  485. ' double z1 = z0 - k1 + G4;
  486. ' double w1 = w0 - l1 + G4;
  487. ' double x2 = x0 - i2 + 2.0*G4;' Offsets for third corner in (x,y,z,w) coords
  488. ' double y2 = y0 - j2 + 2.0*G4;
  489. ' double z2 = z0 - k2 + 2.0*G4;
  490. ' double w2 = w0 - l2 + 2.0*G4;
  491. ' double x3 = x0 - i3 + 3.0*G4;' Offsets for fourth corner in (x,y,z,w) coords
  492. ' double y3 = y0 - j3 + 3.0*G4;
  493. ' double z3 = z0 - k3 + 3.0*G4;
  494. ' double w3 = w0 - l3 + 3.0*G4;
  495. ' double x4 = x0 - 1.0 + 4.0*G4;' Offsets for last corner in (x,y,z,w) coords
  496. ' double y4 = y0 - 1.0 + 4.0*G4;
  497. ' double z4 = z0 - 1.0 + 4.0*G4;
  498. ' double w4 = w0 - 1.0 + 4.0*G4;
  499.  
  500.  
  501. '' Work out the hashed gradient indices of the five simplex corners
  502. ' int ii = i & 255;
  503. ' int jj = j & 255;
  504. ' int kk = k & 255;
  505. ' int ll = l & 255;
  506. ' int gi0 = perm[ii+perm[jj+perm[kk+perm[ll]]]] % 32;
  507. ' int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]] % 32;
  508. ' int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]] % 32;
  509. ' int gi3 = perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]] % 32;
  510. ' int gi4 = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32;
  511.  
  512.  
  513. '' Calculate the contribution from the five corners
  514. ' double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0;
  515. ' if(t0<0) n0 = 0.0;
  516. ' else {
  517. ' t0 *= t0;
  518. ' n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
  519. ' }
  520. ' double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1;
  521. ' if(t1<0) n1 = 0.0;
  522. ' else {
  523. ' t1 *= t1;
  524. ' n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
  525. ' }
  526. ' double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2;
  527. ' if(t2<0) n2 = 0.0;
  528. ' else {
  529. ' t2 *= t2;
  530. ' n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
  531. ' }
  532. ' double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3;
  533. ' if(t3<0) n3 = 0.0;
  534. ' else {
  535. ' t3 *= t3;
  536. ' n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
  537. ' }
  538. ' double t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4;
  539. ' if(t4<0) n4 = 0.0;
  540. ' else {
  541. ' t4 *= t4;
  542. ' n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
  543. ' }
  544.  
  545.  
  546. '' Sum up and scale the result to cover the range [-1,1]
  547. ' return 27.0 * (n0 + n1 + n2 + n3 + n4);
  548.  
  549. 'private static int grad4[][]= {{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1},
  550. '{0,-1,1,1}, {0,-1,1,-1}, {0,-1,-1,1}, {0,-1,-1,-1},
  551. '{1,0,1,1}, {1,0,1,-1}, {1,0,-1,1}, {1,0,-1,-1},
  552. '{-1,0,1,1}, {-1,0,1,-1}, {-1,0,-1,1}, {-1,0,-1,-1},
  553. '{1,1,0,1}, {1,1,0,-1}, {1,-1,0,1}, {1,-1,0,-1},
  554. '{-1,1,0,1}, {-1,1,0,-1}, {-1,-1,0,1}, {-1,-1,0,-1},
  555. '{1,1,1,0}, {1,1,-1,0}, {1,-1,1,0}, {1,-1,-1,0},
  556. '{-1,1,1,0}, {-1,1,-1,0}, {-1,-1,1,0}, {-1,-1,-1,0}};
  557.  
  558. '' A lookup table to traverse the simplex around a given point in 4D.
  559. '' Details can be found where this table is used, in the 4D noise method.
  560. ' private static int simplex[][] = {
  561. ' {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},
  562. ' {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},
  563. ' {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},
  564. ' {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},
  565. ' {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},
  566. ' {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},
  567. ' {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},
  568. ' {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}};
  569.  
  570.  
  571. '}
  572. '}
  573.  
  574.  
  575.  

I missed some integer assignments here:

"  '' Work out the hashed gradient indices of the three simplex corners
    ' int ii = i & 255;
    ii% = i AND 255
    ' int jj = j & 255;
    jj% = j AND 255
    ' int gi0 = perm[ii+perm[jj]] % 12;
    gi0% = perm(ii + perm(jj + j1)) MOD 12
    ' int gi1 = perm[ii+i1+perm[jj+j1]] % 12;
    gi1% = perm(ii + i1 + perm(jj + j1)) MOD 12
    ' int gi2 = perm[ii+1+perm[jj+1]] % 12;
    gi2% = perm(ii + 1 + perm(jj + 1)) MOD 12"
« Last Edit: February 25, 2022, 01:21:12 pm by Phlashlite »

Marked as best answer by Phlashlite on February 25, 2022, 08:34:44 am

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Simplex Noise Anyone? SOLVED!
« Reply #2 on: February 25, 2022, 01:26:10 pm »
I found it!

Copy paste got me!!!!

This code works now!!!

Code: QB64: [Select]
  1.   '$DEBUG
  2. _TITLE "Simplex noise port"
  3.  
  4. ' ported to QB64 by: Phlashlite, February 20, 2022
  5. '    from the paper: Simplex noise demystified
  6. '            author: Stefan Gustavson, Link%u201Dping University, Sweden (stegu@itn.liu.se), 2005-03-22
  7. '               URL: https://weber.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  8. ' - Simplex noise has a lower computational complexity and requires fewer multiplications.
  9. ' - Simplex noise scales to higher dimensions (4D, 5D and up) with much less computational
  10. '   cost, the complexity is for dimensions instead of the of classic Noise.
  11. ' - Simplex noise has no noticeable directional artifacts.
  12. ' - Simplex noise has a well-defined and continuous gradient everywhere that can be computed
  13. '   quite cheaply.
  14. ' - Simplex noise is easy to implement in hardware
  15.  
  16.  
  17. SCREEN _NEWIMAGE(800, 600, 32)
  18.  
  19. DEFDBL A-Z
  20.  
  21. 'private static int grad3[][] = {{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}};
  22. g3:
  23. DATA 1,1,0
  24. DATA -1,1,0
  25. DATA 1,-1,0
  26. DATA -1,-1,0
  27. DATA 1,0,1
  28. DATA -1,0,1
  29. DATA 1,0,-1
  30. DATA -1,0,-1
  31. DATA 0,1,1
  32. DATA 0,-1,1
  33. DATA 0,1,-1
  34. DATA 0,-1,-1
  35.  
  36. TYPE TriBit
  37.     x AS INTEGER
  38.     y AS INTEGER
  39.     z AS INTEGER
  40. DIM SHARED grad3(12) AS TriBit
  41.  
  42. FOR g% = 0 TO 11
  43.  
  44.     READ grad3(g%).x
  45.     READ grad3(g%).y
  46.     READ grad3(g%).z
  47.  
  48.     'PRINT grad3(g%).x, grad3(g%).y, grad3(g%).z
  49.  
  50. NEXT g%
  51.  
  52.  
  53. 'private static int p[] = {151,160,137,91,90,15,
  54. '131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  55. '190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  56. '88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  57. '77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  58. '102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,196,
  59. '135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,250,124,123,
  60. '5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  61. '223,183,170,213,119,248,152,2,44,154,163,70,221,153,101,155,167,43,172,9,
  62. '129,22,39,253,19,98,108,110,79,113,224,232,178,185,112,104,218,246,97,228,
  63. '251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249,14,239,107,
  64. '49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,
  65. '138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
  66. p:
  67. DATA 151,160,137,091,090,015,131,013,201,095
  68. DATA 096,053,194,233,007,225,140,036,103,030
  69. DATA 069,142,008,099,037,240,021,010,023,190
  70. DATA 006,148,247,120,234,075,000,026,197,062
  71. DATA 094,252,219,203,117,035,011,032,057,177
  72. DATA 033,088,237,149,056,087,174,020,125,136
  73. DATA 171,168,068,175,074,165,071,134,139,048
  74. DATA 027,166,077,146,158,231,083,111,229,122
  75. DATA 060,211,133,230,220,105,092,041,055,046
  76. DATA 245,040,244,102,143,054,065,025,063,161
  77. DATA 001,216,080,073,209,076,132,187,208,089
  78. DATA 018,169,200,196,135,130,116,188,159,086
  79. DATA 164,100,109,198,173,186,003,064,052,217
  80. DATA 226,250,124,123,005,202,038,147,118,126
  81. DATA 255,082,085,212,207,206,059,227,047,016
  82. DATA 058,017,182,189,028,042,223,183,170,213
  83. DATA 119,248,152,002,044,154,163,070,221,153
  84. DATA 101,155,167,043,172,009,129,022,039,253
  85. DATA 019,098,108,110,079,113,224,232,178,185
  86. DATA 112,104,218,246,097,228,251,034,242,193
  87. DATA 238,210,144,012,191,179,162,241,081,051
  88. DATA 145,235,249,014,239,107,049,192,214,031
  89. DATA 181,199,106,157,184,084,204,176,115,121
  90. DATA 050,045,127,004,150,254,138,236,205,093
  91. DATA 222,114,067,029,024,072,243,141,128,195
  92. DATA 078,066,215,061,156,180
  93. DATA 151,160,137,091,090,015,131,013,201,095
  94. DATA 096,053,194,233,007,225,140,036,103,030
  95. DATA 069,142,008,099,037,240,021,010,023,190
  96. DATA 006,148,247,120,234,075,000,026,197,062
  97. DATA 094,252,219,203,117,035,011,032,057,177
  98. DATA 033,088,237,149,056,087,174,020,125,136
  99. DATA 171,168,068,175,074,165,071,134,139,048
  100. DATA 027,166,077,146,158,231,083,111,229,122
  101. DATA 060,211,133,230,220,105,092,041,055,046
  102. DATA 245,040,244,102,143,054,065,025,063,161
  103. DATA 001,216,080,073,209,076,132,187,208,089
  104. DATA 018,169,200,196,135,130,116,188,159,086
  105. DATA 164,100,109,198,173,186,003,064,052,217
  106. DATA 226,250,124,123,005,202,038,147,118,126
  107. DATA 255,082,085,212,207,206,059,227,047,016
  108. DATA 058,017,182,189,028,042,223,183,170,213
  109. DATA 119,248,152,002,044,154,163,070,221,153
  110. DATA 101,155,167,043,172,009,129,022,039,253
  111. DATA 019,098,108,110,079,113,224,232,178,185
  112. DATA 112,104,218,246,097,228,251,034,242,193
  113. DATA 238,210,144,012,191,179,162,241,081,051
  114. DATA 145,235,249,014,239,107,049,192,214,031
  115. DATA 181,199,106,157,184,084,204,176,115,121
  116. DATA 050,045,127,004,150,254,138,236,205,093
  117. DATA 222,114,067,029,024,072,243,141,128,195
  118. DATA 078,066,215,061,156,180
  119.  
  120.  
  121. 'To remove the need for index wrapping, double the permutation table length
  122. 'private static int perm[] = new int[512];
  123. DIM SHARED perm(512) AS INTEGER
  124.  
  125. 'static { for(int i=0; i<512; i++) perm[i]=p[i & 255]; }
  126. FOR i% = 0 TO 511
  127.     READ p(i%)
  128.     perm(i%) = p(i%) AND 255
  129.  
  130.     'PRINT perm(i%)
  131.  
  132.  
  133. '' 2D simplex noise************************************************************
  134.  
  135. ' final double F2 = 0.5*(Math.sqrt(3.0)-1.0);
  136. CONST F2 = .5 * (SQR(3) - 1)
  137. ' final double G2 = (3.0-Math.sqrt(3.0))/6.0;
  138. CONST G2 = (3 - SQR(3)) / 6
  139.  
  140. scale = .01
  141. '------------------------------------------------------------------------------
  142. FOR x% = 0 TO 800
  143.     FOR y% = 0 TO 600
  144.  
  145.         'used to grayscale input
  146.         gs% = map!(noise2D(scale * x%, scale * y%), -1, 1, 0, 255)
  147.         c& = _RGB(gs%, gs%, gs%)
  148.         PSET (x%, y%), c&
  149.  
  150.         'PRINT INT(map!(noise2D(x%, y%), -1, 1, 0, 255));
  151.         'PRINT INT((noise2D(x%, y%) + 1) / 2.0 * 255.0);
  152.  
  153.     NEXT
  154. '------------------------------------------------------------------------------
  155.  
  156.  
  157. ' public static double noise(double xin, double yin) {
  158. FUNCTION noise2D (xin, yin)
  159.     ' double n0, n1, n2;' Noise contributions from the three corners
  160.     DIM n0, n1, n2
  161.  
  162.     '' Skew the input space to determine which simplex cell we're in
  163.     ' double s = (xin+yin)*F2;' Hairy factor for 2D
  164.     s = (xin + yin) * F2
  165.     ' int i = fastfloor(xin+s);
  166.     i% = INT(xin + s)
  167.     ' int j = fastfloor(yin+s);
  168.     j% = INT(yin + s)
  169.  
  170.  
  171.     ' double t = (i+j)*G2;
  172.     t = (i% + j%) * G2
  173.     ' double X0 = i-t;' Unskew the cell origin back to (x,y) space
  174.     BX0 = i% - t
  175.     ' double Y0 = j-t;
  176.     BY0 = j% - t
  177.     ' double x0 = xin-X0;' The x,y distances from the cell origin
  178.     x0 = xin - BX0
  179.     ' double y0 = yin-Y0;
  180.     y0 = yin - BY0
  181.  
  182.  
  183.     '' For the 2D case, the simplex shape is an equilateral triangle
  184.     '' Determine which simplex we are in.
  185.     ' int i1, j1;' Offsets for second (middle) corner of simplex in (i,j) coords
  186.     DIM AS INTEGER i1, j1
  187.     ' if(x0>y0) {i1=1; j1=0;}' lower triangle, XY order: (0,0)->(1,0)->(1,1)
  188.     IF x0 > y0 THEN
  189.         i1% = 1: j1% = 0
  190.         ' else {i1=0; j1=1;}' upper triangle, YX order: (0,0)->(0,1)->(1,1)
  191.     ELSE
  192.         i1% = 0: j1% = 1
  193.     END IF
  194.  
  195.  
  196.     '' A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  197.     '' a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  198.     '' c = (3-sqrt(3))/6 which is G2
  199.     '
  200.     ' double x1 = x0 - i1 + G2;' Offsets for middle corner in (x,y) unskewed coords
  201.     x1 = x0 - i1% + G2
  202.     ' double y1 = y0 - j1 + G2;
  203.     y1 = y0 - j1% + G2
  204.     ' double x2 = x0 - 1.0 + 2.0 * G2;' Offsets for last corner in (x,y) unskewed coords
  205.     x2 = x0 - 1 + 2 * G2
  206.     ' double y2 = y0 - 1.0 + 2.0 * G2;
  207.     y2 = y0 - 1 + 2 * G2
  208.  
  209.  
  210.     '' Work out the hashed gradient indices of the three simplex corners
  211.     ' int ii = i & 255;
  212.     ii% = i% AND 255
  213.     ' int jj = j & 255;
  214.     jj% = j% AND 255
  215.     ' int gi0 = perm[ii+perm[jj]] % 12;
  216.     gi0% = perm(ii% + perm(jj%)) MOD 12
  217.     ' int gi1 = perm[ii+i1+perm[jj+j1]] % 12;
  218.     gi1% = perm(ii% + i1% + perm(jj% + j1%)) MOD 12
  219.     ' int gi2 = perm[ii+1+perm[jj+1]] % 12;
  220.     gi2% = perm(ii% + 1 + perm(jj% + 1)) MOD 12
  221.  
  222.  
  223.     '' Calculate the contribution from the three corners
  224.     '
  225.     ' double t0 = 0.5 - x0*x0-y0*y0;
  226.     t0 = .5 - x0 * x0 - y0 * y0
  227.     ' if(t0<0) n0 = 0.0;
  228.     IF t0 < 0 THEN
  229.         n0 = 0
  230.         ' else {
  231.     ELSE
  232.         ' t0 *= t0;
  233.         t0 = t0 * t0
  234.         ' n0 = t0 * t0 * dot(grad3[gi0], x0, y0);' (x,y) of grad3 used for 2D gradient
  235.         n0 = t0 * t0 * DotP2(gi0%, x0, y0)
  236.         ' }
  237.     END IF
  238.  
  239.  
  240.     ' double t1 = 0.5 - x1*x1-y1*y1;
  241.     t1 = .5 - x1 * x1 - y1 * y1
  242.     ' if(t1<0) n1 = 0.0;
  243.     IF t1 < 0 THEN
  244.         n1 = 0
  245.         ' else {
  246.     ELSE
  247.         ' t1 *= t1;
  248.         t1 = t1 * t1
  249.         ' n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
  250.         n1 = t1 * t1 * DotP2(gi1%, x1, y1)
  251.         ' }
  252.     END IF
  253.  
  254.  
  255.     ' double t2 = 0.5 - x2*x2-y2*y2;
  256.     t2 = .5 - x2 * x2 - y2 * y2
  257.     ' if(t2<0) n2 = 0.0;
  258.     IF t2 < 0 THEN
  259.         n2 = 0
  260.         ' else {
  261.     ELSE
  262.         ' t2 *= t2;
  263.         t2 = t2 * t2
  264.         ' n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
  265.         n2 = t2 * t2 * DotP2(gi2%, x2, y2)
  266.         ' }
  267.     END IF
  268.  
  269.  
  270.     '' Add contributions from each corner to get the final noise value.
  271.     '' The result is scaled to return values in the interval [-1,1].
  272.     '
  273.     ' return 70.0 * (n0 + n1 + n2);
  274.     noise2D = 70 * (n0 + n1 + n2)
  275.  
  276.     ' }
  277.  
  278.  
  279.  
  280. ' This method is a *lot* faster than using (int)Math.floor(x)
  281. ' private static int fastfloor(double x) {
  282. ' return x>0 ? (int)x : (int)x-1;}
  283.  
  284. ' private static double dot(int g[], double x, double y) {
  285. ' return g[0]*x + g[1]*y; }
  286. FUNCTION DotP2 (g%, x, y)
  287.     DotP2 = grad3(g%).x * x + grad3(g%).y * y
  288.  
  289. ' Map function I found or translated from somewhere.
  290. ' The Coding Train" guy on youtube, where I translated the rain code from explained it in one of his videos.
  291. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  292.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  293.  
  294.  
  295. ' private static double dot(int g[], double x, double y, double z, double w) {
  296. ' private static double dot(int g[], double x, double y) {
  297. ' return g[0]*x + g[1]*y; }
  298.  
  299.  
  300. ' private static double dot(int g[], double x, double y, double z) {
  301. ' return g[0]*x + g[1]*y + g[2]*z; }
  302.  
  303.  
  304. ' private static double dot(int g[], double x, double y, double z, double w) {
  305. ' return g[0]*x + g[1]*y + g[2]*z + g[3]*w; }
  306. ' return g[0]*x + g[1]*y + g[2]*z + g[3]*w; }
  307.  
  308.  
  309.  
  310. ' ******* NOTE: 3D portion below not ported yet ~Phlashlite *******
  311.  
  312.  
  313.  
  314. '' 3D simplex noise
  315. ' public static double noise(double xin, double yin, double zin) {
  316. ' double n0, n1, n2, n3;' Noise contributions from the four corners
  317.  
  318.  
  319. '' Skew the input space to determine which simplex cell we're in
  320. ' final double F3 = 1.0/3.0;
  321. ' double s = (xin+yin+zin)*F3;' Very nice and simple skew factor for 3D
  322. ' int i = fastfloor(xin+s);
  323. ' int j = fastfloor(yin+s);
  324. ' int k = fastfloor(zin+s);
  325. ' final double G3 = 1.0/6.0;' Very nice and simple unskew factor, too
  326. ' double t = (i+j+k)*G3;
  327. ' double X0 = i-t;' Unskew the cell origin back to (x,y,z) space
  328. ' double Y0 = j-t;
  329. ' double Z0 = k-t;
  330. ' double x0 = xin-X0;' The x,y,z distances from the cell origin
  331. ' double y0 = yin-Y0;
  332. ' double z0 = zin-Z0;
  333.  
  334.  
  335. '' For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  336. '' Determine which simplex we are in.
  337. ' int i1, j1, k1;' Offsets for second corner of simplex in (i,j,k) coords
  338. ' int i2, j2, k2;' Offsets for third corner of simplex in (i,j,k) coords
  339. ' if(x0>=y0) {
  340. ' if(y0>=z0)
  341. ' { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; }' X Y Z order
  342. ' else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; }' X Z Y order
  343. ' else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; }' Z X Y order
  344. ' }
  345. ' else {' x0<y0
  346. ' if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; }' Z Y X order
  347. ' else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; }' Y Z X order
  348. ' else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; }' Y X Z order
  349. ' }
  350.  
  351.  
  352. '' A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  353. '' a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  354. '' a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  355. '' c = 1/6.
  356. ' double x1 = x0 - i1 + G3;' Offsets for second corner in (x,y,z) coords
  357. ' double y1 = y0 - j1 + G3;
  358. ' double z1 = z0 - k1 + G3;
  359. ' double x2 = x0 - i2 + 2.0*G3;' Offsets for third corner in (x,y,z) coords
  360. ' double y2 = y0 - j2 + 2.0*G3;
  361. ' double z2 = z0 - k2 + 2.0*G3;
  362. ' double x3 = x0 - 1.0 + 3.0*G3;' Offsets for last corner in (x,y,z) coords
  363. ' double y3 = y0 - 1.0 + 3.0*G3;
  364. ' double z3 = z0 - 1.0 + 3.0*G3;
  365.  
  366.  
  367. '' Work out the hashed gradient indices of the four simplex corners
  368. ' int ii = i & 255;
  369. ' int jj = j & 255;
  370. ' int kk = k & 255;
  371. ' int gi0 = perm[ii+perm[jj+perm[kk]]] % 12;
  372. ' int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1]]] % 12;
  373. ' int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2]]] % 12;
  374. ' int gi3 = perm[ii+1+perm[jj+1+perm[kk+1]]] % 12;
  375.  
  376.  
  377. '' Calculate the contribution from the four corners
  378. ' double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0;
  379. ' if(t0<0) n0 = 0.0;
  380. ' else {
  381. ' t0 *= t0;
  382. ' n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
  383. ' }
  384. ' double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1;
  385. ' if(t1<0) n1 = 0.0;
  386. ' else {
  387. ' t1 *= t1;
  388. ' n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
  389. ' }
  390. ' double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2;
  391. ' if(t2<0) n2 = 0.0;
  392. ' else {
  393. ' t2 *= t2;
  394. ' n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
  395. ' }
  396. ' double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3;
  397. ' if(t3<0) n3 = 0.0;
  398. ' else {
  399. ' t3 *= t3;
  400. ' n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
  401. ' }
  402.  
  403.  
  404. '' Add contributions from each corner to get the final noise value.
  405. '' The result is scaled to stay just inside [-1,1]
  406. ' return 32.0*(n0 + n1 + n2 + n3);
  407. ' }
  408.  
  409.  
  410. '' 4D simplex noise
  411. ' double noise(double x, double y, double z, double w) {
  412.  
  413. '' The skewing and unskewing factors are hairy again for the 4D case
  414. ' final double F4 = (Math.sqrt(5.0)-1.0)/4.0;
  415. ' final double G4 = (5.0-Math.sqrt(5.0))/20.0;
  416. ' double n0, n1, n2, n3, n4;' Noise contributions from the five corners
  417.  
  418.  
  419. '' Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
  420. '  double s = (x + y + z + w) * F4;' Factor for 4D skewing
  421. ' int i = fastfloor(x + s);
  422. ' int j = fastfloor(y + s);
  423. ' int k = fastfloor(z + s);
  424. ' int l = fastfloor(w + s);
  425. ' double t = (i + j + k + l) * G4;' Factor for 4D unskewing
  426. ' double X0 = i - t;' Unskew the cell origin back to (x,y,z,w) space
  427. ' double Y0 = j - t;
  428. ' double Z0 = k - t;
  429. ' double W0 = l - t;
  430. ' double x0 = x - X0;' The x,y,z,w distances from the cell origin
  431. ' double y0 = y - Y0;
  432. ' double z0 = z - Z0;
  433. ' double w0 = w - W0;
  434.  
  435.  
  436. '' For the 4D case, the simplex is a 4D shape I won't even try to describe.
  437. '' To find out which of the 24 possible simplices we're in, we need to
  438. '' determine the magnitude ordering of x0, y0, z0 and w0.
  439. '' The method below is a good way of finding the ordering of x,y,z,w and
  440. '' then find the correct traversal order for the simplex we%u2019re in.
  441. '' First, six pair-wise comparisons are performed between each possible pair
  442. '' of the four coordinates, and the results are used to add up binary bits
  443. '' for an integer index.
  444. ' int c1 = (x0 > y0) ? 32 : 0;
  445. ' int c2 = (x0 > z0) ? 16 : 0;
  446. ' int c3 = (y0 > z0) ? 8 : 0;
  447. ' int c4 = (x0 > w0) ? 4 : 0;
  448. ' int c5 = (y0 > w0) ? 2 : 0;
  449. ' int c6 = (z0 > w0) ? 1 : 0;
  450. ' int c = c1 + c2 + c3 + c4 + c5 + c6;
  451. ' int i1, j1, k1, l1;' The integer offsets for the second simplex corner
  452. ' int i2, j2, k2, l2;' The integer offsets for the third simplex corner
  453. ' int i3, j3, k3, l3;' The integer offsets for the fourth simplex corner
  454.  
  455.  
  456. '' simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
  457. '' Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
  458. '' impossible. Only the 24 indices which have non-zero entries make any sense.
  459. '' We use a thresholding to set the coordinates in turn from the largest magnitude.
  460.  
  461.  
  462. '' The number 3 in the "simplex" array is at the position of the largest coordinate.
  463. ' i1 = simplex[c][0]>=3 ? 1 : 0;
  464. ' j1 = simplex[c][1]>=3 ? 1 : 0;
  465. ' k1 = simplex[c][2]>=3 ? 1 : 0;
  466. ' l1 = simplex[c][3]>=3 ? 1 : 0;
  467.  
  468.  
  469. '' The number 2 in the "simplex" array is at the second largest coordinate.
  470. ' i2 = simplex[c][0]>=2 ? 1 : 0;
  471. ' j2 = simplex[c][1]>=2 ? 1 : 0;
  472. ' k2 = simplex[c][2]>=2 ? 1 : 0;
  473. ' l2 = simplex[c][3]>=2 ? 1 : 0;
  474.  
  475.  
  476. '' The number 1 in the "simplex" array is at the second smallest coordinate.
  477. ' i3 = simplex[c][0]>=1 ? 1 : 0;
  478. ' j3 = simplex[c][1]>=1 ? 1 : 0;
  479. ' k3 = simplex[c][2]>=1 ? 1 : 0;
  480. ' l3 = simplex[c][3]>=1 ? 1 : 0;
  481.  
  482.  
  483. '' The fifth corner has all coordinate offsets = 1, so no need to look that up.
  484. ' double x1 = x0 - i1 + G4;' Offsets for second corner in (x,y,z,w) coords
  485. ' double y1 = y0 - j1 + G4;
  486. ' double z1 = z0 - k1 + G4;
  487. ' double w1 = w0 - l1 + G4;
  488. ' double x2 = x0 - i2 + 2.0*G4;' Offsets for third corner in (x,y,z,w) coords
  489. ' double y2 = y0 - j2 + 2.0*G4;
  490. ' double z2 = z0 - k2 + 2.0*G4;
  491. ' double w2 = w0 - l2 + 2.0*G4;
  492. ' double x3 = x0 - i3 + 3.0*G4;' Offsets for fourth corner in (x,y,z,w) coords
  493. ' double y3 = y0 - j3 + 3.0*G4;
  494. ' double z3 = z0 - k3 + 3.0*G4;
  495. ' double w3 = w0 - l3 + 3.0*G4;
  496. ' double x4 = x0 - 1.0 + 4.0*G4;' Offsets for last corner in (x,y,z,w) coords
  497. ' double y4 = y0 - 1.0 + 4.0*G4;
  498. ' double z4 = z0 - 1.0 + 4.0*G4;
  499. ' double w4 = w0 - 1.0 + 4.0*G4;
  500.  
  501.  
  502. '' Work out the hashed gradient indices of the five simplex corners
  503. ' int ii = i & 255;
  504. ' int jj = j & 255;
  505. ' int kk = k & 255;
  506. ' int ll = l & 255;
  507. ' int gi0 = perm[ii+perm[jj+perm[kk+perm[ll]]]] % 32;
  508. ' int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]] % 32;
  509. ' int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]] % 32;
  510. ' int gi3 = perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]] % 32;
  511. ' int gi4 = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32;
  512.  
  513.  
  514. '' Calculate the contribution from the five corners
  515. ' double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0;
  516. ' if(t0<0) n0 = 0.0;
  517. ' else {
  518. ' t0 *= t0;
  519. ' n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
  520. ' }
  521. ' double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1;
  522. ' if(t1<0) n1 = 0.0;
  523. ' else {
  524. ' t1 *= t1;
  525. ' n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
  526. ' }
  527. ' double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2;
  528. ' if(t2<0) n2 = 0.0;
  529. ' else {
  530. ' t2 *= t2;
  531. ' n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
  532. ' }
  533. ' double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3;
  534. ' if(t3<0) n3 = 0.0;
  535. ' else {
  536. ' t3 *= t3;
  537. ' n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
  538. ' }
  539. ' double t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4;
  540. ' if(t4<0) n4 = 0.0;
  541. ' else {
  542. ' t4 *= t4;
  543. ' n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
  544. ' }
  545.  
  546.  
  547. '' Sum up and scale the result to cover the range [-1,1]
  548. ' return 27.0 * (n0 + n1 + n2 + n3 + n4);
  549.  
  550. 'private static int grad4[][]= {{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1},
  551. '{0,-1,1,1}, {0,-1,1,-1}, {0,-1,-1,1}, {0,-1,-1,-1},
  552. '{1,0,1,1}, {1,0,1,-1}, {1,0,-1,1}, {1,0,-1,-1},
  553. '{-1,0,1,1}, {-1,0,1,-1}, {-1,0,-1,1}, {-1,0,-1,-1},
  554. '{1,1,0,1}, {1,1,0,-1}, {1,-1,0,1}, {1,-1,0,-1},
  555. '{-1,1,0,1}, {-1,1,0,-1}, {-1,-1,0,1}, {-1,-1,0,-1},
  556. '{1,1,1,0}, {1,1,-1,0}, {1,-1,1,0}, {1,-1,-1,0},
  557. '{-1,1,1,0}, {-1,1,-1,0}, {-1,-1,1,0}, {-1,-1,-1,0}};
  558.  
  559. '' A lookup table to traverse the simplex around a given point in 4D.
  560. '' Details can be found where this table is used, in the 4D noise method.
  561. ' private static int simplex[][] = {
  562. ' {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},
  563. ' {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},
  564. ' {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},
  565. ' {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},
  566. ' {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},
  567. ' {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},
  568. ' {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},
  569. ' {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}};
  570.  
  571.  
  572. '}
  573. '}
  574.  
  575.  
  576.  


***  had an extra "+j1"  ***
 ' int gi0 = perm[ii+perm[jj]] % 12;
    gi0% = perm(ii + perm(jj + j1)) MOD 12
« Last Edit: February 25, 2022, 01:36:16 pm by Phlashlite »

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Simplex Noise Anyone?
« Reply #3 on: February 25, 2022, 01:30:40 pm »
Guess all I needed was a little motivation... and luck! :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simplex Noise Anyone?
« Reply #4 on: February 25, 2022, 01:45:25 pm »
 
image_2022-02-25_134512.png


Well I guess that is what it is supposed to look like but can you use it for anything? :)

Without all the garbage:
Code: QB64: [Select]
  1. '$DEBUG
  2. _Title "Simplex noise port"
  3.  
  4. ' ported to QB64 by: Phlashlite, February 20, 2022
  5. '    from the paper: Simplex noise demystified
  6. '            author: Stefan Gustavson, Link%u201Dping University, Sweden (stegu@itn.liu.se), 2005-03-22
  7. '               URL: https://weber.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  8. ' - Simplex noise has a lower computational complexity and requires fewer multiplications.
  9. ' - Simplex noise scales to higher dimensions (4D, 5D and up) with much less computational
  10. '   cost, the complexity is for dimensions instead of the of classic Noise.
  11. ' - Simplex noise has no noticeable directional artifacts.
  12. ' - Simplex noise has a well-defined and continuous gradient everywhere that can be computed
  13. '   quite cheaply.
  14. ' - Simplex noise is easy to implement in hardware
  15.  
  16.  
  17. Screen _NewImage(800, 600, 32)
  18.  
  19. DefDbl A-Z
  20.  
  21. 'private static int grad3[][] = {{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}};
  22. g3:
  23. Data 1,1,0
  24. Data -1,1,0
  25. Data 1,-1,0
  26. Data -1,-1,0
  27. Data 1,0,1
  28. Data -1,0,1
  29. Data 1,0,-1
  30. Data -1,0,-1
  31. Data 0,1,1
  32. Data 0,-1,1
  33. Data 0,1,-1
  34. Data 0,-1,-1
  35.  
  36. Type TriBit
  37.     x As Integer
  38.     y As Integer
  39.     z As Integer
  40. Dim Shared grad3(12) As TriBit
  41.  
  42. For g% = 0 To 11
  43.  
  44.     Read grad3(g%).x
  45.     Read grad3(g%).y
  46.     Read grad3(g%).z
  47.  
  48.     'PRINT grad3(g%).x, grad3(g%).y, grad3(g%).z
  49.  
  50. Next g%
  51.  
  52. p:
  53. Data 151,160,137,091,090,015,131,013,201,095
  54. Data 096,053,194,233,007,225,140,036,103,030
  55. Data 069,142,008,099,037,240,021,010,023,190
  56. Data 006,148,247,120,234,075,000,026,197,062
  57. Data 094,252,219,203,117,035,011,032,057,177
  58. Data 033,088,237,149,056,087,174,020,125,136
  59. Data 171,168,068,175,074,165,071,134,139,048
  60. Data 027,166,077,146,158,231,083,111,229,122
  61. Data 060,211,133,230,220,105,092,041,055,046
  62. Data 245,040,244,102,143,054,065,025,063,161
  63. Data 001,216,080,073,209,076,132,187,208,089
  64. Data 018,169,200,196,135,130,116,188,159,086
  65. Data 164,100,109,198,173,186,003,064,052,217
  66. Data 226,250,124,123,005,202,038,147,118,126
  67. Data 255,082,085,212,207,206,059,227,047,016
  68. Data 058,017,182,189,028,042,223,183,170,213
  69. Data 119,248,152,002,044,154,163,070,221,153
  70. Data 101,155,167,043,172,009,129,022,039,253
  71. Data 019,098,108,110,079,113,224,232,178,185
  72. Data 112,104,218,246,097,228,251,034,242,193
  73. Data 238,210,144,012,191,179,162,241,081,051
  74. Data 145,235,249,014,239,107,049,192,214,031
  75. Data 181,199,106,157,184,084,204,176,115,121
  76. Data 050,045,127,004,150,254,138,236,205,093
  77. Data 222,114,067,029,024,072,243,141,128,195
  78. Data 078,066,215,061,156,180
  79. Data 151,160,137,091,090,015,131,013,201,095
  80. Data 096,053,194,233,007,225,140,036,103,030
  81. Data 069,142,008,099,037,240,021,010,023,190
  82. Data 006,148,247,120,234,075,000,026,197,062
  83. Data 094,252,219,203,117,035,011,032,057,177
  84. Data 033,088,237,149,056,087,174,020,125,136
  85. Data 171,168,068,175,074,165,071,134,139,048
  86. Data 027,166,077,146,158,231,083,111,229,122
  87. Data 060,211,133,230,220,105,092,041,055,046
  88. Data 245,040,244,102,143,054,065,025,063,161
  89. Data 001,216,080,073,209,076,132,187,208,089
  90. Data 018,169,200,196,135,130,116,188,159,086
  91. Data 164,100,109,198,173,186,003,064,052,217
  92. Data 226,250,124,123,005,202,038,147,118,126
  93. Data 255,082,085,212,207,206,059,227,047,016
  94. Data 058,017,182,189,028,042,223,183,170,213
  95. Data 119,248,152,002,044,154,163,070,221,153
  96. Data 101,155,167,043,172,009,129,022,039,253
  97. Data 019,098,108,110,079,113,224,232,178,185
  98. Data 112,104,218,246,097,228,251,034,242,193
  99. Data 238,210,144,012,191,179,162,241,081,051
  100. Data 145,235,249,014,239,107,049,192,214,031
  101. Data 181,199,106,157,184,084,204,176,115,121
  102. Data 050,045,127,004,150,254,138,236,205,093
  103. Data 222,114,067,029,024,072,243,141,128,195
  104. Data 078,066,215,061,156,180
  105.  
  106.  
  107. 'To remove the need for index wrapping, double the permutation table length
  108. 'private static int perm[] = new int[512];
  109. Dim Shared perm(512) As Integer
  110.  
  111. 'static { for(int i=0; i<512; i++) perm[i]=p[i & 255]; }
  112. For i% = 0 To 511
  113.     Read p(i%)
  114.     perm(i%) = p(i%) And 255
  115.  
  116.     'PRINT perm(i%)
  117.  
  118.  
  119. '' 2D simplex noise************************************************************
  120.  
  121. ' final double F2 = 0.5*(Math.sqrt(3.0)-1.0);
  122. Const F2 = .5 * (Sqr(3) - 1)
  123. ' final double G2 = (3.0-Math.sqrt(3.0))/6.0;
  124. Const G2 = (3 - Sqr(3)) / 6
  125.  
  126. scale = .01
  127. '------------------------------------------------------------------------------
  128. For x% = 0 To 800
  129.     For y% = 0 To 600
  130.  
  131.         'used to grayscale input
  132.         gs% = map!(noise2D(scale * x%, scale * y%), -1, 1, 0, 255)
  133.         c& = _RGB(gs%, gs%, gs%)
  134.         PSet (x%, y%), c&
  135.  
  136.         'PRINT INT(map!(noise2D(x%, y%), -1, 1, 0, 255));
  137.         'PRINT INT((noise2D(x%, y%) + 1) / 2.0 * 255.0);
  138.  
  139.     Next
  140. '------------------------------------------------------------------------------
  141.  
  142.  
  143. ' public static double noise(double xin, double yin) {
  144. Function noise2D (xin, yin)
  145.     ' double n0, n1, n2;' Noise contributions from the three corners
  146.     Dim n0, n1, n2
  147.  
  148.     '' Skew the input space to determine which simplex cell we're in
  149.     ' double s = (xin+yin)*F2;' Hairy factor for 2D
  150.     s = (xin + yin) * F2
  151.     ' int i = fastfloor(xin+s);
  152.     i% = Int(xin + s)
  153.     ' int j = fastfloor(yin+s);
  154.     j% = Int(yin + s)
  155.  
  156.  
  157.     ' double t = (i+j)*G2;
  158.     t = (i% + j%) * G2
  159.     ' double X0 = i-t;' Unskew the cell origin back to (x,y) space
  160.     BX0 = i% - t
  161.     ' double Y0 = j-t;
  162.     BY0 = j% - t
  163.     ' double x0 = xin-X0;' The x,y distances from the cell origin
  164.     x0 = xin - BX0
  165.     ' double y0 = yin-Y0;
  166.     y0 = yin - BY0
  167.  
  168.  
  169.     '' For the 2D case, the simplex shape is an equilateral triangle
  170.     '' Determine which simplex we are in.
  171.     ' int i1, j1;' Offsets for second (middle) corner of simplex in (i,j) coords
  172.     Dim As Integer i1, j1
  173.     ' if(x0>y0) {i1=1; j1=0;}' lower triangle, XY order: (0,0)->(1,0)->(1,1)
  174.     If x0 > y0 Then
  175.         i1% = 1: j1% = 0
  176.         ' else {i1=0; j1=1;}' upper triangle, YX order: (0,0)->(0,1)->(1,1)
  177.     Else
  178.         i1% = 0: j1% = 1
  179.     End If
  180.  
  181.  
  182.     '' A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  183.     '' a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  184.     '' c = (3-sqrt(3))/6 which is G2
  185.     '
  186.     ' double x1 = x0 - i1 + G2;' Offsets for middle corner in (x,y) unskewed coords
  187.     x1 = x0 - i1% + G2
  188.     ' double y1 = y0 - j1 + G2;
  189.     y1 = y0 - j1% + G2
  190.     ' double x2 = x0 - 1.0 + 2.0 * G2;' Offsets for last corner in (x,y) unskewed coords
  191.     x2 = x0 - 1 + 2 * G2
  192.     ' double y2 = y0 - 1.0 + 2.0 * G2;
  193.     y2 = y0 - 1 + 2 * G2
  194.  
  195.  
  196.     '' Work out the hashed gradient indices of the three simplex corners
  197.     ' int ii = i & 255;
  198.     ii% = i% And 255
  199.     ' int jj = j & 255;
  200.     jj% = j% And 255
  201.     ' int gi0 = perm[ii+perm[jj]] % 12;
  202.     gi0% = perm(ii% + perm(jj%)) Mod 12
  203.     ' int gi1 = perm[ii+i1+perm[jj+j1]] % 12;
  204.     gi1% = perm(ii% + i1% + perm(jj% + j1%)) Mod 12
  205.     ' int gi2 = perm[ii+1+perm[jj+1]] % 12;
  206.     gi2% = perm(ii% + 1 + perm(jj% + 1)) Mod 12
  207.  
  208.  
  209.     '' Calculate the contribution from the three corners
  210.     '
  211.     ' double t0 = 0.5 - x0*x0-y0*y0;
  212.     t0 = .5 - x0 * x0 - y0 * y0
  213.     ' if(t0<0) n0 = 0.0;
  214.     If t0 < 0 Then
  215.         n0 = 0
  216.         ' else {
  217.     Else
  218.         ' t0 *= t0;
  219.         t0 = t0 * t0
  220.         ' n0 = t0 * t0 * dot(grad3[gi0], x0, y0);' (x,y) of grad3 used for 2D gradient
  221.         n0 = t0 * t0 * DotP2(gi0%, x0, y0)
  222.         ' }
  223.     End If
  224.  
  225.  
  226.     ' double t1 = 0.5 - x1*x1-y1*y1;
  227.     t1 = .5 - x1 * x1 - y1 * y1
  228.     ' if(t1<0) n1 = 0.0;
  229.     If t1 < 0 Then
  230.         n1 = 0
  231.         ' else {
  232.     Else
  233.         ' t1 *= t1;
  234.         t1 = t1 * t1
  235.         ' n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
  236.         n1 = t1 * t1 * DotP2(gi1%, x1, y1)
  237.         ' }
  238.     End If
  239.  
  240.  
  241.     ' double t2 = 0.5 - x2*x2-y2*y2;
  242.     t2 = .5 - x2 * x2 - y2 * y2
  243.     ' if(t2<0) n2 = 0.0;
  244.     If t2 < 0 Then
  245.         n2 = 0
  246.         ' else {
  247.     Else
  248.         ' t2 *= t2;
  249.         t2 = t2 * t2
  250.         ' n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
  251.         n2 = t2 * t2 * DotP2(gi2%, x2, y2)
  252.         ' }
  253.     End If
  254.  
  255.  
  256.     '' Add contributions from each corner to get the final noise value.
  257.     '' The result is scaled to return values in the interval [-1,1].
  258.     '
  259.     ' return 70.0 * (n0 + n1 + n2);
  260.     noise2D = 70 * (n0 + n1 + n2)
  261.  
  262.     ' }
  263.  
  264.  
  265.  
  266. ' This method is a *lot* faster than using (int)Math.floor(x)
  267. ' private static int fastfloor(double x) {
  268. ' return x>0 ? (int)x : (int)x-1;}
  269.  
  270. ' private static double dot(int g[], double x, double y) {
  271. ' return g[0]*x + g[1]*y; }
  272. Function DotP2 (g%, x, y)
  273.     DotP2 = grad3(g%).x * x + grad3(g%).y * y
  274.  
  275. ' Map function I found or translated from somewhere.
  276. ' The Coding Train" guy on youtube, where I translated the rain code from explained it in one of his videos.
  277. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  278.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  279.  

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
2D Simplex Noise - Cleaned of all java code and unnecessary comments.
« Reply #5 on: February 25, 2022, 01:45:58 pm »
Code: QB64: [Select]
  1. _TITLE "Simplex noise port"
  2.  
  3. ' ported to QB64 by: Phlashlite, February 20, 2022
  4. '    from the paper: Simplex noise demystified
  5. '            author: Stefan Gustavson, Link%u201Dping University, Sweden (stegu@itn.liu.se), 2005-03-22
  6. '               URL: https://weber.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  7. ' - Simplex noise has a lower computational complexity and requires fewer multiplications.
  8. ' - Simplex noise scales to higher dimensions (4D, 5D and up) with much less computational
  9. '   cost, the complexity is for dimensions instead of the of classic Noise.
  10. ' - Simplex noise has no noticeable directional artifacts.
  11. ' - Simplex noise has a well-defined and continuous gradient everywhere that can be computed
  12. '   quite cheaply.
  13. ' - Simplex noise is easy to implement in hardware
  14.  
  15.  
  16. SCREEN _NEWIMAGE(800, 600, 32)
  17.  
  18. DEFDBL A-Z
  19.  
  20. g3:
  21. DATA 1,1,0
  22. DATA -1,1,0
  23. DATA 1,-1,0
  24. DATA -1,-1,0
  25. DATA 1,0,1
  26. DATA -1,0,1
  27. DATA 1,0,-1
  28. DATA -1,0,-1
  29. DATA 0,1,1
  30. DATA 0,-1,1
  31. DATA 0,1,-1
  32. DATA 0,-1,-1
  33.  
  34. TYPE TriBit
  35.     x AS INTEGER
  36.     y AS INTEGER
  37.     z AS INTEGER
  38. DIM SHARED grad3(12) AS TriBit
  39.  
  40. FOR g% = 0 TO 11
  41.  
  42.     READ grad3(g%).x
  43.     READ grad3(g%).y
  44.     READ grad3(g%).z
  45.  
  46. NEXT g%
  47.  
  48. p:
  49. DATA 151,160,137,091,090,015,131,013,201,095
  50. DATA 096,053,194,233,007,225,140,036,103,030
  51. DATA 069,142,008,099,037,240,021,010,023,190
  52. DATA 006,148,247,120,234,075,000,026,197,062
  53. DATA 094,252,219,203,117,035,011,032,057,177
  54. DATA 033,088,237,149,056,087,174,020,125,136
  55. DATA 171,168,068,175,074,165,071,134,139,048
  56. DATA 027,166,077,146,158,231,083,111,229,122
  57. DATA 060,211,133,230,220,105,092,041,055,046
  58. DATA 245,040,244,102,143,054,065,025,063,161
  59. DATA 001,216,080,073,209,076,132,187,208,089
  60. DATA 018,169,200,196,135,130,116,188,159,086
  61. DATA 164,100,109,198,173,186,003,064,052,217
  62. DATA 226,250,124,123,005,202,038,147,118,126
  63. DATA 255,082,085,212,207,206,059,227,047,016
  64. DATA 058,017,182,189,028,042,223,183,170,213
  65. DATA 119,248,152,002,044,154,163,070,221,153
  66. DATA 101,155,167,043,172,009,129,022,039,253
  67. DATA 019,098,108,110,079,113,224,232,178,185
  68. DATA 112,104,218,246,097,228,251,034,242,193
  69. DATA 238,210,144,012,191,179,162,241,081,051
  70. DATA 145,235,249,014,239,107,049,192,214,031
  71. DATA 181,199,106,157,184,084,204,176,115,121
  72. DATA 050,045,127,004,150,254,138,236,205,093
  73. DATA 222,114,067,029,024,072,243,141,128,195
  74. DATA 078,066,215,061,156,180
  75. DATA 151,160,137,091,090,015,131,013,201,095
  76. DATA 096,053,194,233,007,225,140,036,103,030
  77. DATA 069,142,008,099,037,240,021,010,023,190
  78. DATA 006,148,247,120,234,075,000,026,197,062
  79. DATA 094,252,219,203,117,035,011,032,057,177
  80. DATA 033,088,237,149,056,087,174,020,125,136
  81. DATA 171,168,068,175,074,165,071,134,139,048
  82. DATA 027,166,077,146,158,231,083,111,229,122
  83. DATA 060,211,133,230,220,105,092,041,055,046
  84. DATA 245,040,244,102,143,054,065,025,063,161
  85. DATA 001,216,080,073,209,076,132,187,208,089
  86. DATA 018,169,200,196,135,130,116,188,159,086
  87. DATA 164,100,109,198,173,186,003,064,052,217
  88. DATA 226,250,124,123,005,202,038,147,118,126
  89. DATA 255,082,085,212,207,206,059,227,047,016
  90. DATA 058,017,182,189,028,042,223,183,170,213
  91. DATA 119,248,152,002,044,154,163,070,221,153
  92. DATA 101,155,167,043,172,009,129,022,039,253
  93. DATA 019,098,108,110,079,113,224,232,178,185
  94. DATA 112,104,218,246,097,228,251,034,242,193
  95. DATA 238,210,144,012,191,179,162,241,081,051
  96. DATA 145,235,249,014,239,107,049,192,214,031
  97. DATA 181,199,106,157,184,084,204,176,115,121
  98. DATA 050,045,127,004,150,254,138,236,205,093
  99. DATA 222,114,067,029,024,072,243,141,128,195
  100. DATA 078,066,215,061,156,180
  101.  
  102.  
  103. 'To remove the need for index wrapping, double the permutation table length
  104. DIM SHARED perm(512) AS INTEGER
  105.  
  106. FOR i% = 0 TO 511
  107.  
  108.     READ p(i%)
  109.     perm(i%) = p(i%) AND 255
  110.  
  111.  
  112.  
  113. '' 2D simplex noise************************************************************
  114.  
  115. CONST F2 = .5 * (SQR(3) - 1)
  116. CONST G2 = (3 - SQR(3)) / 6
  117.  
  118. scale = .007 'Adjustable
  119.  
  120. '------------------------------------------------------------------------------
  121. FOR x% = 0 TO 800
  122.     FOR y% = 0 TO 600
  123.  
  124.         'used to grayscale input
  125.         gs% = map!(noise2D(scale * x%, scale * y%), -1, 1, 0, 255)
  126.         c& = _RGB(gs%, gs%, gs%)
  127.         PSET (x%, y%), c&
  128.  
  129.  
  130.     NEXT
  131. '------------------------------------------------------------------------------
  132.  
  133. FUNCTION noise2D (xin, yin)
  134.     DIM n0, n1, n2
  135.  
  136.     '' Skew the input space to determine which simplex cell we're in
  137.     s = (xin + yin) * F2
  138.     i% = INT(xin + s)
  139.     j% = INT(yin + s)
  140.  
  141.  
  142.  
  143.     t = (i% + j%) * G2
  144.     ' Unskew the cell origin back to (x,y) space
  145.     BX0 = i% - t
  146.     BY0 = j% - t
  147.     ' The x,y distances from the cell origin
  148.     x0 = xin - BX0
  149.     y0 = yin - BY0
  150.  
  151.  
  152.     '' For the 2D case, the simplex shape is an equilateral triangle
  153.     '' Determine which simplex we are in.
  154.     ' Offsets for second (middle) corner of simplex in (i,j) coords
  155.     DIM AS INTEGER i1, j1
  156.     IF x0 > y0 THEN
  157.         i1% = 1: j1% = 0
  158.     ELSE
  159.         i1% = 0: j1% = 1
  160.     END IF
  161.  
  162.  
  163.     '' A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  164.     '' a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  165.     '' c = (3-sqrt(3))/6 which is G2
  166.     '
  167.     x1 = x0 - i1% + G2
  168.     y1 = y0 - j1% + G2
  169.     ' Offsets for last corner in (x,y) unskewed coords
  170.     x2 = x0 - 1 + 2 * G2
  171.     y2 = y0 - 1 + 2 * G2
  172.  
  173.  
  174.     '' Work out the hashed gradient indices of the three simplex corners
  175.     ii% = i% AND 255
  176.     jj% = j% AND 255
  177.     gi0% = perm(ii% + perm(jj%)) MOD 12
  178.     gi1% = perm(ii% + i1% + perm(jj% + j1%)) MOD 12
  179.     gi2% = perm(ii% + 1 + perm(jj% + 1)) MOD 12
  180.  
  181.  
  182.     '' Calculate the contribution from the three corners
  183.     '
  184.     t0 = .5 - x0 * x0 - y0 * y0
  185.     IF t0 < 0 THEN
  186.         n0 = 0
  187.     ELSE
  188.         t0 = t0 * t0
  189.         n0 = t0 * t0 * DotP2(gi0%, x0, y0)
  190.     END IF
  191.  
  192.  
  193.     t1 = .5 - x1 * x1 - y1 * y1
  194.     IF t1 < 0 THEN
  195.         n1 = 0
  196.     ELSE
  197.         t1 = t1 * t1
  198.         n1 = t1 * t1 * DotP2(gi1%, x1, y1)
  199.     END IF
  200.  
  201.  
  202.     t2 = .5 - x2 * x2 - y2 * y2
  203.     IF t2 < 0 THEN
  204.         n2 = 0
  205.     ELSE
  206.         t2 = t2 * t2
  207.         n2 = t2 * t2 * DotP2(gi2%, x2, y2)
  208.     END IF
  209.  
  210.  
  211.     '' Add contributions from each corner to get the final noise value.
  212.     '' The result is scaled to return values in the interval [-1,1].
  213.     '
  214.     noise2D = 70 * (n0 + n1 + n2)
  215.  
  216.  
  217.  
  218. ' This method is a *lot* faster than using (int)Math.floor(x)
  219. ' private static int fastfloor(double x) {
  220. ' return x>0 ? (int)x : (int)x-1;}
  221.  
  222. ' private static double dot(int g[], double x, double y) {
  223. ' return g[0]*x + g[1]*y; }
  224. FUNCTION DotP2 (g%, x, y)
  225.     DotP2 = grad3(g%).x * x + grad3(g%).y * y
  226.  
  227. ' Map function I found or translated from somewhere.
  228. ' The Coding Train" guy on youtube, where I translated the rain code from explained it in one of his videos.
  229. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  230.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  231.  
  232.  

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Simplex Noise Anyone?
« Reply #6 on: February 25, 2022, 01:50:20 pm »

Quote
Well I guess that is what it is supposed to look like but can you use it for anything? :)

Not too useful without the some extra iterations.... but I'll work on that next... along with the 3 and 4D parts.

It's supposedly faster than Perlin and without some of the visual artifacts.
« Last Edit: February 25, 2022, 02:02:20 pm by Phlashlite »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simplex Noise Anyone?
« Reply #7 on: February 25, 2022, 01:57:54 pm »
Well good job so far. I looked at Simplex and said to myself, "Nah!" So you are braver than I. :)

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Simplex Noise Anyone?
« Reply #8 on: February 25, 2022, 02:04:44 pm »
Well good job so far. I looked at Simplex and said to myself, "Nah!" So you are braver than I. :)

Are you serious?  You look at the Perlin noise algorithm for the clouds you sent me???

I had to make a flow chart to decipher the program flow!!! LOL

And Toshi was no help either!  That guy's stuff is amazing btw.
« Last Edit: February 25, 2022, 02:26:09 pm by Phlashlite »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simplex Noise Anyone?
« Reply #9 on: February 25, 2022, 02:30:25 pm »
Ha! I was wondering if that inspired this.

Well was no work for me because I just translated from Yabasic to QB64 maybe added a mod or 2, _vince saved the day by getting it to move like clouds.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Simplex Noise Anyone?
« Reply #10 on: February 25, 2022, 06:25:24 pm »
Are you sure that's not a Hunter Biden painting? Because it looks like a Hunter Biden Painting.

Pete

If graphics was my thing I'd have a thingectomy.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Simplex Noise Anyone?
« Reply #11 on: February 25, 2022, 06:47:52 pm »
Ha! I was wondering if that inspired this.

Well was no work for me because I just translated from Yabasic to QB64 maybe added a mod or 2, _vince saved the day by getting it to move like clouds.

Yeah... it was pretty slick.

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Simplex Noise Anyone?
« Reply #12 on: February 25, 2022, 06:49:16 pm »
Are you sure that's not a Hunter Biden painting? Because it looks like a Hunter Biden Painting.

Pete

If graphics was my thing I'd have a thingectomy.

hahaha

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Simplex Noise Anyone?
« Reply #13 on: February 25, 2022, 06:54:05 pm »
:D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/