Làm việc với glTF và GLB
Lý làm việc với glTF và GLB
Aspose.3D cho .NET hỗ trợ đọc và ghi glTF 2.0 (.gltf với các tài nguyên riêng biệt) và GLB (tệp nhị phân đơn glTF). Sử dụng GltfLoadOptions để cấu hình hành vi tải và GltfSaveOptions để điều khiển đầu ra xuất.
Tải một tệp glTF
Tải một tệp .gltf hoặc .glb trực tiếp bằng Scene.Open. Cung cấp GltfLoadOptions để kiểm soát chi tiết hơn:
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);Các tệp glTF tham chiếu các bộ đệm
.binvà texture bên ngoài bằng đường dẫn tương đối. Giữ tất cả các tài nguyên được tham chiếu trong cùng thư mục với tệp.gltfkhi tải.
Lưu dưới dạng glTF hoặc GLB
Lưu bất kỳ cảnh nào dưới dạng glTF hoặc định dạng nhị phân gọn 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);Kiểm soát Định dạng Hình ảnh Nhúng
Khi các texture được nhúng, kiểm soát cách dữ liệu hình ảnh được lưu trữ bằng GltfEmbeddedImageFormat:
var saveOpts = new GltfSaveOptions(FileFormat.GLTF2_Binary);
saveOpts.ImageFormat = GltfEmbeddedImageFormat.Jpeg;
scene.Save("output.glb", saveOpts);GltfEmbeddedImageFormat giá trị | Mô tả |
|---|---|
Jpeg | Mã hóa lại các texture nhúng dưới dạng JPEG (có mất dữ liệu, kích thước nhỏ hơn) |
Png | Mã hóa lại các texture nhúng dưới dạng PNG (không mất dữ liệu) |
NoChange | Giữ mã hoá gốc (mặc định) |
API Quick Reference
| Member | Mô tả |
|---|---|
new GltfLoadOptions() | Cấu hình nhập glTF |
new GltfSaveOptions(format) | Cấu hình xuất glTF/GLB |
saveOpts.EmbedAssets | Nhúng bộ đệm nhị phân vào .gltf JSON |
saveOpts.ImageFormat | Mã hoá texture nhúng (GltfEmbeddedImageFormat) |
FileFormat.GLTF2 | Định dạng mục tiêu: riêng .gltf + .bin |
FileFormat.GLTF2_Binary | Định dạng mục tiêu: đơn .glb |
Xem thêm
- Aspose.3D for .NET — Enterprise Documentation
- Hỗ trợ định dạng — danh sách đầy đủ các định dạng 3D được hỗ trợ