การเรนเดอร์ฉาก 3 มิติ
การเรนเดอร์ฉาก 3 มิติ
Aspose.3D สำหรับ .NET สามารถเรนเดอร์ฉากเป็นภาพราสเตอร์ได้. กระบวนการเรนเดอร์ต้องการ Camera (จุดมองเห็น), หนึ่งหรือหลายแหล่ง Light, และ ImageRenderOptions (ความละเอียดและรูปแบบของผลลัพธ์). คลาส Renderer ควบคุมการผ่านการเรนเดอร์.
การตั้งค่ากล้อง
เพิ่มเอนทิตี้ Camera ไปยังโหนดของฉาก. ตั้งตำแหน่งของกล้องผ่าน Transform ของโหนด, และชี้ไปที่เป้าหมายโดยใช้ LookAt:
using Aspose.ThreeD;
using Aspose.ThreeD.Entities;
using Aspose.ThreeD.Utilities;
var scene = new Scene();
// Create the camera entity
var camera = new Camera(ProjectionType.Perspective);
camera.NearPlane = 0.1;
camera.FarPlane = 1000.0;
camera.FieldOfViewY = 45.0;
// Attach to a node and position it
Node cameraNode = scene.RootNode.CreateChildNode("main_camera", camera);
cameraNode.Transform.Translation = new Vector3(0, 5, 10);
// Aim the camera toward the scene origin by rotating around the X axis
cameraNode.Transform.EulerAngles = new Vector3(-30, 0, 0);สำหรับการเรนเดอร์แบบออร์โธกราฟิก, ตั้งค่า ProjectionType.Orthographic และระบุ OrthoHeight.
การเพิ่มแสง
ฉากที่ไม่มีแสงจะเรนเดอร์เป็นความมืดสนิท. เพิ่มอย่างน้อยหนึ่ง Light เพื่อให้แสงส่องสว่างเรขาคณิต:
using Aspose.ThreeD.Entities;
// Directional (sun-like) light
var sunLight = new Light("sun");
sunLight.LightType = LightType.Directional;
sunLight.Color = new Vector3(1.0, 1.0, 0.95); // slightly warm white
Node sunNode = scene.RootNode.CreateChildNode("sun", sunLight);
sunNode.Transform.EulerAngles = new Vector3(45, -30, 0); // direction in degrees
// Point light
var pointLight = new Light("fill");
pointLight.LightType = LightType.Point;
pointLight.Intensity = 150.0;
Node pointNode = scene.RootNode.CreateChildNode("fill_light", pointLight);
pointNode.Transform.Translation = new Vector3(-3, 4, 3);การเรนเดอร์เป็นไฟล์รูปภาพ
ใช้ Scene.Render กับ ImageRenderOptions เพื่อสร้างภาพ PNG, JPEG หรือ BMP:
using Aspose.ThreeD;
using Aspose.ThreeD.Utilities;
// Render to a PNG file at 1280x720 — size is Vector2, format is a string
scene.Render(camera, "output.png", new Vector2(1280, 720), "png");เคล็ดลับ: สำหรับการใช้งานในผลิตภัณฑ์ โปรดตรวจสอบให้มีแสงทิศทางหรือแสงรอบแวดล้อมอย่างน้อยหนึ่งแหล่ง ฉากที่ไม่มีการให้แสงจะเรนเดอร์เป็นเงารูปทรงหรือพื้นสีดำสนิท ขึ้นอยู่กับวัสดุ.
API Quick Reference
| Member | คำอธิบาย |
|---|---|
new Camera(projectionType) | สร้างกล้อง; ใช้ ProjectionType.Perspective หรือ Orthographic |
camera.FieldOfViewY | มุมมองแนวตั้งในหน่วยองศา (เฉพาะมุมมองแบบ perspective) |
camera.NearPlane | ระยะของระนาบตัดใกล้ |
camera.FarPlane | ระยะของระนาบตัดไกล |
new Light(name) | สร้างแหล่งแสง |
light.LightType | Directional, Point, หรือ Spot |
light.Intensity | ความเข้มของแสง (float บวก) |
light.Color | สี RGB เป็น Vector3 |
new ImageRenderOptions() | การกำหนดค่าการเรนเดอร์เอาต์พุต (สีพื้นหลัง, เงา) |
scene.Render(camera, path, size, format) | เรนเดอร์ฉากจากมุมมองกล้อง; size คือ Vector2, format คือ "png", "jpg", เป็นต้น. |
ดูเพิ่มเติม
- Aspose.3D for .NET — Enterprise Documentation
- การทำงานกับวัสดุและการเชดดิ้ง — วัสดุ PBR และ Phong มีผลต่อผลลัพธ์การเรนเดอร์