WPF的3D图形渲染能力基于Direct3D封装,通过一套完整的3D场景组件,让开发者可以在WPF应用中轻松实现立方体、球体等基础3D模型的渲染,还能添加材质、光照和交互效果。

WPF 3D渲染核心组件
要实现WPF的3D渲染,首先需要了解几个核心组件,它们共同构成了完整的3D场景:
- Viewport3D:3D场景的容器,所有3D元素都需要放在这个控件中才能显示
- Camera:相机组件,决定观察3D场景的视角和位置,常用的是PerspectiveCamera透视相机
- Model3D:3D模型的基类,常用的子类是GeometryModel3D,用于定义模型的几何形状和材质
- Geometry3D:定义3D模型的几何结构,比如MeshGeometry3D可以定义顶点、三角形索引等
- Material:材质,决定模型表面的外观,比如漫反射材质DiffuseMaterial、镜面材质SpecularMaterial
- Light:光照,3D场景必须添加光照才能看到模型细节,常用DirectionalLight方向光、PointLight点光等
基础3D场景搭建步骤
1. 添加Viewport3D容器
首先在XAML页面中添加Viewport3D作为3D场景的载体,设置合适的宽高:
<Window x:Class="Wpf3DDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF 3D渲染示例" Height="450" Width="800">
<Grid>
<!-- 3D场景容器 -->
<Viewport3D x:Name="viewport" Width="800" Height="450"/>
</Grid>
</Window>
2. 配置相机
相机决定了我们观察3D场景的位置和角度,PerspectiveCamera是最常用的透视相机,符合人眼的观察习惯:
// 创建透视相机,设置位置、观察方向和上方向 PerspectiveCamera camera = new PerspectiveCamera(); camera.Position = new Point3D(0, 0, 5); // 相机位于Z轴5的位置 camera.LookDirection = new Vector3D(0, 0, -1); // 看向Z轴负方向 camera.UpDirection = new Vector3D(0, 1, 0); // 上方向为Y轴正方向 camera.FieldOfView = 60; // 视野角度60度 viewport.Camera = camera;
3. 创建3D模型
以创建一个立方体为例,需要先定义MeshGeometry3D的几何结构,再绑定材质:
// 创建立方体的几何结构 MeshGeometry3D mesh = new MeshGeometry3D(); // 定义立方体的8个顶点 Point3DCollection positions = new Point3DCollection(); positions.Add(new Point3D(-1, -1, -1)); // 0: 左前下 positions.Add(new Point3D(1, -1, -1)); // 1: 右前下 positions.Add(new Point3D(1, 1, -1)); // 2: 右前上 positions.Add(new Point3D(-1, 1, -1)); // 3: 左前上 positions.Add(new Point3D(-1, -1, 1)); // 4: 左后下 positions.Add(new Point3D(1, -1, 1)); // 5: 右后下 positions.Add(new Point3D(1, 1, 1)); // 6: 右后上 positions.Add(new Point3D(-1, 1, 1)); // 7: 左后上 mesh.Positions = positions; // 定义三角形索引,每个面由2个三角形组成,共12个三角形 Int32Collection triangleIndices = new Int32Collection(); // 前面 triangleIndices.Add(0); triangleIndices.Add(1); triangleIndices.Add(2); triangleIndices.Add(0); triangleIndices.Add(2); triangleIndices.Add(3); // 右面 triangleIndices.Add(1); triangleIndices.Add(5); triangleIndices.Add(6); triangleIndices.Add(1); triangleIndices.Add(6); triangleIndices.Add(2); // 后面 triangleIndices.Add(5); triangleIndices.Add(4); triangleIndices.Add(7); triangleIndices.Add(5); triangleIndices.Add(7); triangleIndices.Add(6); // 左面 triangleIndices.Add(4); triangleIndices.Add(0); triangleIndices.Add(3); triangleIndices.Add(4); triangleIndices.Add(3); triangleIndices.Add(7); // 上面 triangleIndices.Add(3); triangleIndices.Add(2); triangleIndices.Add(6); triangleIndices.Add(3); triangleIndices.Add(6); triangleIndices.Add(7); // 下面 triangleIndices.Add(4); triangleIndices.Add(5); triangleIndices.Add(1); triangleIndices.Add(4); triangleIndices.Add(1); triangleIndices.Add(0); mesh.TriangleIndices = triangleIndices; // 创建漫反射材质,设置红色表面 DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Colors.Red)); // 创建GeometryModel3D并绑定几何和材质 GeometryModel3D model = new GeometryModel3D(mesh, material);
4. 添加光照
3D场景没有光照的话模型会显示为全黑,需要添加至少一种光照,这里使用方向光:
// 创建方向光,设置颜色和方向 DirectionalLight light = new DirectionalLight(); light.Color = Colors.White; light.Direction = new Vector3D(-1, -1, -1); // 光从右上前方照射
5. 组装场景
把模型、光照都添加到Viewport3D的Children集合中,完成场景组装:
// 创建模型可视化组件 ModelVisual3D modelVisual = new ModelVisual3D(); modelVisual.Content = model; // 创建光照可视化组件 ModelVisual3D lightVisual = new ModelVisual3D(); lightVisual.Content = light; // 添加到Viewport3D viewport.Children.Add(modelVisual); viewport.Children.Add(lightVisual);
实现3D模型旋转交互
可以给3D模型添加旋转动画,让立方体自动旋转,提升视觉效果:
// 创建旋转变换 RotateTransform3D rotateTransform = new RotateTransform3D(); AxisAngleRotation3D rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0); // 绕Y轴旋转 rotateTransform.Rotation = rotation; // 把旋转变换绑定到模型 model.Transform = rotateTransform; // 创建动画,让旋转角度从0到360循环 DoubleAnimation animation = new DoubleAnimation(0, 360, TimeSpan.FromSeconds(5)); animation.RepeatBehavior = RepeatBehavior.Forever; rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
常见问题说明
- 模型显示为黑色:通常是没有添加光照,或者光照方向不对,没有照射到模型表面
- 模型显示变形:可能是相机的位置或视野角度设置不合理,调整PerspectiveCamera的FieldOfView属性即可
- 性能问题:如果渲染大量复杂3D模型,建议优化几何结构,减少顶点数量,或者使用实例化渲染提升性能
WPF的3D渲染适合实现轻量级的3D展示需求,如果需要高性能的复杂3D场景,建议结合SharpDX等底层库使用。
WPF3D图形渲染Viewport3DGeometryModel3DDirectionalLight修改时间:2026-07-04 17:12:36