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
- Add a Video Device In TOP set to your webcam.
- Connect to a Null TOP named
CAM.
Part 2: Write the Sort Shader
- Add a GLSL TOP at the same resolution as
CAM. - Connect
CAMto input 0. - 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
uThresholdsample from a position shifted right by(brightness - threshold) × strength. Brighter pixels reach further and leave longer streaks. Below the threshold the offset is0, so dark pixels stay put without anifbranch.
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:
| Name | Default | Min | Max |
|---|---|---|---|
uThreshold | 0.5 | 0.0 | 1.0 |
uSortStrength | 0.3 | 0.0 | 1.0 |
uDirection | 1.0 | -1.0 | 1.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.
- Add a Feedback TOP.
- Cross TOP: Input 0 = GLSL output, Input 1 = Feedback. Cross →
0.7(70% history, 30% new frame). - Connect to a Null TOP named
SORT_OUT. - Set Feedback TOP Target →
SORT_OUT.
Performance toggle
Add a Switch TOP after
SORT_OUTwith the raw GLSL output on its other input. Flipping its Index toggles between sorted and datamoshed during a performance.
Part 5: Polish
- HSV Adjust TOP → Saturation Multiplier
1.3. - Bloom TOP (Threshold:
0.5, Intensity:0.6). - Connect to a Null TOP named
OUT.
Variations
| Variant | How to do it |
|---|---|
| Vertical sort | Change vec2(uv.x + offset, uv.y) to vec2(uv.x, uv.y + offset) |
| Color channel split | Three GLSL TOPs (one per channel), different uSortStrength each. Merge with Reorder TOP. |
| Motion-triggered | Route Optical Flow TOP luma to uThreshold so motion activates the sort. |
| Audio-reactive | Bind uSortStrength to an Analyze CHOP RMS so beats extend the streaks. |
Troubleshooting
- “Whole image turns to horizontal bars.”
uSortStrengthis too high. Drop it to0.1and build up. - “Nothing moves, looks like a normal webcam.” Check
uThresholdis below1.0and that GLSL input 0 is connected toCAM. 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 isSORT_OUT.
Next Steps
- Hardware knob: Bind
uThresholdto 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 differentuSortStrength, recombine. Channels drift apart as chromatic aberration.
Parameter Tuning & Behavior
| Parameter | Behavior |
|---|---|
| uThreshold | Lower = more pixels sorted; Higher = only peak highlights affected. |
| uSortStrength | Higher = long streaks; Lower = subtle shimmer. |
| uDirection | 1.0 = streaks right; -1.0 = streaks left. |
| Cross (feedback) | Higher = datamosh smear; Lower = crisp per-frame sort. |
| Bloom Intensity | Higher = 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 ──▶ OUTSources:
(y) Return to Recipes & Projects | (y) Return to TouchDesigner | (y) Return to Home