کار کردن با 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 به بافرها و تکستچرهای خارجی .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

همچنین ببینید:

 فارسی