Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active March 29, 2022 09:50
Show Gist options
  • Save KrabCode/eb9708c04cdae1e991cc5aaf0bf72b2d to your computer and use it in GitHub Desktop.
Save KrabCode/eb9708c04cdae1e991cc5aaf0bf72b2d to your computer and use it in GitHub Desktop.
Simple feedback effect in processing / glsl... feedback.glsl belongs in the data folder
uniform sampler2D texture; // the current canvas is passed to this shader in Processing as "texture" which is a bad name for it but whatever
uniform vec2 resolution; //set automatically by Processing
uniform float time; //set manually in code
void main(){
vec2 uv = gl_FragCoord.xy / resolution.xy; // coordinate with 0,0 at bottom left, and 1,1 at top right
vec2 offset = vec2(cos(time), sin(time)) / resolution.xy; // the rotating offset to sample a neighbour at
vec4 thisPixelColor = texture2D(texture, uv);
vec4 nearPixelColor = texture2D(texture, uv + offset);
vec4 finalColor = mix(thisPixelColor, nearPixelColor, 0.5); // lerp between the two colors, 0.5 specifies how quickly the effect fades into the background
gl_FragColor = vec4(finalColor.rgb, 1.);
}
PShader feedback;
float t;
void setup() {
size(640, 640, P2D);
smooth(16);
background(0);
colorMode(HSB, 1, 1, 1, 1);
feedback = loadShader("feedback.glsl");
}
void draw() {
t += 0.001;
translate(width / 2, height / 2);
//rotate(t * 10);
fill(0, 0.1);
strokeWeight(3);
stroke(t % 1, 1, 1);
rectMode(CENTER);
rect(0, 0, 120, 120);
feedback.set("time", t);
filter(feedback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment