Draw a wallpaper's subject outline first when changing to it

helpers/wallfx.py finds the subject with BiRefNet (onnxruntime, CPU),
bakes outline maps per wallpaper and caches them; scans warm the cache in
the background. The transition shader gains an outline mode: the line is
drawn, the subject fills in, then the rest grows out of it. Uncached or
subject-less wallpapers keep the colonies. wallpaper.outlineExclude opts
wallpapers out. The Nix package provides the interpreter and the model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mxandClaude Opus 5.5 committed 2026-11-08 22:37:33 +01:00
1 parent ad740ef631
commit b6d0da4679
10 files changed
+491 -28

No files matched your search

@@ -0,0 +1,191 @@
#version 440
// Wallpaper change, two kinds, both in crisp pixel cells:
//
// COLONIES (mode 0), any wallpaper: colonies of the new wallpaper land at a
// few places and moments and eat through the old one along noise-warped
// fronts. Each flipped cell burns for an instant in the accent colour, a thin
// glow rides the front, and now and then a spark jumps ahead. Procedural:
// `seed` makes every change differ.
//
// OUTLINE (mode 1), a wallpaper with a clear subject: the old wallpaper steps
// back, a thin line draws the subject's outline (two pens from its top), the
// subject fills in from that outline, and the rest of the picture grows out
// of it. The maps come from helpers/wallfx.py, in the new image's space.
//
// Extra effects vanish at both ends, so t=0 is exactly the old wallpaper and
// t=1 exactly the new one.
// Rebuild transition.frag.qsb with scripts/build-shaders after editing.
layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor;
layout(std140, binding = 0) uniform buf {
mat4 qt_Matrix;
float qt_Opacity;
float t; // 0..1, linear in time
float mode; // 0 colonies, 1 outline
float sw; // item size, px
float sh;
float cell; // cell size, px
float glow; // 0..1, strength of the burn and the front glow
float seed;
float fw; // fieldTex / lineTex size, texels (outline only)
float fh;
float lineW; // outline width, px
float sdRange; // fieldTex G spans +-sdRange map px
vec4 cropOld; // texture uv = xy + item uv * zw (PreserveAspectCrop)
vec4 cropNew;
vec4 ember; // accent: the colonies' burning edge
vec4 ink; // light tone: the outline's line
};
layout(binding = 1) uniform sampler2D oldTex;
layout(binding = 2) uniform sampler2D newTex;
layout(binding = 3) uniform sampler2D fieldTex;
layout(binding = 4) uniform sampler2D lineTex;
const float PI = 3.14159265;
const int COLS = 3; // seeds are stratified on a COLS x ROWS grid, so
const int ROWS = 2; // no part of the screen is far from every colony
const float START = 0.22; // latest moment a colony may land
const float WARP = 0.22; // how far the noise bends the fronts, screen heights
const float GRAIN = 0.035; // per-cell jitter: ragged, crisp edges
const float LIFE = 0.06; // how long a flipped cell burns
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7)) + seed * 13.17) * 43758.5453); }
float hash1(float n) { return fract(sin(n * 91.345 + seed * 7.91) * 47453.5453); }
float escena(float x) { x = clamp(x, 0.0, 1.0); return x * x * (3.0 - 2.0 * x); }
float vnoise(vec2 p) {
vec2 i = floor(p), f = p - i;
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x),
mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
return 0.52 * vnoise(p) + 0.30 * vnoise(p * 2.7 + 17.0) + 0.18 * vnoise(p * 7.3 + 41.0);
}
// Latest arrival, measured over many seeds and aspect ratios (16:9, 21:9, 1:1,
// 9:16): the worst case stays under half the latest landing plus one zone
// diagonal. Any straggler past it is closed at t=1.
float aspect() { return sw / sh; }
float maxArrival() {
vec2 zone = vec2(aspect() / float(COLS), 1.0 / float(ROWS));
return START * 0.5 + length(zone) + 0.04;
}
// Arrival (as a fraction of maxArrival) at which a given fraction of the
// screen is covered, from the same measurements: mapping time through it
// makes the covered AREA grow evenly instead of crawling, then rushing.
float areaToArrival(float f) {
if (f < 0.10) return mix(0.00, 0.22, f / 0.10);
if (f < 0.25) return mix(0.22, 0.29, (f - 0.10) / 0.15);
if (f < 0.50) return mix(0.29, 0.37, (f - 0.25) / 0.25);
if (f < 0.75) return mix(0.37, 0.46, (f - 0.50) / 0.25);
if (f < 0.90) return mix(0.46, 0.54, (f - 0.75) / 0.15);
return mix(0.54, 1.00, (f - 0.90) / 0.10);
}
// When the cell at `c` (cell coordinates) flips, in the same units as maxArrival().
float arrivalAt(vec2 c) {
vec2 q = (c + 0.5) * cell / sh; // screen-height units
vec2 w = vec2(fbm(q * 3.1 + seed), fbm(q * 3.1 + seed + 23.7)) - 0.5;
q += w * WARP;
float a = 1e6;
for (int y = 0; y < ROWS; y++) {
for (int x = 0; x < COLS; x++) {
float n = float(y * COLS + x);
vec2 zone = vec2(aspect() / float(COLS), 1.0 / float(ROWS));
vec2 s = (vec2(float(x), float(y)) + 0.2 + 0.6 * vec2(hash1(n), hash1(n + 31.0))) * zone;
float t0 = START * hash1(n + 57.0);
float speed = 0.85 + 0.3 * hash1(n + 83.0); // some colonies are hungrier
a = min(a, t0 + length(q - s) / speed);
}
}
return a + GRAIN * hash(c + 0.37);
}
vec3 oldAt(vec2 uv) { return texture(oldTex, cropOld.xy + uv * cropOld.zw).rgb; }
vec2 newUv(vec2 uv, float zoom) { return cropNew.xy + (0.5 + (uv - 0.5) / zoom) * cropNew.zw; }
vec3 newAt(vec2 uv, float zoom) { return texture(newTex, newUv(uv, zoom)).rgb; }
float luma(vec3 c) { return dot(c, vec3(0.299, 0.587, 0.114)); }
vec3 colonies(vec2 uv) {
float energy = sin(PI * t);
float span = maxArrival();
float T = areaToArrival(escena(t)) * (span + LIFE + 0.01) - 0.01;
vec2 cellPos = floor(uv * vec2(sw, sh) / cell);
float a = arrivalAt(cellPos);
float flick = hash(cellPos + 0.71);
vec3 hot = mix(ember.rgb, vec3(1.0), 0.18);
vec3 c;
if (a > T) {
c = oldAt(uv);
float ahead = a - T; // a spark just ahead of the front
if (flick > 0.985 && ahead < 0.03)
c = mix(c, hot, (1.0 - ahead / 0.03) * 0.8 * glow * step(0.0, T));
} else {
c = newAt(uv, 1.0 + 0.025 * (1.0 - escena(t))); // settles from a touch closer
bool edge = max(max(arrivalAt(cellPos + vec2(1.0, 0.0)), arrivalAt(cellPos - vec2(1.0, 0.0))),
max(arrivalAt(cellPos + vec2(0.0, 1.0)), arrivalAt(cellPos - vec2(0.0, 1.0)))) > T;
float life = clamp((T - a) / LIFE, 0.0, 1.0);
float burn = (1.0 - life) * (1.0 - life) * (edge ? 1.0 : 0.55);
c = mix(c, hot, burn * (0.55 + 0.45 * flick) * 0.85 * glow);
}
return c + ember.rgb * 0.22 * glow * energy * exp(-abs(a - T) / 0.012);
}
// 16-bit order in lineTex G (high) and B (low), interpolated by hand: the
// sampler's bilinear filter would blend the two bytes separately.
float order16(vec2 texel) {
texel = clamp(texel, vec2(0.0), vec2(fw, fh) - 1.0);
vec4 f = texture(lineTex, (texel + 0.5) / vec2(fw, fh));
return (floor(f.g * 255.0 + 0.5) * 256.0 + floor(f.b * 255.0 + 0.5)) / 65535.0;
}
float orderAt(vec2 tuv) {
vec2 p = tuv * vec2(fw, fh) - 0.5;
vec2 i = floor(p), f = p - i;
return mix(mix(order16(i), order16(i + vec2(1.0, 0.0)), f.x),
mix(order16(i + vec2(0.0, 1.0)), order16(i + vec2(1.0, 1.0)), f.x), f.y);
}
vec3 outline(vec2 uv) {
// The subject settles from a touch closer than its background: a little depth.
float zIn = 1.0 + 0.03 * (1.0 - escena(t));
float zOut = 1.0 + 0.015 * (1.0 - escena(t));
vec4 f = texture(fieldTex, newUv(uv, zIn));
float sdPx = (f.g * 2.0 - 1.0) * sdRange * sw * zIn / (fw * cropNew.z); // screen px, > 0 outside
float aa = max(fwidth(sdPx), 1e-3);
float inside = clamp(0.5 - sdPx / aa, 0.0, 1.0);
// The old wallpaper steps back: a fifth darker, a quarter greyer.
float back = escena(t / 0.18) * (1.0 - escena((t - 0.85) / 0.15));
vec3 st = oldAt(uv);
st = mix(st, vec3(luma(st)), 0.25 * back) * (1.0 - 0.2 * back);
// Fronts decide per cell centre, so they are crisp and stepped; the
// silhouette itself stays smooth.
vec2 grid = vec2(sw, sh) / cell;
vec2 cuv = (floor(uv * grid) + 0.5) / grid;
float Ti = escena((t - 0.32) / 0.32) * 1.02 - 0.01; // fill, once the line is nearly drawn
vec3 cIn = texture(fieldTex, newUv(cuv, zIn)).r <= Ti ? newAt(uv, zIn) : st;
float To = escena((t - 0.45) / 0.50) * 1.02 - 0.01; // then the world grows out of it
vec3 cOut = orderAt(newUv(cuv, zOut)) <= To ? newAt(uv, zOut) : st;
vec3 c = mix(cOut, cIn, inside);
// The line: thin and quiet, drawn by two pens from the top of every
// contour, gone once the subject is in.
float drawn = escena((t - 0.02) / 0.33);
float lp = texture(lineTex, newUv(uv, zIn)).r;
float fade = 1.0 - escena((t - 0.45) / 0.25);
if (lp > 0.0 && lp <= drawn && fade > 0.0) {
float core = clamp(0.5 - (abs(sdPx) - lineW * 0.5) / aa, 0.0, 1.0);
c = mix(c, ink.rgb, core * 0.75 * fade);
}
return c;
}
void main() {
vec2 uv = qt_TexCoord0;
vec3 c = t >= 1.0 ? newAt(uv, 1.0) : (mode < 0.5 ? colonies(uv) : outline(uv));
fragColor = vec4(clamp(c, 0.0, 1.0), 1.0) * qt_Opacity;
}