Skip to content

Instantly share code, notes, and snippets.

@marcedwards
Last active August 19, 2019 09:26
Show Gist options
  • Save marcedwards/4ac0ee0c47fb3fb58ed95e2775bc93a7 to your computer and use it in GitHub Desktop.
Save marcedwards/4ac0ee0c47fb3fb58ed95e2775bc93a7 to your computer and use it in GitHub Desktop.
Cubes of cubes in Processing
//
// Cubes of cubes.
// 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/1152196928853049344
//
void setup() {
size(400, 400, P3D);
frameRate(30);
smooth(8);
}
void draw() {
background(#292929);
pushMatrix();
translate(width / 2, height / 2 + sin(timeLoop(60) * TAU) * 4);
rotateX(0.8);
rotateZ(timeLoop(120) * tau(0.25));
scale(18, 18, 18);
drawCubes(4, #ff3351, 0);
drawCubes(3, #9152e1, 40);
drawCubes(2, #33e2ff, 80);
popMatrix();
}
void drawCubes(int size, color col, int offset) {
float scale = size * 2;
fill(lerpColor(col, #000000, 0.5));
stroke(col);
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();
translate(x, y, z);
float nx = (x + size) / scale;
float ny = (y + size) / scale;
float nz = (z + size) / scale;
float v = Ease.hermite5(Ease.tri(gradientRadial(nx * width, ny * height, timeLoop(120, nz * 30 + offset), 1)), 1);
strokeWeight(v * 0.13);
box(v * 0.8);
popMatrix();
}
}
}
}
}
//
float timeLoop(float totalframes, float offset) {
return (frameCount + offset) % totalframes / totalframes;
}
float timeLoop(float totalframes) {
return timeLoop(totalframes, 0);
}
float tau(float fraction) {
return TAU * fraction;
}
float gradientRadial(float x, float y, float offset, float scale) {
float biggest = max(width, height) * scale;
float xd = abs((width / 2) - x);
float yd = abs((height / 2) - y);
return wrap(sqrt(xd * xd + yd * yd) / biggest, 1 - offset);
}
float wrap(float value, float offset) {
return (value + offset) % 1;
}
static class Ease {
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;
}
static public float tri(float t) {
return t < 0.5 ? t * 2 : 2 - (t * 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment