Created
March 13, 2014 23:04
-
-
Save AdmiralPotato/9539026 to your computer and use it in GitHub Desktop.
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
boolean | |
exportMode = true; | |
int | |
size = 480, | |
maxFrames = 48, | |
currentFrame = 0, | |
numThings = 12, | |
samplesPerFrame = 32; | |
float | |
time = 0.0, | |
motionBlurFactor = 1.0, | |
frac = 1.0 / (float)maxFrames, | |
half = size / 2.0; | |
int[][] | |
motionBlurBuffer; | |
void setup(){ | |
size(size, size, P2D); | |
motionBlurBuffer = new int[width * height][4]; | |
smooth(8); | |
strokeWeight(0); | |
} | |
float blep(float thingSpacing, int i, int j, float time){ | |
return | |
(j * thingSpacing) | |
+ ( | |
sin(TWO_PI * (time + (((j * numThings) + i) * (0.93 / (float)numThings)))) | |
* thingSpacing | |
); | |
} | |
void sample(){ | |
int i, j; | |
float | |
thingSize = (float)half / (float)numThings, | |
thingSpacing = (float)size / (float)numThings, | |
x, y, w, h; | |
background(0); | |
fill(255); | |
for(j = -4; j < numThings + 4; j++){ | |
for(i = 0; i < numThings; i++){ | |
x = thingSpacing * i + (thingSpacing * 0.5); | |
y = blep(thingSpacing, i, j, time); | |
w = thingSize; | |
h = thingSize; | |
fill(255); | |
ellipse(x, y, w, h); | |
y = blep(thingSpacing, i, j, time + 0.02); | |
w *= 0.5; | |
h *= 0.5; | |
fill(0); | |
ellipse(x, y, w, h); | |
} | |
} | |
} | |
void draw(){ | |
time = currentFrame * frac; | |
multiSample(); | |
currentFrame++; | |
if(!exportMode){ | |
currentFrame %= maxFrames; | |
} else { | |
saveFrame("output-" + numThings + "/####.png"); | |
if(currentFrame >= maxFrames){ | |
exit(); | |
} | |
} | |
} | |
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 | |
maxFrames, //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