'Based on:
'Coding in the Cabana
'The Coding Train / Daniel SULffman
'https://thecodingtrain.com/CodingInTheCabana/004-worley-noise.html
'[youtube]https://youtu.be/4066MndcyCk[/youtube]
'QB64 Port by Phlashlite
'Other resources used: https://www.carljohanrosen.com/share/CellNoiseAndProcessing.pdf
'This version only calculates noise for a 2D Vector. But, a third dimension (or more)
'could easily be added.
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CONST CellCount
= 20 'Number of random points to scatter CONST Nth
= 1 'Adjustable within the number of CellPnts(CellCount), -> 1 to CellCount - 1 '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Start
= LBOUND(Distances
) '__________________Used for sorting in SUB Main...Finish
= UBOUND(Distances
) '_________________... ... .... ^
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'The greater GradS, the lighter the overall feel is. Playing with these adjusts
'the "tightnes" of the gradient between light and dark. The larger the delta,
'the smoother the gradient
GradS = 200 '_______________________________Used to adjust color gradient... (< 0)
GradF = -100 '________________________________in SUB Main, Map! Function call (<-> 0)
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Setup
Main
'Demo random settings and text.___________________________________________________
GradS = Rndm(1, 300)
GradF = Rndm(-100, 500)
PRINT "IM STILL WORKING! Give me a few seconds... " '__________________________________________________________________________
'Distribute the random points
CellPnts
(i
).cx
= RND * WDTH
CellPnts
(i
).cy
= RND * HGHT
'Visual queue, not "required". Turn off "_DISPLAY" below to see these.
CIRCLE (CellPnts
(i
).cx
, CellPnts
(i
).cy
), 2, _RGB(255, 0, 0)
'Where the magic happens.
'Adjust STEP to play with an interlaced or screen grid type effect.
FOR x
= 0 TO WDTH
STEP 1 '___________________________From every screen point... FOR y
= 0 TO HGHT
STEP 1 '_______________________... ... ... ^ FOR i
= 1 TO CellCount
STEP 1 '______________To every "CellPnts"
'Check the distance between and store value in Distances()
Distances(i) = dist(x, y, CellPnts(i).cx, CellPnts(i).cy)
'Sort Distances()
CALL QuickSort
(Start
, Finish
, Distances
())
'"Map!" the initial distance() between a specific screen point and a
'CellPnts to a user definable distance. Affects the color .gradient
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Gradient = map!(Distances(Nth), Distances(Start), Distances(Finish), GradS, GradF)
'Draw it
PSET (x
, y
), _RGB(Gradient
, Gradient
, Gradient
) '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
_DISPLAY 'Comment this line to watch the screen be drawn. It's slooooow.
'______________________________________________________________________________
' Map function I found or translated from somewhere.
' The Coding Train" guy on youtube, where I translated the rain code from explained it in one of his videos.
FUNCTION map!
(value!
, minRange!
, maxRange!
, newMinRange!
, newMaxRange!
) map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
'______________________________________________________________________________
'The dist() function calculates the Euclidean distance between two points in 2D.
'2D formula:
' dist = sqr{(x1-x2)^2+(y1-y2)^2}
dist
= SQR((x1
- x2
) * (x1
- x2
) + (y1
- y2
) * (y1
- y2
))'______________________________________________________________________________
'Standard random numbers in a range of mn! (min) to mx! (max)
Rndm!
= RND * (mx!
- mn!
) + mn!
'______________________________________________________________________________
'Straight from the QB64 wiki
Hi = finish
Lo = start
Middle = array((Lo + Hi) / 2) 'find middle of array
Lo = Lo + 1
Hi = Hi - 1
SWAP array
(Lo
), array
(Hi
) Lo = Lo + 1
Hi = Hi - 1
IF Hi
> start
THEN CALL QuickSort
(start
, Hi
, array
()) IF Lo
< finish
THEN CALL QuickSort
(Lo
, finish
, array
())
'______________________________________________________________________________
'Original P5js code:
'// Worley Noise
'// Coding in the Cabana
'// The Coding Train / Daniel SULffman
'// https://thecodingtrain.com/CodingInTheCabana/004-worley-noise.html
'// [youtube]https://youtu.be/4066MndcyCk[/youtube]
'// p5 port: https://editor.p5js.org/codingtrain/sketches/QsiCWVczZ
'function setup() {
' createCanvas(100, 100);
' pixelDensity(1);
' for (let i = 0; i < 20; i++) {
' points[i] = createVector(random(width), random(height), random(width));
' }
'}
'function draw() {
' LLadPixels();
' for (let x = 0; x < width; x++) {
' for (let y = 0; y < height; y++) {
' let distances = [];
' for (let i = 0; i < points.length; i++) {
' let v = points[i];
' let z = frameCount % width;
' let d = dist(x, y, z, v.x, v.y, v.z);
' distances[i] = d;
' }
' let sorted = sort(distances);
' let r = map(sorted[0], 0, 150, 0, 255);
' let g = map(sorted[1], 0, 50, 255, 0);
' let b = map(sorted[2], 0, 200, 255, 0);
' let index = (x + y * width) * 4;
' pixels[index + 0] = r;
' pixels[index + 1] = g;
' pixels[index + 2] = b;
' pixels[index + 3] = 255;
' }
' }
' updatePixels();
'}