Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Phlashlite

Pages: [1] 2 3 4
1
Programs / Re: Putting it together, Rolling Clouds
« on: February 28, 2022, 01:02:56 am »
Cool... Graveyard and some creepy music... hey...  Nicely done!

Thanks!... I'm still working on it though.  I have the speed turned up in order to see the cloud progression.  If you turn it down to something approching realistic, you can afford to turn up the iteration setting and get some really detailed clouds.  This thing has so many knobs to turn, that I could mess with it all night!... I haven't even got to adding the alpha channel and colors, or the 3rd noise dimension.

@bplus Here is the latest iteration.  I think its better. :)

Code: QB64: [Select]
  1. _TITLE "Rolling Clouds 1.0"
  2.  
  3. ' Ported to QB64 by: Phlashlite, February 20, 2022
  4. '
  5. '          Resource: 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. '
  9. '          Resource: Working with Simplex Noise
  10. '            Author: Christian Maher
  11. '               URL: https://cmaher.github.io/posts/working-with-simplex-noise/
  12. '
  13. '          Resource: FLRMAP - simple floormapper with diagram and explanation.
  14. '         Developer: Toshi Horie March 2001
  15. '          Based on: Floormapper Ecliptorial and Qasir's constant-z optimization explanation.
  16. '
  17. '
  18. ' Simplex noise is a method for constructing an n-dimensional noise function comparable to
  19. ' Perlin noise ("classic" noise) but with fewer directional artifacts and, in higher dimensions, a lower computational overhead. Ken Perlin designed the algorithm in 2001[1] to address the limitations of his classic noise function, especially in higher dimensions.
  20. ' The advantages of simplex noise over Perlin noise:
  21. '
  22. ' - Simplex noise has a lower computational complexity and requires fewer multiplications.
  23. ' - Simplex noise scales to higher dimensions (4D, 5D and up) with much less computational
  24. '   cost, the complexity is for dimensions instead of the of classic Noise.
  25. ' - Simplex noise has no noticeable directional artifacts.
  26. ' - Simplex noise has a well-defined and continuous gradient everywhere that can be computed
  27. '   quite cheaply.
  28. ' - Simplex noise is easy to implement in hardware
  29. '
  30. ' ~Wikipedia
  31. ' https://en.wikipedia.org/wiki/Simplex_noise#:~:text=Simplex%20noise%20is%20a%20method,dimensions%2C%20a%20lower%20computational%20overhead.
  32.  
  33.  
  34. DEFDBL A-Z
  35.  
  36. '==============================================================================
  37.  
  38. g3: 'gradient
  39. DATA 1,1,0
  40. DATA -1,1,0
  41. DATA 1,-1,0
  42. DATA -1,-1,0
  43. DATA 1,0,1
  44. DATA -1,0,1
  45. DATA 1,0,-1
  46. DATA -1,0,-1
  47. DATA 0,1,1
  48. DATA 0,-1,1
  49. DATA 0,1,-1
  50. DATA 0,-1,-1
  51.  
  52. TYPE TriBit 'data structure to hold gradient data
  53.     x AS INTEGER
  54.     y AS INTEGER
  55.     z AS INTEGER
  56. DIM SHARED grad3(12) AS TriBit
  57.  
  58. FOR g& = 0 TO 11
  59.     READ grad3(g&).x
  60.     READ grad3(g&).y
  61.     READ grad3(g&).z
  62. NEXT g&
  63.  
  64. '______________________________________________________________________________
  65.  
  66. p: 'permutation table
  67. DATA 151,160,137,091,090,015,131,013,201,095,096,053,194,233,007,225,140,036,103,030
  68. DATA 069,142,008,099,037,240,021,010,023,190,006,148,247,120,234,075,000,026,197,062
  69. DATA 094,252,219,203,117,035,011,032,057,177,033,088,237,149,056,087,174,020,125,136
  70. DATA 171,168,068,175,074,165,071,134,139,048,027,166,077,146,158,231,083,111,229,122
  71. DATA 060,211,133,230,220,105,092,041,055,046,245,040,244,102,143,054,065,025,063,161
  72. DATA 001,216,080,073,209,076,132,187,208,089,018,169,200,196,135,130,116,188,159,086
  73. DATA 164,100,109,198,173,186,003,064,052,217,226,250,124,123,005,202,038,147,118,126
  74. DATA 255,082,085,212,207,206,059,227,047,016,058,017,182,189,028,042,223,183,170,213
  75. DATA 119,248,152,002,044,154,163,070,221,153,101,155,167,043,172,009,129,022,039,253
  76. DATA 019,098,108,110,079,113,224,232,178,185,112,104,218,246,097,228,251,034,242,193
  77. DATA 238,210,144,012,191,179,162,241,081,051,145,235,249,014,239,107,049,192,214,031
  78. DATA 181,199,106,157,184,084,204,176,115,121,050,045,127,004,150,254,138,236,205,093
  79. DATA 222,114,067,029,024,072,243,141,128,195,078,066,215,061,156,180
  80. DATA 151,160,137,091,090,015,131,013,201,095,096,053,194,233,007,225,140,036,103,030
  81. DATA 069,142,008,099,037,240,021,010,023,190,006,148,247,120,234,075,000,026,197,062
  82. DATA 094,252,219,203,117,035,011,032,057,177,033,088,237,149,056,087,174,020,125,136
  83. DATA 171,168,068,175,074,165,071,134,139,048,027,166,077,146,158,231,083,111,229,122
  84. DATA 060,211,133,230,220,105,092,041,055,046,245,040,244,102,143,054,065,025,063,161
  85. DATA 001,216,080,073,209,076,132,187,208,089,018,169,200,196,135,130,116,188,159,086
  86. DATA 164,100,109,198,173,186,003,064,052,217,226,250,124,123,005,202,038,147,118,126
  87. DATA 255,082,085,212,207,206,059,227,047,016,058,017,182,189,028,042,223,183,170,213
  88. DATA 119,248,152,002,044,154,163,070,221,153,101,155,167,043,172,009,129,022,039,253
  89. DATA 019,098,108,110,079,113,224,232,178,185,112,104,218,246,097,228,251,034,242,193
  90. DATA 238,210,144,012,191,179,162,241,081,051,145,235,249,014,239,107,049,192,214,031
  91. DATA 181,199,106,157,184,084,204,176,115,121,050,045,127,004,150,254,138,236,205,093
  92. DATA 222,114,067,029,024,072,243,141,128,195,078,066,215,061,156,180
  93.  
  94. 'To remove the need for index wrapping, double the permutation table length
  95. DIM SHARED perm(512) AS INTEGER
  96.  
  97. FOR i& = 0 TO 511
  98.     READ p(i&)
  99.     perm(i&) = p(i&) AND 255
  100.  
  101. '==============================================================================
  102.  
  103. CONST WDTH = 800
  104. CONST HGHT = 600
  105. CONST COLORMODE = 32
  106.  
  107. '2D simplex noise. From: "Simplex noise demystified"___________________________
  108. CONST F2 = .5 * (SQR(3) - 1)
  109. CONST G2 = (3 - SQR(3)) / 6
  110. CONST G2x2 = G2 * 2
  111.  
  112. 'Settings for noise and brownian motion. From: "Working with Simplex Noise"____
  113. CONST SCALE = .009 '       Noise SCALE
  114. CONST ITERATIONS = 7 '     Affects the noise layers (octaves)
  115. CONST PERSISTENCE = 0.5 '  Amount each octave contributes to the noise structure
  116. CONST LOW = 25 '           Low end of point luminance
  117. CONST HIGH = 167 '         High end of luminance
  118. CONST SPEED = 1 '        Cloud (scroll) speed
  119.  
  120. 'From: FLRMAP__________________________________________________________________
  121. CONST H = 6 '              Height of PoV @ the plane of the "Y" axis
  122. CONST DPTH = .618 * HGHT ' Depth of the "Z" axis
  123. CONST HLV = -499 '         Horizontal Line between sky and GP
  124. CONST GP = 0 '             Ground Plane
  125.  
  126. DIM lut(HLV TO GP)
  127. DIM HF(HLV TO GP)
  128. DIM D(HLV TO GP)
  129. DIM Texture&(WDTH, HGHT)
  130.  
  131. HFV = -(WDTH * .7)
  132. FOR y& = HLV TO GP
  133.     lut(y&) = H / (H - y&)
  134.     HF(y&) = HFV * lut(y&)
  135.     D(y&) = DPTH * lut(y&)
  136. HL = 450
  137. vofs = 250
  138. '==============================================================================
  139. SCREEN _NEWIMAGE(WDTH, HGHT, COLORMODE)
  140.  
  141.     vofs = vofs + SPEED
  142.     FOR y& = HLV TO GP
  143.  
  144.         v = vofs + D(y&)
  145.         u = HF(y&)
  146.         du = lut(y&)
  147.  
  148.         FOR x& = 0 TO WDTH - 1
  149.  
  150.             'Load the texture array with noise
  151.             gs& = INT(map(sumOcatave(ITERATIONS, u, v + HL, PERSISTENCE, SCALE), -1, 1, LOW, HIGH))
  152.  
  153.             'Grayscale pallette
  154.             c& = _RGB(gs&, gs&, gs&)
  155.  
  156.             'Draw the screen
  157.             PSET (x&, y& + HL), c&
  158.  
  159.             u = u + du
  160.         NEXT
  161.     NEXT
  162.     _DISPLAY
  163.     '_LIMIT 30
  164. '==============================================================================
  165.  
  166.  
  167. FUNCTION noise2D (xin, yin) 'From: Simplex noise demystified___________________
  168.  
  169.     '' Skew the input space to determine which simplex cell we're in
  170.     s = (xin + yin) * F2
  171.     i& = INT(xin + s)
  172.     j& = INT(yin + s)
  173.  
  174.     t = (i& + j&) * G2
  175.  
  176.     ' Unskew the cell origin back to (x,y) space
  177.     BX0 = i& - t
  178.     BY0 = j& - t
  179.  
  180.     ' The x,y distances from the cell origin
  181.     x0 = xin - BX0
  182.     y0 = yin - BY0
  183.  
  184.  
  185.     ' For the 2D case, the simplex shape is an equilateral triangle
  186.     ' Determine which simplex we are in.
  187.     ' Offsets for second (middle) corner of simplex in (i,j) coords
  188.     IF x0 > y0 THEN
  189.         i1& = 1: j1& = 0
  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.     x1 = x0 - i1& + G2
  199.     y1 = y0 - j1& + G2
  200.  
  201.     ' Offsets for last corner in (x,y) unskewed coords
  202.     x2 = x0 - 1 + G2x2
  203.     y2 = y0 - 1 + G2x2
  204.  
  205.     ' Work out the hashed gradient indices of the three simplex corners
  206.     ii& = i& AND 255
  207.     jj& = j& AND 255
  208.     gi0& = perm(ii& + perm(jj&)) MOD 12
  209.     gi1& = perm(ii& + i1& + perm(jj& + j1&)) MOD 12
  210.     gi2& = perm(ii& + 1 + perm(jj& + 1)) MOD 12
  211.  
  212.  
  213.     ' Calculate the contribution from the three corners
  214.  
  215.     ' First corner
  216.     t0 = .5 - x0 * x0 - y0 * y0
  217.     IF t0 < 0 THEN
  218.         n0 = 0
  219.     ELSE
  220.         t0 = t0 * t0
  221.         n0 = t0 * t0 * DotP2(gi0&, x0, y0)
  222.     END IF
  223.  
  224.     ' Second corner
  225.     t1 = .5 - x1 * x1 - y1 * y1
  226.     IF t1 < 0 THEN
  227.         n1 = 0
  228.     ELSE
  229.         t1 = t1 * t1
  230.         n1 = t1 * t1 * DotP2(gi1&, x1, y1)
  231.     END IF
  232.  
  233.     ' Third corner
  234.     t2 = .5 - x2 * x2 - y2 * y2
  235.     IF t2 < 0 THEN
  236.         n2 = 0
  237.     ELSE
  238.         t2 = t2 * t2
  239.         n2 = t2 * t2 * DotP2(gi2&, x2, y2)
  240.     END IF
  241.  
  242.  
  243.     ' Add contributions from each corner to get the final noise value.
  244.     ' The result is scaled to return values in the interval [-1,1].
  245.     noise2D = 70 * (n0 + n1 + n2)
  246.  
  247.  
  248. ' From: "Working with Simplex Noise"___________________________________________
  249. FUNCTION sumOcatave (iterations, x, y, persistence, scale)
  250.  
  251.     DIM maxAmp
  252.     DIM amp
  253.     DIM freq
  254.     DIM noise
  255.  
  256.     noise = 0
  257.     maxAmp = 0
  258.     amp = 1.1
  259.     freq = scale
  260.  
  261.     ' Add successively smaller, higher-frequency terms
  262.     FOR i& = 0 TO iterations
  263.         noise = (noise2D(x * freq, y * freq) * amp) + noise
  264.         maxAmp = maxAmp + amp
  265.         amp = amp * persistence
  266.         freq = freq * 2
  267.     NEXT
  268.  
  269.     sumOcatave = noise
  270.  
  271. '2D DotProduct. From: Simplex noise demystified________________________________
  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
  277. ' explained it in one of his videos.
  278. FUNCTION map (value, minRange, maxRange, newMinRange, newMaxRange)
  279.     map = ((value - minRange) / (maxRange - minRange)) * (newMaxRange - newMinRange) + newMinRange
  280.  
  281.  

2
Programs / Re: Putting it together, Rolling Clouds
« on: February 27, 2022, 02:11:08 pm »
I wonder if you could do pixels at front end and multi pixels groups towards back to avoid blocks of one shade tiles eg use 1 pixel to represent groups, larger and larger going towards horizon line. Just a thought, probably too much more calcs.

I had to look really hard at Toshi's code to figure out roughly how it worked.  I think it works exactly opposite of what you are proposing. LOL... but try changing the "H" CONST to 20.

Anyway, it's still a work in progress. :)

3
Programs / Re: Domain Coloring
« on: February 27, 2022, 04:20:52 am »
Very nice!

4
Programs / Re: Ray Trace a translation from SpecBAS
« on: February 27, 2022, 04:19:38 am »
AWESOME!

5
Programs / Putting it together, Rolling Clouds
« on: February 27, 2022, 04:03:16 am »
Demo of some Simplex noise generated clouds on a rolling sky.  Put together with lots of other developers' code

@bplus , It has some issues... but it is starting to come together.

~Phlashlite

Code: QB64: [Select]
  1.  
  2. _TITLE "Rolling Clouds 1.0"
  3. ' Ported to QB64 by: Phlashlite, February 20, 2022
  4. '
  5. '          Resource: 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. '
  9. '          Resource: Working with Simplex Noise
  10. '            Author: Christian Maher
  11. '               URL: https://cmaher.github.io/posts/working-with-simplex-noise/
  12. '
  13. '          Resource: FLRMAP - simple floormapper with diagram and explanation.
  14. '         Developer: Toshi Horie March 2001
  15. '          Based on: Floormapper Ecliptorial and Qasir's constant-z optimization explanation.
  16. '
  17. '
  18. ' Simplex noise is a method for constructing an n-dimensional noise function comparable to
  19. ' Perlin noise ("classic" noise) but with fewer directional artifacts and, in higher dimensions, a lower computational overhead. Ken Perlin designed the algorithm in 2001[1] to address the limitations of his classic noise function, especially in higher dimensions.
  20. ' The advantages of simplex noise over Perlin noise:
  21. '
  22. ' - Simplex noise has a lower computational complexity and requires fewer multiplications.
  23. ' - Simplex noise scales to higher dimensions (4D, 5D and up) with much less computational
  24. '   cost, the complexity is for dimensions instead of the of classic Noise.
  25. ' - Simplex noise has no noticeable directional artifacts.
  26. ' - Simplex noise has a well-defined and continuous gradient everywhere that can be computed
  27. '   quite cheaply.
  28. ' - Simplex noise is easy to implement in hardware
  29. '
  30. ' ~Wikipedia
  31. ' https://en.wikipedia.org/wiki/Simplex_noise#:~:text=Simplex%20noise%20is%20a%20method,dimensions%2C%20a%20lower%20computational%20overhead.
  32.  
  33.  
  34. DEFDBL A-Z
  35.  
  36. '==============================================================================
  37.  
  38. g3: 'gradient
  39. DATA 1,1,0
  40. DATA -1,1,0
  41. DATA 1,-1,0
  42. DATA -1,-1,0
  43. DATA 1,0,1
  44. DATA -1,0,1
  45. DATA 1,0,-1
  46. DATA -1,0,-1
  47. DATA 0,1,1
  48. DATA 0,-1,1
  49. DATA 0,1,-1
  50. DATA 0,-1,-1
  51.  
  52. TYPE TriBit 'data structure to hold gradient data
  53.     x AS INTEGER
  54.     y AS INTEGER
  55.     z AS INTEGER
  56. DIM SHARED grad3(12) AS TriBit
  57.  
  58. FOR g& = 0 TO 11
  59.     READ grad3(g&).x
  60.     READ grad3(g&).y
  61.     READ grad3(g&).z
  62. NEXT g&
  63.  
  64. '______________________________________________________________________________
  65.  
  66. p: 'permutation table
  67. DATA 151,160,137,091,090,015,131,013,201,095,096,053,194,233,007,225,140,036,103,030
  68. DATA 069,142,008,099,037,240,021,010,023,190,006,148,247,120,234,075,000,026,197,062
  69. DATA 094,252,219,203,117,035,011,032,057,177,033,088,237,149,056,087,174,020,125,136
  70. DATA 171,168,068,175,074,165,071,134,139,048,027,166,077,146,158,231,083,111,229,122
  71. DATA 060,211,133,230,220,105,092,041,055,046,245,040,244,102,143,054,065,025,063,161
  72. DATA 001,216,080,073,209,076,132,187,208,089,018,169,200,196,135,130,116,188,159,086
  73. DATA 164,100,109,198,173,186,003,064,052,217,226,250,124,123,005,202,038,147,118,126
  74. DATA 255,082,085,212,207,206,059,227,047,016,058,017,182,189,028,042,223,183,170,213
  75. DATA 119,248,152,002,044,154,163,070,221,153,101,155,167,043,172,009,129,022,039,253
  76. DATA 019,098,108,110,079,113,224,232,178,185,112,104,218,246,097,228,251,034,242,193
  77. DATA 238,210,144,012,191,179,162,241,081,051,145,235,249,014,239,107,049,192,214,031
  78. DATA 181,199,106,157,184,084,204,176,115,121,050,045,127,004,150,254,138,236,205,093
  79. DATA 222,114,067,029,024,072,243,141,128,195,078,066,215,061,156,180
  80. DATA 151,160,137,091,090,015,131,013,201,095,096,053,194,233,007,225,140,036,103,030
  81. DATA 069,142,008,099,037,240,021,010,023,190,006,148,247,120,234,075,000,026,197,062
  82. DATA 094,252,219,203,117,035,011,032,057,177,033,088,237,149,056,087,174,020,125,136
  83. DATA 171,168,068,175,074,165,071,134,139,048,027,166,077,146,158,231,083,111,229,122
  84. DATA 060,211,133,230,220,105,092,041,055,046,245,040,244,102,143,054,065,025,063,161
  85. DATA 001,216,080,073,209,076,132,187,208,089,018,169,200,196,135,130,116,188,159,086
  86. DATA 164,100,109,198,173,186,003,064,052,217,226,250,124,123,005,202,038,147,118,126
  87. DATA 255,082,085,212,207,206,059,227,047,016,058,017,182,189,028,042,223,183,170,213
  88. DATA 119,248,152,002,044,154,163,070,221,153,101,155,167,043,172,009,129,022,039,253
  89. DATA 019,098,108,110,079,113,224,232,178,185,112,104,218,246,097,228,251,034,242,193
  90. DATA 238,210,144,012,191,179,162,241,081,051,145,235,249,014,239,107,049,192,214,031
  91. DATA 181,199,106,157,184,084,204,176,115,121,050,045,127,004,150,254,138,236,205,093
  92. DATA 222,114,067,029,024,072,243,141,128,195,078,066,215,061,156,180
  93.  
  94. 'To remove the need for index wrapping, double the permutation table length
  95. DIM SHARED perm(512) AS INTEGER
  96.  
  97. FOR i& = 0 TO 511
  98.     READ p(i&)
  99.     perm(i&) = p(i&) AND 255
  100.  
  101. '==============================================================================
  102.  
  103. CONST WDTH = 800
  104. CONST HGHT = 600
  105. CONST COLORMODE = 32
  106.  
  107. 'From: FLRMAP__________________________________________________________________
  108. CONST H = 5 '       Height of PoV @ the plane of the "Y" axis
  109. CONST DPTH = WDTH ' Depth of the "Z" axis
  110. CONST HLV = -500 '  Horizontal Line between sky and GP
  111. CONST GP = -10 '    Ground Plane
  112.  
  113. '2D simplex noise. From: "Simplex noise demystified"___________________________
  114. CONST F2 = .5 * (SQR(3) - 1)
  115. CONST G2 = (3 - SQR(3)) / 6
  116. CONST G2x2 = G2 * 2
  117.  
  118. 'Settings for noise and brownian motion. From: "Working with Simplex Noise"____
  119. CONST SCALE = .02 '       Noise SCALE
  120. CONST ITERATIONS = 5 '    Affects the noise layers (octaves)
  121. CONST PERSISTENCE = 0.5 ' Amount each octave contributes to the noise structure
  122. CONST LOW = 0 '           Low end of point luminance
  123. CONST HIGH = 196 '        High end of luminance
  124. CONST SPEED = .20 '       Cloud (scroll) speed
  125.  
  126. DIM lut(HLV TO GP)
  127. DIM HF(HLV TO GP)
  128. DIM D(HLV TO GP)
  129. DIM Texture&(WDTH, HGHT)
  130.  
  131. HFV! = -(WDTH * .5)
  132. FOR y& = HLV TO GP
  133.     lut(y&) = H / (H - y&)
  134.     HF(y&) = HFV * lut(y&)
  135.     D(y&) = DPTH * lut(y&)
  136. HL = 500
  137.  
  138. '==============================================================================
  139. SCREEN _NEWIMAGE(WDTH, HGHT, COLORMODE)
  140.  
  141.     vofs = vofs + SPEED
  142.     FOR y& = HLV TO GP
  143.  
  144.         v = vofs + D(y&)
  145.         u = HF(y&)
  146.         du = lut(y&)
  147.  
  148.         FOR x& = 0 TO WDTH - 1
  149.  
  150.             'Load the texture array with noise
  151.             Texture&(u, v) = map(sumOcatave(ITERATIONS, u, v + HL, PERSISTENCE, SCALE), -1, 1, LOW, HIGH)
  152.  
  153.             'Grayscale pallette
  154.             c& = _RGB(Texture&(u, v), Texture&(u, v), Texture&(u, v))
  155.  
  156.             'Draw the screen
  157.             PSET (x&, y& + HL), c&
  158.  
  159.             u = u + du
  160.         NEXT
  161.     NEXT
  162.     _DISPLAY
  163.     _LIMIT 30
  164. '==============================================================================
  165.  
  166.  
  167. FUNCTION noise2D (xin, yin) 'From: Simplex noise demystified___________________
  168.  
  169.     '' Skew the input space to determine which simplex cell we're in
  170.     s = (xin + yin) * F2
  171.     i& = INT(xin + s)
  172.     j& = INT(yin + s)
  173.  
  174.     t = (i& + j&) * G2
  175.  
  176.     ' Unskew the cell origin back to (x,y) space
  177.     BX0 = i& - t
  178.     BY0 = j& - t
  179.  
  180.     ' The x,y distances from the cell origin
  181.     x0 = xin - BX0
  182.     y0 = yin - BY0
  183.  
  184.  
  185.     ' For the 2D case, the simplex shape is an equilateral triangle
  186.     ' Determine which simplex we are in.
  187.     ' Offsets for second (middle) corner of simplex in (i,j) coords
  188.     IF x0 > y0 THEN
  189.         i1& = 1: j1& = 0
  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.     x1 = x0 - i1& + G2
  199.     y1 = y0 - j1& + G2
  200.  
  201.     ' Offsets for last corner in (x,y) unskewed coords
  202.     x2 = x0 - 1 + G2x2
  203.     y2 = y0 - 1 + G2x2
  204.  
  205.     ' Work out the hashed gradient indices of the three simplex corners
  206.     ii& = i& AND 255
  207.     jj& = j& AND 255
  208.     gi0& = perm(ii& + perm(jj&)) MOD 12
  209.     gi1& = perm(ii& + i1& + perm(jj& + j1&)) MOD 12
  210.     gi2& = perm(ii& + 1 + perm(jj& + 1)) MOD 12
  211.  
  212.  
  213.     ' Calculate the contribution from the three corners
  214.  
  215.     ' First corner
  216.     t0 = .5 - x0 * x0 - y0 * y0
  217.     IF t0 < 0 THEN
  218.         n0 = 0
  219.     ELSE
  220.         t0 = t0 * t0
  221.         n0 = t0 * t0 * DotP2(gi0&, x0, y0)
  222.     END IF
  223.  
  224.     ' Second corner
  225.     t1 = .5 - x1 * x1 - y1 * y1
  226.     IF t1 < 0 THEN
  227.         n1 = 0
  228.     ELSE
  229.         t1 = t1 * t1
  230.         n1 = t1 * t1 * DotP2(gi1&, x1, y1)
  231.     END IF
  232.  
  233.     ' Third corner
  234.     t2 = .5 - x2 * x2 - y2 * y2
  235.     IF t2 < 0 THEN
  236.         n2 = 0
  237.     ELSE
  238.         t2 = t2 * t2
  239.         n2 = t2 * t2 * DotP2(gi2&, x2, y2)
  240.     END IF
  241.  
  242.  
  243.     ' Add contributions from each corner to get the final noise value.
  244.     ' The result is scaled to return values in the interval [-1,1].
  245.     noise2D = 70 * (n0 + n1 + n2)
  246.  
  247.  
  248. ' From: "Working with Simplex Noise"___________________________________________
  249. FUNCTION sumOcatave (iterations, x&, y&, persistence, scale)
  250.  
  251.     DIM maxAmp
  252.     DIM amp
  253.     DIM freq
  254.     DIM noise
  255.  
  256.     noise = 0
  257.     maxAmp = 0
  258.     amp = 1
  259.     freq = scale
  260.  
  261.     ' Add successively smaller, higher-frequency terms
  262.     FOR i& = 0 TO iterations
  263.         noise = (noise2D(x& * freq, y& * freq) * amp) + noise
  264.         maxAmp = maxAmp + amp
  265.         amp = amp * persistence
  266.         freq = freq * 2
  267.     NEXT
  268.  
  269.     sumOcatave = noise
  270.  
  271. '2D DotProduct. From: Simplex noise demystified________________________________
  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
  277. ' explained it in one of his videos.
  278. FUNCTION map (value, minRange, maxRange, newMinRange, newMaxRange)
  279.     map = ((value - minRange) / (maxRange - minRange)) * (newMaxRange - newMinRange) + newMinRange
  280.  
  281.  

6
Programs / Re: Simplex Noise Anyone?
« 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

7
Programs / Re: Simplex Noise Anyone?
« 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.

8
Programs / Re: Simplex Noise Anyone?
« 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.

9
Programs / Re: Simplex Noise Anyone?
« 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.

10
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.  

11
Programs / Re: Simplex Noise Anyone?
« on: February 25, 2022, 01:30:40 pm »
Guess all I needed was a little motivation... and luck! :)

12
Programs / Re: Simplex Noise Anyone? SOLVED!
« 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

13
Programs / Re: Simplex Noise Anyone?
« 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"

14
Programs / 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.  

15
I think I can see Agent Smith.

That's a different program :)

Pages: [1] 2 3 4