smasher
Make game assets small.
smasher packs sprites into atlases, turns textures into KTX2, compresses
models with Draco or meshopt, and cuts each model to what a phone, a desktop
or a big screen can draw. It is a Rust library and a CLI. Draco,
meshoptimizer and Basis Universal are compiled in, so there is no Node and no
external tool to install.
Install
cargo install --git https://github.com/joydle/smasher smasher-cli
Needs a C++ compiler for the codecs. Prebuilt binaries are on
Releases.
Use
Results go to ./smashed. Inputs are never touched.
smasher mesh hero.glb # Draco or meshopt, KTX2 textures ≤ 1024 px
smasher mesh hero.glb --levels phone,desktop,high # one file per device + levels.json
smasher atlas sprites/*.png --max-size 2048 # atlas.png + TexturePacker JSON
smasher strip run_*.png --frame 128 # fixed-grid strip
smasher unpack atlas.png atlas.json --frames frames # back to loose frames
smasher compress art/*.png --max-size 1024 --format png --quantize
smasher gutters hero.glb # audit UV gutters; `dilate` fills them
--json prints the machine answer. With no command, smasher reads one JSON
request on stdin and writes one JSON answer, for tools and agents:
echo '{"root":".","op":"pack_atlas","paths":["a.png","b.png"],"out_dir":"out"}' | smasher
From Rust (examples/quickstart.rs):
use smasher_glb::{optimize, Options, TextureMode};
let glb = std::fs::read("hero.glb")?;
let (small, report) = optimize(&glb, Options {
textures: TextureMode::Clamp(1024),
ktx2: true,
..Options::default()
})?;
std::fs::write("hero.small.glb", small)?;
What it does
- Atlases: MaxRects packing with trim, padding and power-of-two; a
TexturePacker JSON-hash manifest Pixi loads as is; named animations; unpack
round-trips. Packing that would double the bytes is refused with the reason.
- Images: cap the longest edge (Lanczos3), re-encode as PNG (a lossless
palette at 256 colours or fewer, quantized on request) or JPEG. A result is
written only if it is smaller.
- Geometry: meshopt for animated models, Draco for static ones, chosen per
file. Quantization, deduplication, mesh joining, constant animation channels
pruned. Draco input passes through.
- Textures: KTX2 with Basis Universal: UASTC for normal,
metallic-roughness and occlusion maps, ETC1S for colour, full mip chain, colour resized in linear light. UV
gutters filled so mips don’t bleed dark seams.
- Levels: one source cut per device class to a triangle budget for what
the model is.
- Fail-open: an error or a panic returns the original bytes. Inputs are
never overwritten.
Levels
How hard to smash. Triangles for the whole model:
| asset class |
phone |
desktop |
high |
| food, pickups |
1,500 |
6,000 |
24,000 |
| props |
2,500 |
10,000 |
40,000 |
| characters (rigged) |
6,000 |
24,000 |
96,000 |
| thin-walled: cups, bowls, rims |
7,000 |
28,000 |
112,000 |
| set pieces, buildings, backdrops |
9,000 |
36,000 |
144,000 |
| textures, longest edge |
1024 px |
2048 px |
4096 px |
| max surface error |
2% |
1% |
0.5% |
The phone column is the top of what our games shipped on an iPhone 11-class
phone. The simplifier stops at the error bound even when the budget is not met.
--class names the class; otherwise skinned means character, anything else a
prop.
Compared
Textures capped at 1024 px everywhere. dB is the worst data map (normal,
roughness, occlusion): PSNR against the source map resized without compression.
| model |
original |
smasher |
gltfpack, UASTC data |
gltfpack default |
gltf-transform |
| DamagedHelmet |
3.77 MB |
2.26 MB 39.6 dB |
2.65 MB 39.7 dB |
0.81 MB 27.1 dB |
0.87 MB 34.6 dB |
| Lantern |
9.56 MB |
1.18 MB 41.2 dB |
1.29 MB 41.2 dB |
0.42 MB 28.4 dB |
2.96 MB 58.4 dB |
| BoomBox |
10.6 MB |
0.97 MB 41.7 dB |
1.11 MB 41.5 dB |
0.26 MB 21.9 dB |
2.14 MB 52.3 dB |
| rigged, 35 clips, Draco |
4.08 MB |
2.65 MB |
can’t read Draco |
can’t read Draco |
2.74 MB |
smasher and the first gltfpack column (-c -tc -tu normal -tu attrib -tl 1024)
both use UASTC for data maps and ETC1S for colour: at the same quality smasher
is 9–15% smaller, in the same range of time. gltfpack’s default
(-c -tc -tl 1024) is ETC1S everywhere: smallest, at 22–28 dB. gltf-transform
(optimize --compress meshopt --texture-size 1024) keeps JPEG, which decodes to
raw RGBA on the GPU; its KTX2 needs KTX-Software. The helmet’s textures take
106.7 MB of GPU memory as shipped and under 7 MB smashed, measured live on
the site. The first three are
Khronos sample models;
the last is in crates/smasher-glb/tests/fixtures.
Crates
| crate |
|
smasher |
images, atlases, strips, the GLB front end, levels, web previews |
smasher-glb |
the in-process GLB optimizer and decoder |
smasher-cli |
the smasher binary |
smasher-glb’s ktx2 and draco features (on by default) compile the
vendored C++. Without them, textures stay PNG/JPEG and static meshes use
meshopt.
Switches, read from the environment: SMASHER_MESH_OPTIMIZE=off,
SMASHER_GLB_KTX2=off, SMASHER_GLB_COMPRESS=draco|meshopt,
SMASHER_MESH_TEXTURE_PX=<px>, SMASHER_GLB_DILATE=off,
SMASHER_KTX2_THREADS=<n> (1 makes KTX2 output byte-reproducible).
Test
cargo test --workspace
cargo test -p smasher-glb --no-default-features
The comparisons against the gltf-transform CLI are #[ignore]d; run them
with cargo test --release -p smasher-glb -- --ignored.
License
MIT or Apache-2.0, at your option. Vendored: Draco and Basis Universal
(Apache-2.0), meshoptimizer (MIT), Zstandard (BSD), each with its license in
crates/smasher-glb/vendor.
评论