Skip to content

Instantly share code, notes, and snippets.

@Bleuje
Created January 31, 2023 21:42
Show Gist options
  • Save Bleuje/cd2de169127d5674454b0ebed35abe26 to your computer and use it in GitHub Desktop.
Save Bleuje/cd2de169127d5674454b0ebed35abe26 to your computer and use it in GitHub Desktop.
// Processing code by Etienne JACOB
// inspired by a shader from tdhooper : https://www.shadertoy.com/view/wsfGDS
// motion blur template by beesandbombs
int[][] result;
float t, c;
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.2/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);
t %= 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 = 11;
int numFrames = 150;
float shutterAngle = 1.6;
boolean recording = true;
PVector rotX(PVector u,float angle)
{
float x = u.x;
float y = u.y*cos(angle) - u.z*sin(angle);
float z = u.y*sin(angle) + u.z*cos(angle);
return new PVector(x,y,z);
}
PVector rotY(PVector u,float angle)
{
float y = u.y;
float x = u.x*cos(angle) - u.z*sin(angle);
float z = u.x*sin(angle) + u.z*cos(angle);
return new PVector(x,y,z);
}
PVector rotZ(PVector u,float angle)
{
float z = u.z;
float y = u.y*cos(angle) - u.x*sin(angle);
float x = u.y*sin(angle) + u.x*cos(angle);
return new PVector(x,y,z);
}
PVector rotate2D(PVector v,float angle)
{
return new PVector(cos(angle)*v.x-sin(angle)*v.y,sin(angle)*v.x+cos(angle)*v.y);
}
class vec4
{
float x,y,z,w;
vec4(float a,float b,float c,float d)
{
x = a;
y = b;
z = c;
w = d;
}
}
int n = 104;
class Dot
{
vec4 pos4D;
int I,J;
Dot(int i,int j)
{
I = i;
J = j;
}
void show()
{
float theta = map(I+(J%2==0?0:0.5)*(1-t),0,n,0,TWO_PI);
float phi = map(J+(I%2==0?0:0.5)*t,0,n,0,TWO_PI);
pos4D = new vec4(cos(theta)/sqrt(2),sin(theta)/sqrt(2),cos(phi)/sqrt(2),sin(phi)/sqrt(2));
float x,y,z,w;
x = pos4D.x;
y = pos4D.y;
z = pos4D.z;
w = pos4D.w;
PVector toRot1 = new PVector(z,y);
toRot1 = rotate2D(toRot1,HALF_PI*t);
z = toRot1.x;
y = toRot1.y;
PVector toRot2 = new PVector(x,w);
toRot2 = rotate2D(toRot2,HALF_PI*t);
x = toRot2.x;
w = toRot2.y;
float dist4 = 1.2;
w += dist4;
if(w<=0)
{
return;
}
float X = x/w;
float Y = y/w;
float Z = z/w;
PVector v3 = new PVector(X,Y,Z);
v3 = rotX(v3,0);
float dist3 = 1.6;
v3.z += dist3;
if(v3.z<=0)
{
return;
}
float xx = 200*v3.x/v3.z;
float yy = 200*v3.y/v3.z;
float sw = 2.7/v3.z;
float f = map(v3.z,0.7,2.3,1.8,0,true);
stroke(255,255*f);
strokeWeight(sw);
point(xx,yy);
}
}
Dot [] array = new Dot[n*n];
void setup(){
size(600,600,P3D);
result = new int[width*height][3];
smooth(8);
int k = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
array[k] = new Dot(i,j);
k++;
}
}
}
void draw_(){
background(0);
push();
translate(width/2,height/2);
for(int k=0;k<n*n;k++)
{
array[k].show();
}
pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment