Skip to content

Instantly share code, notes, and snippets.

@Bleuje
Last active December 18, 2022 09:38
Show Gist options
  • Save Bleuje/14cd23f2ce3a69d7712f5b66aaceaa0d to your computer and use it in GitHub Desktop.
Save Bleuje/14cd23f2ce3a69d7712f5b66aaceaa0d to your computer and use it in GitHub Desktop.
// Processing code by Etienne JACOB
// inspired by beesandbombs : https://twitter.com/beesandbombs/status/1563110024204787712
// motion blur template by beesandbombs
int[][] result;
float t, c;
// utils... most unused here
float c01(float x)
{
return constrain(x,0,1);
}
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 map(float x, float a, float b, float c, float d, boolean constr)
{
return constr ? constrain(map(x,a,b,c,d),min(c,d),max(c,d)) : map(x,a,b,c,d);
}
float mp01(float x, float a, float b)
{
return map(x,a,b,0,1,true);
}
float pow_(float p,float g)
{
return 1-pow(1-p,g);
}
float tanh(float x)
{
return (float)Math.tanh(x);
}
float softplus(float q,float p){
float qq = q+p;
if(qq<=0){
return 0;
}
if(qq>=2*p){
return qq-p;
}
return 1/(4*p)*qq*qq;
}
float mn = .5*sqrt(3), ia = atan(sqrt(.5));
void push() {
pushMatrix();
pushStyle();
}
void pop() {
popStyle();
popMatrix();
}
void draw() {
if (!recording) {
t = (mouseX*1.4/width)%1;
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();
if (frameCount<=numFrames)
{
saveFrame("fr###.gif");
println(frameCount,"/",numFrames);
}
if (frameCount==numFrames)
stop();
}
}
//////////////////////////////////////////////////////////////////////////////
int samplesPerFrame = 5;
int numFrames = 270;
float shutterAngle = .8;
boolean recording = true;
int n = 7;
float L = 150.0; // cube size
float baseCameraZ = 700;
PVector project(float x,float y,float z,float p)
{
float f = 600.0;
float addCZ = 740.0/(1.000001-p); // camera going to infinity when p goes to 1
float CZ = baseCameraZ + addCZ;
// maths to keep same cube size on screen : f*x/bCZ = f2*x/CZ; --> f2 = f*CZ/bCZ;
float f2 = f*CZ/baseCameraZ;
float px = f2*x/(CZ-z);
float py = f2*y/(CZ-z);
return new PVector(px,py);
}
PVector rotX(PVector v_in,float theta)
{
float y = v_in.y*cos(theta) - v_in.z*sin(theta);
float z = v_in.y*sin(theta) + v_in.z*cos(theta);
return new PVector(v_in.x,y,z);
}
PVector rotY(PVector v_in,float theta)
{
float x = v_in.x*cos(theta) - v_in.z*sin(theta);
float z = v_in.x*sin(theta) + v_in.z*cos(theta);
return new PVector(x,v_in.y,z);
}
void showCube(float p,int mode,float sz,int showEdges)
{
int nn = (showEdges==0?n:2*(n-1)+1);
for(int i=0;i<nn;i++)
{
for(int j=0;j<nn;j++)
{
for(int k=0;k<nn;k++)
{
if(showEdges==1)
{
int cnt1 = ((i!=0)&&(i!=(nn-1))?1:0); // 1 if not on x axis edges
int cnt2 = ((j!=0)&&(j!=(nn-1))?1:0); // 1 if not on y axis edges
int cnt3 = ((k!=0)&&(k!=(nn-1))?1:0); // 1 if not on z axis edges
int cnt = cnt1+cnt2+cnt3;
if(cnt>=2) // don't draw in this case (kind of a simple brute force algo to draw only on the edges)
continue;
}
float x0 = map(i,0,nn-1,-L,L);
float y0 = map(j,0,nn-1,-L,L);
float z0 = map(k,0,nn-1,-L,L);
PVector v0 = new PVector(x0,y0,z0);
float d = v0.mag();
float s = map(pow(c01(sin(PI*((7*t-0.0065*d+1234*4)%1))),2.5),0,1,3.2,5.0);
PVector vrot = rotY(v0,2*HALF_PI*(mode==0?p:-p)+QUARTER_PI);
vrot = rotX(vrot,-0.27*HALF_PI*(mode==0?1:-1));
PVector proj = project(vrot.x,vrot.y,vrot.z,1-sin(PI*p));
stroke(255,255,255,120);
strokeWeight(sz*s);
point(proj.x,proj.y);
}
}
}
}
void setup(){
size(600,600,P2D);
result = new int[width*height][3];
}
void draw_(){
background(0);
push();
translate(width/2,height/2);
float m = 60; // number of drawn dots with more or less delay, per particle
for(int i=0;i<m;i++)
{
float delay = 0.135*i/m;
float sz = pow(map(i,0,m,1,0),2.0);
float p0 = (2*t+1234-delay)%2;
float p = p0%1;
showCube(p,(p0>=1?1:0),sz,0);
showCube(p,(p0>=1?1:0),sz,1);
}
pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment