check_transition.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. # Checks --transition: hotswaps a scene onto another one headlessly on the GPU (headless_render.sh) with a fixed time step, so
  3. # frame N always shows the same moment, and looks at the picture halfway through the transition and after it.
  4. # fade: halfway the picture is half old, half new (mean colour between the two)
  5. # slide-left: halfway the right side shows the new wallpaper and the left side the right half of the old one
  6. # Screenshots never contain a transition (they read the wallpaper's own buffer), so the engine dumps the output at the halfway
  7. # frame (LWE_TRANSITION_DUMP).
  8. # Usage: tools/check_transition.sh <engine binary> [assets dir] [workshop content dir]
  9. # Exit code 0 when they look right (or the wallpapers are missing, then it says SKIP). Needs Pillow.
  10. set -euo pipefail
  11. HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  12. BINARY=${1:?engine binary}
  13. ASSETS=${2:-/workspace/SteamLibrary/steamapps/common/wallpaper_engine/assets}
  14. CONTENT=${3:-/workspace/SteamLibrary/steamapps/workshop/content/431960}
  15. FROM=1120596121 TO=1111948150
  16. STEP=0.05 SWAP_FRAME=30 DURATION=1000
  17. HALF=$((SWAP_FRAME + DURATION / 2 / 50)) END=$((HALF + 10))
  18. for id in $FROM $TO; do [ -d "$CONTENT/$id" ] || { echo "SKIP: wallpaper $id not in $CONTENT"; exit 0; }; done
  19. work=$(mktemp -d /tmp/lwe-transition.XXXXXX)
  20. trap 'rm -rf "$work"' EXIT
  21. export LWE_FIXED_TIMESTEP=$STEP
  22. render() { # <out> <frame> <wallpaper> [engine args...]
  23. local out=$1 frame=$2 id=$3; shift 3
  24. HEADLESS_RENDER_DELAY=$frame "$HERE/headless_render.sh" "$BINARY" "$out" --assets-dir "$ASSETS" "$@" "$CONTENT/$id" \
  25. > "$out.log" 2>&1 || { cat "$out.log"; exit 1; }
  26. }
  27. render "$work/old.png" $((SWAP_FRAME - 1)) $FROM
  28. render "$work/new-half.png" "$HALF" $TO
  29. echo "path=$CONTENT/$TO" > "$work/ctl"
  30. for mode in fade slide-left; do
  31. LWE_TRANSITION_DUMP="$work/$mode-half.ppm" LWE_HOTSWAP_AT_FRAME=$SWAP_FRAME render "$work/$mode.png" "$END" $FROM \
  32. --control-file "$work/ctl" --transition "$mode" --transition-duration $DURATION
  33. done
  34. python3 -I - "$work" <<'PY'
  35. import sys
  36. from PIL import Image, ImageChops, ImageOps, ImageStat
  37. work = sys.argv[1]
  38. load = lambda name: Image.open(f"{work}/{name}").convert("RGB")
  39. mean = lambda im: ImageStat.Stat(im).mean
  40. diff = lambda a, b: sum(ImageStat.Stat(ImageChops.difference(a, b)).mean) / 3
  41. part = lambda im, x0, x1: im.crop((int(im.width * x0), 0, int(im.width * x1), im.height))
  42. bad = 0
  43. def verdict(ok, text):
  44. global bad
  45. print(("PASS " if ok else "FAIL ") + text)
  46. bad += not ok
  47. old, new = load("old.png"), load("new-half.png")
  48. fade, slide = ImageOps.flip(load("fade-half.ppm")), ImageOps.flip(load("slide-left-half.ppm"))
  49. a, b, m = mean(old), mean(new), mean(fade)
  50. span = sum(abs(x - y) for x, y in zip(a, b))
  51. if span < 20:
  52. print("SKIP fade midpoint: the two wallpapers have nearly the same average colour")
  53. else:
  54. share = sum((mc - ac) * (bc - ac) for mc, ac, bc in zip(m, a, b)) / sum((bc - ac) ** 2 for ac, bc in zip(a, b))
  55. verdict(0.3 < share < 0.7, f"fade: halfway the picture is {share:.0%} new")
  56. verdict(diff(part(slide, 0.6, 1.0), part(new, 0.6, 1.0)) < 14, "slide-left: halfway the right side shows the new wallpaper")
  57. best = min(diff(part(slide, 0.0, 0.4), part(old, s, s + 0.4)) for s in [0.5 + i / 100 for i in range(11)])
  58. verdict(best < 14, f"slide-left: halfway the left side is the right part of the old picture (difference {best:.1f})")
  59. sys.exit(1 if bad else 0)
  60. PY