Skip to content

Instantly share code, notes, and snippets.

@AdmiralPotato
Last active Jan 4, 2016
Embed
What would you like to do?
boolean
exportMode = true;
int
fps = 24,
outputScale = 1,
size = 480 * outputScale,
half = size / 2,
currentFrame = 0,
maxFrames = 24,
samplesPerFrame = 64,
preserveAlpha = 0xff000000,
numberOfThings = 72;
int[][]
motionBlurBuffer;
float
time = 0,
motionBlurFactor = 1.0f,
pi = PI,
deg = pi / 180.0,
tau = pi * 2.0,
xTrixelOffset = cos(deg * 30.0),
yTrixelOffset = sin(deg * 30.0),
triangleRotOffset = 90.0,
triangleSeparationScale = 0.9,
t0x = cos(deg * (000.0 + triangleRotOffset)) * triangleSeparationScale,
t0y = sin(deg * (000.0 + triangleRotOffset)) * triangleSeparationScale,
t1x = cos(deg * (120.0 + triangleRotOffset)) * triangleSeparationScale,
t1y = sin(deg * (120.0 + triangleRotOffset)) * triangleSeparationScale,
t2x = cos(deg * (240.0 + triangleRotOffset)) * triangleSeparationScale,
t2y = sin(deg * (240.0 + triangleRotOffset)) * triangleSeparationScale;
String[]
starTrixelData,
test0TrixelData,
test1TrixelData,
test2TrixelData,
backgroundTrixelData;
void setup() {
size(size, size, P3D);
frameRate(fps);
colorMode(RGB, 255);
smooth(8);
motionBlurBuffer = new int[width * height][4];
starTrixelData = loadStrings("star.csv");
test0TrixelData = loadStrings("test0.csv");
test1TrixelData = loadStrings("test1.csv");
test2TrixelData = loadStrings("test2.csv");
backgroundTrixelData = loadStrings("background.csv");
}
void sample(){
float
yShift = size * 0.0625,
radialDistance = 0.3875,
littleShapeScale = 0.03;
background(0);
pushMatrix();
//scale(1.735); //right to the edge
scale(2);
rotate(deg * -90);
drawTrixelData(backgroundTrixelData, ((time % 1) - 1.0) * -size, 0, size * 0.024051, 0, false);
drawTrixelData(backgroundTrixelData, ((time % 1) - 0.0) * -size, 0, size * 0.024051, 0, false);
popMatrix();
drawTrixelData(starTrixelData, 0, yShift, size * 0.05, time * tau * (1.0 / 3.0) * 2, true);
drawTrixelData(test0TrixelData, cos(deg * -90 ) * size * radialDistance, (sin(deg * -90 ) * size * radialDistance) + yShift, size * littleShapeScale, time * deg * 120, true);
drawTrixelData(test1TrixelData, cos(deg * -330) * size * radialDistance, (sin(deg * -330) * size * radialDistance) + yShift, size * littleShapeScale, time * pi, true);
drawTrixelData(test2TrixelData, cos(deg * -210) * size * radialDistance, (sin(deg * -210) * size * radialDistance) + yShift, size * littleShapeScale, time * pi, true);
}
void drawTriangle(float xpos, float ypos, float rot){
pushMatrix();
translate(xpos, ypos);
rotate(rot);
triangle(t0x, t0y, t1x, t1y, t2x, t2y);
popMatrix();
}
void drawTrixelData(String[] trixelData, float xpos, float ypos, float scale, float rot, boolean drawCenter){
int
i, num = trixelData.length,
cols, rows,
col, row, r, g, b, a;
float
colHalf = 0, rowHalf = 0,
colOffset = 0, rowOffset = 0,
x, y, trixelRotation;
String[]
line;
colorMode(RGB, 255);
pushMatrix();
translate(xpos, ypos);
scale(scale);
rotate(rot);
stroke(0, 0);
for(i = 0; i < num; i++){
line = trixelData[i].split(", ");
if(line.length == 2){
cols = int(line[0]);
rows = int(line[1]);
//these maths were the hardest part for me to get right
colHalf = (cols - 2) * xTrixelOffset * 0.5;
rowHalf = rows * 1.5 * 0.5 - 1 - ((rows % 2) * 0.25);
}
if(line.length >= 6){
col = int(line[0]);
row = int(line[1]);
r = int(line[2]);
g = int(line[3]);
b = int(line[4]);
a = int(line[5]);
if(a > 0){
//these maths were also hard, but not as hard as the above
rowOffset = ((row + 1) % 2) * xTrixelOffset;
colOffset = ((col + 1) % 2) * yTrixelOffset;
trixelRotation = ((col) % 2) * pi;
x = col * xTrixelOffset - colHalf - rowOffset;
y = row * 1.5 - rowHalf - colOffset;
fill(r, g, b, a);
drawTriangle(x, y, trixelRotation);
}
}
}
//used to illustrate the center of rotation used for this trixelData
if(drawCenter){
fill(0,127);
ellipse(0, 0, triangleSeparationScale * 1.5, triangleSeparationScale * 1.5);
fill(255);
ellipse(0, 0, 0.15, 0.15);
}
popMatrix();
}
void draw(){
pushMatrix();
translate(half, half);
multiSample();
popMatrix();
if(exportMode){
saveFrame("a-" + maxFrames + "/###.png");
}
currentFrame++;
if(exportMode && currentFrame >= maxFrames){
exit();
}
time = (float) currentFrame / maxFrames;
}
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