Wpf 3D入门指南(Windows Presentation Foundation (WPF) 3D Tutorial)(三) .
Add the code
添加代码
Before we start digging into the nuts and bolts, make sure you import the using System.Windows.Media.Media3D namespace into the code-behind. That namespace has all of the classes we'll be working with:
首先要确认导入命名空间system.windows.media.media3.这个命名空间有所有我们需要的类:
using System.Windows.Media.Media3D;Now let's create a simple mesh and add it to the Viewport3D. We'll start with the simplest mesh possible: a triangle. It'll be located near the origin of the model (remember where the PerspectiveCamera is pointing?) and will have one side along the X-axis (5 units long), one along the Z-axis (5 units long), and a hypotenuse connecting the first two sides.
First, create a new MeshGeometry3D:
现在让我们一起创建一个简单的网格,并把它添加到Viewport3D中. 我们将先创建一个简单可行的网格:三角形. 它会被定位在模型附近(记下PerspectiveCamera所指方向?),并将一边沿着X轴(5单位长方向),一边沿Z轴(5单位长)方向,第三边连接前面两边.
首先,创造一个新的MeshGeometry3D:
MeshGeometry3D triangleMesh = new MeshGeometry3D();Next, define the three points of the triangle:
之后,确定三角形的3点:
Point3D point0 = new Point3D(0, 0, 0);Point3D point1 = new Point3D(5, 0, 0);Point3D point2 = new Point3D(0, 0, 5);Next, add the three points as positions in the mesh:
接下来,放入三点作为网格的位置:
triangleMesh.Positions.Add(point0);triangleMesh.Positions.Add(point1);triangleMesh.Positions.Add(point2);Now we'll add the triangle indeces to define the triangle of the mesh. Since our entire mesh is a triangle, this might seem redundant, but WPF doesn't know how the points connect. Remember the right-hand-rule. We want the top surface of the triangle to be visible, so add the indeces in the appropriate order:
现在我们添加一个三角形索引定义的网格的三角形. 因为我们的整个网是一个三角形,这似乎是多余的,但wpf不知道该如何点连接. 记住右手定则. 我们要使顶面三角形是可见的,所以要用适当的语句添加索引:
triangleMesh.TriangleIndices.Add(0);triangleMesh.TriangleIndices.Add(2);triangleMesh.TriangleIndices.Add(1);Next we'll add the normal vectors for the mesh positions. Since this one's easy (the normal will point straight up in the Y direction), we'll just create a normal vector with known dimensions rather than computing a cross product:
接下来我们要为网格位置添加一个法线矢量. 因为这样一来容易(法线会直接指向Y方向),我们就要创建一个长度未知的法线,而不是计算一个法线交叉的结果:
Vector3D normal = new Vector3D(0, 1, 0);triangleMesh.Normals.Add(normal);triangleMesh.Normals.Add(normal);triangleMesh.Normals.Add(normal);Next we'll need to create a DiffuseMaterial for the surface, add the mesh to a model, and add the model to the Viewport3D:
下面我们需要为表面创建一个diffusematerial,为模型添加网格,并且向viewport3d添加模型:
Material material = new DiffuseMaterial( new SolidColorBrush(Colors.DarkKhaki));GeometryModel3D triangleModel = new GeometryModel3D( triangleMesh, material);ModelVisual3D model = new ModelVisual3D();model.Content = triangleModel;this.mainViewport.Children.Add(model);When all is said and done, the simpleButtonClick event handler will look like this:
simplebuttonclick事件处理如下:
private void simpleButtonClick(object sender, RoutedEventArgs e){ MeshGeometry3D triangleMesh = new MeshGeometry3D(); Point3D point0 = new Point3D(0, 0, 0); Point3D point1 = new Point3D(5, 0, 0); Point3D point2 = new Point3D(0, 0, 5); triangleMesh.Positions.Add(point0); triangleMesh.Positions.Add(point1); triangleMesh.Positions.Add(point2); triangleMesh.TriangleIndices.Add(0); triangleMesh.TriangleIndices.Add(2); triangleMesh.TriangleIndices.Add(1); Vector3D normal = new Vector3D(0, 1, 0); triangleMesh.Normals.Add(normal); triangleMesh.Normals.Add(normal); triangleMesh.Normals.Add(normal); Material material = new DiffuseMaterial( new SolidColorBrush(Colors.DarkKhaki)); GeometryModel3D triangleModel = new GeometryModel3D( triangleMesh, material); ModelVisual3D model = new ModelVisual3D(); model.Content = triangleModel; this.mainViewport.Children.Add(model);}That's it! The code produces the following result:
好的! 这段代码运行The code produces the following result:这段这段的结果如下:
Hardly exciting, I know, but just think - now you understand the building blocks of 3D meshes! Still not excited? Ok, then next we'll move on to a cube.
很难令人兴奋,是的,但是你想想,现在你了解怎样构建一个三维网格! 还不兴奋吗? ok,那么,下面我们将继续学习如何创建一个立方体.
浙公网安备 33010602011771号