简体   繁体   English

如何在WPF中绘制3D曲线?

[英]How to draw a 3D curve in WPF?

Is it possible to draw a 3D curve in WPF? 是否可以在WPF中绘制3D曲线? ie Which classes do I need? 即我需要哪些课程? Can you give me so code snippets. 您能给我这样的代码片段吗?

在此处输入图片说明

To render a 3D scene in WPF, you have to use an element called ViewPort3D . 要在WPF中渲染3D场景,必须使用一个名为ViewPort3D的元素。 This has a Camera , a collection of Visual3D . 它有一个Camera ,是Visual3D的集合。 A concrete sub type of Visual3D is ModelVisual3D . Visual3D一个具体子类型是ModelVisual3D This visual helps render the Model3D . 该视觉效果有助于渲染Model3D All the 3D shapes are represented by a GeometryModel3D or the more complex Model3DGroup (all derive from Model3D ). 所有3D形状均由GeometryModel3D或更复杂的Model3DGroup (均源自Model3D )表示。 We just care about the property GeometryModel3D.Geometry (of type Geometry3D ) here. 我们在这里只关心属性GeometryModel3D.Geometry (类型Geometry3D )。 Currently, WPF just supports only 1 kind of Geometry3D called MeshGeometry3D . 目前,WPF只是支持1种 Geometry3D称为MeshGeometry3D But this geometry consists of a large number of triangles. 但是这种几何形状由大量三角形组成。 To define the triangles, it has a collection of Point3D s ( Positions ) representing the vertices of the triangles and a collection of int ( TriangleIndices ) used to indicate how to connect the vertices (saved in Positions ) to make the actual triangles. 为了定义三角形,它具有表示三角形顶点的Point3D s( Positions )的集合和用于指示如何连接顶点(保存在Positions )以构成实际三角形的int集合( TriangleIndices )。

That means the curve you want to draw should have some shape, such as some kind of cylinder (like vermicelli) or just like a strip of cloth... Anyway it's not easy. 这意味着您要绘制的曲线应具有某种形状,例如某种圆柱体(如细线)或就像一块布一样……无论如何,这并不容易。 The following demo code renders the curve as a thin strip (not round), however in fact we make it so thin that you can just see it looks like a line (depending on how close we view the scene). 以下演示代码将曲线渲染为细条(不是圆形),但是实际上,我们将其制作得如此之细,以至于您只能看到它看起来像一条线(取决于我们查看场景的距离)。

As I said above, what we need to build the curve is fill the info ( Positions and TriangleIndices ) for a MeshGeometry3D . 就像我在上面说的,构建曲线所需的是填充MeshGeometry3D的信息( PositionsTriangleIndices )。 We need some equation of the curve, make some loop to get a collection of Point3Ds (belong to the curve) and at the same time fill the TriangleIndices appropriately. 我们需要曲线的一些方程式,进行一些循环以获得Point3D的集合(属于曲线),同时适当地填充TriangleIndices

Here is the code detail: 这是代码详细信息:

XAML : XAML

<Grid Name="grid">        
    <Viewport3D>
        <Viewport3D.Camera>
            <PerspectiveCamera Position="55,55,55" LookDirection="-1,-1,-1"
                               UpDirection="0,1,0" FieldOfView="20"/>
        </Viewport3D.Camera>
        <Viewport3D.Children>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <Model3DGroup>
                        <DirectionalLight Color="White" Direction="1,0,-1"/>
                        <AmbientLight Color="Black"/>
                        <GeometryModel3D>
                            <GeometryModel3D.Material>
                                <MaterialGroup>
                                   <DiffuseMaterial Brush="Red"/>
                                   <EmissiveMaterial Brush="#330000ff"/>
                                </MaterialGroup>
                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <MaterialGroup>
                                    <DiffuseMaterial Brush="Red"/>
                                    <EmissiveMaterial Brush="#330000ff"/>
                                </MaterialGroup>
                            </GeometryModel3D.BackMaterial>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D x:Name="geo"/>
                            </GeometryModel3D.Geometry>
                            <GeometryModel3D.Transform>
                                <RotateTransform3D>
                                    <RotateTransform3D.Rotation>
                                        <AxisAngleRotation3D Axis="0,1,0" 
                                                        Angle="0" x:Name="rot"/>
                                    </RotateTransform3D.Rotation>
                                </RotateTransform3D>
                            </GeometryModel3D.Transform>
                        </GeometryModel3D>
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D.Children>
        <Viewport3D.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard Storyboard.TargetName="rot">
                        <DoubleAnimation Storyboard.TargetProperty="Angle" By="360"
                                     RepeatBehavior="Forever" Duration="00:00:10"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Viewport3D.Triggers>
    </Viewport3D>
</Grid>

Note about the Name of the MeshGeometry3D in XAML code is geo , we need to refer to this to build the curve using code. 注意,在XAML代码中, MeshGeometry3DNamegeo ,我们需要参考此代码以使用代码构建曲线。

Code behind : 后面的代码

int k = 0;
for (float n = 0f; n < 1; n += 0.01f, k++) {
     //calculate the current Point3D based on the equations of the curve
     var x = -13 * Math.Pow(n, 3) - 12 * n * n;
     var y = 35 * Math.Pow(n, 5) - 13 * n * n + 3 * n + 1;
     var z = -30 * Math.Pow(n, 3) + 20 * n * n - n - 1;
     //the current Point3D
     var p = new Point3D(x, y, z);
     //here is where we make it thin, 
     //you can replace .1 with such as 10 to enlarge the strip
     var u = new Point3D(x, y - .1, z);
     geo.Positions.Add(p);
     geo.Positions.Add(u);                                              
     if (k > 0) {
          geo.TriangleIndices.Add(k);
          geo.TriangleIndices.Add(k - 1);
          geo.TriangleIndices.Add(k + 1);                    
          geo.TriangleIndices.Add(k);                    
     }
     geo.TriangleIndices.Add(k);
     geo.TriangleIndices.Add(k + 1);
     k++;
 }

NOTE : I highly recommend you to read about basic 3D concepts in WPF first, at least you should understand how to connect the points (in Positions ) to indicate the triangle surfaces as what you want. 注意 :我强烈建议您首先阅读WPF中的基本3D概念,至少您应该了解如何连接这些点(在Positions )以指示所需的三角形曲面。 Once understand that, you can understand the code above. 一旦了解了这一点,就可以理解上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM