ใน โพสต์แนะนำ, เราได้ครอบคลุมว่า Aspose.3D FOSS สำหรับ .NET คืออะไรและวิธีเริ่มต้น. โพสต์นี้จะเจาะลึกลงไปในคุณลักษณะสำคัญที่ประกอบเป็นไลบรารี พร้อมตัวอย่างโค้ดสำหรับแต่ละส่วน.
ตัวอย่างทั้งหมดสมมติว่ามีการใช้คำสั่ง using ดังต่อไปนี้:
using Aspose.ThreeD;
using Aspose.ThreeD.Entities;
using Aspose.ThreeD.Shading;
และแพคเกจ NuGet:
dotnet add package Aspose.3D --version 26.1.0
กราฟฉาก
กราฟฉากเป็นโครงสร้างข้อมูลหลัก. A Scene มี RootNode, และทุก Node สามารถมีโหนดลูกและแนบ Entity (เช่น a Mesh หรือ Camera).
การสร้างและการนำทางโหนด
var scene = new Scene();
// Create a hierarchy
var parent = scene.RootNode.CreateChildNode("Parent");
var child1 = parent.CreateChildNode("Child1");
var child2 = parent.CreateChildNode("Child2");
// Navigate the tree
Console.WriteLine("Root children: " + scene.RootNode.ChildNodes.Count);
Console.WriteLine("Parent children: " + parent.ChildNodes.Count);
การแนบ Entity ไปยัง Node
เอนทิตี้เป็นเนื้อหาภาพของฉาก – meshes, cameras, and lights. Attach them using CreateChildNode:
var scene = new Scene();
// Create a box primitive and attach it to the scene
var box = new Box(2, 2, 2);
var boxNode = scene.RootNode.CreateChildNode("BoxNode", box);
// Create a sphere primitive
var sphere = new Sphere(1);
var sphereNode = scene.RootNode.CreateChildNode("SphereNode", sphere);
scene.Save("primitives.gltf");
รูปทรงพื้นฐานในตัว
เวอร์ชัน .NET มีคลาสรูปทรงพารามิเตอร์ที่สร้างเรขาคณิตโดยไม่ต้องสร้างเวอร์เท็กซ์ด้วยตนเอง:
| Primitive | Description |
|---|---|
Box | กล่องที่จัดแนวแกนพร้อมความกว้าง, ความสูง, และความลึกที่กำหนดค่าได้ |
Sphere | ทรงกลมพารามิเตอร์พร้อมรัศมีที่กำหนดค่าได้ |
Cylinder | ทรงกระบอกพารามิเตอร์พร้อมรัศมีบน/ล่างที่กำหนดค่าได้และความสูง |
primitives เหล่านี้สามารถแนบโดยตรงไปยังโหนดหรือแปลงเป็น a Mesh ผ่าน ToMesh():
var cylinder = new Cylinder(1, 1, 2);
var mesh = cylinder.ToMesh();
Console.WriteLine("Vertices: " + mesh.ControlPoints.Count);
Console.WriteLine("Polygons: " + mesh.PolygonCount);
การสร้าง Mesh
หากคุณต้องการการควบคุมเต็มรูปแบบ ให้สร้าง mesh ตั้งแต่ต้นโดยใช้จุดควบคุมและการกำหนดโพลิกอน:
var mesh = new Mesh();
// Add vertex positions
mesh.ControlPoints.Add(new Vector4(0, 0, 0, 1.0f));
mesh.ControlPoints.Add(new Vector4(1, 0, 0, 1.0f));
mesh.ControlPoints.Add(new Vector4(0.5f, 1, 0, 1.0f));
// Define a triangle face
mesh.CreatePolygon(0, 1, 2);
// Attach to a scene
var scene = new Scene();
scene.RootNode.CreateChildNode("triangle", mesh);
scene.Save("triangle.stl");
จุดควบคุมใช้ Vector4 กับ w component ตั้งค่าเป็น 1.0f สำหรับตำแหน่งคาร์ทีเซียนมาตรฐาน รูปหลายเหลี่ยมถูกกำหนดโดยการส่งดัชนีจุดควบคุมไปยัง CreatePolygon().
การแปลง
ทุก Node มี Transform คุณสมบัติที่ควบคุมตำแหน่งในท้องถิ่น การหมุน และสเกลของมัน:
var scene = new Scene();
var node = scene.RootNode.CreateChildNode("Moved");
node.Transform.Translation = new FVector3(5, 0, 0);
node.Transform.Scale = new FVector3(2, 2, 2);
การสืบทอด Transform
Transform จะถูกประกอบกันผ่านลำดับชั้นของฉาก ตำแหน่งในพื้นที่โลกของวัตถุตัวลูกเป็นผลคูณของ Transform ของบรรพบุรุษทั้งหมด:
var scene = new Scene();
var parent = scene.RootNode.CreateChildNode("Parent");
parent.Transform.Translation = new FVector3(10, 0, 0);
var child = parent.CreateChildNode("Child");
child.Transform.Translation = new FVector3(5, 0, 0);
// Child's world position is (15, 0, 0)
// Access via child.GlobalTransform
GlobalTransform
คุณสมบัติแบบอ่านอย่างเดียว GlobalTransform บนแต่ละ Node ให้เมทริกซ์การแปลงในอวกาศโลกที่คำนวณแล้วหลังจากรวมการแปลงของบรรพบุรุษทั้งหมด เข้าถึงผลลัพธ์ผ่าน node.GlobalTransform.Matrix.
วัสดุ
ไลบรารีมีประเภทวัสดุสามประเภทที่ความซับซ้อนเพิ่มขึ้น:
LambertMaterial
วัสดุแบบกระจายแสงคลาสสิก (diffuse‑only):
var material = new LambertMaterial("WoodMaterial");
material.Diffuse = new Vector4(0.6f, 0.4f, 0.2f, 1.0f);
material.Ambient = new Vector4(0.1f, 0.1f, 0.1f, 1.0f);
material.Transparency = 0.0f;
PhongMaterial
ขยายจาก Lambert ด้วยไฮไลท์แบบสเปคูลาร์:
var material = new PhongMaterial("ShinyMetal");
material.Specular = new Vector4(0.8f, 0.8f, 0.8f, 1.0f);
material.Shininess = 50.0f;
material.SpecularPower = 32.0f;
PbrMaterial
วัสดุเรนเดอร์แบบอิงฟิสิกส์ (Physically based rendering) ที่ใช้โดย glTF 2.0:
var material = new PbrMaterial("GoldPBR");
material.BaseColor = new Vector4(1.0f, 0.8f, 0.2f, 1.0f);
material.Metallic = 0.9f;
material.Roughness = 0.1f;
material.Occlusion = 1.0f;
วัสดุ PBR รองรับช่องใส่เทกซ์เจอร์สำหรับสีพื้นฐาน, metallic/roughness, normal, emissive, และแผนที่ occlusion ผ่านคุณสมบัติพาธเทกซ์เจอร์แบบสตริง.
ยูทิลิตี้คณิตศาสตร์
ไลบรารีให้ชุดของ primitive คณิตศาสตร์สำหรับการดำเนินการเชิงพื้นที่ 3 มิติ:
ประเภทเวกเตอร์
| ประเภท | คอมโพเนนต์ | ความแม่นยำ | การใช้งานทั่วไป |
|---|---|---|---|
Vector2 | X, Y | float | พิกัดเทกเจอร์ UV |
Vector3 | X, Y, Z | float | เวกเตอร์ 3 ส่วนทั่วไป |
Vector4 | X, Y, Z, W | float | จุดควบคุม, เวกเตอร์ปกติ |
FVector3 | X, Y, Z | float | คุณสมบัติการแปลง (การย้าย, การสเกล) |
FVector4 | X, Y, Z, W | float | ข้อมูลองค์ประกอบเวอร์เท็กซ์ |
Quaternion
ควอร์เทอร์เนียนแสดงการหมุนโดยไม่มี gimbal lock:
var rotation = new Quaternion(0, 0, 0, 1); // Identity
node.Transform.Rotation = rotation;
Matrix4
4x4 transformation matrices for composing transforms:
// Matrix operations are used internally by Transform
// and GlobalTransform for world-space computation
BoundingBox
กล่องขอบเขตที่จัดแนวตามแกนสำหรับการค้นหาทางพื้นที่:
// BoundingBox stores Minimum and Maximum FVector3 corners
// Used for frustum culling and spatial partitioning
องค์ประกอบเวอร์เท็กซ์
เมชสามารถบรรจุชั้นข้อมูลต่อเวอร์เท็กซ์เพิ่มเติมนอกเหนือจากตำแหน่ง:
- VertexElementNormal – เวกเตอร์ปกติของพื้นผิวสำหรับการคำนวณแสง.
- VertexElementUV – พิกัดเทกเจอร์สำหรับการแมปภาพลงบนเรขาคณิต.
- VertexElementVertexColor – ข้อมูลสี RGBA ต่อเวอร์เท็กซ์.
แต่ละองค์ประกอบเวอร์เท็กซ์มี MappingMode (ต่อจุดควบคุม, ต่อเวอร์เท็กซ์ของโพลิกอน, หรือต่อโพลิกอน) และ ReferenceMode (ค่าตรงหรือค่าที่อ้างอิงจากดัชนี).
คลิปแอนิเมชัน
คลาส Scene คลาสสนับสนุนคลิปแอนิเมชันที่มีชื่อผ่าน CreateAnimationClip() และ GetAnimationClip(). ในรุ่น FOSS ปัจจุบัน การสร้างและการค้นหาคลิปแอนิเมชันทำงานได้, แต่ข้อมูลคีย์เฟรมและการเล่นยังไม่ได้พัฒนา.
var scene = new Scene();
var clip = scene.CreateAnimationClip("Walk");
// Retrieve by name
var found = scene.GetAnimationClip("Walk");
Console.WriteLine("Clip found: " + (found != null));
ต่อไป
โพสต์ถัดไปครอบคลุม การทำงานกับรูปแบบไฟล์ 3D ใน .NET – คู่มือเชิงปฏิบัติที่อธิบายรูปแบบต่อรูปแบบสำหรับ OBJ, STL, glTF, FBX, และ 3MF พร้อมตัวเลือกการโหลด/บันทึกและรูปแบบการแปลงเป็นชุด.