optimize_glb.sh 1.8 KB

1234567891011121314151617181920212223242526
  1. #!/usr/bin/env bash
  2. # Web-optimize a GenRecon scene.glb into a browser-loadable walkthrough asset:
  3. # weld → simplify (meshoptimizer) → resize + webp textures. NO draco/meshopt geometry
  4. # codec, so a plain three.js GLTFLoader loads it (no extra decoder in the viewer).
  5. # BEST-EFFORT: every step falls back to the previous file, so this NEVER fails a job;
  6. # worst case it copies the raw glb through. Uses a LOCAL gltf-transform install
  7. # (a split @gltf-transform/core between separate globals dies with null.getLogger).
  8. set -uo pipefail
  9. IN="$1"; OUT="$2"
  10. # Aggressive by default: the walkthrough runs on PHONES, where a multi-hundred-k
  11. # triangle mesh + big textures exhausts mobile WebGL memory (blank scene). ~0.04
  12. # vertex ratio ≈ a few hundred k triangles; 1024px textures. Tune up on desktop.
  13. RATIO="${GENRECON_SIMPLIFY_RATIO:-0.04}"
  14. TEXSZ="${GENRECON_TEX_SIZE:-1024}"
  15. export NODE_OPTIONS="--max-old-space-size=${GENRECON_GLTF_HEAP_MB:-49152}"
  16. GT="/opt/genrecon/gltftool/node_modules/.bin/gltf-transform"
  17. [ -x "$GT" ] || GT="$(command -v gltf-transform || true)"
  18. if [ -z "${GT:-}" ] || [ ! -e "$GT" ]; then echo "web-optimize: no gltf-transform, copying raw"; cp -f "$IN" "$OUT"; echo "OPTIMIZE_SKIPPED"; exit 0; fi
  19. T=$(mktemp -d); cp -f "$IN" "$T/cur.glb"
  20. try(){ local label="$1"; shift; if "$GT" "$@" 2>&1 | tail -1; then mv -f "$T/next.glb" "$T/cur.glb" 2>/dev/null && echo " $label ok"; else echo " $label skipped"; fi; }
  21. try weld weld "$T/cur.glb" "$T/next.glb"
  22. try simplify simplify "$T/cur.glb" "$T/next.glb" --ratio "$RATIO" --error 0.002
  23. try resize resize "$T/cur.glb" "$T/next.glb" --width "$TEXSZ" --height "$TEXSZ"
  24. try webp webp "$T/cur.glb" "$T/next.glb" --quality 82
  25. cp -f "$T/cur.glb" "$OUT"; rm -rf "$T"
  26. echo "OPTIMIZE_DONE $(du -h "$OUT" | cut -f1)"