Bright pixels get dragged horizontally across the frame, leaving streaks of color that look like corrupted data. This is pixel sorting, popularized by Kim Asendorf’s Processing sketches and now common in live visuals and music videos.

A GLSL shader does the sort. An optional feedback loop turns it into datamosh.

Operator Families in this Recipe

  • TOPs (Texture Operators): Webcam input and compositing.
  • GLSL: Brightness-based pixel shift as a per-pixel shader.

Part 1: Webcam Input

  1. Add a Video Device In TOP set to your webcam.
  2. Connect to a Null TOP named CAM.

Part 2: Write the Sort Shader

  1. Add a GLSL TOP at the same resolution as CAM.
  2. Connect CAM to input 0.
  3. Right-click → Edit Pixel Shader. Paste:
uniform sampler2D sTD2DInputs[TD_NUM_2D_INPUTS];
 
uniform float uThreshold;
uniform float uSortStrength;
uniform float uDirection;   // 1.0 = right, -1.0 = left
 
layout(location = 0) out vec4 fragColor;
 
void main()
{
    vec2 uv    = vUV.st;
    vec4 color = texture(sTD2DInputs[0], uv);
    float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114));
 
    // Zero below the threshold, so dark pixels sample themselves
    float offset = max(luma - uThreshold, 0.0) * uSortStrength * uDirection;
    fragColor = TDOutputSwizzle(texture(sTD2DInputs[0], vec2(uv.x + offset, uv.y)));
}

What the shader does

Pixels above uThreshold sample from a position shifted right by (brightness - threshold) × strength. Brighter pixels reach further and leave longer streaks. Below the threshold the offset is 0, so dark pixels stay put without an if branch.


Part 3: Set the Uniforms

On the GLSL TOP’s Vectors page, add three uniforms with these names. The Min/Max columns are the useful range to explore:

NameDefaultMinMax
uThreshold0.50.01.0
uSortStrength0.30.01.0
uDirection1.0-1.01.0

Start with the defaults. Drop uThreshold to 0.3 to sort more of the image, or raise it to 0.7 to isolate the brightest highlights.


Part 4: Add Feedback for Datamosh

Without feedback each frame sorts fresh from the clean webcam. With feedback, displaced pixels pile up across frames and the image slowly disintegrates.

  1. Add a Feedback TOP.
  2. Cross TOP: Input 0 = GLSL output, Input 1 = Feedback. Cross → 0.7 (70% history, 30% new frame).
  3. Connect to a Null TOP named SORT_OUT.
  4. Set Feedback TOP Target → SORT_OUT.

Performance toggle

Add a Switch TOP after SORT_OUT with the raw GLSL output on its other input. Flipping its Index toggles between sorted and datamoshed during a performance.


Part 5: Polish

  1. HSV Adjust TOP → Saturation Multiplier 1.3.
  2. Bloom TOP (Threshold: 0.5, Intensity: 0.6).
  3. Connect to a Null TOP named OUT.

Variations

VariantHow to do it
Vertical sortChange vec2(uv.x + offset, uv.y) to vec2(uv.x, uv.y + offset)
Color channel splitThree GLSL TOPs (one per channel), different uSortStrength each. Merge with Reorder TOP.
Motion-triggeredRoute Optical Flow TOP luma to uThreshold so motion activates the sort.
Audio-reactiveBind uSortStrength to an Analyze CHOP RMS so beats extend the streaks.

Troubleshooting

  • “Whole image turns to horizontal bars.” uSortStrength is too high. Drop it to 0.1 and build up.
  • “Nothing moves, looks like a normal webcam.” Check uThreshold is below 1.0 and that GLSL input 0 is connected to CAM. Check the Info DAT for compile errors.
  • “Shader compile error (green or pink output).” Open the GLSL TOP → Info DAT for the error. Usually a missing semicolon or uniform name mismatch.
  • “Datamosh fills completely, loses image.” The Cross TOP value is too high. Try 0.5, and confirm the Feedback Target is SORT_OUT.

Next Steps

  • Hardware knob: Bind uThreshold to a MIDI In CHOP so a physical knob sweeps the sort boundary live.
  • Masked sort: Use a body segmentation mask to restrict pixel sorting to the webcam subject only.
  • Chromatic sort: Split webcam into R, G, B with Reorder TOP, sort each channel with a different uSortStrength, recombine. Channels drift apart as chromatic aberration.

Parameter Tuning & Behavior

ParameterBehavior
uThresholdLower = more pixels sorted; Higher = only peak highlights affected.
uSortStrengthHigher = long streaks; Lower = subtle shimmer.
uDirection1.0 = streaks right; -1.0 = streaks left.
Cross (feedback)Higher = datamosh smear; Lower = crisp per-frame sort.
Bloom IntensityHigher = neon glow; Lower = harsh clean glitch.

Network Architecture

Video Device In ──▶ CAM
                     │
                     ▼
               GLSL TOP (sort shader)
                     │
            Cross TOP (0.7 old + 0.3 new) ◀── Feedback TOP
                     │                         (target: SORT_OUT)
                     ▼
               SORT_OUT
                     │
          HSV Adjust ──▶ Bloom ──▶ OUT

Sources:

(y) Return to Recipes & Projects | (y) Return to TouchDesigner | (y) Return to Home