Kết xuất cảnh 3D

Kết xuất Cảnh 3D

Aspose.3D cho .NET có thể kết xuất một cảnh thành ảnh raster. Quy trình kết xuất yêu cầu một Camera (điểm quan sát), một hoặc nhiều nguồn Light, và ImageRenderOptions (độ phân giải và định dạng đầu ra). Lớp Renderer điều khiển lượt kết xuất.


Cài đặt Camera

Thêm một thực thể Camera vào một node cảnh. Đặt vị trí của camera thông qua Transform của node, và hướng nó tới một mục tiêu bằng cách sử dụng 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);

Đối với việc kết xuất trực giao, thiết lập ProjectionType.Orthographic và chỉ định OrthoHeight.


Thêm Đèn

Cảnh không có đèn sẽ được kết xuất hoàn toàn tối. Thêm ít nhất một Light để chiếu sáng hình học:

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

Kết xuất thành tệp hình ảnh

Sử dụng Scene.Render với ImageRenderOptions để tạo ra ảnh PNG, JPEG hoặc 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");

Mẹo: Đối với môi trường sản xuất, hãy đảm bảo có ít nhất một nguồn sáng định hướng hoặc ánh sáng môi trường. Một cảnh không có ánh sáng sẽ được hiển thị dưới dạng bóng đen hoặc bề mặt màu đen đặc, tùy thuộc vào vật liệu.


API Quick Reference

MemberMô tả
new Camera(projectionType)Tạo một camera; sử dụng ProjectionType.Perspective hoặc Orthographic
camera.FieldOfViewYTrường nhìn dọc tính bằng độ (chỉ áp dụng cho phối cảnh)
camera.NearPlaneKhoảng cách mặt phẳng cắt gần
camera.FarPlaneKhoảng cách mặt phẳng cắt xa
new Light(name)Tạo nguồn sáng
light.LightTypeDirectional, Point hoặc Spot
light.IntensityCường độ ánh sáng (float dương)
light.ColorMàu RGB dưới dạng Vector3
new ImageRenderOptions()Cấu hình đầu ra render (màu nền, bóng đổ)
scene.Render(camera, path, size, format)Render cảnh từ góc nhìn của camera; size là Vector2, format là "png", "jpg", v.v.

Xem thêm

 Tiếng Việt