Skip to content

Instantly share code, notes, and snippets.

@AdmiralPotato
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdmiralPotato/d473ac21cec2c02f28e6 to your computer and use it in GitHub Desktop.
Save AdmiralPotato/d473ac21cec2c02f28e6 to your computer and use it in GitHub Desktop.
//configurable variables
boolean
exportMode = false; //set to true when exporting, false when designing
String
folderPrefix = "c";
int
fps = 24,
outputScale = 1, //set to 2 or 4 when exporting, 1 when designing
windowSize = 480 * outputScale,
numFrames = 48,
samplesPerFrame = 16; //set to 64 or 128 when exporting, 4 or 8 when designing
float
motionBlurFactor = 0.5;
//you should probably leave these variables alone
int
currentFrame = 0,
preserveAlpha = 0xff000000;
int[][]
motionBlurBuffer;
float
time = 0,
half = windowSize / 2,
pi = PI,
tau = pi * 2.0,
deg = pi / 180.0;
void sample(){
float ellipseScale;
clear();
pushMatrix();
rotate(time * TAU);
ellipseMode(RADIUS);
fill(0);
ellipseScale = half * 0.8;
ellipse(0,0, ellipseScale, ellipseScale);
translate(0, 0, 1);
fill(1);
arc(0,0, ellipseScale, ellipseScale, -HALF_PI, HALF_PI, CHORD);
translate(0, 0, 1);
ellipseScale = half * 0.4;
ellipse(0, half * 0.4, ellipseScale, ellipseScale);
fill(0);
ellipse(0, half * -0.4, ellipseScale, ellipseScale);
translate(0, 0, 1);
fill(1);
ellipseScale /= 2.75;
ellipse(0, half * -0.4, ellipseScale, ellipseScale);
fill(0);
ellipse(0, half * 0.4, ellipseScale, ellipseScale);
popMatrix();
}
void setup() {
size(windowSize, windowSize, P3D);
ortho();
frameRate(fps);
smooth(8);
motionBlurBuffer = new int[width * height][4];
colorMode(HSB, 1.0);
noStroke();
}
void draw(){
pushMatrix();
translate(half, half);
//scale(outputScale);
multiSample();
popMatrix();
if(exportMode){
saveFrame(folderPrefix + "-" + numFrames + "/####.png");
}
currentFrame++;
if(exportMode && currentFrame >= numFrames){
exit();
}
time = (float) currentFrame / numFrames;
}
void multiSample(){
int
pixelIndex,
channelIndex,
sampleIndex;
if(samplesPerFrame < 2){
sample();
}
else {
//set the motionBlurBuffer back to empty
for (pixelIndex = 0; pixelIndex < width * height; pixelIndex++){
for (channelIndex = 0; channelIndex < 4; channelIndex++){
motionBlurBuffer[pixelIndex][channelIndex] = 0;
}
}
for (sampleIndex = 0; sampleIndex < samplesPerFrame; sampleIndex++) {
time = map(
currentFrame + ((sampleIndex * motionBlurFactor) / samplesPerFrame), // value
0, //start1
numFrames, //stop1
0, //start2
1 //stop2
);
sample();
loadPixels();
for (int i=0; i<pixels.length; i++) {
motionBlurBuffer[i][0] += pixels[i] >> 24 & 0xff; //alpha
motionBlurBuffer[i][1] += pixels[i] >> 16 & 0xff; //red
motionBlurBuffer[i][2] += pixels[i] >> 8 & 0xff; //green
motionBlurBuffer[i][3] += pixels[i] & 0xff; //blue
}
}
loadPixels();
for(pixelIndex = 0; pixelIndex < pixels.length; pixelIndex++){
pixels[pixelIndex] =
(motionBlurBuffer[pixelIndex][0]/samplesPerFrame) << 24 | //alpha
(motionBlurBuffer[pixelIndex][1]/samplesPerFrame) << 16 | //red
(motionBlurBuffer[pixelIndex][2]/samplesPerFrame) << 8 | //green
(motionBlurBuffer[pixelIndex][3]/samplesPerFrame); //blue
//invert and preserveAlpha
//preserveAlpha = pixels[pixelIndex] >> 24 & 0xff;
//pixels[pixelIndex] = (~pixels[pixelIndex]) & 0x00ffffff | (preserveAlpha << 24);
}
updatePixels();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment