Creating a Feedback Effect is Pretty Simple

Someone asked about video feedback effects for a computer game. It's a powerful tool that can produce amazing effects in the hand of a master but the core principles are really simple. These are the notes I wrote :)

Feedback is a loop where you have a current state and an operation on that state to create a new one. Audio feedback is a good example. The current state is what's going into the microphone and the operation is the audio being amplified, coming out of the speakers and then going back into the microphone.

If the input is silence and the operation adds no new content then the output is silence, not very interesting. Add some input and you can start a feedback loop where you don't need to add any more input but your initial seed will cycle around. If the feedback overloads you can end up with the system getting overloaded

The trick to feedback is tweaking the input and operation so it doesn't overload but gives you an interesting effect.

This is generally how audio effects work; deary, reverb, chorus or flange are all examples of feedback loops that are carefully constructed to produce feedback loops with interesting outputs that don't overload to a screaming howl.

And you can do the same with video. You actually do this in analogue pretty much the same as audio by pointing a video camera at the same screen it's outputting to. Pointed at a black screen isn't very interesting but if you introduce just a little input into the system you'll get interesting patterns generated. The light travelling through the camera and onto the screen gets corrupted by being quantised into transmissible information and those corruptions end up being magnified with every iteration. It's like taking a photocopy of a photocopy of a photocopy.

http://www.youtube.com/watch?v=mKK-q3yjph4

So doing something similar on a computer is kind of easy. You just need a couple of off-screen buffers, a shader to act as the operator and some kind of initial input.

Here's an easy start:

Create textures you can render into - 0 and 1
Clear both of them

loop:
    Draw something new onto screen 0 (input)
    Draw a full screen quad into buffer 1 using buffer 0 and buffer 1 as inputs using your feedback shader (operator)
    Screen 1 is your feedback texture you use in the main game

    Draw something new onto screen 1 (input)
    Draw a full screen quad into buffer 0 using buffer 1 and buffer 0 as inputs using your feedback shader (operator)
    Screen 0 is your feedback texture you use in the main game

goto loop

Feedback Shader

It has two texture inputs: last screen and current screen and you merge these two together.

Here's an easy one, a simple scale and blend where you take the old screen, scale it slightly, blend with the new screen.

It's pretty easy:

  • Read a texel from the current screen
  • Scale the UVs by a small amount (1.01 for example)
  • Read a texel from the previous screen using the scaled UVs
  • Blend them (try 0.9 of current screen and 0.1 of last screen)
  • Return blended pixel

That should scale the texture from one of the corners as UVs run from 0 to 1. If you want to scale from the centre then you need to subtract 0.5 from the UV co-ordinates before scaling and then add 0.5 back before doing the texture read from the previous screen.

I'll try and find some code this weekend but that's the basics. You can put all sorts of fancy stuff in the shader. It gets fun when you operate on te colours as well. You really need a few input parameters that you can tweak as it's easy to overload the feedback and end up with a white screen. The interesting stuff is where the various parameters and inputs add up to something that's just on the edge of overload. You get all manner of interesting shit then. But to get that type of thing you need input parameters you can tweak and a way of saving off interesting effects.