Skip to content

Instantly share code, notes, and snippets.

@futurefabric
Created October 1, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futurefabric/2444d9e0e87930b093c8ca9e98f56c72 to your computer and use it in GitHub Desktop.
Save futurefabric/2444d9e0e87930b093c8ca9e98f56c72 to your computer and use it in GitHub Desktop.
Spinning Cube Stack
// Guy Moorhouse – 2015
// Spinning Cube Stack
// This is the source code for this animation:
// https://www.mooooooving.com/post/108991844461/spinning-cube-stack
int[][] result;
float t;
float ease(float p) {
return 3*p*p - 2*p*p*p;
}
float ease(float p, float g) {
if (p < 0.5)
return 0.5 * pow(2*p, g);
else
return 1 - 0.5 * pow(2*(1 - p), g);
}
void setup() {
size(500, 500, P3D);
ortho();
noStroke();
smooth(8);
for(int i=1; i<count; i++)
b[i] = new cuboid();
result = new int[width*height][3];
}
void draw() {
if (!recording) {
t = mouseX*1.0/width;
draw_();
} else {
for (int i=0; i<width*height; i++)
for (int a=0; a<3; a++)
result[i][a] = 0;
for (int sa=0; sa<samplesPerFrame; sa++) {
t = map(frameCount-1 + sa*shutterAngle/samplesPerFrame, 0, numFrames, 0, 1);
draw_();
loadPixels();
for (int i=0; i<pixels.length; i++) {
result[i][0] += pixels[i] >> 16 & 0xff;
result[i][1] += pixels[i] >> 8 & 0xff;
result[i][2] += pixels[i] & 0xff;
}
}
loadPixels();
for (int i=0; i<pixels.length; i++)
pixels[i] = 0xff << 24 |
int(result[i][0]*1.0/samplesPerFrame) << 16 |
int(result[i][1]*1.0/samplesPerFrame) << 8 |
int(result[i][2]*1.0/samplesPerFrame);
updatePixels();
saveFrame("f###.png");
if (frameCount==numFrames)
exit();
}
}
//////////////////////////////////////////////////////////////////////////////
int samplesPerFrame = 15;
int numFrames = 250;
float shutterAngle = 0;
// change recording to true to output a png sequence
// when set to false you'll have a scrubbable version of the animation in the sketch.
boolean recording = false;
class cuboid {
void draw(float x, float y, float w) {
// Front
beginShape(QUADS);
fill(face1);
vertex(-x, -y, w);
vertex( x, -y, w);
vertex( x, y, w);
vertex(-x, y, w);
endShape();
// Back
beginShape(QUADS);
fill(face2);
vertex( x, -y, -w);
vertex(-x, -y, -w);
vertex(-x, y, -w);
vertex( x, y, -w);
endShape();
// Bottom
beginShape(QUADS);
fill(face3);
vertex(-x, y, w);
vertex( x, y, w);
vertex( x, y, -w);
vertex(-x, y, -w);
endShape();
// Top
beginShape(QUADS);
fill(face4);
vertex(-x, -y, -w);
vertex( x, -y, -w);
vertex( x, -y, w);
vertex(-x, -y, w);
endShape();
// Right
beginShape(QUADS);
fill(face5);
vertex( x, -y, w);
vertex( x, -y, -w);
vertex( x, y, -w);
vertex( x, y, w);
endShape();
// Left
beginShape(QUADS);
fill(face6);
vertex(-x, -y, -w);
vertex(-x, -y, w);
vertex(-x, y, w);
vertex(-x, y, -w);
endShape();
}
}
float x,y,rotx,roty,rotz;
float t_, eased_t_;
int count = 6;
float w = 25;
color face1 = color(255);
color face2 = color(255,0,0);
color face3 = color(255,0,0);
color face4 = color(255,0,0);
color face5 = color(157,0,132);
color face6 = color(157,0,132);
cuboid[] b = new cuboid[count];
void draw_() {
pushMatrix();
translate(width/2,height/2);
background(255);
for(int i=1; i<count; i++) {
w = (width/10) * i;
translate(0,0,(width + 100)*-1);
if((t > 0.05) && (t < 0.95)) {
t_ = map(t,0.05,0.95,0,1);
t_ = ease(t_, PI);
rotx = (PI) * t_;
roty = (PI) * t_;
rotz = (PI) * t_;
if((i == 2) || (i == 4)) {
rotx = rotx * -1;
roty = roty * -1;
rotz = rotz * -1;
}
face1 = color(255);
face2 = color(255-(i*10),0,0);
face3 = color(255-(i*10),0,0);
face4 = color(255-(i*10),0,0);
face5 = color(157-(i*10),0,132-(i*10));
face6 = color(157-(i*10),0,132-(i*10));
}
if(t >= 0.95) {
rotx = 0;
roty = 0;
rotz = 0;
}
pushMatrix();
rotateX(rotx);
rotateY(roty);
rotateZ(rotz);
b[i].draw(w,w,w);
popMatrix();
}
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment