Quản lý Cảnh

Quản lý Cảnh

Lớp Scene là điểm vào cho tất cả nội dung 3D trong Aspose.3D cho .NET. Nó cung cấp các phương thức để tạo cảnh từ đầu, tải các tệp hiện có ở bất kỳ định dạng nào được hỗ trợ, lưu sang bất kỳ định dạng đích nào, và kiểm tra siêu dữ liệu của cảnh.


Tạo và Mở Cảnh

Tạo một cảnh trống bằng new Scene(), hoặc tải từ một tệp:

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 tự động phát hiện định dạng dựa trên phần mở rộng tệp và các byte ma thuật. Sử dụng overload FileFormat khi phần mở rộng không rõ ràng hoặc thiếu.


Lưu Cảnh

Lưu một cảnh vào tệp hoặc luồng ở bất kỳ định dạng đầu ra nào được hỗ trợ:

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

Xóa nội dung cảnh

Để loại bỏ tất cả các nút và tài sản khỏi một cảnh trong khi vẫn giữ lại đối tượng Scene:

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

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

Scene.Clear() có sẵn và xóa nội dung cảnh, khôi phục cài đặt mặc định; hoặc, tạo một thể hiện Scene mới như đã trình bày ở trên.


Siêu dữ liệu cảnh

Thuộc tính AssetInfo trên một Scene cho phép truy cập siêu dữ liệu được nhúng trong các định dạng được hỗ trợ (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);

Đặt siêu dữ liệu trước khi lưu để bảo tồn nó trong tệp đầu ra:

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

MemberMô tả
new Scene()Tạo một cảnh trống
Scene.FromFile(path)Trợ giúp tĩnh: mở một tệp và trả về cảnh đã tải
scene.Open(path, format?)Tải tệp vào cảnh hiện có
scene.Open(stream, format)Tải từ luồng
scene.Save(path, format?)Lưu vào tệp
scene.Save(stream, format)Lưu vào luồng
scene.RootNodeGốc Node của cây phân cấp cảnh
scene.AssetInfoKhối siêu dữ liệu (người tạo, dấu thời gian, đơn vị)
scene.AssetInfo.ApplicationNameTên ứng dụng được nhúng trong tệp
scene.AssetInfo.UnitScaleFactorTỷ lệ đơn vị cảnh so với mét

Xem thêm

 Tiếng Việt