Skip to content

Instantly share code, notes, and snippets.

@just-Addict
Last active August 16, 2022 07:21
Show Gist options
  • Save just-Addict/67a2b98a19ff3ef1b87425351c9f1ad2 to your computer and use it in GitHub Desktop.
Save just-Addict/67a2b98a19ff3ef1b87425351c9f1ad2 to your computer and use it in GitHub Desktop.
draws gridlines at set intervals (either horizontal, vertical or both) color density, distance and line thickness configurable.
texture tex : WAVEFORMDATA;
sampler sTex = sampler_state
{
Texture = (tex);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Clamp;
};
struct VS_IN
{
float2 pos : POSITION;
float2 tc : TEXCOORD0;
};
struct PS_IN
{
float4 pos : SV_POSITION;
float2 tc : TEXCOORD0;
};
float4 backgroundColor : BACKGROUNDCOLOR;
float4 highlightColor : HIGHLIGHTCOLOR;
float4 selectionColor : SELECTIONCOLOR;
float4 textColor : TEXTCOLOR;
float cursorPos : CURSORPOSITION;
bool cursorVisible : CURSORVISIBLE;
float seekPos : SEEKPOSITION;
bool seeking : SEEKING;
float4 replayGain : REPLAYGAIN; // album gain, track gain, album peak, track peak
float2 viewportSize : VIEWPORTSIZE;
bool horizontal : ORIENTATION;
bool flipped : FLIPPED;
bool shade_played : SHADEPLAYED;
float trackDuration : TRACKDURATION;
PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN)0;
float2 half_pixel = float2(1,-1) / viewportSize;
output.pos = float4(input.pos - half_pixel, 0, 1);
if (horizontal)
{
output.tc = float2((input.tc.x + 1.0) / 2.0, input.tc.y);
}
else
{
output.tc = float2((-input.tc.y + 1.0) / 2.0, input.tc.x);
}
if (flipped)
output.tc.x = 1.0 - output.tc.x;
return output;
}
#define GRID_VERTICAL
#define GRID_HORIZONTAL
//#define ABOVE_WAVE
//#define BELOW_WAVE
//#define INSIDE_WAVE
#define OUTSIDE_WAVE
float4 grid = float4(5.0,0.0,1.0,0.75);
float4 draw_grid( float2 tc, float4 aColor, float2 vpDelta, bool insideWave )
{
//
// grid.x : x and/or y interval
// grid.y : currently unused
// grid.z : thickness of the gridline
// grid.a : color intensity of gridline. darker< 1 >lighter
//
#if defined(ABOVE_WAVE)
if (tc.y > 0 && !insideWave)
#elif defined(BELOW_WAVE)
if (tc.y < 0 && !insideWave)
#elif defined(INSIDE_WAVE)
if (insideWave)
#elif defined(OUTSIDE_WAVE)
if (!insideWave)
#endif
{
#ifdef GRID_HORIZONTAL
if (grid.x > 1
&& (tc.x % (vpDelta.x*grid.x) < (vpDelta.x*grid.z)))
aColor *= grid.a;
#endif
#ifdef GRID_VERTICAL
if (grid.x > 1
&& ((1-tc.y) % (vpDelta.y*grid.x*2) < (vpDelta.y*grid.z*2)))
aColor *= grid.a;
#endif
}
return aColor;
}
float4 bar( float pos, float2 tc, float4 fg, float4 bg, float width, bool show )
{
float dist = abs(pos - tc.x);
float4 c = (show && dist < width)
? lerp(fg, bg, smoothstep(0, width, dist))
: bg;
return c;
}
float4 faded_bar( float pos, float2 tc, float4 fg, float4 bg, float width, bool show, float vert_from, float vert_to )
{
float dist = abs(pos - tc.x);
float fluff = smoothstep(vert_from, vert_to, abs(tc.y));
float4 c = show
? lerp(fg, bg, max(fluff, smoothstep(0, width, dist)))
: bg;
return c;
}
// #define BORDER_ON_HIGHLIGHT
float4 played( float pos, float2 tc, float4 fg, float4 bg, float alpha)
{
float4 c = bg;
float2 d = 1 / viewportSize;
if (pos > tc.x)
{
#ifdef BORDER_ON_HIGHLIGHT
if (tc.x < d.x || tc.y >= (1 - d.y) || tc.y <= (2 * d.y - 1))
c = selectionColor;
else
#endif
c = lerp(c, fg, saturate(alpha));
}
return c;
}
float4 evaluate( float2 tc, inout bool insideWave )
{
// alpha 1 indicates biased texture
float4 minmaxrms = tex1D(sTex, tc.x);
minmaxrms.rgb -= 0.5 * minmaxrms.a;
minmaxrms.rgb *= 1.0 + minmaxrms.a;
float below = tc.y - minmaxrms.r;
float above = tc.y - minmaxrms.g;
float factor = min(abs(below), abs(above));
bool outside = (below < 0 || above > 0);
bool inside_rms = abs(tc.y) <= minmaxrms.b;
float4 bgColor = backgroundColor;
float4 wave = outside
? bgColor
: lerp(bgColor, textColor, 7.0 * factor);
insideWave = !outside;
return saturate(wave);
}
float4 PS( PS_IN input ) : SV_Target
{
float dx, dy;
float2 d;
float2 vp = 1/viewportSize;
if (horizontal)
{
d.xy = vp.xy;
}
else
{
d.xy = vp.yx;
}
float seekWidth = 2.5 * d.x;
float positionWidth = 2.5 * d.x;
// passed thru an inout argument so we don't need to jump hoops to re-use it
bool insideWave;
float4 c0 = evaluate(input.tc, insideWave);
c0 = draw_grid(input.tc, c0, d, insideWave);
c0 = bar(cursorPos, input.tc, selectionColor, c0, positionWidth, cursorVisible);
c0 = bar(seekPos, input.tc, selectionColor, c0, seekWidth, seeking );
if (shade_played)
c0 = played(cursorPos, input.tc, highlightColor, c0, 0.3);
return c0;
}
technique Render9
{
pass
{
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}
@just-Addict
Copy link
Author

just-Addict commented Jun 4, 2016

seekbar-griddrawing
note: colors used need to have at least two of their components non-zero, so pure red/blue/green or black won't show gridlines. And white can only show darker lines of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment