ทำงานกับ glTF และ GLB
ทำงานกับ glTF และ GLB
Aspose.3D สำหรับ .NET รองรับการอ่านและเขียน glTF 2.0 (.gltf พร้อมแอสเซ็ตแยก) และ GLB (ไฟล์ไบนารีแบบไฟล์เดียว glTF). ใช้ GltfLoadOptions เพื่อกำหนดพฤติกรรมการโหลดและ GltfSaveOptions เพื่อควบคุมผลลัพธ์การส่งออก.
การโหลดไฟล์ glTF
โหลดไฟล์ .gltf หรือ .glb โดยตรงด้วย Scene.Open. ให้ค่า 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 files อ้างอิงบัฟเฟอร์และเทกเจอร์ภายนอก
.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 import |
new GltfSaveOptions(format) | กำหนดค่า glTF/GLB export |
saveOpts.EmbedAssets | ฝังบัฟเฟอร์ไบนารีภายใน .gltf JSON |
saveOpts.ImageFormat | การเข้ารหัสเทกเจอร์แบบฝัง (GltfEmbeddedImageFormat) |
FileFormat.GLTF2 | รูปแบบเป้าหมาย: แยก .gltf + .bin |
FileFormat.GLTF2_Binary | รูปแบบเป้าหมาย: แบบเดียว .glb |
ดูเพิ่มเติม
- Aspose.3D for .NET — Enterprise Documentation
- การรองรับรูปแบบ — รายการเต็มของรูปแบบ 3D ที่รองรับ