使用 glTF 和 GLB
使用 glTF 和 GLB
Aspose.3D 用于 .NET 支持读取和写入 glTF 2.0(.gltf,带有独立资源)以及 GLB(单文件二进制 glTF)。使用 GltfLoadOptions 配置加载行为,使用 GltfSaveOptions 控制导出输出。
加载 glTF 文件
使用 Scene.Open 直接加载 .gltf 或 .glb 文件。提供 GltfLoadOptions 以获得更细粒度的控制:
using Aspose.ThreeD;
using Aspose.ThreeD.Formats;
// Basic load
var scene = Scene.FromFile("model.gltf");
// Load with options
var opts = new GltfLoadOptions();
var sceneWithOpts = new Scene();
sceneWithOpts.Open("model.gltf", opts);glTF 文件通过相对路径引用外部的
.bin缓冲区和纹理。加载时请将所有引用的资产保存在与.gltf文件相同的目录中。
保存为 glTF 或 GLB
将任意场景保存为 glTF 或紧凑的二进制 GLB 格式:
using Aspose.ThreeD;
using Aspose.ThreeD.Formats;
var scene = new Scene();
scene.RootNode.CreateChildNode("box", new Aspose.ThreeD.Entities.Box());
// Save as glTF (separate JSON + .bin buffer)
scene.Save("output.gltf", FileFormat.GLTF2);
// Save as GLB (single binary file)
scene.Save("output.glb", FileFormat.GLTF2_Binary);
// Save with custom options — embed the binary buffer inside the JSON
var saveOpts = new GltfSaveOptions(FileFormat.GLTF2);
saveOpts.EmbedAssets = true;
scene.Save("output_embedded.gltf", saveOpts);控制嵌入的图像格式
当纹理被嵌入时,使用 GltfEmbeddedImageFormat 来控制图像数据的存储方式:
var saveOpts = new GltfSaveOptions(FileFormat.GLTF2_Binary);
saveOpts.ImageFormat = GltfEmbeddedImageFormat.Jpeg;
scene.Save("output.glb", saveOpts);GltfEmbeddedImageFormat 值 | 描述 |
|---|---|
Jpeg | 将嵌入的纹理重新编码为 JPEG(有损,体积更小) |
Png | 将嵌入的纹理重新编码为 PNG(无损) |
NoChange | 保持原始编码(默认) |
API Quick Reference
| Member | 描述 |
|---|---|
new GltfLoadOptions() | 配置 glTF 导入 |
new GltfSaveOptions(format) | 配置 glTF/GLB 导出 |
saveOpts.EmbedAssets | 在 .gltf JSON 中嵌入二进制缓冲区 |
saveOpts.ImageFormat | 嵌入式纹理编码 (GltfEmbeddedImageFormat) |
FileFormat.GLTF2 | 目标格式:分离的 .gltf + .bin |
FileFormat.GLTF2_Binary | 目标格式:单个 .glb |
另请参阅
- Aspose.3D for .NET — Enterprise Documentation
- 格式支持 — 支持的 3D 格式完整列表