Aspose.3D FOSS for .NET 支持七种 3D 文件格式:OBJSTLglTFGLBFBXCollada3MF。本文是一篇实用的、按格式逐一的指南,展示如何加载和保存每种格式、可用的选项以及如何在它们之间转换。

所有示例均使用 Aspose.ThreeD 命名空间:

using Aspose.ThreeD;
using Aspose.ThreeD.Formats;

NuGet 包:

dotnet add package Aspose.3D --version 26.1.0

OBJ(Wavefront)

OBJ 是一种被广泛支持的基于文本的网格几何格式。Aspose.3D FOSS 能加载 OBJ 文件及其伴随的 .mtl 材质文件。

加载 OBJ

var scene = new Scene();
scene.Open("model.obj");

Console.WriteLine("Nodes: " + scene.RootNode.ChildNodes.Count);

使用选项加载

var opts = new ObjLoadOptions();
// Configure loading behavior as needed
var scene = new Scene();
scene.Open("model.obj", opts);

保存为 OBJ

var scene = new Scene();
var box = new Entities.Box(2, 2, 2);
scene.RootNode.CreateChildNode("BoxNode", box);

scene.Save("output.obj");

OBJ 保存选项

var opts = new ObjSaveOptions();
scene.Save("output.obj", opts);

STL(立体光刻)

STL 是 3D 打印的标准格式。它存储原始的三角网格几何信息,不包含材料或层次结构。Aspose.3D FOSS 支持二进制和 ASCII STL。

加载 STL

var scene = new Scene();
scene.Open("part.stl");

Console.WriteLine("Nodes: " + scene.RootNode.ChildNodes.Count);

保存为 STL

var scene = new Scene();
var sphere = new Entities.Sphere(1);
scene.RootNode.CreateChildNode("SphereNode", sphere);

scene.Save("output.stl");

带保存选项的 STL

var opts = new StlSaveOptions();
// Control binary mode, scale, and coordinate flipping
scene.Save("output.stl", opts);

STL 往返

// 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 和 GLB

glTF 是用于网络和实时 3D 的现代标准。它支持 PBR 材质、完整的场景层次结构以及高效的二进制打包(GLB)。

加载 glTF

var scene = new Scene();
scene.Open("model.gltf");

使用选项加载

var opts = new GltfLoadOptions();
var scene = new Scene();
scene.Open("model.gltf", opts);

保存为 glTF

var scene = new Scene();
var box = new Entities.Box(2, 2, 2);
scene.RootNode.CreateChildNode("BoxNode", box);

scene.Save("output.gltf");

保存为 GLB(二进制 glTF)

要生成一个自包含的二进制 .glb 文件,请使用 .glb 扩展名保存。库会根据扩展名推断二进制 glTF 格式:

scene.Save("output.glb");

FBX(Filmbox)

FBX 在游戏开发和数字内容创作中被广泛使用。Aspose.3D FOSS for .NET 支持 FBX 导入和导出。导出生成二进制 FBX;源代码中存在 ASCII FBX 导出代码,但在此版本的导出路径中未被调用。FbxSaveOptions 类提供默认设置。

加载 FBX

var scene = new Scene();
scene.Open("model.fbx");

使用选项加载

var opts = new FbxLoadOptions();
var scene = new Scene();
scene.Open("model.fbx", opts);

保存为 FBX

var scene = new Scene();
var box = new Entities.Box(2, 2, 2);
scene.RootNode.CreateChildNode("BoxNode", box);

// FBX export always produces binary FBX output
scene.Save("output.fbx");

使用选项保存

// Use FbxSaveOptions for format-specific defaults
var opts = new FbxSaveOptions();
scene.Save("output.fbx", opts);

3MF(3D 制造格式)

3MF 是由 3MF Consortium 开发的现代 3D 打印格式。它在单个基于 ZIP 的包中支持丰富的元数据、材料和多对象场景。

加载 3MF

var scene = new Scene();
scene.Open("model.3mf");

使用选项加载

var opts = new TmfLoadOptions();
var scene = new Scene();
scene.Open("model.3mf", opts);

保存为 3MF

var scene = new Scene();
var box = new Entities.Box(2, 2, 2);
scene.RootNode.CreateChildNode("BoxNode", box);

scene.Save("output.3mf");

使用选项保存

var opts = new TmfSaveOptions();
scene.Save("output.3mf", opts);

基于流的 I/O

所有格式都支持从流加载和保存,这对于 Web 服务和云处理非常有用:

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);

格式检测

该库可以根据文件扩展名或流内容自动检测格式:

// 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

批量转换

要转换多个文件,请遍历目录并逐个转换:

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);
}

格式比较

特性OBJSTLglTFFBX3MF
材质MTLPBR
场景层次结构
动画存根
二进制模式GLBZIP
适用于3D打印受限
网页交付

摘要

Aspose.3D FOSS for .NET 为您提供统一一致的 API,以处理所有主要的 3D 文件格式。使用模式始终相同:创建 Scene,调用 Open() 加载,可选地配置特定格式的选项,然后调用 Save() 导出。格式检测、基于流的 I/O 和批量转换使其能够轻松集成到任何 .NET 流程中。

欲了解更多信息: