Skip to content

Instantly share code, notes, and snippets.

@AdmiralPotato
Last active August 29, 2015 13:55
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/8776685 to your computer and use it in GitHub Desktop.
Save AdmiralPotato/8776685 to your computer and use it in GitHub Desktop.
A Processing Sketch for playing with Temporal Displacement Mapping. Uses a PNG image sequence containing an animation, and a greyscale "Temporal Displacement Map" PNG to control how many frames forward from the current frame any pixel should be displayed.
boolean
exportMode = false;
int
scale = 480,
numFrames = 48,
frameRate = 24,
currentFrame = 0;
float
time = 0,
temporalOffsetFactor = 1.0; //positive numbers only plz
String
srcVariant = "rainbow_48",
mapVariant = "magnet_poles";
PImage temporalDisplacementMap;
PImage[] imageSequence = new PImage[numFrames];
void setup(){
int i;
size(scale, scale, P2D);
frameRate(frameRate);
for(i = 0; i < numFrames; i++){
imageSequence[i] = loadImage("source-" + srcVariant + "/" + nf(i + 1, 4) + ".png");
imageSequence[i].loadPixels();
}
temporalDisplacementMap = loadImage("map-" + mapVariant + ".png");
temporalDisplacementMap.loadPixels();
}
void draw(){
int
x, y, currentPixelPosition,
frameA = 0, frameB = 0,
frameAColor = 0, frameBColor = 0,
frameOffset = 0, resultColor = 0;
float
temporalOffset = 0,
fractionalFrameOffset = 0,
abFraction = 0;
clear();
loadPixels();
colorMode(RGB, 1);
for(y = 0; y < height; y++){
for(x = 0; x < width; x++){
currentPixelPosition = y * width + x;
temporalOffset = red(temporalDisplacementMap.pixels[currentPixelPosition]);
fractionalFrameOffset = (currentFrame + (temporalOffset * temporalOffsetFactor * (numFrames - 1))) % numFrames;
frameA = floor(fractionalFrameOffset) % numFrames;
frameB = ceil(fractionalFrameOffset) % numFrames;
abFraction = fractionalFrameOffset - frameA;
frameAColor = imageSequence[frameA].pixels[currentPixelPosition];
frameBColor = imageSequence[frameB].pixels[currentPixelPosition];
resultColor = lerpColor(frameAColor, frameBColor, abFraction);
pixels[currentPixelPosition] = resultColor;
}
}
updatePixels();
currentFrame += 1;
if(!exportMode){
currentFrame %= numFrames;
} else {
saveFrame("output-" + srcVariant + "-" + mapVariant + "/####.png");
if(exportMode && currentFrame >= numFrames){
exit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment