如何在WPF中绘制3D曲线? [英] How to draw a 3D curve in WPF?
问题描述
是否可以在WPF中绘制3D曲线?即我需要哪些课程?
Is it possible to draw a 3D curve in WPF? i.e. Which classes do I need? Can you give me so code snippets.
推荐答案
要在WPF中渲染3D场景,必须使用名为 ViewPort3D的元素
。它有一个相机
,一个 Visual3D
的集合。 Visual3D
的具体子类型是 ModelVisual3D
。此视觉效果有助于呈现 Model3D
。所有3D形状均由 GeometryModel3D
或更复杂的 Model3DGroup
表示(均来自 Model3D
)。我们只关心此处的属性 GeometryModel3D.Geometry
(类型为 Geometry3D
)。当前,WPF仅支持 仅1 种名为 MeshGeometry3D $ c $的
Geometry3D
c>。但是这种几何形状由大量三角形组成。要定义三角形,它具有表示三角形顶点的 Point3D
s( Positions
)的集合和一个集合 int
( TriangleIndices
)的值,用于指示如何连接顶点(保存在 Positions
)来制作实际的三角形。
To render a 3D scene in WPF, you have to use an element called ViewPort3D
. This has a Camera
, a collection of Visual3D
. A concrete sub type of Visual3D
is ModelVisual3D
. This visual helps render the Model3D
. All the 3D shapes are represented by a GeometryModel3D
or the more complex Model3DGroup
(all derive from Model3D
). We just care about the property GeometryModel3D.Geometry
(of type Geometry3D
) here. Currently, WPF just supports only 1 kind of Geometry3D
called 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.
这意味着您要绘制的曲线应具有某种形状,例如某种圆柱体(例如细线) )或就像一条布一样...无论如何都不容易。下面的演示代码将曲线呈现为一条细条(不是圆形),但是实际上我们将其绘制得如此之细,以至于您只能看到它看起来像一条线(取决于我们查看场景的距离)。
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).
如上所述,我们需要构建曲线是填充信息(位置
和 TriangleIndices
)的 MeshGeometry3D
。我们需要一些曲线方程,进行一些循环以获取Point3D的集合(属于曲线),并同时适当填充 TriangleIndices
。
As I said above, what we need to build the curve is fill the info (Positions
and TriangleIndices
) for a MeshGeometry3D
. 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.
以下是代码详细信息:
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>
请注意<$ c geo
,我们需要参考此代码以使用代码构建曲线。
Note about the Name
of the MeshGeometry3D
in XAML code is geo
, we need to refer to this to build the curve using code.
后面的代码:
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++;
}
注意:强烈建议您阅读以下内容首先要了解WPF中的基本3D概念,至少您应该了解如何连接点(在 Positions
中)以指示所需的三角形曲面。一旦了解了这一点,就可以理解上面的代码。
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. Once understand that, you can understand the code above.
这篇关于如何在WPF中绘制3D曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!