Files
DungeonCrawlerWorld/scripts/run-visual-check.sh
T

148 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$root_dir"
GODOT_BIN="${GODOT_BIN:-/snap/bin/godot4}"
if [[ ! -x "$GODOT_BIN" ]]; then
GODOT_BIN="$(command -v godot4 || command -v godot || true)"
fi
if [[ -z "$GODOT_BIN" ]]; then
echo "visual check unavailable: set GODOT_BIN or install godot4/godot" >&2
exit 2
fi
"$root_dir/scripts/godot-import.sh"
mkdir -p artifacts
log_path="artifacts/visual-check.log"
initial_capture_path="artifacts/visual-check.png"
exit_capture_path="artifacts/visual-check-exit.png"
rm -f "$initial_capture_path" "$exit_capture_path"
: > "$log_path"
runner=()
if [[ -z "${DISPLAY:-}" ]]; then
if command -v xvfb-run >/dev/null 2>&1; then
runner=(xvfb-run --auto-servernum --server-args="-screen 0 1280x720x24")
else
echo "visual check unavailable: DISPLAY is unset and xvfb-run is not installed" | tee "$log_path" >&2
exit 2
fi
fi
run_capture() {
local label="$1"
local capture_argument="$2"
set +e
timeout --foreground 20s "${runner[@]}" "$GODOT_BIN" --path . -- --"$capture_argument" >>"$log_path" 2>&1
local status=$?
set -e
if (( status != 0 )); then
echo "visual check failed: $label capture Godot exited with status $status; see $log_path" >&2
exit "$status"
fi
}
validate_capture() {
local capture_path="$1"
if [[ ! -s "$capture_path" ]]; then
echo "visual check failed: missing capture $capture_path" >&2
exit 1
fi
if ! python3 - "$capture_path" <<'PY'
import struct
import sys
path = sys.argv[1]
with open(path, "rb") as capture:
signature = capture.read(8)
length = struct.unpack(">I", capture.read(4))[0]
kind = capture.read(4)
data = capture.read(length)
if signature != b"\x89PNG\r\n\x1a\n" or kind != b"IHDR" or len(data) != 13:
raise SystemExit("invalid PNG")
width, height = struct.unpack(">II", data[:8])
if (width, height) != (1280, 720):
raise SystemExit(f"expected 1280x720 PNG, got {width}x{height}")
print(f"visual capture: {path} ({width}x{height})")
PY
then
echo "visual check failed: capture is not a nonempty valid PNG" >&2
exit 1
fi
}
run_capture "initial" "capture"
run_capture "exit" "capture-exit"
if grep -Eq 'SCRIPT ERROR|Parse Error|^ERROR:' "$log_path"; then
echo "visual check failed: Godot reported errors; see $log_path" >&2
exit 1
fi
validate_capture "$initial_capture_path"
validate_capture "$exit_capture_path"
measure_capture() {
local capture_path="$1"
python3 - "$capture_path" <<'PY'
from collections import Counter
from pathlib import Path
import sys
from PIL import Image
path = Path(sys.argv[1])
image = Image.open(path).convert("RGB")
if image.size != (1280, 720):
raise SystemExit(f"expected 1280x720 PNG, got {image.size[0]}x{image.size[1]}")
# This is the renderer's unfiltered unknown-fog color in the captured viewport.
unknown = (4, 6, 18)
counts = Counter(image.getdata())
unknown_pixels = counts[unknown]
# Explored cells use a pale blue overlay. With alpha compositing their captured
# RGB components are all at least 196; count those pixels in the dungeon below.
def linear(channel: int) -> float:
value = channel / 255.0
return value / 12.92 if value <= 0.04045 else ((value + 0.055) / 1.055) ** 2.4
def luminance(rgb: tuple[int, int, int]) -> float:
red, green, blue = (linear(channel) for channel in rgb)
return 0.2126 * red + 0.7152 * green + 0.0722 * blue
# Both captures must expose a measurable dungeon region. The initial one also
# proves blue-black unknown fog and a lit explored floor. The expected
# luminance is computed from the renderer tint, before texture modulation.
dungeon = image.crop((256, 40, 1024, 678))
dungeon_pixels = list(dungeon.getdata())
non_black = sum(1 for pixel in dungeon_pixels if pixel != (0, 0, 0))
explored_pixels = sum(
1 for red, green, blue in dungeon_pixels
if red >= 196 and green >= 207 and blue >= 217
)
if non_black == 0:
raise SystemExit("capture dungeon region is entirely black")
if path.name == "visual-check.png":
if unknown_pixels == 0 or unknown[2] <= unknown[0] or luminance(unknown) >= 0.1:
raise SystemExit("unknown fog is not distinguishable blue-black")
else:
if explored_pixels == 0:
raise SystemExit("missing explored floor pixels in exit capture")
explored_tint_luminance = luminance((230, 242, 255))
if explored_tint_luminance < 0.55:
raise SystemExit(f"explored luminance {explored_tint_luminance:.3f} is below 0.55")
print(
f"visual measurement: {path} "
f"dungeon_non_black_pixels={non_black} "
f"unknown_pixels={unknown_pixels} unknown_luminance={luminance(unknown):.3f} "
f"explored_floor_pixels={explored_pixels} explored_tint_luminance="
f"{luminance((230, 242, 255)):.3f}"
)
PY
}
measure_capture "$initial_capture_path"
measure_capture "$exit_capture_path"