场景管理

场景管理

Scene 类是 Aspose.3D 中针对 .NET 的所有 3D 内容的入口点。它提供了从零创建场景、加载任意受支持格式的现有文件、保存至任意目标格式以及检查场景元数据的方法。


创建和打开场景

使用 new Scene() 创建一个空场景,或从文件加载:

using Aspose.ThreeD;

// Empty scene
var empty = new Scene();

// Load from file — format is detected automatically
var scene = Scene.FromFile("model.fbx");

// Load using Scene.Open with explicit format
var scene2 = new Scene();
scene2.Open("model.obj", FileFormat.WavefrontOBJ);

// Load from a stream (useful when reading from memory or network)
using var stream = System.IO.File.OpenRead("model.stl");
var scene3 = new Scene();
scene3.Open(stream, FileFormat.STLBinary);

Scene.FromFile 会自动从文件扩展名和魔数检测格式。当扩展名模糊或缺失时,请使用 FileFormat 重载。


保存场景

将场景保存为任意受支持输出格式的文件或流:

// Save to file — format determined from extension
scene.Save("output.gltf");

// Save with explicit format
scene.Save("output.bin", FileFormat.GLTF2_Binary);

// Save to a stream
using var output = System.IO.File.Create("output.fbx");
scene.Save(output, FileFormat.FBX7700Binary);

// Save with format-specific options
var opts = new Aspose.ThreeD.Formats.ObjSaveOptions();
opts.PointCloud = false;
scene.Save("output.obj", opts);

清除场景内容

要在保留 Scene 对象的同时删除场景中的所有节点和资源,请执行以下操作:

// Remove all direct children of the root node
scene.RootNode.ChildNodes.Clear();

// Or replace the scene entirely
scene = new Scene();

Scene.Clear() 可用并会清除场景内容,恢复默认设置;或者,按上述方式创建一个新的 Scene 实例。


场景元数据

Scene 上的 AssetInfo 属性公开嵌入在支持的格式(FBX、Collada、glTF)中的元数据:

var info = scene.AssetInfo;
Console.WriteLine("Author:       " + info.Author);
Console.WriteLine("Creation UTC: " + info.CreationTime);
Console.WriteLine("Units:        " + info.UnitName + " @ " + info.UnitScaleFactor);

在保存之前设置元数据,以便在输出文件中保留它:

scene.AssetInfo.ApplicationName = "My Application 1.0";
scene.AssetInfo.UnitName = "meter";
scene.AssetInfo.UnitScaleFactor = 1.0;
scene.Save("output_with_meta.fbx");

API Quick Reference

Member描述
new Scene()创建一个空场景
Scene.FromFile(path)静态帮助方法:打开文件并返回加载的场景
scene.Open(path, format?)将文件加载到现有场景中
scene.Open(stream, format)从流加载
scene.Save(path, format?)保存到文件
scene.Save(stream, format)保存到流
scene.RootNode场景层次结构的根 Node
scene.AssetInfo元数据块(创建者、时间戳、单位)
scene.AssetInfo.ApplicationName嵌入文件中的应用程序名称
scene.AssetInfo.UnitScaleFactor相对于米的场景单位比例

另请参阅

 中文