glb-compressor-library
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseglb-compressor Library
glb-compressor Library
Programmatic API for multi-phase GLB/glTF compression. Skinned-model-aware
pipeline with gltfpack-first strategy and meshopt WASM fallback.
用于多阶段GLB/glTF压缩的可编程API。采用gltfpack优先策略并以meshopt WASM作为备选方案的蒙皮模型识别处理流水线。
Quick Start
Quick Start
ts
import { compress, init, PRESETS } from 'glb-compressor';
await init(); // Pre-warm WASM (optional, called automatically)
const glbBytes = await Bun.file('model.glb').bytes();
const result = await compress(glbBytes, { preset: 'aggressive' });
await Bun.write('out.glb', result.buffer);ts
import { compress, init, PRESETS } from 'glb-compressor';
await init(); // Pre-warm WASM (optional, called automatically)
const glbBytes = await Bun.file('model.glb').bytes();
const result = await compress(glbBytes, { preset: 'aggressive' });
await Bun.write('out.glb', result.buffer);Package Exports
Package Exports
ts
import { ... } from 'glb-compressor'; // Library API
import { ... } from 'glb-compressor/server'; // Server (guarded by import.meta.main)
import { ... } from 'glb-compressor/cli'; // CLI entryConditional exports: field for Bun runtime, for Node.js.
"bun""node"ts
import { ... } from 'glb-compressor'; // Library API
import { ... } from 'glb-compressor/server'; // Server (guarded by import.meta.main)
import { ... } from 'glb-compressor/cli'; // CLI entry条件导出:针对Bun运行时的字段,针对Node.js的字段。
"bun""node"Core API
Core API
init(): Promise<void>
init(): Promise<void>init(): Promise<void>
init(): Promise<void>Pre-warm Draco + Meshopt WASM and configure glTF-Transform I/O. Called
automatically by , but can be called at boot to eliminate cold-start
latency. Safe to call multiple times (shared promise).
compress()预加载Draco + Meshopt WASM并配置glTF-Transform输入输出。会被自动调用,但也可在启动时调用以消除冷启动延迟。支持多次调用(共享同一个Promise)。
compress()compress(input, options?): Promise<CompressResult>
compress(input, options?): Promise<CompressResult>compress(input, options?): Promise<CompressResult>
compress(input, options?): Promise<CompressResult>Main entry point. Runs the full 6-phase pipeline.
Parameters:
| Param | Type | Description |
|---|---|---|
| | Raw GLB file bytes |
| | |
| | Additional simplification in |
| | Progress callback (used by SSE) |
| | Suppress console output |
Returns:
CompressResultts
interface CompressResult {
buffer: Uint8Array; // Compressed GLB binary
method: string; // 'gltfpack' | 'meshopt'
originalSize?: number; // Input byte count
}主入口方法。运行完整的6阶段处理流水线。
Parameters:
| Param | Type | Description |
|---|---|---|
| | 原始GLB文件字节数据 |
| | |
| | 额外的简化比例,取值范围 |
| | 进度回调函数(用于SSE) |
| | 抑制控制台输出 |
Returns:
CompressResultts
interface CompressResult {
buffer: Uint8Array; // Compressed GLB binary
method: string; // 'gltfpack' | 'meshopt'
originalSize?: number; // Input byte count
}getHasGltfpack(): boolean
getHasGltfpack(): booleangetHasGltfpack(): boolean
getHasGltfpack(): booleanWhether the binary was found during initialization.
gltfpack初始化过程中是否找到二进制文件。
gltfpackIn This Reference
In This Reference
| File | Purpose |
|---|---|
| api.md | Full API surface: types, constants, utilities |
| transforms.md | Custom glTF-Transform transforms for a-la-carte use |
| File | Purpose |
|---|---|
| api.md | 完整API覆盖:类型、常量、工具函数 |
| transforms.md | 可单独使用的自定义glTF-Transform转换操作 |
Reading Order
Reading Order
| Task | Files to Read |
|---|---|
| Basic compression | This file only |
| Custom pipeline / advanced | This file + transforms.md |
| Full type reference | This file + api.md |
| Task | Files to Read |
|---|---|
| 基础压缩操作 | 仅需阅读本文档 |
| 自定义流水线 / 高级用法 | 本文档 + transforms.md |
| 完整类型参考 | 本文档 + api.md |
Pipeline Phases
Pipeline Phases
- Cleanup - dedup, prune, remove unused UVs (+ flatten/join/weld for static)
- Geometry - merge by distance, remove degenerate faces, auto-decimate
- GPU - instancing, vertex reorder, sparse encoding
- Animation - resample keyframes, remove static tracks, normalize weights
- Textures - compress to WebP via sharp (max 1024x1024)
- Final - gltfpack (preferred) or meshopt WASM (fallback)
- 清理阶段 - 去重、裁剪、移除未使用的UV(静态模型额外执行合并/连接/焊接操作)
- 几何处理阶段 - 按距离合并网格、移除退化面、自动简化
- GPU优化阶段 - 实例化、顶点重排序、稀疏编码
- 动画处理阶段 - 重采样关键帧、移除静态轨道、归一化权重
- 纹理处理阶段 - 通过sharp压缩为WebP格式(最大分辨率1024x1024)
- 最终压缩阶段 - 使用gltfpack(优先选择)或meshopt WASM(备选方案)
Skinned Model Awareness
Skinned Model Awareness
When skins are detected, the pipeline automatically skips destructive
transforms: flatten, join, weld, mergeByDistance, reorder, quantize, and
auto-decimate. This prevents broken skeletons, weight denormalization, and mesh
clipping.
当检测到蒙皮模型时,处理流水线会自动跳过破坏性转换操作:合并、连接、焊接、按距离合并、重排序、量化和自动简化。这可防止骨骼损坏、权重归一化失效以及网格裁剪问题。
Presets
Presets
ts
type CompressPreset = 'default' | 'balanced' | 'aggressive' | 'max';| Preset | Skinned behavior | Static behavior |
|---|---|---|
| | |
| + animation quant ( | Same + |
| + stronger quant ( | Same + |
| + supercompression ( | Same |
ts
type CompressPreset = 'default' | 'balanced' | 'aggressive' | 'max';| Preset | 蒙皮模型处理行为 | 静态模型处理行为 |
|---|---|---|
| | |
| + 动画量化( | 同默认 + |
| + 更强的量化( | 同默认 + |
| + 超级压缩( | 同默认 |
Anti-Patterns
Anti-Patterns
- Don't flatten/join/weld/quantize skinned models.
- Don't import from or
cli/- lib is the dependency root.server/ - Don't bypass barrel for public API additions.
mod.ts
- 不要对蒙皮模型执行合并/连接/焊接/量化操作。
- 不要从或
cli/导入模块 - 库的依赖根目录为lib。server/ - 不要绕过入口文件来添加公共API。
mod.ts