ทำงานกับ glTF และ GLB

ทำงานกับ 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

ดูเพิ่มเติม

 ภาษาไทย