Skip to content

Instantly share code, notes, and snippets.

@Trimad
Created April 7, 2022 22:30
Show Gist options
  • Save Trimad/9ab8f2e55d2e2d12af8c0829f0f1eccf to your computer and use it in GitHub Desktop.
Save Trimad/9ab8f2e55d2e2d12af8c0829f0f1eccf to your computer and use it in GitHub Desktop.
Blending Textures With Open Simplex Noise
/* * * * * * *
Tristan Madden
08/18/2019
* * * * * * * */
PImage img1;
PImage img2;
PImage imgMask;
//This is the number of frames the animation will loop if record == true
int totalFrames = 60;
boolean record = true;
//Larger values zoom out, smaller values zoom in.
float increment = 0.008;
float zoff = 0;
OpenSimplexNoise noise;
void setup() {
size(512, 512);
img1 = loadImage("textures/texture01.jpg");
img1.resize(width, height);
img2 = loadImage("textures/texture02.jpg");
img2.resize(width, height);
imgMask = createImage(width, height, ALPHA);
noise = new OpenSimplexNoise();
}
void draw() {
float percent = 0;
if (record) {
percent = float(frameCount) / totalFrames;
} else {
percent = float(frameCount % totalFrames) / totalFrames;
}
render(percent);
image(img2, 0, 0);
img1.mask(imgMask);
image(img1, 0, 0);
if (record) {
saveFrame("output/gif-"+nf(frameCount, 3)+".png");
if (frameCount == totalFrames) {
exit();
}
}
}
void render(float percent) {
float angle = map(percent, 0, 1, 0, TWO_PI);
float uoff = map(sin(angle), -1, 1, 0, 1);
float voff = map(cos(angle), -1, 1, 0, 1);
float xoff = 0;
imgMask.loadPixels();
for (int x = 0; x < width; x++) {
float yoff = 0;
for (int y = 0; y < height; y++) {
float n;
if (record) {
// 4D Open Simplex Noise is very slow!
n = (float) noise.eval(xoff, yoff, uoff, voff);
} else {
// If you aren't worried about looping run this instead for speed!
n = (float) noise.eval(xoff, yoff, zoff);
}
float bright = map(n, 0, 1, 0, 255);
imgMask.pixels[x + y * width] = color(bright);
yoff += increment;
}
xoff += increment;
}
imgMask.updatePixels();
if (!record) {
zoff += increment;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment