ball wave
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
| 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(); | |
| } | |
| void draw() { | |
| if (!recording) { | |
| t = mouseX*1.0/width; | |
| c = mouseY*1.0/height; | |
| if (mousePressed) | |
| println(c); | |
| draw_(); | |
| } else { | |
| 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("t###.gif"); | |
| if (frameCount==numFrames) | |
| exit(); | |
| } | |
| } | |
| ////////////////////////////////////////////////////////////////////////////// | |
| int samplesPerFrame = 4; | |
| int numFrames = 180; | |
| float shutterAngle = .4; | |
| boolean recording = false; | |
| void setup() { | |
| size(750, 750, P3D); | |
| result = new int[width*height][3]; | |
| rectMode(CENTER); | |
| colorMode(HSB, 1); | |
| noStroke(); | |
| } | |
| float x, y, z, tt; | |
| int ballDetail = 16; | |
| float th1, th2, ph1, ph2; | |
| void sphereVertex(float r_, float th_, float ph_) { | |
| vertex(r_*cos(ph_)*cos(th_), r_*sin(ph_), r_*cos(ph_)*sin(th_)); | |
| } | |
| void hemisph() { | |
| beginShape(TRIANGLES); | |
| for (int i=0; i<ballDetail; i++) { | |
| th1 = i*PI/ballDetail; | |
| th2 = (i+1)*PI/ballDetail; | |
| for (int j=0; j<ballDetail; j++) { | |
| ph1 = map(j, 0, ballDetail, -HALF_PI, HALF_PI); | |
| ph2 = map(j+1, 0, ballDetail, -HALF_PI, HALF_PI); | |
| sphereVertex(rad, th1, ph1); | |
| sphereVertex(rad, th2, ph1); | |
| sphereVertex(rad, th1, ph2); | |
| sphereVertex(rad, th2, ph2); | |
| sphereVertex(rad, th1, ph2); | |
| sphereVertex(rad, th2, ph1); | |
| } | |
| } | |
| endShape(); | |
| } | |
| void ball(float hue) { | |
| fill(hue, .9, .9); | |
| hemisph(); | |
| fill((hue+.5)%1, 1, 1); | |
| push(); | |
| scale(1, 1, -1); | |
| hemisph(); | |
| pop(); | |
| } | |
| int n = 5; | |
| float rad = 18, space = 3*rad; | |
| float hexDist; | |
| void draw_() { | |
| background(.05); | |
| push(); | |
| translate(width/2, height/2 - 40); | |
| rotateX(-0.5); | |
| for (int i=-n; i<=n; i++) { | |
| for (int j=-n; j<=n; j++) { | |
| x = i*space; | |
| z = j*mn*space; | |
| if (j%2 != 0) | |
| x += .5*space; | |
| hexDist = max(abs(z), abs(.5*z+mn*x), abs(.5*z-mn*x)); | |
| y = 5*rad*cos(TWO_PI*t - 0.01*dist(x, z, 0, 0)); | |
| push(); | |
| translate(x, y, z); | |
| rotateY(TWO_PI*t); | |
| if (hexDist < 250) | |
| ball(.125*hexDist % 1); | |
| pop(); | |
| } | |
| } | |
| pop(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gif: https://twitter.com/beesandbombs/status/887349750349139968