Skip to content

Instantly share code, notes, and snippets.

@butterw

butterw/bSide.hlsl

Last active Jun 20, 2020
Embed
What would you like to do?
hlsl pixel shader tested in mpc-hc video player. Apply effects to an xy-zone of the video (half the screen, or a box). Use: Side by side effect comparisons. Pixelate zone, Mask logo. Horizontal/Vertical Line.
sampler s0: register(s0);
float4 p1: register(c1);
#define px p1.x //: one_over_width: 1/W
#define py p1.y //: one_over_height: 1/H
/* bSide.hlsl by butterw v1.0 (ZoneShader_mpc)
- this directX pixel shader shows how to apply effects to an xy-zone of the video (half the screen, or a box)
applications:
- half the screen: Side by side effect comparisons.
- box: pixelate zone, remove logo.
- optionally plots horizontal/vertical red lines at desired coordinates.
tested in mpc-hc v1.9.3:
! coordinates are correct post-resize only when you go full-screen: use pre-resize or full-screen post-resize
Perf: (2 texture, 12 arithmetic)
*/
bool insideBox(float2 tex, float2 topLeft, float2 bottomRight) {
/* returns true if tex coordinates inside the box, returns false otherwise */
float2 s = step(topLeft, tex) - step(bottomRight, tex);
return s.x * s.y;
}
#define Color_rgba float4(1, 0, 0, 0)
float4 Saturated(float4 colorInput) {
/* ex: saturate the red channel */
colorInput+= Color_rgba; //add or remove (if negative) rgb color
return saturate(colorInput); //ensures the output rgba is in [0, 1] range
}
/* Pixelate, with rectangular pixels*/
#define Npixels 80. //float>0, Ex: 100., Lower is more aggressive. 1. is single pixel
float4 Pixelate1(float2 tex){
return tex2D(s0, floor(tex *Npixels)/Npixels);
}
/* --- Main --- */
float4 main(float2 tex: TEXCOORD0): COLOR {
float4 c0 = tex2D(s0, tex); //No Effect
/* Plot x/y Red lines */
#define Red float4(1, 0, 0, 0)
#define plotlines_mode 1 //1 plot lines, otherwise don't.
#define xline 0. //float 0. [0, 1], vertical line at x==xline
#define yline 0.5 //float 0.5 [0, 1], horiz line at y==yline
#if plotlines_mode == 1
/* Plot a single pixel vertical red line*/
if (px > abs(tex.x-xline)) return Red;
/* Plot a single pixel horizontal red line at yline */
if (py > abs(tex.y-yline)) return Red;
#endif
/* Side by side comparison of Effects: Left/Right, Top/Bottom */
if (tex.x <= 0.5) return Pixelate1(tex); // Left Side
// if (tex.y <= 0.5) return c0(tex); //Top Side
// if (tex.y > 0.5) return c0; // Bottom Side
// if (tex.x > tex.y) return Pixelate1(tex); // Right Diagonal Top
/* Define a box */
float2 uv0 = {0.78, 0.82}; //topLeft of box, range: [0-1], {0., 0.}: topleft of screen
float2 dim0 = {0.22, 0.18}; //dimensions of rectangle
/* Apply effect to box: possible use masking a logo, viewing another part of picture, etc. */
if (insideBox(tex, uv0, uv0 + dim0)) return Saturated(c0); //Red; //Pixelate1(tex);
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.