簡體   English   中英

嘗試在 WPF 中繪制矩形立方體的邊緣但未成功

[英]Trying to draw the edges of a rectangular cube in WPF but not successful

我正在嘗試僅繪制和顯示矩形立方體的邊緣或側面,但使用 WPF 仍然沒有成功。它位於我想在 windows 表單 (c#) 應用程序中顯示的用戶控件內。 正如我所見,WPF 中的 3D 個對象是使用三角形制作的,所以我嘗試使用矩形繪制線條,其中矩形的寬度很小(足以被識別為立方體的邊緣或側面)但它沒有顯示正確。 這是我正在使用的代碼:

<UserControl x:Class="Spatial_and_Temporal_Research.BoundingBoxes"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" DataContext="{Binding}" Height="509" Width="739" FontFamily="Times New Roman">
        <Grid>

            <!-- Place a Label control at the top of the view. -->
            <Label 
                    HorizontalAlignment="Center" 
                    TextBlock.TextAlignment="Center" 
                    FontSize="20" 
                    Foreground="Red" 
                    Content="Model: Cone"/>

            <!-- Viewport3D is the rendering surface. -->
            <Viewport3D Name="myViewport" >

                <!-- Add a camera. -->
                <Viewport3D.Camera>
                    <PerspectiveCamera 
                            FarPlaneDistance="20" 
                            LookDirection="-6, -5, -4" 
                            UpDirection="0,1,0" 
                            NearPlaneDistance="1" 
                            Position="6 5 4"
                            FieldOfView="45" />

                </Viewport3D.Camera>

                <!-- Add models. -->
                <Viewport3D.Children>

                    <ModelVisual3D>
                        <ModelVisual3D.Content>

                            <Model3DGroup >
                                <Model3DGroup.Children>

                                    <!-- Lights, MeshGeometry3D and DiffuseMaterial objects are added to the ModelVisual3D. -->
                                    <AmbientLight Color="White" />

                                    <!-- Define a red cone. -->
                                    <GeometryModel3D>

                                        <GeometryModel3D.Geometry>
                                            <MeshGeometry3D 
        Positions="-0.55 -0.5 -0.5  -0.45 -0.5 -0.5  -0.55 -0.5 0.5  -0.45 -0.5 0.5  -0.55 0.5 -0.5  -0.45 0.5 -0.5  -0.55 0.5 0.5  -0.45 0.5 0.5  0.45 -0.5 -0.5  0.55 -0.5 -0.5  0.45 -0.5 0.5  0.55 -0.5 0.5  -0.5 -0.55 -0.5  -0.5 -0.45 -0.5  0.5 -0.55 -0.5  0.5 -0.45 -0.5  -0.5 -0.55 0.5  -0.5 -0.45 0.5  0.5 -0.55 0.5  0.5 -0.45 0.5  0.45 0.5 -0.5  0.55 0.5 -0.5  0.45 0.5 0.5  0.55 0.5 0.5  -0.5 0.45 -0.5  -0.5 0.55 -0.5  0.5 0.45 -0.5  0.5 0.55 -0.5  -0.5 0.45 0.5  -0.5 0.55 0.5  0.5 0.45 0.5  0.5 0.55 0.5"

         TriangleIndices="0 2 3  3 1 0  4 6 7  7 5 4  1 0 4  4 5 1  3 2 6  6 7 3  8 10 11  11 9 8  13 12 14  14 15 13  17 16 18  18 19 17  20 22 23  23 21 20  17 16 20  20 21 17  19 18 22  22 23 19  25 24 26  26 27 25  29 28 30  30 31 29"                                       
             >

                                            </MeshGeometry3D>
                                        </GeometryModel3D.Geometry>

                                        <GeometryModel3D.Material>
                                            <DiffuseMaterial>
                                                <DiffuseMaterial.Brush>
                                                    <SolidColorBrush 
                                Color="Red" 
                                Opacity="1.0"/>
                                                </DiffuseMaterial.Brush>
                                            </DiffuseMaterial>
                                        </GeometryModel3D.Material>                            

                                    </GeometryModel3D>

                                </Model3DGroup.Children>
                            </Model3DGroup>

                        </ModelVisual3D.Content>

                    </ModelVisual3D>

                </Viewport3D.Children>

            </Viewport3D>
        </Grid>

</UserControl>

我正在考慮嘗試其他圖形庫,如 opengl 或 gdi+。 如果您知道如何在 wpf 中繪制一條 3D 線,請舉例說明。

無論您繪制什么,都需要對其進行細分,因為 WPF 本質上繪制的是三角形。

因此,對於 model 一條線作為一組鑲嵌曲面,首先將該線視為實體,或者更具體地說,是具有小橫截面的長直角棱柱。 如果你這樣想這條線,你就有了一個六邊形實體(包括線的兩個“端蓋”,如果你想要的話),你可以定義適當的三角形,WPF 會很好地渲染它。

當然有點麻煩,但絕對可行。

如果在 WPF 中有更好的方法可以做到這一點,我很想聽聽。

如果你想渲染你的 3D object 的網格,有人推薦Helixtoolkit插件。 但是這個附加組件中包含的“linevisual3D”非常蹩腳......

我鼓勵您不要考慮 3D 棱鏡(如 SlimsGhost 所說),而是考慮 2D 矩形,然后使用背面材料屬性使其雙向可見。 我已經編寫了這個並且它工作得很好(比 Helix Linevisual3D 更好)

PS:如果這真的引起了興趣,我可以在另一個專用線程中發布該方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM