QB64.org Forum

Active Forums => Programs => Topic started by: Phlashlite on January 30, 2022, 10:31:41 am

Title: Worley Noise Demo
Post by: Phlashlite on January 30, 2022, 10:31:41 am
Ported Worley Noise generator. Enjoy!

-Phlashlite



Code: QB64: [Select]
  1. _TITLE "Worley Noise Demo"
  2.  
  3. 'Based on:
  4. 'Coding in the Cabana
  5. 'The Coding Train / Daniel SULffman
  6. 'https://thecodingtrain.com/CodingInTheCabana/004-worley-noise.html
  7. '[youtube]https://youtu.be/4066MndcyCk[/youtube]
  8.  
  9. 'QB64 Port by Phlashlite
  10. 'Other resources used: https://www.carljohanrosen.com/share/CellNoiseAndProcessing.pdf
  11.  
  12. 'This version only calculates noise for a 2D Vector.  But, a third dimension (or more)
  13. 'could easily be added.
  14.  
  15.  
  16. TYPE Coordinates
  17.     cx AS INTEGER
  18.     cy AS INTEGER
  19.  
  20. '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  21. CONST WDTH = 400
  22. CONST HGHT = 400
  23. CONST CellCount = 20 'Number of random points to scatter
  24. CONST Nth = 1 'Adjustable within the number of CellPnts(CellCount), -> 1 to CellCount - 1
  25. '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  26.  
  27. DIM SHARED CellPnts(CellCount) AS Coordinates
  28. DIM SHARED Distances(CellCount) AS INTEGER
  29.  
  30. Start = LBOUND(Distances) '__________________Used for sorting in SUB Main...
  31. Finish = UBOUND(Distances) '_________________... ... .... ^
  32.  
  33. '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  34. 'The greater GradS, the lighter the overall feel is.  Playing with these adjusts
  35. 'the "tightnes" of the gradient between light and dark.  The larger the delta,
  36. 'the smoother the gradient
  37.  
  38. GradS = 200 '_______________________________Used to adjust color gradient...  (< 0)
  39. GradF = -100 '________________________________in SUB Main, Map! Function call (<-> 0)
  40. '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  41.  
  42.     Setup
  43.     Main
  44.  
  45.     'Demo random settings and text.___________________________________________________
  46.     GradS = Rndm(1, 300)
  47.     GradF = Rndm(-100, 500)
  48.     PRINT "IM STILL WORKING!  Give me a few seconds... "
  49.     _DISPLAY
  50.     '__________________________________________________________________________
  51.  
  52.  
  53. SUB Setup
  54.     SCREEN _NEWIMAGE(WDTH, HGHT, 32)
  55.  
  56.     'Distribute the random points
  57.     FOR i = 1 TO CellCount
  58.         CellPnts(i).cx = RND * WDTH
  59.         CellPnts(i).cy = RND * HGHT
  60.  
  61.         'Visual queue, not "required". Turn off "_DISPLAY" below to see these.
  62.         CIRCLE (CellPnts(i).cx, CellPnts(i).cy), 2, _RGB(255, 0, 0)
  63.     NEXT
  64.  
  65.  
  66. SUB Main
  67.     'Where the magic happens.
  68.  
  69.     'Adjust STEP to play with an interlaced or screen grid type effect.
  70.     FOR x = 0 TO WDTH STEP 1 '___________________________From every screen point...
  71.         FOR y = 0 TO HGHT STEP 1 '_______________________... ... ... ^
  72.             FOR i = 1 TO CellCount STEP 1 '______________To every "CellPnts"
  73.  
  74.                 'Check the distance between and store value in Distances()
  75.                 Distances(i) = dist(x, y, CellPnts(i).cx, CellPnts(i).cy)
  76.  
  77.                 'Sort Distances()
  78.                 CALL QuickSort(Start, Finish, Distances())
  79.  
  80.                 '"Map!" the initial distance() between a specific screen point and a
  81.                 'CellPnts to a user definable distance.  Affects the color .gradient
  82.                 '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  83.                 Gradient = map!(Distances(Nth), Distances(Start), Distances(Finish), GradS, GradF)
  84.  
  85.                 'Draw it
  86.                 PSET (x, y), _RGB(Gradient, Gradient, Gradient)
  87.                 '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  88.             NEXT
  89.         NEXT
  90.     NEXT
  91.  
  92.     _DISPLAY 'Comment this line to watch the screen be drawn.  It's slooooow.
  93.  
  94.  
  95.  
  96. '______________________________________________________________________________
  97.  
  98. ' Map function I found or translated from somewhere.
  99. ' The Coding Train" guy on youtube, where I translated the rain code from explained it in one of his videos.
  100.  
  101. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  102.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  103.  
  104. '______________________________________________________________________________
  105. FUNCTION dist (x1, y1, x2, y2)
  106.     'The dist() function calculates the Euclidean distance between two points in 2D.
  107.     '2D formula:
  108.     '  dist = sqr{(x1-x2)^2+(y1-y2)^2}
  109.     dist = SQR((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
  110. '______________________________________________________________________________
  111.  
  112. 'Standard random numbers in a range of mn! (min) to mx! (max)
  113.  
  114. FUNCTION Rndm! (mn!, mx!)
  115.     IF mn! > mx! THEN
  116.         SWAP mn!, mx!
  117.     END IF
  118.     Rndm! = RND * (mx! - mn!) + mn!
  119.  
  120. '______________________________________________________________________________
  121. SUB QuickSort (start AS INTEGER, finish AS INTEGER, array() AS INTEGER)
  122.     'Straight from the QB64 wiki
  123.     DIM Hi AS INTEGER, Lo AS INTEGER, Middle AS INTEGER
  124.     Hi = finish
  125.     Lo = start
  126.     Middle = array((Lo + Hi) / 2) 'find middle of array
  127.  
  128.     DO
  129.         DO WHILE array(Lo) < Middle
  130.             Lo = Lo + 1
  131.         LOOP
  132.  
  133.         DO WHILE array(Hi) > Middle
  134.             Hi = Hi - 1
  135.         LOOP
  136.  
  137.         IF Lo <= Hi THEN
  138.             SWAP array(Lo), array(Hi)
  139.             Lo = Lo + 1
  140.             Hi = Hi - 1
  141.         END IF
  142.     LOOP UNTIL Lo > Hi
  143.  
  144.     IF Hi > start THEN CALL QuickSort(start, Hi, array())
  145.     IF Lo < finish THEN CALL QuickSort(Lo, finish, array())
  146.  
  147.  
  148. '______________________________________________________________________________
  149.  
  150. 'Original P5js code:
  151.  
  152. '// Worley Noise
  153. '// Coding in the Cabana
  154. '// The Coding Train / Daniel SULffman
  155. '// https://thecodingtrain.com/CodingInTheCabana/004-worley-noise.html
  156. '// [youtube]https://youtu.be/4066MndcyCk[/youtube]
  157. '// p5 port: https://editor.p5js.org/codingtrain/sketches/QsiCWVczZ
  158.  
  159. 'function setup() {
  160. '  createCanvas(100, 100);
  161. '  pixelDensity(1);
  162. '  for (let i = 0; i < 20; i++) {
  163. '    points[i] = createVector(random(width), random(height), random(width));
  164. '  }
  165. '}
  166.  
  167. 'function draw() {
  168. '  LLadPixels();
  169. '  for (let x = 0; x < width; x++) {
  170. '    for (let y = 0; y < height; y++) {
  171. '      let distances = [];
  172. '      for (let i = 0; i < points.length; i++) {
  173. '        let v = points[i];
  174. '        let z = frameCount % width;
  175. '        let d = dist(x, y, z, v.x, v.y, v.z);
  176. '        distances[i] = d;
  177. '      }
  178. '      let sorted = sort(distances);
  179. '      let r = map(sorted[0], 0, 150, 0, 255);
  180. '      let g = map(sorted[1], 0, 50, 255, 0);
  181. '      let b = map(sorted[2], 0, 200, 255, 0);
  182. '      let index = (x + y * width) * 4;
  183. '      pixels[index + 0] = r;
  184. '      pixels[index + 1] = g;
  185. '      pixels[index + 2] = b;
  186. '      pixels[index + 3] = 255;
  187. '    }
  188. '  }
  189. '  updatePixels();
  190. '}
  191.  
Title: Re: Worley Noise Demo
Post by: bplus on January 30, 2022, 11:25:39 am
Ha, that reminds me of this: https://qb64forum.alephc.xyz/index.php?topic=3890.msg132266#msg132266

Playing with Voronoi code.
Title: Re: Worley Noise Demo
Post by: Phlashlite on January 30, 2022, 11:38:00 am
Ha, that reminds me of this: https://qb64forum.alephc.xyz/index.php?topic=3890.msg132266#msg132266

Playing with Voronoi code.

@bplus
Yep, they are very similar!  I am working on cloud textures... But this is very slow.  Both the education and the code... LOL
Title: Re: Worley Noise Demo
Post by: bplus on January 30, 2022, 11:44:54 am
You know I think there is a fractal for cloud making, @Phlashlite is that different than what you're after?
Title: Re: Worley Noise Demo
Post by: Phlashlite on January 30, 2022, 11:46:50 am
You know I think there is a fractal for cloud making, @Phlashlite is that different than what you're after?

Don't know.  Haven't seen it... LOL.  Maybe.
Title: Re: Worley Noise Demo
Post by: Phlashlite on January 30, 2022, 12:11:34 pm
@bplus

Like a Diamond-square algorithm?  Possibly.
Title: Re: Worley Noise Demo
Post by: bplus on January 30, 2022, 01:00:41 pm
Yeah checking it now, but QB64 is bugging out with a recursive attempt, so after lunch I will try a non recursive effort which I am pretty sure might work.
Title: Re: Worley Noise Demo
Post by: bplus on January 31, 2022, 12:35:44 pm
I think I am getting closer, my original try with recursion was buggy as heck and then I discovered I had to color the corners of the new squares being used to determine middle dot, so now it's almost looking like clouds.

Clouds Picasso style!
Code: QB64: [Select]
  1. Screen _NewImage(800, 600, 32)
  2. DefLng A-Z
  3. 'seed
  4. fcirc 0, 0, 2, _RGB32(200)
  5. fcirc _Width - 1, 0, 2, _RGB32(164)
  6. fcirc _Width - 1, _Height - 1, 2, _RGB32(184)
  7. fcirc 0, _Height - 1, 2, _RGB32(128)
  8.  
  9.     Cls
  10.     fcirc 0, 0, 2, _RGB32(200)
  11.     fcirc _Width - 1, 0, 2, _RGB32(164)
  12.     fcirc _Width - 1, _Height - 1, 2, _RGB32(184)
  13.     fcirc 0, _Height - 1, 2, _RGB32(128)
  14.  
  15.     cloud 0, 0, _Width - 1, _Height - 1
  16.     _Display
  17.     _Limit 1
  18.  
  19. Sub cloud (x1, y1, x2, y2) ' corners of square or rect
  20.     Dim c As Long
  21.     mx = (x1 + x2) / 2
  22.     my = (y1 + y2) / 2
  23.     If (mx <= x1 + 1) Or (my <= y + 1) Then Exit Sub
  24.     c = _Red32(Point(x1, y1)) + _Red32(Point(x2, y1)) + _Red32(Point(x1, y2)) + _Red32(Point(x2, y2))
  25.     If Rnd < .50 Then fcirc mx, my, 3, _RGB32((c + c / 4 + Rnd * 64 - 32) / 5) Else fcirc mx, my, 2, _RGB32(c)
  26.     'AHA! need to color corners of future squares  NOT just the middle!
  27.     fcirc mx, y1, 3, _RGB32((c + c / 4 + Rnd * 64 - 32) / 5)
  28.     fcirc x1, my, 3, _RGB32((c + c / 4 + Rnd * 64 - 32) / 5)
  29.     fcirc x2, my, 3, _RGB32((c + c / 4 + Rnd * 64 - 32) / 5)
  30.     fcirc mx, y2, 3, _RGB32((c + c / 4 + Rnd * 64 - 32) / 5)
  31.     cloud x1, y1, mx, my
  32.     cloud mx, y1, x2, my
  33.     cloud x1, my, mx, y2
  34.     cloud mx, my, x2, y2
  35.  
  36. Sub fcirc (CX As Long, CY As Long, R As Long, C As _Unsigned Long)
  37.     Dim Radius As Long, RadiusError As Long
  38.     Dim X As Long, Y As Long
  39.     Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
  40.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  41.     Line (CX - X, CY)-(CX + X, CY), C, BF
  42.     While X > Y
  43.         RadiusError = RadiusError + Y * 2 + 1
  44.         If RadiusError >= 0 Then
  45.             If X <> Y + 1 Then
  46.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  47.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  48.             End If
  49.             X = X - 1
  50.             RadiusError = RadiusError - X * 2
  51.         End If
  52.         Y = Y + 1
  53.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  54.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  55.     Wend
  56.  
  57.  
  58.  

Needs some blending, shmearing :)
Title: Re: Worley Noise Demo
Post by: _vince on January 31, 2022, 12:56:27 pm
very opulent flooring!
Title: Re: Worley Noise Demo
Post by: bplus on January 31, 2022, 02:44:13 pm
very opulent flooring!

Yeah that maybe a better app than trying to make clouds. I tried smear but the blocks are too distinct to blur out easily.
Title: Re: Worley Noise Demo
Post by: Phlashlite on February 01, 2022, 10:46:59 pm
@bplus  I'll have to study it.  It might work for what I'm looking for... with some modification of course!... Thanks!


Clouds Picasso style!

Title: Re: Worley Noise Demo
Post by: bplus on February 02, 2022, 05:43:32 am
Perlin Noise might be better approach.
Title: Re: Worley Noise Demo
Post by: FellippeHeitor on February 02, 2022, 06:38:41 am
Code: QB64: [Select]
  1. 'noise function related variables
  2. DIM SHARED perlin_octaves AS SINGLE, perlin_amp_falloff AS SINGLE
  3.  
  4. DIM SHARED noiseScale AS _FLOAT
  5. noiseScale = 0.02
  6.  
  7. SCREEN _NEWIMAGE(300, 300, 32)
  8.  
  9.     CLS , _RGB32(51)
  10.  
  11.     COLOR _RGB32(255)
  12.     _PRINTSTRING (0, 0), "Move your mouse"
  13.  
  14.     FOR x = 0 TO _WIDTH
  15.         noiseVal = noise((_MOUSEX + x) * noiseScale, _MOUSEY * noiseScale, 0)
  16.         COLOR _RGB32(noiseVal * 255)
  17.         LINE (x, _MOUSEY + noiseVal * 80)-(x, _HEIGHT)
  18.     NEXT
  19.     _DISPLAY
  20.     _LIMIT 60
  21.  
  22. FUNCTION noise! (x AS SINGLE, y AS SINGLE, z AS SINGLE)
  23.     STATIC p5NoiseSetup AS _BYTE
  24.     STATIC perlin() AS SINGLE
  25.     STATIC PERLIN_YWRAPB AS SINGLE, PERLIN_YWRAP AS SINGLE
  26.     STATIC PERLIN_ZWRAPB AS SINGLE, PERLIN_ZWRAP AS SINGLE
  27.     STATIC PERLIN_SIZE AS SINGLE
  28.  
  29.     CONST true = -1, false = 0
  30.  
  31.     IF NOT p5NoiseSetup THEN
  32.         p5NoiseSetup = true
  33.  
  34.         PERLIN_YWRAPB = 4
  35.         PERLIN_YWRAP = INT(1 * (2 ^ PERLIN_YWRAPB))
  36.         PERLIN_ZWRAPB = 8
  37.         PERLIN_ZWRAP = INT(1 * (2 ^ PERLIN_ZWRAPB))
  38.         PERLIN_SIZE = 4095
  39.  
  40.         perlin_octaves = 4
  41.         perlin_amp_falloff = 0.5
  42.  
  43.         REDIM perlin(PERLIN_SIZE + 1) AS SINGLE
  44.         DIM i AS SINGLE
  45.         FOR i = 0 TO PERLIN_SIZE + 1
  46.             perlin(i) = RND
  47.         NEXT
  48.     END IF
  49.  
  50.     x = ABS(x)
  51.     y = ABS(y)
  52.     z = ABS(z)
  53.  
  54.     DIM xi AS SINGLE, yi AS SINGLE, zi AS SINGLE
  55.     xi = INT(x)
  56.     yi = INT(y)
  57.     zi = INT(z)
  58.  
  59.     DIM xf AS SINGLE, yf AS SINGLE, zf AS SINGLE
  60.     xf = x - xi
  61.     yf = y - yi
  62.     zf = z - zi
  63.  
  64.     DIM r AS SINGLE, ampl AS SINGLE, o AS SINGLE
  65.     r = 0
  66.     ampl = .5
  67.  
  68.     FOR o = 1 TO perlin_octaves
  69.         DIM of AS SINGLE, rxf AS SINGLE
  70.         DIM ryf AS SINGLE, n1 AS SINGLE, n2 AS SINGLE, n3 AS SINGLE
  71.         of = xi + INT(yi * (2 ^ PERLIN_YWRAPB)) + INT(zi * (2 ^ PERLIN_ZWRAPB))
  72.  
  73.         rxf = 0.5 * (1.0 - COS(xf * _PI))
  74.         ryf = 0.5 * (1.0 - COS(yf * _PI))
  75.  
  76.         n1 = perlin(of AND PERLIN_SIZE)
  77.         n1 = n1 + rxf * (perlin((of + 1) AND PERLIN_SIZE) - n1)
  78.         n2 = perlin((of + PERLIN_YWRAP) AND PERLIN_SIZE)
  79.         n2 = n2 + rxf * (perlin((of + PERLIN_YWRAP + 1) AND PERLIN_SIZE) - n2)
  80.         n1 = n1 + ryf * (n2 - n1)
  81.  
  82.         of = of + PERLIN_ZWRAP
  83.         n2 = perlin(of AND PERLIN_SIZE)
  84.         n2 = n2 + rxf * (perlin((of + 1) AND PERLIN_SIZE) - n2)
  85.         n3 = perlin((of + PERLIN_YWRAP) AND PERLIN_SIZE)
  86.         n3 = n3 + rxf * (perlin((of + PERLIN_YWRAP + 1) AND PERLIN_SIZE) - n3)
  87.         n2 = n2 + ryf * (n3 - n2)
  88.  
  89.         n1 = n1 + (0.5 * (1.0 - COS(zf * _PI))) * (n2 - n1)
  90.  
  91.         r = r + n1 * ampl
  92.         ampl = ampl * perlin_amp_falloff
  93.         xi = INT(xi * (2 ^ 1))
  94.         xf = xf * 2
  95.         yi = INT(yi * (2 ^ 1))
  96.         yf = yf * 2
  97.         zi = INT(zi * (2 ^ 1))
  98.         zf = zf * 2
  99.  
  100.         IF xf >= 1.0 THEN xi = xi + 1: xf = xf - 1
  101.         IF yf >= 1.0 THEN yi = yi + 1: yf = yf - 1
  102.         IF zf >= 1.0 THEN zi = zi + 1: zf = zf - 1
  103.     NEXT
  104.     noise! = r
  105.  
  106. SUB noiseDetail (lod!, falloff!)
  107.     IF lod! > 0 THEN perlin_octaves = lod!
  108.     IF falloff! > 0 THEN perlin_amp_falloff = falloff!
  109.  
  110. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  111.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  112.  

From https://github.com/AshishKingdom/p5js.bas/blob/master/p5js.bas
Title: Re: Worley Noise Demo
Post by: bplus on February 02, 2022, 11:32:49 am
 
Code: QB64: [Select]
  1. 'noise function related variables
  2. DIM SHARED perlin_octaves AS SINGLE, perlin_amp_falloff AS SINGLE
  3.  
  4. DIM SHARED noiseScale AS _FLOAT
  5. noiseScale = 0.02
  6.  
  7. SCREEN _NEWIMAGE(300, 300, 32)
  8.  
  9.     CLS , _RGB32(51)
  10.  
  11.     COLOR _RGB32(255)
  12.     _PRINTSTRING (0, 0), "Move your mouse"
  13.  
  14.     FOR x = 0 TO _WIDTH
  15.         noiseVal = noise((_MOUSEX + x) * noiseScale, _MOUSEY * noiseScale, 0)
  16.         COLOR _RGB32(noiseVal * 255)
  17.         LINE (x, _MOUSEY + noiseVal * 80)-(x, _HEIGHT)
  18.     NEXT
  19.     _DISPLAY
  20.     _LIMIT 60
  21.  
  22. FUNCTION noise! (x AS SINGLE, y AS SINGLE, z AS SINGLE)
  23.     STATIC p5NoiseSetup AS _BYTE
  24.     STATIC perlin() AS SINGLE
  25.     STATIC PERLIN_YWRAPB AS SINGLE, PERLIN_YWRAP AS SINGLE
  26.     STATIC PERLIN_ZWRAPB AS SINGLE, PERLIN_ZWRAP AS SINGLE
  27.     STATIC PERLIN_SIZE AS SINGLE
  28.  
  29.     CONST true = -1, false = 0
  30.  
  31.     IF NOT p5NoiseSetup THEN
  32.         p5NoiseSetup = true
  33.  
  34.         PERLIN_YWRAPB = 4
  35.         PERLIN_YWRAP = INT(1 * (2 ^ PERLIN_YWRAPB))
  36.         PERLIN_ZWRAPB = 8
  37.         PERLIN_ZWRAP = INT(1 * (2 ^ PERLIN_ZWRAPB))
  38.         PERLIN_SIZE = 4095
  39.  
  40.         perlin_octaves = 4
  41.         perlin_amp_falloff = 0.5
  42.  
  43.         REDIM perlin(PERLIN_SIZE + 1) AS SINGLE
  44.         DIM i AS SINGLE
  45.         FOR i = 0 TO PERLIN_SIZE + 1
  46.             perlin(i) = RND
  47.         NEXT
  48.     END IF
  49.  
  50.     x = ABS(x)
  51.     y = ABS(y)
  52.     z = ABS(z)
  53.  
  54.     DIM xi AS SINGLE, yi AS SINGLE, zi AS SINGLE
  55.     xi = INT(x)
  56.     yi = INT(y)
  57.     zi = INT(z)
  58.  
  59.     DIM xf AS SINGLE, yf AS SINGLE, zf AS SINGLE
  60.     xf = x - xi
  61.     yf = y - yi
  62.     zf = z - zi
  63.  
  64.     DIM r AS SINGLE, ampl AS SINGLE, o AS SINGLE
  65.     r = 0
  66.     ampl = .5
  67.  
  68.     FOR o = 1 TO perlin_octaves
  69.         DIM of AS SINGLE, rxf AS SINGLE
  70.         DIM ryf AS SINGLE, n1 AS SINGLE, n2 AS SINGLE, n3 AS SINGLE
  71.         of = xi + INT(yi * (2 ^ PERLIN_YWRAPB)) + INT(zi * (2 ^ PERLIN_ZWRAPB))
  72.  
  73.         rxf = 0.5 * (1.0 - COS(xf * _PI))
  74.         ryf = 0.5 * (1.0 - COS(yf * _PI))
  75.  
  76.         n1 = perlin(of AND PERLIN_SIZE)
  77.         n1 = n1 + rxf * (perlin((of + 1) AND PERLIN_SIZE) - n1)
  78.         n2 = perlin((of + PERLIN_YWRAP) AND PERLIN_SIZE)
  79.         n2 = n2 + rxf * (perlin((of + PERLIN_YWRAP + 1) AND PERLIN_SIZE) - n2)
  80.         n1 = n1 + ryf * (n2 - n1)
  81.  
  82.         of = of + PERLIN_ZWRAP
  83.         n2 = perlin(of AND PERLIN_SIZE)
  84.         n2 = n2 + rxf * (perlin((of + 1) AND PERLIN_SIZE) - n2)
  85.         n3 = perlin((of + PERLIN_YWRAP) AND PERLIN_SIZE)
  86.         n3 = n3 + rxf * (perlin((of + PERLIN_YWRAP + 1) AND PERLIN_SIZE) - n3)
  87.         n2 = n2 + ryf * (n3 - n2)
  88.  
  89.         n1 = n1 + (0.5 * (1.0 - COS(zf * _PI))) * (n2 - n1)
  90.  
  91.         r = r + n1 * ampl
  92.         ampl = ampl * perlin_amp_falloff
  93.         xi = INT(xi * (2 ^ 1))
  94.         xf = xf * 2
  95.         yi = INT(yi * (2 ^ 1))
  96.         yf = yf * 2
  97.         zi = INT(zi * (2 ^ 1))
  98.         zf = zf * 2
  99.  
  100.         IF xf >= 1.0 THEN xi = xi + 1: xf = xf - 1
  101.         IF yf >= 1.0 THEN yi = yi + 1: yf = yf - 1
  102.         IF zf >= 1.0 THEN zi = zi + 1: zf = zf - 1
  103.     NEXT
  104.     noise! = r
  105.  
  106. SUB noiseDetail (lod!, falloff!)
  107.     IF lod! > 0 THEN perlin_octaves = lod!
  108.     IF falloff! > 0 THEN perlin_amp_falloff = falloff!
  109.  
  110. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  111.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  112.  

From https://github.com/AshishKingdom/p5js.bas/blob/master/p5js.bas


Yeah I tried running p5js Perlin Noise when Ashish mentioned it at Discord. Wouldn't work possible from the 2.0 change in using variables with Function names.

But the above is only 1D, not cloudy at all, but reminds me of landscape building.

https://en.wikipedia.org/wiki/Perlin_noise
Mentions Dot Product which I dont know if you are using in p5js?
Title: Re: Worley Noise Demo
Post by: Phlashlite on February 02, 2022, 12:32:29 pm
@bplus  Actually, there is a method of using Perlin noise to distribute the points for the Worley noise that is interesting.

Also, if one is industrious, it can be sped up by 15% to 20% by avoiding unnecessary calculations as described in this paper:

 https://jcgt.org/published/0008/01/02/paper.pdf (https://jcgt.org/published/0008/01/02/paper.pdf)

I also tried 2 stepping the x,y loops which sped it up by a factor of 4... but I would need to fuzz the neighbors which might end up just killing those gains...

Anyway, I am working on it :).

I appreciate the interactions here.  Thanks all!
Title: Re: Worley Noise Demo
Post by: bplus on February 02, 2022, 12:36:56 pm
Here is one less Picasso and more moving:
Code: QB64: [Select]
  1. Screen _NewImage(800, 600, 32)
  2. DefLng A-Z
  3. 'seed
  4. fcirc 0, 0, 2, _RGB32(128, 128, 128, 200)
  5. fcirc _Width - 1, 0, 2, _RGB32(164, 164, 164, 200)
  6. fcirc _Width - 1, _Height - 1, 2, _RGB32(184, 184, 184, 200)
  7. fcirc 0, _Height - 1, 2, _RGB32(200, 200, 200, 200)
  8. cloud 0, 0, _Width - 1, _Height - 1, -1
  9.     cloud 0, 0, _Width - 1, _Height - 1, 0
  10.     _Display
  11.     _Limit 30
  12.  
  13. Sub cloud (x1, y1, x2, y2, initTF) ' corners of square or rect
  14.     Dim As Long c, ave, al
  15.     al = 255
  16.     mx = (x1 + x2) / 2
  17.     my = (y1 + y2) / 2
  18.     If (mx <= x1 + 1) Or (my <= y + 1) Then Exit Sub
  19.     c = (_Red32(Point(x1, y1)) + _Red32(Point(x2, y1)) + _Red32(Point(x1, y2)) + _Red32(Point(x2, y2))) / 4
  20.     ave = (5 * c + Rnd * 64 - 32) / 5
  21.     fcirc mx, my, 3, _RGB32(ave, ave, ave, al)
  22.     If init Then
  23.         'AHA! need to color corners of future squares  NOT just the middle!
  24.         fcirc mx, y1, 3, _RGB32(c + 32 * Rnd - 16, c + 32 * Rnd - 16, c + 32 * Rnd - 16, al)
  25.         fcirc x1, my, 3, _RGB32(c + 32 * Rnd - 16, c + 32 * Rnd - 16, c + 32 * Rnd - 16, al)
  26.         fcirc x2, my, 3, _RGB32(c + 32 * Rnd - 16, c + 32 * Rnd - 16, c + 32 * Rnd - 16, al)
  27.         fcirc mx, y2, 3, _RGB32(c + 32 * Rnd - 16, c + 32 * Rnd - 16, c + 32 * Rnd - 16, al)
  28.     End If
  29.     cloud x1, y1, mx, my, 0
  30.     cloud mx, y1, x2, my, 0
  31.     cloud x1, my, mx, y2, 0
  32.     cloud mx, my, x2, y2, 0
  33.  
  34. Sub fcirc (CX As Long, CY As Long, R As Long, C As _Unsigned Long)
  35.     Dim Radius As Long, RadiusError As Long
  36.     Dim X As Long, Y As Long
  37.     Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
  38.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  39.     Line (CX - X, CY)-(CX + X, CY), C, BF
  40.     While X > Y
  41.         RadiusError = RadiusError + Y * 2 + 1
  42.         If RadiusError >= 0 Then
  43.             If X <> Y + 1 Then
  44.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  45.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  46.             End If
  47.             X = X - 1
  48.             RadiusError = RadiusError - X * 2
  49.         End If
  50.         Y = Y + 1
  51.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  52.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  53.     Wend
  54.  
Title: Re: Worley Noise Demo
Post by: Phlashlite on February 02, 2022, 12:55:02 pm
Ooooh... That is close! :)
Title: Re: Worley Noise Demo
Post by: bplus on February 02, 2022, 01:02:33 pm
Yeah close, but not IT yet!
Title: Re: Worley Noise Demo
Post by: Phlashlite on February 02, 2022, 01:11:58 pm
I have to step through these things to figure them out...

I mean, I get the gist of it.. but I need details!

LOL!
Title: Re: Worley Noise Demo
Post by: FellippeHeitor on February 02, 2022, 01:26:11 pm
https://en.wikipedia.org/wiki/Perlin_noise
Mentions Dot Product which I dont know if you are using in p5js?

I have no idea. Whatever p5.js is doing, this is doing.
Title: Re: Worley Noise Demo
Post by: bplus on February 02, 2022, 02:04:58 pm
I have no idea. Whatever p5.js is doing, this is doing.

Yeah my version of p5js has several Noise examples 1, 2, 3 could be dimensions? So what you reassembled may have been only the first dimension?

Update: OK I ran p5js through QB64 v1.5 to test the 3 "Perlin Noise" samples, plus I show that my version of p5js for QB64 has errors. It shows the Perlins as variations of 1 dimension.
 
Title: Re: Worley Noise Demo
Post by: bplus on February 02, 2022, 10:16:20 pm
OK time to give your CPU's a workout! I found some pretty nice code from RetroBASIC forum by Galileo who found code from FreeBasic some years ago, I modified to get sky moving but a little jerky. Maybe with Mem tricks we can rewrite the noise array faster.

Code: QB64: [Select]
  1. _Title "Noise Texture Generator" ' trans Yabasic Port by Galileo to QB64 b+ 2022-02-02
  2. '//Noise texure generator
  3. '//Taken from
  4. '//http://www.student.kuleuven.be/~m0216922/CG/randomnoise.html/CG/randomnoise.html
  5. '//=======================================================================
  6. '// Ported from FreeBASIC to Yabasic by Galileo, 1/2018
  7. '// Original code: https://www.freebasic.net/forum/viewtopic.php?f=7&t=13842
  8.  
  9. Const twidth = 800, theight = 600, zoom = 128
  10. Dim Shared noise(twidth * theight) '//the noise array
  11. Dim Shared texture(twidth * theight) '//texture array
  12. Dim Shared pal(256) As _Unsigned Long '//color palette
  13.  
  14. Screen _NewImage(twidth, theight, 32)
  15. _ScreenMove 100, 100
  16. Dim x, y
  17.  
  18. MakePalette 255, 255, 255, 100, 100, 180
  19. GenerateNoise
  20.     For y = 0 To theight - 1
  21.         For x = 0 To twidth - 1
  22.             If x <> twidth - 1 Then
  23.                 noise(x + y * theight) = noise((x + 1) + y * theight)
  24.             Else
  25.                 If Rnd < .5 Then noise(x + y * theight) = Rnd Else noise(x + y * theight) = noise((x - 1) + y * theight)
  26.             End If
  27.         Next
  28.     Next
  29.     buildtexture
  30.     drawtexture
  31.     _Display
  32.  
  33. '//interpolation code by rattrapmax6
  34. Sub MakePalette (sr, sg, sb, er, eg, eb) ' (b+) start and end RGB's ? yes
  35.     Dim i, istart(3), iend(3), ishow(3), rend(3), interpol(3)
  36.  
  37.     interpol(0) = 255
  38.     istart(1) = sr
  39.     istart(2) = sg
  40.     istart(3) = sb
  41.     iend(1) = er
  42.     iend(2) = eg
  43.     iend(3) = eb
  44.     interpol(1) = (istart(1) - iend(1)) / interpol(0)
  45.     interpol(2) = (istart(2) - iend(2)) / interpol(0)
  46.     interpol(3) = (istart(3) - iend(3)) / interpol(0)
  47.     rend(1) = istart(1)
  48.     rend(2) = istart(2)
  49.     rend(3) = istart(3)
  50.  
  51.     For i = 0 To 255
  52.         ishow(1) = rend(1)
  53.         ishow(2) = rend(2)
  54.         ishow(3) = rend(3)
  55.  
  56.         pal(i) = _RGB32(ishow(1), ishow(2), ishow(3))
  57.  
  58.         rend(1) = rend(1) - interpol(1)
  59.         rend(2) = rend(2) - interpol(2)
  60.         rend(3) = rend(3) - interpol(3)
  61.     Next i
  62.  
  63. '//generates random noise.
  64. Sub GenerateNoise ()
  65.     Dim As Long x, y
  66.  
  67.     For x = 0 To twidth - 1
  68.         For y = 0 To theight - 1
  69.             noise(x + y * twidth) = Rnd
  70.         Next y
  71.     Next x
  72.  
  73. Function SmoothNoise (x, y)
  74.     '//get fractional part of x and y
  75.     Dim fractx, fracty, x1, y1, x2, y2, value
  76.     fractx = x - Int(x)
  77.     fracty = y - Int(y)
  78.  
  79.     '//wrap around
  80.     x1 = (Int(x) + twidth) Mod twidth
  81.     y1 = (Int(y) + theight) Mod theight
  82.  
  83.     '//neighbor values
  84.     x2 = (x1 + twidth - 1) Mod twidth
  85.     y2 = (y1 + theight - 1) Mod theight
  86.  
  87.     '//smooth the noise with bilinear interpolation
  88.     value = 0.0
  89.     value = value + fractx * fracty * noise(x1 + y1 * twidth)
  90.     value = value + fractx * (1 - fracty) * noise(x1 + y2 * twidth)
  91.     value = value + (1 - fractx) * fracty * noise(x2 + y1 * twidth)
  92.     value = value + (1 - fractx) * (1 - fracty) * noise(x2 + y2 * twidth)
  93.  
  94.     SmoothNoise = value
  95.  
  96. Function Turbulence (x, y, size)
  97.     Dim value, initialsize
  98.  
  99.     initialsize = size
  100.     While (size >= 1)
  101.         value = value + SmoothNoise(x / size, y / size) * size
  102.         size = size / 2.0
  103.     Wend
  104.     Turbulence = (128.0 * value / initialsize)
  105.  
  106. '//builds the texture.
  107. Sub buildtexture
  108.     Dim x, y
  109.  
  110.     For x = 0 To twidth - 1
  111.         For y = 0 To theight - 1
  112.             texture(x + y * twidth) = Turbulence(x, y, zoom)
  113.         Next y
  114.     Next x
  115.  
  116. '//draws texture to screen.
  117. Sub drawtexture ()
  118.     Dim x, y
  119.  
  120.     For x = 0 To twidth - 1
  121.         For y = 0 To theight - 1
  122.             PSet (x, y), pal(texture((x + y * twidth)))
  123.         Next y
  124.     Next x
  125.  
  126.  

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Worley Noise Demo
Post by: _vince on February 05, 2022, 06:57:26 am
nice, that is a lot of mingling of basics.  How would you rank your favourite BASICs?  Justbasic, freebasic, xbasic, qbasic, qb64, libertybasic, VB.NET etc
Title: Re: Worley Noise Demo
Post by: bplus on February 05, 2022, 09:06:43 am
nice, that is a lot of mingling of basics.  How would you rank your favourite BASICs?  Justbasic, freebasic, xbasic, qbasic, qb64, libertybasic, VB.NET etc

Using QB64 all the time now but first Eval function was in JB and 100 line Interpreter (with double parking) built from SmallBASIC Interpreter (no line numbers!).
Title: Re: Worley Noise Demo
Post by: _vince on February 06, 2022, 08:27:52 am
mod of a B+ mod!

Code: QB64: [Select]
  1. _Title "Noise Texture Generator" ' trans Yabasic Port by Galileo to QB64 b+ 2022-02-02
  2. '//Noise texure generator
  3. '//Taken from
  4. '//http://www.student.kuleuven.be/~m0216922/CG/randomnoise.html/CG/randomnoise.html
  5. '//=======================================================================
  6. '// Ported from FreeBASIC to Yabasic by Galileo, 1/2018
  7. '// Original code: https://www.freebasic.net/forum/viewtopic.php?f=7&t=13842
  8.  
  9. Const twidth = 800, theight = 600, zoom = 128
  10. Dim Shared noise(10*twidth * theight) '//the noise array
  11. Dim Shared texture(10*twidth * theight) '//texture array
  12. Dim Shared pal(256) As _Unsigned Long '//color palette
  13.  
  14. Screen _NewImage(twidth, theight, 32)
  15. _ScreenMove 100, 100
  16. Dim x, y
  17.  
  18. locate 1,1
  19. ? "please give us a few seconds"
  20.  
  21. MakePalette 255, 255, 255, 100, 100, 180
  22.  
  23. GenerateNoise
  24. buildtexture
  25.  
  26.  
  27. for i=0 to 9*(twidth )
  28.         drawtexture i
  29.         _limit 30
  30.         _display
  31.  
  32. 'Do
  33. '    For y = 0 To theight - 1
  34. '        For x = 0 To twidth - 1
  35. '            If x <> twidth - 1 Then
  36. '                noise(x + y * theight) = noise((x + 1) + y * theight)
  37. '            Else
  38. '                If Rnd < .5 Then noise(x + y * theight) = Rnd Else noise(x + y * theight) = noise((x - 1) + y * theight)
  39. '            End If
  40. '        Next
  41. '    Next
  42. '    buildtexture
  43. '    drawtexture
  44. '    _Display
  45. 'Loop Until _KeyDown(27)
  46.  
  47. '//interpolation code by rattrapmax6
  48. Sub MakePalette (sr, sg, sb, er, eg, eb) ' (b+) start and end RGB's ? yes
  49.     Dim i, istart(3), iend(3), ishow(3), rend(3), interpol(3)
  50.  
  51.     interpol(0) = 255
  52.     istart(1) = sr
  53.     istart(2) = sg
  54.     istart(3) = sb
  55.     iend(1) = er
  56.     iend(2) = eg
  57.     iend(3) = eb
  58.     interpol(1) = (istart(1) - iend(1)) / interpol(0)
  59.     interpol(2) = (istart(2) - iend(2)) / interpol(0)
  60.     interpol(3) = (istart(3) - iend(3)) / interpol(0)
  61.     rend(1) = istart(1)
  62.     rend(2) = istart(2)
  63.     rend(3) = istart(3)
  64.  
  65.     For i = 0 To 255
  66.         ishow(1) = rend(1)
  67.         ishow(2) = rend(2)
  68.         ishow(3) = rend(3)
  69.  
  70.         pal(i) = _RGB32(ishow(1), ishow(2), ishow(3))
  71.  
  72.         rend(1) = rend(1) - interpol(1)
  73.         rend(2) = rend(2) - interpol(2)
  74.         rend(3) = rend(3) - interpol(3)
  75.     Next i
  76.  
  77. '//generates random noise.
  78. Sub GenerateNoise ()
  79.     Dim As Long x, y
  80.  
  81.     For x = 0 To 10*twidth - 1
  82.         For y = 0 To theight - 1
  83.             noise(x + y * twidth) = Rnd
  84.         Next y
  85.     Next x
  86.  
  87. Function SmoothNoise (x, y)
  88.     '//get fractional part of x and y
  89.     Dim fractx, fracty, x1, y1, x2, y2, value
  90.     fractx = x - Int(x)
  91.     fracty = y - Int(y)
  92.  
  93.     '//wrap around
  94.     x1 = (Int(x) + 10*twidth) Mod twidth
  95.     y1 = (Int(y) + theight) Mod theight
  96.  
  97.     '//neighbor values
  98.     x2 = (x1 + 10*twidth - 1) Mod twidth
  99.     y2 = (y1 + theight - 1) Mod theight
  100.  
  101.     '//smooth the noise with bilinear interpolation
  102.     value = 0.0
  103.     value = value + fractx * fracty * noise(x1 + y1 * twidth)
  104.     value = value + fractx * (1 - fracty) * noise(x1 + y2 * twidth)
  105.     value = value + (1 - fractx) * fracty * noise(x2 + y1 * twidth)
  106.     value = value + (1 - fractx) * (1 - fracty) * noise(x2 + y2 * twidth)
  107.  
  108.     SmoothNoise = value
  109.  
  110. Function Turbulence (x, y, size)
  111.     Dim value, initialsize
  112.  
  113.     initialsize = size
  114.     While (size >= 1)
  115.         value = value + SmoothNoise(x / size, y / size) * size
  116.         size = size / 2.0
  117.     Wend
  118.     Turbulence = (128.0 * value / initialsize)
  119.  
  120. '//builds the texture.
  121. Sub buildtexture
  122.     Dim x, y
  123.  
  124.     For x = 0 To 10*twidth - 1
  125.         For y = 0 To theight - 1
  126.             texture(x + y * 10*twidth) = Turbulence(x, y, zoom)
  127.         Next y
  128.     Next x
  129.  
  130. '//draws texture to screen.
  131. Sub drawtexture (dx )
  132.     Dim x, y
  133.  
  134.     For x = 0 To twidth - 1
  135.         For y = 0 To theight - 1
  136.             PSet (x, y), pal(texture(((x + dx) + y * 10*twidth)))
  137.         Next y
  138.     Next x
  139.  
  140.  
Title: Re: Worley Noise Demo
Post by: bplus on February 06, 2022, 10:04:43 am
@_vince  yeah! nice mod much better!
Title: Re: Worley Noise Demo
Post by: Phlashlite on February 06, 2022, 11:57:23 am
Those are nice!  Thank you both!  Lots to process... :)
Title: Re: Worley Noise Demo
Post by: SierraKen on February 06, 2022, 05:37:20 pm
Really cool clouds! I tried to make my own mod with this but no success. I was trying to make it all different colors at once instead of just white and blue.
Title: Re: Worley Noise Demo
Post by: bplus on February 06, 2022, 09:39:20 pm
Really cool clouds! I tried to make my own mod with this but no success. I was trying to make it all different colors at once instead of just white and blue.

You can redefine the pallet making sub, put in your own 255 colors best if colors close to each other in number are also close in hue.