如何在 .NET 中将 3D 场景渲染为图像

如何在 .NET 中将 3D 场景渲染为图像

如何在 .NET 中将 3D 场景渲染为图像

本文说明了如何使用 Aspose.3D 为 .NET 从 3D 场景生成光栅图像。最小配置需要一个 Camera,至少一个 Light,以及对 Scene.Render 的调用。


步骤 1 — 安装软件包

dotnet add package Aspose.3D.FOSS

步骤 2 — 加载或构建场景

using Aspose.ThreeD;
using Aspose.ThreeD.Entities;

// Load an existing model
var scene = Scene.FromFile("model.fbx");

// Or build a scene in code
var scene = new Scene();
scene.RootNode.CreateChildNode("box", new Box(1, 1, 1));

步骤 3 — 添加相机

using Aspose.ThreeD.Entities;
using Aspose.ThreeD.Utilities;

var camera = new Camera(ProjectionType.Perspective);
camera.FieldOfViewY = 45.0;
camera.NearPlane = 0.1;
camera.FarPlane = 500.0;

Node camNode = scene.RootNode.CreateChildNode("camera", camera);
camNode.Transform.Translation = new Vector3(0, 5, 10);
camNode.Transform.EulerAngles = new Vector3(-30, 0, 0); // tilt down toward origin

步骤 4 — 添加灯光

var sun = new Light("sun") { LightType = LightType.Directional };
Node sunNode = scene.RootNode.CreateChildNode("sun", sun);
sunNode.Transform.EulerAngles = new Vector3(45, -30, 0);

var fill = new Light("fill") { LightType = LightType.Point, Intensity = 100 };
Node fillNode = scene.RootNode.CreateChildNode("fill", fill);
fillNode.Transform.Translation = new Vector3(-4, 6, 4);

步骤 5 — 渲染为图像文件

using Aspose.ThreeD.Utilities;

// size is Vector2; format is a string ("png", "jpg", "bmp")
scene.Render(camera, "output.png", new Vector2(1280, 720), "png");

Console.WriteLine("Rendered: output.png");

提示: JPEG 在截图时产生更小的文件。使用 "jpg" 而不是 "png" 当不需要无损质量时。


Troubleshooting

Symptom可能原因修复
黑色输出图像场景中没有灯光至少添加一个 Light 节点
对象不可见相机指向错误检查 LookAt 的目标坐标
场景渲染为空几何体未附加到节点调用 CreateChildNode(name, entity)

相关资源

另请参阅

 中文