Skip to content

Instantly share code, notes, and snippets.

@beesandbombs
Created November 29, 2018 00:52
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save beesandbombs/2d63bbdc7f7bd2b0a92888b6b14c0dbc to your computer and use it in GitHub Desktop.
Save beesandbombs/2d63bbdc7f7bd2b0a92888b6b14c0dbc to your computer and use it in GitHub Desktop.
four cones
// "four cones" by dave :)
int[][] result;
float t, c;
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);
}
float mn = .5*sqrt(3), ia = atan(sqrt(.5));
void push() {
pushMatrix();
pushStyle();
}
void pop() {
popStyle();
popMatrix();
}
float c01(float g) {
return constrain(g, 0, 1);
}
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);
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###.gif");
if (frameCount==numFrames)
exit();
} else if (preview) {
c = mouseY*1.0/height;
if (mousePressed)
println(c);
t = (millis()/(20.0*numFrames))%1;
draw_();
} else {
t = mouseX*1.0/width;
c = mouseY*1.0/height;
if (mousePressed)
println(c);
draw_();
}
}
//////////////////////////////////////////////////////////////////////////////
int samplesPerFrame = 4;
int numFrames = 360;
float shutterAngle = .6;
boolean recording = false,
preview = true;
void setup() {
size(750, 750, P3D);
smooth(8);
pixelDensity(recording ? 1 : 2);
result = new int[width*height][3];
noStroke();
}
float x, y, z, tt;
int N = 120;
float l = 160, r = l*sqrt(.5);
float scale;
color c1 = #1E90A1, c2 = #F7B409;
void cone1() {
fill(c1);
beginShape(TRIANGLE_FAN);
vertex(0, 0, 0);
for (int i=0; i<=N; i++)
vertex(r, r*cos(TWO_PI*i/N), r*sin(TWO_PI*i/N));
endShape();
fill(c2);
beginShape();
for (int i=0; i<=N; i++)
vertex(r, r*cos(TWO_PI*i/N), r*sin(TWO_PI*i/N));
endShape(CLOSE);
}
void cone2() {
fill(c1);
beginShape(TRIANGLE_FAN);
vertex(0, 0, 0);
for (int i=0; i<=N; i++)
vertex(.5*l+.5*l*cos(TWO_PI*i/N), -l, .5*l*sin(TWO_PI*i/N));
endShape();
fill(c2);
beginShape();
for (int i=0; i<=N; i++)
vertex(.5*l+.5*l*cos(TWO_PI*i/N), -l, .5*l*sin(TWO_PI*i/N));
endShape(CLOSE);
}
void draw_() {
scale = map(cos(TWO_PI*2*t), 1, -1, 0.001, 1);
background(30);
push();
translate(width/2, height/2);
scale(1, 1, scale);
translate(0, 0, -l/4);
rotateY(-TWO_PI*t);
rotate(PI*t);
if (t <= .5) {
for (int i=0; i<4; i++) {
push();
rotate(HALF_PI*i);
translate(-l, -l);
rotate(QUARTER_PI);
cone1();
pop();
}
}
else {
for (int i=0; i<4; i++) {
push();
rotate(HALF_PI*i);
translate(-l, 0, 0);
cone2();
pop();
}
}
pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment