Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
depixelate effect/glitch, strobe. Shows how to control an effect vs time (start, duration, repetition).
/*
bTimeEffect_DePixelate (square pixels)
by butterw
control a video pixel shader effect vs time.
tested in mpc-hc v1.9.3:
- Opening the player by double clicking a video starts the clock
- Short effect/glitch repeats every 10 seconds by default (T).
- Note that Loading another video or Pause/Stop does not reset the clock !
The Effect here is Pixellation followed by De-Pixellation. On the first 2 runs it is preceded by a black screen.
Some version of the De-Pixellation effect was used when starting the game super mario world (SNES Mosaic effect).
The npixels values vs time could be further improved.
- at 5s, a fixed duration (td) non-repeating effect (blue screen strobe).
see Strobe.hlsl for a ready to use color frame insert (tstart, duration, repetition period).
*/
sampler s0: register(s0);
float4 p0: register(c0);
#define W p0.x
#define H p0.y
// #define Counter p0.z //float frame counter, starting at 0.
#define Clock p0.w //in s, starting at 0.
#define WH p0.xy
/* --- Pixelate --- */
float4 Pixelate1(float2 tex, float npixels){
/* rectangular pixels, npixels>1. (single pixel), Lower is more aggressive. */
return tex2D(s0, floor(tex * npixels) / npixels);
}
float4 Pixelate2(float2 tex, float npixels){
/* square pixels version */
float pixelSize = max(W, H) / npixels;
return tex2D(s0, floor(tex * WH / pixelSize) * pixelSize/WH);
}
float4 Strobe(float4 inputColor) {
/* returns the provided input color: placeholder for any effect/glitch */
return inputColor;
}
float4 main(float2 tex: TEXCOORD0): COLOR {
float4 c0 = tex2D(s0, tex); // set c0 before calling functions that return tex2D (conditional compilation error !)
/*--- Pixelate Effect ---*/
float T = 10.; // T: Repetition period in seconds, no repetition if T=0.
int nCycles = (T>0) ? int(Clock/T): 0; // Number of cycles calculation
float t = Clock - nCycles*T; //Cycle time in seconds
float4 ts = float4(1., 0.250, 0.350, 0.100); //start time and durations
ts[1]+= ts[0]; ts[2]+= ts[1]; ts[3]+= ts[2]; //update times
if (nCycles<=2 && t>=ts[0]-0.300 && t<ts[0])
return Strobe(float4(1, 1, 1, 1)*16/255.); //black16 Screen
if (t >= ts[0] && t < ts[1])
return Pixelate2(tex, 215-1000.*(t-ts[0])); //fast pixel decrease
if (t >= ts[1] && t < ts[2])
return Pixelate2(tex, 15-20.*(t-ts[1]));
if (t >= ts[2] && t < ts[3])
return Pixelate2(tex, 8); //constant low pixel
/*De-Pixelate Effect */
ts[0] = ts[3]; //starts directly after Pixelate Effect
ts.yzw= float3(0.100, 0.350, 0.250); //set Durations
ts[1]+= ts[0]; ts[2]+= ts[1]; ts[3]+= ts[2]; //update times
if (t >= ts[0] && t < ts[1])
return Pixelate2(tex, 8.);
if (t >= ts[1] && t < ts[2])
return Pixelate2(tex, 8+20.*(t-ts[1])); //slow linear pixel increase
if (t >= ts[2] && t < ts[3])
return Pixelate2(tex, 15+1000.*(t-ts[2])); //fast pixel increase
//end of Effect
float tstart=5.;
float td=0.5; //duration in s
if (Clock>=tstart && Clock<tstart+td) return Strobe(float4(0, 0, 1, 0)); //Blue Screen, No repetition
return c0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.