glTF vs GLB — 同一格式,不同打包

同一份数据,不同文件布局
glTF 2.0(Khronos Group,2017)是 schema。.gltf 把 schema 存成 JSON,加上分离的 .bin 几何缓冲和按 URI 引用的图像文件。.glb 把同样的 JSON、缓冲、图像打包到一个二进制容器里。加载器都能解析 — three.js、Babylon.js、model-viewer、Blender、Unity、Unreal 都能处理。屏幕结果完全一样;选择是关于交付,不是关于能力。
GLB 内部如何包裹 glTF
GLB 文件以 12 字节头开始 — 魔数 'glTF'(4 字节)、版本 2(4 字节)、总文件长度(4 字节)。然后是带长度前缀的块。块 0:JSON(与 .gltf 相同的 JSON,按 4 字节对齐补齐)。块 1:BIN(连接的缓冲数据 — 顶点、索引、UV、法线、动画)。可选块 2+:嵌入图像数据。JSON 通过 byteOffset/byteLength 引用缓冲块 1,所以加载器先解析 JSON、再按偏移切片二进制块。无 ZIP、无压缩,只有长度前缀的二进制。
实战差异
- 文件数 — .gltf 是 3+ 文件(JSON、.bin、纹理);.glb 是单文件。单文件意味着单 HTTP 请求和无破损的子资产引用。
- 可编辑性 — .gltf JSON 人类可读、可手编;.glb 需要解压工具才能检查或修改。
- 大小 — .glb 略小(无 JSON 空白、无路径开销)但未压缩;要真正减小,用 Draco 或 Meshopt。
- CDN 交付 — .glb 更友好(一个 Content-Type、一个缓存项);.gltf 需要为子资产取数配 CORS。
- 流式传输 — .glb 通过字节范围请求支持渐进加载;.gltf 可并行流式取子资产。
- 工具 — gltf-pipeline(Cesium)和 gltf-transform(donmccurdy)能在两种格式间转换并对任一应用 Draco/Meshopt/KTX2 压缩。
该选哪个 — 五条决策规则
- Open pcbviewer.app — 生产 Web 交付用 .glb。一个请求、一个 Content-Type、一个 CDN 项、无破损引用。
- 开发和调试用 .gltf。JSON 在任何文本编辑器里都可读;不用工具就能手编材质、换纹理、检查节点层级。
- 多模型共享纹理的资产库用 .gltf。5 个模型共享 1 个 PNG 节省字节和 CDN 缓存空间。
- 邮件或单文件交接用 .glb。无包分离风险;一个附件就是全部。
- 无论容器,最大压缩用 Draco(KHR_draco_mesh_compression)和 KTX2(KHR_texture_basisu) — 两者在 .gltf 或 .glb 里都能用。
在浏览器里把 STEP、STL 或其他格式转成 glTF/GLB — 部署前用 MakerSuite 3D 预览。
免费试用 MakerSuite 3D为什么格式选择影响管线
CI/CD、CDN 配置、资产版本控制都依赖这个。多文件 .gltf 需要为每个子资产域配 CORS、缓存失效复杂、纹理文件改名就有破损链接的风险。单文件 .glb 部署容易但调试难。按团队瓶颈选 — 资产调试是痛点就开发用 .gltf;CDN 可靠性是痛点就一直用 .glb。
各格式获胜场景
- 电商 3D — .glb 用于产品查看器,CDN 集成更简单
- AR/VR 体验 — .glb 用于网络传输,model-viewer 和 WebXR 都偏好
- 游戏资产管线 — 开发期间用 .gltf 加构建期 Draco 压缩
- 建筑可视化 — 客户预览 .glb,分享源资产时用 .gltf
- 教育与博物馆 3D — 嵌入用 .glb,学习者想检查数据时下载用 .gltf
在浏览器里验证转换
.gltf 和 .glb 都可能含专有 3D 模型。MakerSuite 3D 在浏览器里用 three.js GLTFLoader 完整解析两种格式 — 不走服务器、不上传、不缓存。拖入转后的资产,在部署前验证几何、材质、纹理。NDA 工作、发布前产品可视化、IP 敏感 3D 模型都安全。
常见问题
What's the actual difference — same data?
Same data, different packaging. glTF 2.0 (Khronos Group, 2017) defines the scene-graph schema; .gltf files store it as JSON with separate .bin geometry buffers and texture image files referenced by URI. .glb is the same JSON + buffers + images packed into a single binary container with a 12-byte header (magic 'glTF', version, length) followed by chunks. Loaders parse both — three.js, Babylon.js, model-viewer, Blender, Unreal, Unity all handle either. The on-screen result is identical; only the file layout differs.
Which should I use for web?
GLB, almost always. One file means one HTTP request, no CORS quirks for sub-assets, no broken references when a file gets renamed. Servers can apply Content-Type: model/gltf-binary in one place. CDNs cache the bundle as a unit. The exception: if you want to hand-edit JSON for debugging, .gltf is readable in any text editor. For production, ship .glb and keep .gltf in version control as the editable source if you need it.
Does GLB compress better?
No — GLB is uncompressed binary. The size advantage over .gltf comes from removing JSON whitespace and consolidating files, not from compression. For real size reduction, use Draco (geometry compression, Khronos extension KHR_draco_mesh_compression) or Meshopt (KHR_meshopt_compression). These can reduce mesh data 5-10x at minor decompression cost. KTX2 with Basis Universal compresses textures. All three can be applied to either .gltf or .glb. Tools: gltf-pipeline (Cesium, JS) or gltf-transform (donmccurdy, Node).
What's inside the GLB binary?
Three or more chunks after the 12-byte header. Chunk 0: JSON (the same JSON that would be in .gltf, padded to 4-byte alignment). Chunk 1: BIN (concatenated buffer data — vertices, indices, UVs, normals, animations). Chunks 2+: optional, typically embedded image data referenced from JSON. The JSON in chunk 0 references buffer chunk 1 via byteOffset and byteLength, so loaders parse JSON first, then slice the binary chunk by offset. No ZIP, no compression — just length-prefixed chunks.
Can I extract assets from a GLB?
Yes, with any glTF tool. gltf-pipeline can convert GLB to glTF with separated assets: gltf-pipeline -i model.glb -o model.gltf --separate. The output gives you the JSON, .bin buffers, and individual texture files. Useful for debugging materials, swapping textures, or hand-editing the scene graph. For inspection without extraction, the donmccurdy.com glTF Viewer and gltf.report inspect GLB internals directly. MakerSuite 3D opens both formats in the browser viewer.
相关文章
拖入 .gltf 或 .glb 文件 — 任何浏览器里预览
打开 3D 查看器