#!/usr/bin/env bash # Web-optimize a GenRecon scene.glb into a browser-loadable walkthrough asset: # weld → simplify (meshoptimizer) → resize + webp textures. NO draco/meshopt geometry # codec, so a plain three.js GLTFLoader loads it (no extra decoder in the viewer). # BEST-EFFORT: every step falls back to the previous file, so this NEVER fails a job; # worst case it copies the raw glb through. Uses a LOCAL gltf-transform install # (a split @gltf-transform/core between separate globals dies with null.getLogger). set -uo pipefail IN="$1"; OUT="$2" RATIO="${GENRECON_SIMPLIFY_RATIO:-0.12}" TEXSZ="${GENRECON_TEX_SIZE:-2048}" export NODE_OPTIONS="--max-old-space-size=${GENRECON_GLTF_HEAP_MB:-49152}" GT="/opt/genrecon/gltftool/node_modules/.bin/gltf-transform" [ -x "$GT" ] || GT="$(command -v gltf-transform || true)" if [ -z "${GT:-}" ] || [ ! -e "$GT" ]; then echo "web-optimize: no gltf-transform, copying raw"; cp -f "$IN" "$OUT"; echo "OPTIMIZE_SKIPPED"; exit 0; fi T=$(mktemp -d); cp -f "$IN" "$T/cur.glb" 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; } try weld weld "$T/cur.glb" "$T/next.glb" try simplify simplify "$T/cur.glb" "$T/next.glb" --ratio "$RATIO" --error 0.002 try resize resize "$T/cur.glb" "$T/next.glb" --width "$TEXSZ" --height "$TEXSZ" try webp webp "$T/cur.glb" "$T/next.glb" --quality 82 cp -f "$T/cur.glb" "$OUT"; rm -rf "$T" echo "OPTIMIZE_DONE $(du -h "$OUT" | cut -f1)"