#!/usr/bin/env bash
# Run a GLB through Draco compression before it ships in web-gl-scene/public/.
#
# Usage:
#   ./optimize-glb.sh input.glb output.glb [simplify-error]
#
# simplify-error (optional, default 0.005) is the mesh-simplification error
# tolerance, as a fraction of the mesh's bounding-box extent. 0 disables
# simplification and only quantizes/compresses (safest, smallest win).
# 0.005 is what shrank the jagged-60 star from 2.5MB to 323KB with no visible
# difference in the shatter -- raise it for more aggressive size reduction,
# lower it (or use 0) if a model's silhouette detail actually matters up close.
#
# --flatten/--join/--instance/--palette are all off on purpose: this repo's
# GLBs are shatter rigs with dozens of independently-animated shard nodes
# sharing one material, and those steps would merge nodes/meshes together --
# exactly what the AnimationMixer needs to stay separate. Leave them off
# unless you've checked the export doesn't depend on separate nodes.
set -euo pipefail

if [[ $# -lt 2 ]]; then
  echo "Usage: $0 input.glb output.glb [simplify-error]" >&2
  exit 1
fi

INPUT="$1"
OUTPUT="$2"
SIMPLIFY_ERROR="${3:-0.005}"

npx --yes @gltf-transform/cli@latest optimize "$INPUT" "$OUTPUT" \
  --flatten false \
  --join false \
  --instance false \
  --palette false \
  --simplify true \
  --simplify-error "$SIMPLIFY_ERROR" \
  --compress draco

echo
echo "Before: $(du -h "$INPUT" | cut -f1)"
echo "After:  $(du -h "$OUTPUT" | cut -f1)"
