Last active
October 26, 2023 11:01
-
-
Save KrabCode/d7f2c6c938e1acd2320062a3f842d3f7 to your computer and use it in GitHub Desktop.
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
import java.util.UUID; | |
ArrayList<P> ps = new ArrayList<P>(); | |
int pointCount = 1000; | |
PImage source; | |
private float t; | |
private int framesToCapture = 300; | |
private int captureStart = -1; | |
String id = ""; | |
public void settings() { | |
size(600, 600); | |
} | |
public void setup() { | |
reset(); | |
voronoiFilter(); | |
} | |
void reset() { | |
source = loadImage("https://picsum.photos/600/600.jpg"); | |
ps.clear(); | |
background(0); | |
while (ps.size() < pointCount) { | |
ps.add(new P()); | |
} | |
voronoiFilter(); | |
} | |
public void draw() { | |
t = map(frameCount, 0, framesToCapture, 0, TWO_PI); | |
for (P p : ps) { | |
p.myPixels.clear(); | |
p.update(); | |
} | |
voronoiFilter(); | |
if (captureStart > 0 && frameCount > captureStart && frameCount <= captureStart + framesToCapture) { | |
println((frameCount - captureStart) + " / " + framesToCapture); | |
saveFrame("capture/"+id+"/####.jpg"); | |
} | |
} | |
private void voronoiFilter() { | |
/* | |
background(0); | |
int i = 0; | |
for (P p : ps) { | |
if(i++ == 0){ | |
stroke(255); | |
}else{ | |
stroke(50); | |
} | |
strokeWeight(3); | |
point(p.pos.x, p.pos.y); | |
} | |
*/ | |
image(source, 0, 0, width, height); | |
loadPixels(); | |
for (int x = 0; x < width; x++) { | |
for (int y = 0; y < height; y++) { | |
P nearest = findNearestPoint(x, y); | |
nearest.myPixels.add(new PVector(x, y)); | |
} | |
} | |
for (P p : ps) { | |
int count = p.myPixels.size(); | |
int r = 0; | |
int g = 0; | |
int b = 0; | |
for (PVector px : p.myPixels) { | |
int c = pixels[floor(px.x) + floor(px.y) * width]; | |
r += c >> 16 & 0xFF; | |
g += c >> 8 & 0xFF; | |
b += c & 0xFF; | |
} | |
int averageColor = 0; | |
if (count >0) { | |
averageColor = color(r / count, g / count, b / count); | |
} | |
for (PVector px : p.myPixels) { | |
pixels[floor(px.x) + floor(px.y) * width] = color(averageColor); | |
} | |
} | |
updatePixels(); | |
filter(BLUR, .5f); | |
} | |
public void keyPressed() { | |
if (key == 'r') { | |
reset(); | |
} | |
if (key == 'k') { | |
// saveFrame("capture/####.jpg"); | |
id = UUID.randomUUID().toString(); | |
captureStart = frameCount+1; | |
} | |
} | |
P findNearestPoint(int x, int y) { | |
P result = null; | |
float smallestDistanceFound = width; | |
for (P p : ps) { | |
float d = dist(p.pos.x, p.pos.y, x, y); | |
if (d < smallestDistanceFound) { | |
smallestDistanceFound = d; | |
result = p; | |
} | |
} | |
return result; | |
} | |
class P { | |
PVector origPos = new PVector(random(width), random(height)); | |
PVector pos = new PVector(origPos.x, origPos.y); | |
ArrayList<PVector> myPixels = new ArrayList<PVector>(); | |
public void update() { | |
//simplex noise implementation from here: https://github.com/SRombauts/SimplexNoise/blob/master/references/SimplexNoise.java | |
pos.x = (float) (origPos.x + 5 * (1-2*SimplexNoise.noise(origPos.x, cos(t), sin(t)))); | |
pos.y = (float) (origPos.y + 5 * (1-2*SimplexNoise.noise(origPos.y, cos(t), sin(t)))); | |
} | |
} |
Author
KrabCode
commented
Jun 20, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment