wpf 在wpf中在3d中画一条线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27705012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:48:35  来源:igfitidea点击:

drawing a line in 3d in wpf

c#wpf3d

提问by mans

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)来做到这一点

回答by Thomas Petersson

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". Then of course you must make sure that the orientation of this rectangle is so that it is facing the camera.

要创建一条线,您必须使一条线的长度和宽度成为一个矩形,然后用对角线将其拆分以创建两个窄三角形。所以你需要四个位置和两个三角形,比如“0 1 2 0 1 3”。那么当然你必须确保这个矩形的方向是面向相机的。

You can google or bing for helix toolbox which is an excellent library of utilities for 3D in WPF. There you might find a useful helper function.

您可以在 google 或 bing 上搜索 helix 工具箱,它是 WPF 中一个出色的 3D 实用程序库。在那里您可能会找到一个有用的辅助函数。

回答by Artholl

Why do you want to draw just line. In 3D you usually need triangles. If you have triangle you can determine normals. They are used to define facing of triangle which is used in lighting and texturing.

为什么你只想画线。在 3D 中,您通常需要三角形。如果你有三角形,你可以确定normals。它们用于定义用于照明和纹理的三角形的面。

Camera settings
Typical camera Positionis somewhere in positive z coordinates (something like 0, 0, 2 or 5, 0, 20), LookDirectionvector is 0, 0, -1 and UpDirectionvector 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).
If you change UpDirectionto 1, 0, 0 then positive x axis goes up not y axis.
If you change Positionto negative z coordinates (0, 0, -5) and LookDirectionto 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, 2 或 5, 0, 20),LookDirection向量为 0, 0, -1,UpDirection向量为 0, 1, 0。在这种情况下,应定位轴像往常一样(正 y 轴向上,正 x 轴向右)。
如果您更改UpDirection为 1, 0, 0 则正 x 轴上升而不是 y 轴。
如果您更改Position为负 z 坐标 (0, 0, -5) 并LookDirection更改为 0, 0, 1 然后您从“后面”看您的屏幕,因此正 x 轴向左(而不是向右)但 y 轴仍然往上。

In your settings your camera is pointing to the positive y numbers from origin and z axis goes up. X axis still goes to the right. If I add third point to your code. Point 100, 0, 100, than you can see small black triangle.

在您的设置中,您的相机指向原点的正 y 数,z 轴上升。X 轴仍然向右。如果我在您的代码中添加第三点。点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".

回答by MagmKin209

you're building lines with no thickness....actually this and pixel coloring are the 2 most important problems of WPF.... If you want to draw lines you'll have to build it as a quadrangle (2 connex triangles), if you want to get smarter and have a lower number of triangles you can use other tricks (I'll let you think about it ^^, but for a given mesh you can have a wireframe that has exactly the same number of triangles as your mesh, which would not be the case if you create quadrangles)

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