Aspose.3D FOSS per .NET supporta sette formati di file 3D: OBJ, STL, glTF, GLB, FBX, Collada, e 3MF. Questo post è una guida pratica, formato per formato, che mostra come caricare e salvare ciascuno, quali opzioni sono disponibili e come convertirli tra loro.
Tutti gli esempi usano il Aspose.ThreeD namespace:
using Aspose.ThreeD;
using Aspose.ThreeD.Formats;
Pacchetto NuGet:
dotnet add package Aspose.3D --version 26.1.0
OBJ (Wavefront)
OBJ è un formato basato su testo ampiamente supportato per la geometria delle mesh. Aspose.3D FOSS carica i file OBJ insieme ai loro file di .mtl file dei materiali.
Caricamento OBJ
var scene = new Scene();
scene.Open("model.obj");
Console.WriteLine("Nodes: " + scene.RootNode.ChildNodes.Count);
Caricamento con Opzioni
var opts = new ObjLoadOptions();
// Configure loading behavior as needed
var scene = new Scene();
scene.Open("model.obj", opts);
Salvataggio come OBJ
var scene = new Scene();
var box = new Entities.Box(2, 2, 2);
scene.RootNode.CreateChildNode("BoxNode", box);
scene.Save("output.obj");
OBJ con Opzioni di Salvataggio
var opts = new ObjSaveOptions();
scene.Save("output.obj", opts);
STL (Stereolitografia)
STL è il formato standard per la stampa 3D. Memorizza la geometria triangolata grezza senza materiali né gerarchia. Aspose.3D FOSS supporta sia STL binario che ASCII.
Caricamento STL
var scene = new Scene();
scene.Open("part.stl");
Console.WriteLine("Nodes: " + scene.RootNode.ChildNodes.Count);
Salvataggio come STL
var scene = new Scene();
var sphere = new Entities.Sphere(1);
scene.RootNode.CreateChildNode("SphereNode", sphere);
scene.Save("output.stl");
STL con Opzioni di Salvataggio
var opts = new StlSaveOptions();
// Control binary mode, scale, and coordinate flipping
scene.Save("output.stl", opts);
STL Round-Trip
// Load an STL file and re-save it -- geometry is preserved
var scene = new Scene();
scene.Open("original.stl");
scene.Save("copy.stl");
glTF 2.0 e GLB
glTF è lo standard moderno per il web e la grafica 3D in tempo reale. Supporta materiali PBR, gerarchie di scena complete e un confezionamento binario efficiente (GLB).
Caricamento glTF
var scene = new Scene();
scene.Open("model.gltf");
Caricamento con Opzioni
var opts = new GltfLoadOptions();
var scene = new Scene();
scene.Open("model.gltf", opts);
Salvataggio come glTF
var scene = new Scene();
var box = new Entities.Box(2, 2, 2);
scene.RootNode.CreateChildNode("BoxNode", box);
scene.Save("output.gltf");
Salvataggio come GLB (glTF binario)
Per produrre un binario autonomo .glb file, salva con un .glb estensione. La libreria deduce il formato glTF binario dall’estensione:
scene.Save("output.glb");
FBX (Filmbox)
FBX è ampiamente usato nello sviluppo di giochi e nella creazione di contenuti digitali. Aspose.3D FOSS per .NET supporta FBX importazione ed esportazione in entrambi i modi ASCII e binario.
Caricamento FBX
var scene = new Scene();
scene.Open("model.fbx");
Caricamento con Opzioni
var opts = new FbxLoadOptions();
var scene = new Scene();
scene.Open("model.fbx", opts);
Salvataggio come FBX ASCII
var scene = new Scene();
var box = new Entities.Box(2, 2, 2);
scene.RootNode.CreateChildNode("BoxNode", box);
var opts = new FbxSaveOptions() { IsAscii = true };
scene.Save("output.fbx", opts);
Salvataggio come FBX Binary
var opts = new FbxSaveOptions() { IsAscii = false };
scene.Save("output.fbx", opts);
3MF (3D Manufacturing Format)
3MF is a modern 3D printing format developed by the 3MF Consortium. It supports rich metadata, materials, and multi-object scenes in a single ZIP-based package.
Caricamento 3MF
var scene = new Scene();
scene.Open("model.3mf");
Caricamento con Opzioni
var opts = new TmfLoadOptions();
var scene = new Scene();
scene.Open("model.3mf", opts);
Salvataggio come 3MF
var scene = new Scene();
var box = new Entities.Box(2, 2, 2);
scene.RootNode.CreateChildNode("BoxNode", box);
scene.Save("output.3mf");
Salvataggio con Opzioni
var opts = new TmfSaveOptions();
scene.Save("output.3mf", opts);
I/O basato su stream
Tutti i formati supportano il caricamento da e il salvataggio su stream, il che è utile per i servizi web e l’elaborazione cloud:
using var stream = File.OpenRead("model.obj");
var scene = new Scene();
var opts = new ObjLoadOptions();
scene.Open(stream, opts);
// Save to a memory stream
using var output = new MemoryStream();
var saveOpts = new GltfSaveOptions();
scene.Save(output, saveOpts);
Rilevamento del formato
La libreria può rilevare automaticamente i formati dalle estensioni dei file o dal contenuto dello stream:
// Auto-detect from extension
var scene = new Scene();
scene.Open("model.obj"); // Detects OBJ from .obj extension
// Auto-detect from stream content
using var stream = File.OpenRead("model.obj");
scene.Open(stream); // Inspects stream content to determine format
Conversione batch
Per convertire più file, itera su una directory e converti ciascuno:
using Aspose.ThreeD;
using Aspose.ThreeD.Formats;
var inputDir = "assets/";
var outputDir = "converted/";
foreach (var file in Directory.GetFiles(inputDir, "*.fbx"))
{
var scene = new Scene();
scene.Open(file);
var outputPath = Path.Combine(outputDir,
Path.GetFileNameWithoutExtension(file) + ".glb");
scene.Save(outputPath);
Console.WriteLine("Converted: " + file);
}
Confronto dei formati
| Funzionalità | OBJ | STL | glTF | FBX | 3MF |
|---|---|---|---|---|---|
| Materiali | MTL | No | PBR | Sì | Sì |
| Gerarchia della scena | No | No | Sì | Sì | Sì |
| Stub di animazione | No | No | Sì | Sì | No |
| Modalità binaria | No | Sì | GLB | Sì | ZIP |
| 3D printing ready | Limitato | Sì | No | No | Sì |
| Consegna web | No | No | Sì | No | No |
Riepilogo
Aspose.3D FOSS per .NET ti offre un’API unica e coerente per lavorare con tutti i principali formati di file 3D. Il modello è sempre lo stesso: crea un Scene, chiama Open() per caricare, opzionalmente configurare le opzioni specifiche del formato e chiamare Save() per esportare. Il rilevamento del formato, I/O basato su stream e la conversione batch rendono semplice l’integrazione in qualsiasi pipeline .NET.
Per ulteriori informazioni:
- Riferimento API – documentazione dettagliata di classi e metodi.
- Base di conoscenza – articoli pratici su come fare.
- GitHub – codice sorgente e tracker dei problemi.