Ink Brush

by MusicBar, progress bar

A calligraphy brush paints one long sumi-e stroke up to the playhead with real dry-brush texture, starting from a red seal under faint bamboo leaves, pressing harder when the song is loud and flicking ink drops on every beat.

0 downloads Download file, 9.1 KB
How it installs, its SHA-256 and its code

Click Apply: MusicBar downloads it, checks its SHA-256 and puts it on the taskbar, or just puts it on if it already has it. Needs MusicBar 1.8 or newer.

SHA-256 dea260f32370421da031bd3011ee320ea416975b82449882db6a8a8906a9d5df

//! name: Ink Brush
//! author: MusicBar
//! kind: progress
//! description: A sumi-e brush paints one long dry-brush stroke up to the playhead after a red seal, under faint bamboo leaves, pressing harder when the song is loud and flicking ink drops on every beat.

float hash2(float2 p)
{
    float3 q = frac(float3(p.x, p.y, p.x) * 0.1031);
    q += dot(q, q.yzx + 33.33);
    return frac((q.x + q.y) * q.z);
}

float vnoise(float2 p)
{
    float2 i = floor(p), f = frac(p);
    f = f * f * (3.0 - 2.0 * f);
    return lerp(lerp(hash2(i), hash2(i + float2(1, 0)), f.x), lerp(hash2(i + float2(0, 1)), hash2(i + float2(1, 1)), f.x), f.y);
}

// Lays a color over what is there (premultiplied light, and how much of the pixel it covers).
void layer(inout float3 acc, inout float cover, float3 rgb, float a)
{
    a = saturate(a);
    acc = rgb * a + acc * (1.0 - a);
    cover = a + cover * (1.0 - a);
}

// The stroke's centerline and half width at x (pixels), as the hand moved and pressed.
float strokeY(float x, float u) { return u * (0.33 + 0.07 * sin(x / u * 0.41 + 0.6) + 0.03 * sin(x / u * 1.13 + 2.0)); }
float strokeW(float x, float u) { return u * (0.165 + 0.06 * sin(x / u * 0.37 + 1.2) + 0.03 * sin(x / u * 1.1)); }

// One bamboo leaf from base b along angle a: a slender lance, widest near the base, drooping to a sharp tip.
float leafD(float2 p, float2 b, float a, float len, float wid)
{
    float2 q = p - b;
    q = float2(cos(a) * q.x + sin(a) * q.y, -sin(a) * q.x + cos(a) * q.y);
    q.y += 0.25 * q.x * q.x / len;
    float t = saturate(q.x / len);
    return max(abs(q.y) - wid * sin(3.1416 * pow(t, 0.75)), max(-q.x, q.x - len));
}

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float u = size.y;
    float2 px = float2(uv.x * size.x, (1.0 - uv.y) * u);     // pixels, y up the screen so leaves hang and drops fall
    float head = progress * size.x;
    float x0 = 1.15 * u;                                      // where the stroke begins, after the seal
    float3 ink = float3(0.04, 0.04, 0.05);
    float3 paper = float3(0.94, 0.91, 0.84);

    float3 acc = float3(0, 0, 0);
    float cover = 0;

    // Faint bamboo leaves in pale ink: a twig from the top edge and three blades fanning down, stirring in a breeze.
    float lc = floor(px.x / (6.5 * u));
    float lh = hash2(float2(lc, 4.0));
    float2 lb = float2((lc + 0.3 + 0.4 * lh) * 6.5 * u, 0.86 * u);
    float sw = 0.05 * sin(time * 0.9 + lh * 9.0) + 0.05 * level * sin(time * 2.3);
    float2 ta = lb + float2(-1.0, 0.3) * u;                   // the twig runs down to the right, leaves hang off it
    float lf = min(leafD(px, lerp(ta, lb, 0.35), -0.6 + sw * 1.4, 0.5 * u, 0.065 * u), leafD(px, lerp(ta, lb, 0.68), -1.0 + sw * 1.2, 0.46 * u, 0.06 * u));
    lf = min(lf, leafD(px, lb, -0.35 + sw, 0.6 * u, 0.07 * u));
    float twig = abs(px.y - lb.y - (lb.x - px.x) * 0.3) - 0.012 * u - 0.3;
    lf = min(lf, max(twig, max(px.x - lb.x, ta.x - px.x)));
    float3 leafC = lerp(float3(0.44, 0.47, 0.45), float3(0.62, 0.65, 0.62), vnoise(px * 0.35));
    layer(acc, cover, leafC, smoothstep(0.5, -0.5, lf) * step(lh, 0.8) * 0.42);

    // The stroke: pressed in at the start, swelling where the brush presses now, narrowing into the tip.
    float x = px.x;
    float back = head - x;                                    // pixels behind the brush
    float fresh = exp(-max(back, 0.0) / (0.8 * u));
    float cy = strokeY(x, u);
    float w = strokeW(x, u) * (1.0 + 0.7 * exp(-max(x - x0, 0.0) / (0.4 * u)));
    w *= 1.0 + (0.4 * level + 0.35 * beat) * fresh;
    w = lerp(w, 0.06 * u, exp(-max(back, 0.0) / (0.25 * u)));
    float dS = abs(px.y - cy) - w;
    dS = x < x0 ? length(float2((x0 - x) * 1.4, px.y - cy + (x0 - x) * 0.5)) - w : dS;
    dS = max(dS, x - head);

    // Dry brush: bristle streaks run along the stroke and break into flying white toward its edges and where it ran dry.
    float v = (px.y - cy) / max(w, 0.5);
    float along = x / u;
    float streak = vnoise(float2(v * 5.0 + 11.0, along * 0.6)) * 0.6 + vnoise(float2(v * 14.0, along * 2.0)) * 0.4;
    float patch = smoothstep(0.35, 0.75, vnoise(float2(along * 0.22, 2.0)));
    float dry = (0.05 + 0.5 * pow(abs(v), 3.0) + 0.42 * patch) * (1.0 - 0.75 * fresh);
    float inkA = smoothstep(dry - 0.06, dry + 0.06, streak) * smoothstep(0.5, -0.5, dS);
    float wet = 1.0 - saturate(dry * 1.6);

    // A thin wet bleed into the paper fibers, then the ink: silvery bristle tones, darker pooled edges, a sheen while fresh.
    float fiber = vnoise(float2(px.x * 0.12, px.y * 0.5)) * 0.6 + vnoise(px * 0.2) * 0.4;
    float feather = exp(-max(dS, 0.0) / (0.03 * u * (0.6 + 0.8 * fiber))) * step(0.0, dS) * step(x, head);
    layer(acc, cover, lerp(paper, float3(0.5, 0.5, 0.52), 0.35), feather * (0.1 + 0.2 * wet));
    float3 tone = lerp(float3(0.17, 0.17, 0.18), float3(0.56, 0.56, 0.58), smoothstep(0.45, 0.95, streak) * (0.4 + 0.6 * saturate(dry * 1.8)));
    tone = lerp(tone, ink, smoothstep(0.35, 0.05, streak) * 0.8);
    tone = lerp(tone, ink, smoothstep(0.55, 0.95, abs(v)) * (0.3 + 0.6 * wet));
    tone += float3(0.55, 0.55, 0.57) * exp(-pow((v - 0.45) / 0.15, 2.0)) * (0.05 + 0.35 * fresh) * wet;
    layer(acc, cover, tone, inkA);
    layer(acc, cover, float3(0.6, 0.6, 0.62), smoothstep(1.0, 0.0, abs(dS + 0.3)) * inkA * 0.5);

    // Old splatter dots beside the stroke.
    float2 gc = floor(px / (0.22 * u));
    float gh = hash2(gc + 31.0);
    float2 gp = (gc + 0.2 + 0.6 * float2(hash2(gc + 2.0), hash2(gc + 9.0))) * 0.22 * u;
    float near = step(abs(gp.y - strokeY(gp.x, u)), strokeW(gp.x, u) + 0.16 * u) * step(gp.x, head - 0.3 * u) * step(x0, gp.x);
    float spl = length(px - gp) - u * (0.01 + 0.022 * frac(gh * 7.0));
    layer(acc, cover, float3(0.5, 0.5, 0.52), smoothstep(1.2, 0.0, abs(spl)) * step(gh, 0.09) * near * 0.3);
    layer(acc, cover, ink + 0.1, smoothstep(0.5, -0.5, spl) * step(gh, 0.09) * near);

    // The brush at the playhead: ink-soaked hair whose tip drags back along the stroke, a white-haired belly,
    // a lacquer ferrule and a bamboo handle. It dips on the beat.
    float2 tip = float2(head, strokeY(head, u) - 0.03 * u * beat);
    float2 dir = normalize(float2(0.3, 1.0));
    float2 bp = px - tip;
    float t = dot(bp, dir) / u;
    float bend = (0.1 + 0.07 * beat + 0.03 * level) * pow(saturate(1.0 - t / 0.4), 2.2);
    float across = dot(bp, float2(dir.y, -dir.x)) / u + bend;
    float hairW = 0.09 * pow(saturate(t / 0.32), 0.6) * (1.0 - 0.15 * smoothstep(0.32, 0.44, t));
    float hairD = max(abs(across) - hairW, max(-t - 0.03, t - 0.44)) * u;
    float3 hair = lerp(ink + 0.05, paper * 0.95, smoothstep(0.22, 0.38, t));
    hair *= 0.82 + 0.18 * sin(across * 150.0) + 0.3 * exp(-pow((across + 0.035) / 0.025, 2.0));
    layer(acc, cover, float3(0.6, 0.6, 0.62), smoothstep(1.2, 0.0, abs(hairD + 0.3)) * 0.5);
    layer(acc, cover, hair, smoothstep(0.5, -0.5, hairD));
    float handD = max(abs(across - bend) - 0.062, 0.44 - t) * u;
    float node = smoothstep(0.012, 0.0, abs(frac((t - 0.52) / 0.32) - 0.5) * 0.32 - 0.145);
    float roundness = sqrt(saturate(1.0 - pow((across - bend) / 0.062, 2.0)));
    float3 bamboo = lerp(float3(0.6, 0.46, 0.22), float3(0.94, 0.82, 0.55), roundness) * (1.0 - 0.4 * node);
    bamboo = lerp(bamboo, float3(0.1, 0.07, 0.06) + float3(0.55, 0.42, 0.14) * step(abs(t - 0.47), 0.008), step(t, 0.5));
    layer(acc, cover, bamboo, smoothstep(0.5, -0.5, handD));

    // Ink drops flicked off the tip on the beat, flying out and falling as it fades.
    float fly = 1.0 - beat;
    float2 dp = px - tip + float2(0.0, 0.22 * u * fly * fly);
    float sa = atan2(dp.y, dp.x);
    float sr = (length(dp) / u - 0.12 - 0.5 * fly) / 0.1;
    float2 sc = float2(floor(sa / 0.3), floor(sr));
    float hs = hash2(sc + 5.0);
    float2 sl = float2((frac(sa / 0.3) - 0.5 - 0.4 * (hs - 0.5)) * 0.3 * length(dp), (frac(sr) - 0.5) * 0.1 * u);
    float dr = u * (0.022 + 0.03 * hs) * (0.55 + 0.45 * beat);
    float drop = length(sl) - dr;
    float dropOn = step(hs, 0.6) * step(0.0, sc.y) * step(sc.y, 1.0) * step(-0.7, sa) * step(sa, 2.9) * pow(beat, 0.5);
    layer(acc, cover, float3(0.5, 0.5, 0.52), smoothstep(1.0, 0.0, drop) * dropOn * 0.35);
    layer(acc, cover, ink + 0.1, smoothstep(0.5, -0.5, drop) * dropOn);
    layer(acc, cover, float3(0.85, 0.85, 0.88), smoothstep(0.5, -0.5, length(sl + dr * float2(0.3, -0.35)) - dr * 0.3) * dropOn);

    // The red seal at the start: a worn square stamp with an abstract carved spiral.
    float2 sq = px - float2(0.55 * u, 0.42 * u);
    sq = float2(sq.x + 0.05 * sq.y, sq.y - 0.05 * sq.x) / u;
    float box = max(abs(sq.x), abs(sq.y)) - 0.17 + (vnoise(px * 0.6) - 0.5) * 0.025;
    float carve = abs(max(abs(sq.x), abs(sq.y)) - 0.12) - 0.016;
    float2 s1 = abs(sq - float2(0.0, 0.055)) - float2(0.07, 0.014);
    float2 s2 = abs(sq - float2(0.055, 0.0)) - float2(0.014, 0.07);
    float2 s3 = abs(sq - float2(0.0, -0.055)) - float2(0.035, 0.014);
    carve = min(carve, min(max(s1.x, s1.y), min(max(s2.x, s2.y), max(s3.x, s3.y))));
    float sealA = smoothstep(0.5, -0.5, box * u) * smoothstep(-0.5, 0.5, carve * u) * (0.8 + 0.2 * step(0.3, vnoise(px * 0.9)));
    layer(acc, cover, colorA.rgb * (0.9 + 0.15 * vnoise(px * 0.4)), sealA);

    return paint(acc / max(cover, 0.001), cover);
}