#!/bin/bash # optimise-models.sh — shrink the generated GLBs for the web. # # hunyuan3d hands back ~120k triangles and 1024px textures per prop, which is # ~4MB of download for a cooking pot. Simplified to 10% of the triangles with a # 0.002 error bound and 512px textures, the same pot is ~800KB and looks # identical at the distances this game shows it from. The fryer bench alone # went from a 13MB download to under 3MB. # # Idempotence guard: anything already under the threshold is skipped, so a # second run cannot simplify an already-simplified mesh into mush. Delete the # file and re-run gen-assets.sh to start from the original. set -uo pipefail cd "$(dirname "$0")/.." MODELS=public/assets/models SKIP_UNDER=${SKIP_UNDER:-1200000} # bytes; optimised props land well under this RATIO=${RATIO:-0.1} ERROR=${ERROR:-0.002} TEX=${TEX:-512} GT="npx --yes @gltf-transform/cli@4.4.2" TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT before=0; after=0; done_n=0; skip_n=0 for f in "$MODELS"/*.glb; do [ -e "$f" ] || continue name=$(basename "$f") sz=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f") before=$((before + sz)) if [ "$sz" -lt "$SKIP_UNDER" ]; then echo " = $name already lean ($((sz/1024))KB)" after=$((after + sz)); skip_n=$((skip_n + 1)); continue fi if $GT simplify "$f" "$TMP/a.glb" --ratio "$RATIO" --error "$ERROR" >/dev/null 2>&1 \ && $GT resize "$TMP/a.glb" "$TMP/b.glb" --width "$TEX" --height "$TEX" >/dev/null 2>&1; then mv "$TMP/b.glb" "$f" nsz=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f") after=$((after + nsz)); done_n=$((done_n + 1)) echo " + $name $((sz/1024))KB → $((nsz/1024))KB" else echo " ! $name FAILED — left untouched" after=$((after + sz)) fi done echo "----" echo "optimised $done_n, skipped $skip_n" echo "total $((before/1048576))MB → $((after/1048576))MB"