Skip to content

Instantly share code, notes, and snippets.

@marcedwards
Last active October 25, 2021 06:40
Show Gist options
  • Save marcedwards/3a52d73d2c13f23de50c2401be1e8466 to your computer and use it in GitHub Desktop.
Save marcedwards/3a52d73d2c13f23de50c2401be1e8466 to your computer and use it in GitHub Desktop.
Cubes and diamonds
//
// Cubes and diamonds.
// Created using Processing 3.5.3.
//
// Code by @marcedwards from @bjango.
//
// A GIF of this code can be seen here:
// https://twitter.com/marcedwards/status/1167765622899347459
//
void setup() {
size(400, 400, P3D);
frameRate(30);
smooth(8);
strokeWeight(0.1);
}
void draw() {
background(#0f091c);
pushMatrix();
translate(width / 2, height / 2 + sin(timeLoop(60) * TAU) * 4);
rotateX(0.8);
rotateZ(timeLoop(120) * TAU * 0.25);
scale(14, 14, 14);
drawCubes(6, #33e2ff, #0f091c, 0);
drawCubes(3, #ff3351, #3c0b12, 60);
popMatrix();
//blendMode(ADD);
//filter(BLUR, 8);
//blendMode(BLEND);
}
void drawCubes(int size, color col, color backgroundcol, int offset) {
float scale = size * 2;
for (int x = -size; x <= size; x++) {
for (int y = -size; y <= size; y++) {
for (int z = -size; z <= size; z++) {
if (abs(x) == size || abs(y) == size || abs(z) == size) {
pushMatrix();
float nx = (x + size) / scale;
float ny = (y + size) / scale;
float nz = (z + size) / scale;
float v = Ease.hermite5(Ease.tri(gradientDiamond(nx * width, ny * height, timeLoop(120, nz * 50 + offset), 2)), 1);
translate(x, y, z);
if (v > 0.2) {
if (v > 0.8) {
fill(lerpColor(col, backgroundcol, 0.1));
stroke(lerpColor(col, #ffffff, 0.6));
} else {
fill(backgroundcol);
stroke(col);
}
box(1);
}
popMatrix();
}
}
}
}
}
float gradientDiamond(float x, float y, float offset, float scale) {
float biggest = max(width * scale, height * scale);
float xd = abs((width / 2) - x);
float yd = abs((height / 2) - y);
return wrap((xd + yd) / biggest, 1 - offset);
}
float wrap(float value, float offset) {
return (value + offset) % 1;
}
float timeLoop(float totalframes, float offset) {
return (frameCount + offset) % totalframes / totalframes;
}
float timeLoop(float totalframes) {
return timeLoop(totalframes, 0);
}
static class Ease {
static public float tri(float t) {
return t < 0.5 ? t * 2 : 2 - (t * 2);
}
static public float hermite5(float t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
static public float hermite5(float t, int repeat) {
for (int i = 0; i < repeat; i++) {
t = hermite5(t);
}
return t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment