Author Topic: Putting it together, Rolling Clouds  (Read 1525 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline Phlashlite

  • Newbie
  • Posts: 50
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.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Putting it together, Rolling Clouds
« Reply #1 on: February 27, 2022, 12:02:50 pm »
I like that angle of perspective you are going with.

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.

Offline Phlashlite

  • Newbie
  • Posts: 50
Re: Putting it together, Rolling Clouds
« Reply #2 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. :)

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
Re: Putting it together, Rolling Clouds
« Reply #3 on: February 27, 2022, 02:52:16 pm »
Cool... Graveyard and some creepy music... hey...  Nicely done!
Logic is the beginning of wisdom.

Marked as best answer by Phlashlite on February 28, 2022, 04:18:03 am

Offline Phlashlite

  • Newbie
  • Posts: 50
Re: Putting it together, Rolling Clouds
« Reply #4 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.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Putting it together, Rolling Clouds
« Reply #5 on: February 28, 2022, 09:35:14 am »
Yes sir, coming along nicely.