Created
February 3, 2023 15:10
-
-
Save beesandbombs/096fd6ba6cae3ba2ab70dde91face8a8 to your computer and use it in GitHub Desktop.
spinning cubes
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
float[][] result; | |
float t, c; | |
float ease(float p) { | |
p = c01(p); | |
return 3*p*p - 2*p*p*p; | |
} | |
float ease(float p, float g) { | |
p = c01(p); | |
if (p < 0.5) | |
return 0.5 * pow(2*p, g); | |
else | |
return 1 - 0.5 * pow(2*(1 - p), g); | |
} | |
float mn = .5*sqrt(3), ia = atan(sqrt(.5)); | |
float c01(float g) { | |
return constrain(g, 0, 1); | |
} | |
float millisFrame1; | |
void draw() { | |
if (recording) { | |
for (int i=0; i<width*height; i++) | |
for (int a=0; a<3; a++) | |
result[i][a] = 0; | |
c = 0; | |
for (int sa=0; sa<samplesPerFrame; sa++) { | |
t = map(frameCount-1 + sa*shutterAngle/samplesPerFrame, 0, numFrames, 0, 1); | |
t %= 1; | |
draw_(); | |
loadPixels(); | |
for (int i=0; i<pixels.length; i++) { | |
result[i][0] += sq(red(pixels[i])/255.0); | |
result[i][1] += sq(green(pixels[i])/255.0); | |
result[i][2] += sq(blue(pixels[i])/255.0); | |
} | |
} | |
loadPixels(); | |
for (int i=0; i<pixels.length; i++) | |
pixels[i] = color(255*sqrt(result[i][0]/samplesPerFrame), | |
255*sqrt(result[i][1]/samplesPerFrame), | |
255*sqrt(result[i][2]/samplesPerFrame)); | |
updatePixels(); | |
saveFrame("f###.png"); | |
if (frameCount==numFrames) | |
exit(); | |
} else if (preview) { | |
if (frameCount==1) | |
millisFrame1 = millis(); | |
c = mouseY*1.0/height; | |
if (mousePressed) | |
println(c); | |
t = ((millis()-millisFrame1)/(33.3*numFrames))%1; | |
draw_(); | |
} else { | |
t = mouseX*1.0/width; | |
c = mouseY*1.0/height; | |
if (mousePressed) | |
println(c); | |
draw_(); | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////// | |
int samplesPerFrame = 8; | |
int numFrames = 150; | |
float shutterAngle = 1; | |
boolean recording = false, | |
preview = true; | |
void setup() { | |
size(1080, 1080, P3D); | |
smooth(8); | |
rectMode(CENTER); | |
//pixelDensity(recording ? 1 : 2); | |
result = new float[width*height][3]; | |
fill(32); | |
noStroke(); | |
ortho(); | |
} | |
PImage[] fr = new PImage[3]; | |
float x, y, z, tt; | |
float l = 43; | |
color cb = #e7d7bc, cs = #211f20, c1 = #d93a32, c2 = #f0b424, c3 = #12628c; | |
color myCol; | |
color[] cols = {c1, c2, c3}; | |
void boxx() { | |
for (int i=0; i<4; i++) { | |
push(); | |
fill(i%2==0?c1:c2); | |
rotateY(HALF_PI*i); | |
translate(0, 0, l/2); | |
square(0, 0, l); | |
pop(); | |
} | |
for (int i=0; i<2; i++) { | |
push(); | |
fill(c3); | |
rotateX(HALF_PI+PI*i); | |
translate(0, 0, l/2); | |
square(0, 0, l); | |
pop(); | |
} | |
} | |
void draw_() { | |
for (int a=0; a<3; a++) { | |
background(cb); | |
push(); | |
translate(width/2, height/2); | |
scale(1-.01*(a-1)); | |
rotateX(-ia); | |
rotateY(QUARTER_PI); | |
for (int i=-5; i<=5; i++) { | |
for (int j=-5; j<=5; j++) { | |
for (int k=-5; k<=5; k++) { | |
x = i*l; | |
y = j*l; | |
z = k*l; | |
push(); | |
translate(x, y, z); | |
tt = (t + atan2(x, z)/TAU + 1)%1; | |
tt = .5*ease(2*tt, 4)+.5*ease(2*tt-1, 4); | |
rotateY(-PI*tt); | |
if ((i+j+k)%2==0) | |
boxx(); | |
pop(); | |
} | |
} | |
} | |
pop(); | |
fr[a] = get(); | |
} | |
for (int a=0; a<3; a++) { | |
fr[a].loadPixels(); | |
} | |
loadPixels(); | |
for (int i=0; i<pixels.length; i++) { | |
pixels[i] = color(red(fr[0].pixels[i]), green(fr[1].pixels[i]), blue(fr[2].pixels[i])); | |
} | |
updatePixels(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment