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 .bin và 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 .gltf khi 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ả
JpegMã 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)
PngMã hóa lại các texture nhúng dưới dạng PNG (không mất dữ liệu)
NoChangeGiữ mã hoá gốc (mặc định)

API Quick Reference

MemberMô tả
new GltfLoadOptions()Cấu hình nhập glTF
new GltfSaveOptions(format)Cấu hình xuất glTF/GLB
saveOpts.EmbedAssetsNhúng bộ đệm nhị phân vào .gltf JSON
saveOpts.ImageFormatMã 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

 Tiếng Việt