Last active
March 27, 2019 00:38
-
-
Save KrabCode/a616881a6d159456dfce451a9f593fa0 to your computer and use it in GitHub Desktop.
double click to generate a new ring system
This file contains hidden or 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
ArrayList<Ring> rings = new ArrayList<Ring>(); | |
float t, maxR, h; | |
PVector rotPos, rotSpd; | |
float startColor = 0; | |
float spd = 0; | |
int frameLastReleasedMouse = -100; | |
int doubleclickFramesThreshold = 20; | |
public void setup() { | |
fullScreen(P3D); | |
colorMode(HSB, 1, 1, 1, 1); | |
rotPos = new PVector(); | |
rotSpd = new PVector(); | |
maxR = min(width, height) * .48f; | |
reset(); | |
} | |
public void mouseReleased() { | |
if (frameLastReleasedMouse + doubleclickFramesThreshold > frameCount) { | |
reset(); | |
frameLastReleasedMouse = -100; | |
} else { | |
frameLastReleasedMouse = frameCount; | |
} | |
} | |
public void reset() { | |
rings.clear(); | |
spd = random(0.001, 0.01); | |
startColor = random(1); | |
int count = floor(random(5, 15)); | |
h = (maxR/count)*.5; | |
for (int i = 1; i < count; i++) { | |
float iN = map(i, 0, count - 1, 0, 1); | |
rings.add(new Ring(iN)); | |
} | |
} | |
public void draw() { | |
background(0); | |
t = radians(frameCount); | |
translate(width * .5f, height * .5f); | |
shininess(.5); | |
specular(1, 0, 1); | |
ambientLight(0, 0, .1); | |
directionalLight(1, 0, 1, 0, 0, -1); | |
rotSpd.mult(.99); | |
rotPos.add(rotSpd); | |
rotateX(rotPos.x); | |
rotateY(rotPos.y); | |
pushMatrix(); | |
for (Ring r : rings) { | |
r.draw(); | |
} | |
popMatrix(); | |
} | |
void mouseDragged() { | |
rotSpd.y = .002*(pmouseX-mouseX); | |
rotSpd.x = .002*(pmouseY-mouseY); | |
} | |
class Ring { | |
PVector center, rot, rotSpd; | |
float outerR, w, iN; | |
int count, fill; | |
PShape shape; | |
Ring(float iN) { | |
this.iN = iN; | |
count = 50; | |
w = 20; | |
outerR = 10 + iN * (maxR-100); | |
center = new PVector(); | |
rot = new PVector(); | |
rotSpd = new PVector(0, 0, 0); | |
float rand = random(1); | |
if (rand < .33f) { | |
rotSpd.x = random(1)<.5?-spd:spd; | |
} else if (rand < .66f) { | |
rotSpd.y = random(1)<.5?-spd:spd; | |
} else { | |
rotSpd.z = random(1)<.5?-spd:spd; | |
} | |
fill = color((startColor+iN * .5f)%1, 1 - iN*.7, 1 - (iN * iN * iN) * .5f); | |
shape = createShape(); | |
drawRingFace(false); | |
drawRingFace(true); | |
drawRingSides(); | |
} | |
void draw() { | |
rot.add(rotSpd); | |
noStroke(); | |
rotateX(rot.x); | |
rotateY(rot.y); | |
rotateZ(rot.z); | |
shape(shape); | |
} | |
private void drawRingFace(boolean inner) { | |
shape.beginShape(QUAD_STRIP); | |
shape.noStroke(); | |
shape.fill(fill); | |
float myR = inner ? outerR : outerR - h; | |
for (int i = 0; i < count; i++) { | |
float a = map(i, 0, count - 1, 0, TWO_PI); | |
float x = cos(a); | |
float y = sin(a); | |
float z = w * .5f; | |
shape.normal(myR * x, myR * y, 0); | |
shape.vertex(myR * x, myR * y, z); | |
shape.vertex(myR * x, myR * y,-z); | |
} | |
shape.endShape(); | |
} | |
private void drawRingSides() { | |
float innerR = outerR - h; | |
shape.beginShape(TRIANGLE_STRIP); | |
shape.noStroke(); | |
shape.fill(fill); | |
for (int i = 0; i < count; i++) { | |
float a = map(i, 0, count - 1, 0, TWO_PI); | |
float x = outerR * cos(a); | |
float y = outerR * sin(a); | |
float z = w * .5f; | |
shape.normal(0, 0, -1); | |
shape.vertex(x, y, z); | |
x = innerR * cos(a); | |
y = innerR * sin(a); | |
shape.vertex(x, y, z); | |
} | |
shape.endShape(); | |
shape.beginShape(TRIANGLE_STRIP); | |
for (int i = 0; i < count; i++) { | |
float a = map(i, 0, count - 1, 0, TWO_PI); | |
float x = outerR*cos(a); | |
float y = outerR*sin(a); | |
float z = -w * .5f; | |
shape.normal(0, 0, -1); | |
shape.vertex(x, y, z); | |
x = innerR * cos(a); | |
y = innerR * sin(a); | |
shape.vertex(x, y, z); | |
} | |
shape.endShape(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment