Skip to content

Instantly share code, notes, and snippets.

@nsbalbi
Last active July 11, 2022 19:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsbalbi/7c09f0101f90c45ecc391e7f64b29c10 to your computer and use it in GitHub Desktop.
Save nsbalbi/7c09f0101f90c45ecc391e7f64b29c10 to your computer and use it in GitHub Desktop.
Universe in a Donut
// Processing Code By Nicholas Sbalbi (@nsbalbi)
// Twitter post: https://twitter.com/nsbalbi/status/1406696979523047437
// Github gist: https://gist.github.com/nsbalbi/7c09f0101f90c45ecc391e7f64b29c10
// 6-22-2021
// Motion blur via @beesandbombs and inspiration from @etiennejcb
/*
Copyright (c) 2021 Nicholas Sbalbi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// @beesandbombs motion blur template
int[][] result;
float t, c;
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("Output/fr###.png");
println(frameCount,"/",numFrames);
if (frameCount==numFrames)
exit();
}
}
/// END OF THE RENDERING SYSTEM
//////////////////////////////////////////////////////////////////////////////
// Number of drawings used to render each final frame with motion blur
int samplesPerFrame = 8;
// Total number of frames in the gif
int numFrames = 160;
// Kind of the time interval used for each frame in the motion blur
float shutterAngle = 0.5;
// If you put this to false you will control time with the mouse and no pictures will be saved
boolean recording = true;
float scale = 10;
float r1 = 10; // Outer radius
float r2 = 6; // Inner radius
// Parameterized surface function
PVector surface(float theta, float phi, float r1, float r2) {
float x = scale*(r1+r2*cos(theta))*cos(phi);
float y = scale*(r1+r2*cos(theta))*sin(phi);
float z = scale*r2*sin(theta);
return new PVector(x,y,z);
}
// Draws parameterized surface, adapted from @etiennejcb
void draw_surface() {
int n1 = 40; // theta subdivisions
int n2 = 80; // phi subdivisions
stroke(255); // white lines
fill(0); // black fill
noStroke(); // comment out for wireframe
for (int i = 0; i < n1; i++) {
beginShape(TRIANGLE_STRIP);
for (int j = 0; j < n2+1; j++) {
float theta1 = map(i,0,n1,0,2*PI);
float theta2 = map(i+1,0,n1,0,2*PI);
float phi = map(j,0,n2,0,2*PI);
PVector v1 = surface(theta1,phi,r1,r2);
PVector v2 = surface(theta2,phi,r1,r2);
vertex(v1.x,v1.y,v1.z);
vertex(v2.x,v2.y,v2.z);
}
endShape();
}
}
class Particle {
float theta;
float phi;
float offset;
float weight;
Particle() {
offset = random(0,1);
theta = random(0,2*PI);
phi = random(0,2*PI);
weight = random(3,4);
}
void draw_point(float t) {
PVector v = surface(theta + 2*PI*t + offset,phi - 2*PI*t,r1,r2-0.1);
stroke(255);
strokeWeight(weight + 2*sin(2*PI*(t+offset)));
point(v.x,v.y,v.z);
}
}
float cameraX = 1;
float cameraY = 190;
float cameraZ = 0;
int numParticles = 3000;
Particle[] particles = new Particle[numParticles];
void setup() {
size(600,600,P3D);
result = new int[width*height][3];
for (int i = 0; i < numParticles; i++) {
particles[i] = new Particle(); // Initialize particles
}
}
void draw_() {
background(0);
camera(cameraX,cameraY,cameraZ,0,0,0,0,-1,0);
pushMatrix();
rotateY(PI/2);
draw_surface(); // Draw underlying surface
for (int i = 0; i < numParticles; i++) {
particles[i].draw_point(t); // Draw particles
}
popMatrix();
// Stops when all the frames are rendered
if(frameCount == numFrames){
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment