简体   繁体   English

在wpf中用3d绘制一条线

[英]drawing a line in 3d in wpf

I am trying to draw a 3D line in wpf and I have this xaml code: 我想在wpf中绘制一条3D线,我有这个xaml代码:

<Grid>
    <Viewport3D x:Name="ViewerViewport"
                RenderOptions.BitmapScalingMode="HighQuality"
                Focusable="True" Grid.RowSpan="2">

        <ModelVisual3D x:Name="Model">

        </ModelVisual3D>

        <!-- Camera -->
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="Camera"
                               Position="0,0,0"
                               LookDirection="0,1,0"
                               UpDirection="0,0,1"
                               FieldOfView="100"
                               FarPlaneDistance="10"
                               NearPlaneDistance="0.1"/>
        </Viewport3D.Camera>

    </Viewport3D>

</Grid>

and this c# code: 这个c#代码:

public MainWindow()
{
        InitializeComponent();
        var ModelsGroup = new Model3DGroup();
         ModelsGroup.Children.Add(this.AddLine(new Point3D(0, 0, 100), new Point3D(0, 100, 100),"line 1)"));
         ModelsGroup.Children.Add(new AmbientLight(Colors.White));
        Model.Content = ModelsGroup;
}

and line creation code: 和行创建代码:

  private Model3D AddLine(Point3D startPoint, Point3D EndPoint, string name)
    {
        SolidColorBrush brush = new SolidColorBrush(Colors.Black);
        var material = new DiffuseMaterial(brush);
        var mesh = new MeshGeometry3D();
        mesh.Positions.Add(startPoint);
        mesh.Positions.Add(EndPoint);
        mesh.TriangleIndices.Add(0);
        mesh.TriangleIndices.Add(1);
        mesh.TriangleIndices.Add(0);
        return new GeometryModel3D(mesh, material);
    }

but it doesn't show any line in output? 但它没有在输出中显示任何一行?

What is wrong with this? 这有什么问题?

I know that there are some 3d libraries that can do this easily, but I like to learn how to do it in WPF and then investigate how to do this using libraries (such as helix3d) 我知道有一些3d库可以很容易地做到这一点,但我想学习如何在WPF中做到这一点,然后研究如何使用库(如helix3d)来做到这一点

You are creating a triangle where two corners are at the same point, that is a triangle with zero area so it can never be seen from any angle. 您正在创建一个三角形,其中两个角位于同一点,即一个零区域的三角形,因此无法从任何角度看到它。 WPF uses only triangles with area WPF仅使用带面积的三角形

To create a line you must make a rectangle the length and width of the line, and split it with a diagonal to create two narrow triangles. 要创建一条线,您必须在线的长度和宽度上创建一个矩形,并用对角线分割它以创建两个窄三角形。 So you need four positions and two triangles, like "0 1 2 0 1 3". 所以你需要四个位置和两个三角形,比如“0 1 2 0 1 3”。 Then of course you must make sure that the orientation of this rectangle is so that it is facing the camera. 当然,你必须确保这个矩形的方向是面向相机。

You can google or bing for helix toolbox which is an excellent library of utilities for 3D in WPF. 你可以google或bing for helix toolbox,它是WPF中3D的优秀实用程序库。 There you might find a useful helper function. 在那里你可能会找到一个有用的辅助函数。

Why do you want to draw just line. 你为什么要画线。 In 3D you usually need triangles. 在3D中,您通常需要三角形。 If you have triangle you can determine normals . 如果你有三角形,你可以确定normals They are used to define facing of triangle which is used in lighting and texturing. 它们用于定义三角形的面,用于照明和纹理。

Camera settings 相机设置
Typical camera Position is somewhere in positive z coordinates (something like 0, 0, 2 or 5, 0, 20), LookDirection vector is 0, 0, -1 and UpDirection vector is 0, 1, 0. In this case axes should be positioned as they usually are (positive y axis goes up and positive x axis goes to the right). 典型的相机Position在正z坐标(类似于LookDirectionLookDirection )的LookDirectionLookDirection向量为0,0,-1, UpDirection向量为UpDirection在这种情况下,轴应为按常规定位(正y轴向上,正x轴向右)。
If you change UpDirection to 1, 0, 0 then positive x axis goes up not y axis. 如果将UpDirection更改为UpDirection ,则正x轴上升而不是y轴。
If you change Position to negative z coordinates (0, 0, -5) and LookDirection to 0, 0, 1 then you look at your sceen from "behind" so positive x axis goes to the left (not to the right) but y axis still goes up. 如果将Position更改为负z坐标(0,0,-5)并将LookDirectionLookDirection ,则从“后方”查看您的方向,因此正x轴向左(不是右侧)但是y轴仍然上升。

In your settings your camera is pointing to the positive y numbers from origin and z axis goes up. 在您的设置中,您的相机指向原点的正y数字,z轴向上。 X axis still goes to the right. X轴仍然向右移动。 If I add third point to your code. 如果我在你的代码中添加第三个点。 Point 100, 0, 100, than you can see small black triangle. 点100,0,100,比你可以看到小的黑色三角形。

mesh.Positions.Add(startPoint);
mesh.Positions.Add(endPoint);
mesh.Positions.Add(new Point3D(100, 0, 100));
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);

This triangle is small because of distance from camera - 100. So as @Samuels says in comment another error in settings is FarPlaneDistance="10" . 这个三角形很小,因为距离相机的距离为100.所以@Samuels在评论中说设置中的另一个错误是FarPlaneDistance="10"

你正在构建没有厚度的线......实际上这和像素着色是WPF的两个最重要的问题....如果你想绘制线条,你必须将它构建为四边形(2个连接三角形) ,如果你想变得更聪明并且三角形数量更少,你可以使用其他技巧(我会让你考虑它^^,但对于给定的网格,你可以有一个线框,其具有完全相同数量的三角形您的网格,如果您创建四边形则不会是这种情况)

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

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