翻譯|使用教程|編輯:胡濤|2022-09-14 09:38:28.780|閱讀 445 次
概述:本文主要介紹如何在Aspose.3D中使用 C# 創(chuàng)建 3D 場景,歡迎體驗(yàn)~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.3D是一個(gè)功能豐富的游戲軟件和計(jì)算機(jī)輔助設(shè)計(jì)(CAD)的API,可以在不依賴任何3D建模和渲染軟件的情況下操作文檔。API支持Discreet3DS, WavefrontOBJ, FBX (ASCII, Binary), STL (ASCII, Binary), Universal3D, Collada, glTF, GLB, PLY, DirectX, Google Draco文件格式等等。開發(fā)人員可以輕松地創(chuàng)建,讀取,轉(zhuǎn)換,修改和控制3D文件格式的實(shí)質(zhì)。
簡單的 3D 場景是幾何數(shù)據(jù)的三維圖形表示。在本文中,我們將學(xué)習(xí)如何使用 C# 從頭開始創(chuàng)建 3D 場景,而無需安裝任何 3D 建模或渲染軟件。我們將以FBX文件格式保存 3D 場景,這是一種用于交換 3D 格式的流行 3D 文件格式。FBX 可以作為二進(jìn)制或 ASCII 數(shù)據(jù)文件保存在磁盤上。
為了創(chuàng)建和讀取 3D 場景,我們將使用Aspose.3D for .NET API。它允許以支持的格式創(chuàng)建和保存 3D 場景。它還可以處理幾何和場景層次結(jié)構(gòu)、場景中的對(duì)象動(dòng)畫以及 3D 文件格式的轉(zhuǎn)換。
API 提供Scene類,它是一個(gè)頂級(jí)對(duì)象,包含節(jié)點(diǎn)、幾何圖形、材質(zhì)、紋理、動(dòng)畫、姿勢、子場景等。CreateChildNode(Entity)方法創(chuàng)建一個(gè)新的子節(jié)點(diǎn),并附加給定的實(shí)體. API 為LambertMaterial、PhongMaterial和ShaderMaterial提供著色模型。燈光照亮場景,API 的Light類可以為場景添加一些燈光。
請下載 API 的 DLL或使用NuGet安裝它。
PM> Install-Package Aspose.3D
我們可以按照以下步驟創(chuàng)建一個(gè)簡單的 3D 場景:
以下代碼示例展示了如何使用 C# 創(chuàng)建簡單的 3D 場景。
// This code example demonstrates how to create a simple 3D scene in FBX file.
// Initialize 3D scene
var scene = new Scene();
// Initialize Cylinder object
var cylinder = new Cylinder();
cylinder.Name = "Cylinder";
// Create a child node and add Cylinder object
var node = scene.RootNode.CreateChildNode(cylinder);
// Set child node properites
var material = new LambertMaterial() { DiffuseColor = new Vector3(Color.LightBlue) };
material.Name = "Lambert";
node.Material = material;
// Add Light entity
var light = new Light() { LightType = LightType.Point };
light.Name = "Light";
scene.RootNode.CreateChildNode(light).Transform.Translation = new Vector3(10, 0, 10);
// Output file path
var output = @"C:\Files\3D\document.fbx";
// Save 3D scene document
scene.Save(output, FileFormat.FBX7500ASCII);
我們可以按照以下步驟從 FBX 文檔中讀取 3D 場景:
以下代碼示例展示了如何使用 C# 加載和讀取 3D 場景。
// This code example demonstrates how to read a 3D scene from FBX file
// Initialize a Scene class object
Scene scene = new Scene();
// Load an existing 3D document
scene.Open(@"C:\Files\3D\document.fbx");
foreach(var node in scene.RootNode.ChildNodes)
{
var entity = node.Entity;
Console.WriteLine("{0}", entity.Name);
if (node.Material != null)
{
Material material = node.Material;
Console.WriteLine("{0}", material.Name);
PropertyCollection props = material.Properties;
//List all properties using foreach
foreach (var prop in props)
{
Console.WriteLine("{0} = {1}", prop.Name, prop.Value);
}
//or using ordinal for loop
for (int i = 0; i < props.Count; i++)
{
var prop = props[i];
Console.WriteLine("{0} = {1}", prop.Name, prop.Value);
}
}
}
;
在本文中,我們學(xué)習(xí)了如何:
以上便是如何在Aspose.3D中使用 C# 創(chuàng)建 3D 場景。如有任何疑問,請隨時(shí)與我們聯(lián)系。
歡迎下載|體驗(yàn)更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn