Skip to content

Instantly share code, notes, and snippets.

@marcedwards
Created May 20, 2020 02:01
Show Gist options
  • Save marcedwards/27dc15d2dee273878d1186ca0e5bf383 to your computer and use it in GitHub Desktop.
Save marcedwards/27dc15d2dee273878d1186ca0e5bf383 to your computer and use it in GitHub Desktop.
Interference Worm in Processing
//
// Interference Worm.
// Created using Processing 3.5.4.
//
// Code by @marcedwards from @bjango.
//
color dark = #333333;
color light = #fbfbfb;
int padding = 60;
int lines = 34;
int lineheight = 10;
int linewidth = 160;
float offset = 0;
void setup() {
size(440, 440, P2D);
frameRate(30);
smooth(8);
stroke(light);
strokeWeight(2);
strokeCap(ROUND);
noFill();
}
void draw() {
background(dark);
blendMode(SCREEN);
offset = 0;
drawLines(#ff0000);
offset = 1;
drawLines(#00ff00);
offset = 2;
drawLines(#0000ff);
}
void drawLines(color col) {
float x = 0;
float y = 0;
stroke(col);
beginShape();
for (int i = 0; i < lines; i += 2) {
for (int j = 0; j < linewidth; j++) {
x = calcX(j);
y = calcY(j, i);
vertex(x, y);
}
if (i != lines - 2) {
corner(x, y, calcX(linewidth), calcY(linewidth, i + 1) - lineheight, false);
}
if (i != lines - 2) {
for (int j = linewidth; j > 0; j--) {
x = calcX(j);
y = calcY(j, i + 1);
vertex(x, y);
}
corner(x, y, calcX(0), calcY(0, i + 2) - lineheight, true);
}
}
endShape();
}
float calcX(float j) {
return padding + j * 2;
}
float calcY(float j, float i) {
float x = calcX(j);
float y = padding + i * lineheight;
float off1 = Ease.hermite5(Ease.tri(gradientDiamond(x, y, timeLoop(120, offset), 0.6)), 3);
float off2 = Ease.hermite5(Ease.tri(gradientDiamond(x, y, timeLoop(120, offset + 15), 0.6)), 5);
float off3 = Ease.hermite5(Ease.tri(gradientDiamond(x, y, timeLoop(240, offset + 30), 0.6)), 5);
float fulloffset = (off1 + off2 + off3) * 6;
return lineheight + y - fulloffset;
}
void corner(float xa, float ya, float xb, float yb, boolean inverted) {
if (!inverted) {
curveVertex(xa, ya);
curveVertex(xa, ya);
curveVertex(xa + lineheight, lerp(ya, yb + lineheight, 0.1));
curveVertex(xa + lineheight, lerp(ya, yb + lineheight, 0.9));
curveVertex(xb, yb + lineheight);
curveVertex(xb, yb + lineheight);
} else {
curveVertex(xa, ya);
curveVertex(xa, ya);
curveVertex(xa - lineheight, lerp(ya, yb + lineheight, 0.1));
curveVertex(xa - lineheight, lerp(ya, yb + lineheight, 0.9));
curveVertex(xb, yb + lineheight);
curveVertex(xb, yb + lineheight);
}
}
float circleNoise(float x, float y, float z, float diameter) {
return noise(cos(x * TAU) * diameter + diameter,
sin(y * TAU) * diameter + diameter,
z);
}
//
float gradientDiamond(float x, float y, float offset, float scale) {
float biggest = max(width * scale, height * scale);
float xd = abs((width / 2) - x);
float yd = abs((height / 2) - y);
return wrap((xd + yd) / biggest, 1 - offset);
}
float wrap(float value, float offset) {
return (value + offset) % 1;
}
static class Ease {
static public float tri(float t, float repeat) {
return t * repeat * 2 % 2 <= 1 ? t * repeat * 2 % 2 : 2 - (t * repeat * 2 % 2);
}
static public float tri(float t) {
return t < 0.5 ? t * 2 : 2 - (t * 2);
}
static public float hermite5(float t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
static public float hermite5(float t, int repeat) {
for (int i = 0; i < repeat; i++) {
t = hermite5(t);
}
return t;
}
}
float timeLoop(float totalframes, float offset) {
return (frameCount + offset) % totalframes / totalframes;
}
float timeLoop(float totalframes) {
return timeLoop(totalframes, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment