Skip to content

Instantly share code, notes, and snippets.

@FreedomGrenade
Created March 21, 2016 00:43
Show Gist options
  • Save FreedomGrenade/2de7dffe814f0f33184d to your computer and use it in GitHub Desktop.
Save FreedomGrenade/2de7dffe814f0f33184d to your computer and use it in GitHub Desktop.
PShape sierp, lightShape;
Col[] cols;
float cubeRot, cubeSize;
float isoAngle;
int lights;
LightInfo[] lightlist;
public void setup() {
size(500, 500, P3D);
cols = new Col[4];
cols[0] = new Col(#4B89AC);
cols[1] = new Col(#ACE6F6);
cols[2] = new Col(#E4FCF9);
cols[3] = new Col(#FAEE5A);
isoAngle = asin(tan(30*PI/180));
cubeSize = width*0.5;
cubeRot = 1.0/180;
noStroke();
fill(cols[3].c); // light graphic
PShape cubeShape = createShape(BOX, width/10.0);
lightShape = createShape(SPHERE, width/20.0);
lights = 3;
lightlist = new LightInfo[lights];
lightlist[0] = new LightInfo(cols[3],cubeShape);
for (int i = 1; i < lights; i++) {
lightlist[i] = new LightInfo(cols[3],lightShape);
}
sierp = createShape(GROUP); // sierpinski graphic
fill(#FFFFFF);
make(3,0,0,0,cubeSize,sierp);
}
public void draw() {
background(cols[0].c);
ortho();
translate(width*0.5, height*0.5, 0);
rotateZ(-frameCount*cubeRot*PI);
rotateX(-isoAngle);
rotateY(QUARTER_PI);
for (int i = 0; i < lights; i++) {
setPath(frameCount*cubeRot+(float)(i)/(lights+2),cubeSize*0.6,lightlist[i]);
}
ambientLight(cols[0].r, cols[0].g, cols[0].b); // cube lighting
lightFalloff(0.0, 0.0, 0.0002);
for (int i = 0; i < lights; i++) lightlist[i].light();
shape(sierp);
if (frameCount < 120) saveFrame();
}
// easy but not the best
public void make(int level, float xp, float yp, float zp, float size, PShape group) {
if (level == 0) {
group.addChild(aBox(xp,yp,zp,size));
return;
}
float thirdSize = size/3.0f;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
if (y == 0 && x==0) continue;
for (int z = -1; z <= 1; z++) {
if (z == 0 && (y == 0 || x == 0)) continue;
make(level-1, xp+x*thirdSize, yp+y*thirdSize, zp+z*thirdSize, thirdSize,group);
}
}
}
}
public PShape aBox(float x, float y, float z, float s) {
PShape result = createShape(BOX,s);
result.translate(x,y,z);
return result;
}
public void mousePressed() {
println(frameCount);
}
public class LightInfo {
float x, y, z;
PShape shape;
Col rgb;
public LightInfo(Col c, PShape s) {
shape = s;
rgb = c;
x = 0; y = 0; z = 0;
}
public void move(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
this.display();
}
public void display() {
pushMatrix();
translate(x,y,z);
shape(shape);
popMatrix();
}
public void light() {
pointLight(rgb.r,rgb.g,rgb.b,x,y,z);
}
}
public class Col {
int c;
int r, g, b;
public Col(int c) {
this.c = c;
r = (c >> 16) & 255;
g = (c >> 8) & 255;
b = (c) & 255;
}
}
public void setPath(float t,float max, LightInfo thing) {
float t0 = t*3;
float stage = (int)(t0)%3; //
float stageT = (t*3)%1; // 0->1, 0->1, 0->1
float d = mouseX*1.0/4.0/width;
float dis = wave(stageT,0, 1.0,1.0,1.0,1.0,0.0,0.0)*max;
float ang = wave(stageT,0, 0.0,0.33333,0.66666,1.0,1.0,1.0)*HALF_PI;
float mult = sin(ang) + cos(ang); // rounder orbit
float axis0 = cos(ang)*dis*mult;
float axis1 = sin(ang)*dis*mult;
if (stage == 0) thing.move(-axis0,-axis1,0); // axis0 = x, axis1 = y (out axis0, in axis1)
if (stage == 1) thing.move(-axis1,0,axis0);
if (stage == 2) thing.move(0,-axis0,axis1);
}
public float wave(float t, float... points) {
int sections = points.length;
float tf = t*sections;
int ti = (int)(tf) % sections ;
tf = (tf % 1);
if (ti >= sections) return points[sections-1];
if (ti <= 0) return points[0];
return tf*(points[ti] - points[ti-1]) + points[ti-1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment