Created
April 19, 2021 22:38
-
-
Save volfegan/3a1fd4e9b4a42bdcc3a3ba3842ca449a to your computer and use it in GitHub Desktop.
Basic implementations of colour metaballs and meta-diamonds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Reference | |
//Coding Train: https://youtu.be/ccYLb7cLB1I | |
//https://github.com/CodingTrain/website/tree/main/CodingChallenges/CC_028_MetaBalls/Processing/CC_028_MetaBalls | |
//https://www.gamedev.net/articles/programming/graphics/exploring-metaballs-and-isosurfaces-in-2d-r2556/ | |
Blob[] blobs = new Blob[20]; | |
float min=1; | |
void setup() { | |
size(720, 360); | |
colorMode(HSB); | |
for (int i = 0; i < blobs.length; i++) { | |
blobs[i] = new Blob(random(width), random(height), i); | |
} | |
} | |
void draw() { | |
clear(); | |
loadPixels(); | |
for (int x = 0; x < width; x++) { | |
for (int y = 0; y < height; y++) { | |
float hue, saturation, brightness; | |
float sum = 0; | |
for (Blob b : blobs) { | |
if (b.order > blobs.length/4) { | |
sum += b.r*b.r / ((x-b.pos.x)*(x-b.pos.x) + (y-b.pos.y)*(y-b.pos.y));//metaball | |
} else { | |
sum += b.r / (2*abs(x-b.pos.x) + 2*abs(y-b.pos.y));//Meta-Diamond | |
} | |
} | |
hue = constrain(sum*255, 10, 50);//blue-cyan cores constrain(sum*255, 100, 150); | |
saturation = constrain(sum*255, 100, 255); | |
if (min*2 > sum) continue; | |
else brightness = sum*69; | |
int index = x + y * width; | |
pixels[index] = color(hue, saturation, brightness); | |
} | |
} | |
updatePixels(); | |
for (Blob b : blobs) { | |
b.update(); | |
//b.show(); | |
} | |
} | |
class Blob { | |
PVector pos; | |
float r; | |
PVector vel; | |
int order; | |
Blob(float x, float y, int number) { | |
this.order = number; | |
this.pos = new PVector(x, y); | |
this.vel = PVector.random2D(); | |
this.vel.mult(random(1, 3)); | |
this.r = random(40, 7); | |
} | |
void update() { | |
this.pos.add(vel); | |
if (this.pos.x > width || this.pos.x < 0) { | |
this.vel.x *= -1; | |
} | |
if (this.pos.y > height || this.pos.y < 0) { | |
this.vel.y *= -1; | |
} | |
} | |
void show() { | |
noFill(); | |
stroke(200); | |
circle(this.pos.x, this.pos.y, this.r*2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment