Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created May 27, 2022 16:56
Show Gist options
  • Save KrabCode/278e2ebec23dead30967d4ac714f86bc to your computer and use it in GitHub Desktop.
Save KrabCode/278e2ebec23dead30967d4ac714f86bc to your computer and use it in GitHub Desktop.
PGraphics pg;
ArrayList<Leaf> leaves = new ArrayList<Leaf>();
ArrayList<Leaf> leavesToRemove = new ArrayList<Leaf>();
int lifeDuration;
public void setup() {
fullScreen(P2D);
pg = createGraphics(width, height, P2D);
}
public void draw() {
pg.colorMode(HSB, 1, 1, 1, 1);
pg.beginDraw();
pg.pushStyle();
pg.blendMode(BLEND);
drawMainBackground();
pg.popStyle();
pg.translate(width/2f, height/2f);
pg.scale(1.5);
updateLeaves();
pg.endDraw();
imageMode(CORNER);
image(pg, 0, 0, width, height);
}
private void updateLeaves() {
lifeDuration = 160;// gui.sliderInt("leaf/spawn/lifetime", 120, 1, Integer.MAX_VALUE);
int leafCount = 160;// gui.sliderInt("leaf/spawn/count", 100);
int particlesPerFrame = max(1, leafCount / lifeDuration);
if (leaves.size() < leafCount) {
for (int i = 0; i < particlesPerFrame; i++) {
leaves.add(new Leaf());
}
}
pg.rectMode(CENTER);
pg.blendMode(ADD);
for (Leaf leaf : leaves) {
leaf.update();
if (leaf.isGarbage()) {
leavesToRemove.add(leaf);
}
}
leaves.removeAll(leavesToRemove);
leavesToRemove.clear();
}
private void drawMainBackground() {
pg.fill(0);//gui.colorPicker("main background", color(0)).hex);
pg.noStroke();
pg.rectMode(CORNER);
pg.rect(0, 0, width, height);
}
class Leaf {
PVector pos = new PVector(), spd = new PVector();
float timePos;
int frameBorn = frameCount;
float sizeModifier = random(1);
float hueModifier = randomGaussian();
float satModifier = randomGaussian();
float brModifier = randomGaussian();
Leaf() {
float x = 0; // gui.slider("leaf/spawn/x", 0);
float y = 0; // gui.slider("leaf/spawn/y", 0);
float range = 100; //gui.slider("leaf/spawn/range");
float spawnRangeX = randomGaussian() * range;
float spawnRangeY = randomGaussian() * range;
pos.x = x + spawnRangeX;
pos.y = y + spawnRangeY;
}
void update() {
PVector acc = new PVector(0,0);
timePos += radians(0.075);
float freq = 0.05; // gui.slider("leaf/move/noise freq", 0.1f);
PVector noise = new PVector(
noise(pos.x * freq, pos.y* freq, timePos),
noise(pos.x * freq +430.95f, pos.y* freq -1740.125f, timePos+320.5f)
);
noise.sub(0.5f, 0.5f);
noise.mult((height-mouseY)/(float)height);// gui.slider("leaf/move/noise power", 1));
acc.add(noise);
spd.mult(0.975); // gui.slider("leaf/move/drag", .98f));
spd.add(acc);
pos.add(spd);
float hue = 0.75;
float sat = 0.5;
float br = 0.5;
float fade = constrain(norm(frameCount, frameBorn, frameBorn + 60), 0, 1);
float fadeOutDuration = 60;
if (frameCount >= frameBorn + lifeDuration - fadeOutDuration) {
fade = 1 - constrain(norm(frameCount, frameBorn + lifeDuration - fadeOutDuration, frameBorn + lifeDuration), 0, 1);
}
int mirrorCount = 1+floor((18*mouseX) / width);// gui.sliderInt("mirrors", 1);
for (int mirrorIndex = 0; mirrorIndex < mirrorCount; mirrorIndex++) {
pg.pushMatrix();
pg.rotate(TAU * norm(mirrorIndex, 0, mirrorCount));
pg.translate(pos.x, pos.y);
pg.rotate(spd.heading());
pg.fill(constrain(hue + hueModifier * 0.1, 0, 1),
constrain(sat + satModifier * 0.2, 0, 1),
constrain(br + brModifier * 0.5, 0, 1),
lerp(0, 1, fade));
pg.noStroke();
float size = 5 + sizeModifier * 5;
pg.rect(0, 0, size, size);
// pg.ellipse(0, 0, size, size);
pg.popMatrix();
}
}
public boolean isGarbage() {
return frameBorn + lifeDuration < frameCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment