2D 容器中的三维:
WPF 中的3D 图形内容封装在一个元素中, Viewport3D 该元素可以参与二维元素结构。 在 WPF 中,图形系统将视为二维 Viewport3D 视觉对象,如许多其他元素。 Viewport3D 作为一个窗口(视区)为一个三维场景。 更准确地说,它是一个在其上投影三维场景的图面。
在传统的2D 应用程序中,可以像使用 Viewport3D 网格或画布这样的其他容器元素一样使用。 虽然您可以 Viewport3D 在同一个场景图中使用与其他2d 绘图对象,但您不能渗透中的2d 和3d 对象 Viewport3D 。 本主题将重点介绍如何在中绘制3D 图形 Viewport3D 。

三维坐标空间:
2D 图形的 WPF 坐标系统将原点定位在呈现区域的左上角 (通常是) 屏幕。 在二维系统中,正 x 轴值向右移动,正值 y 轴值向下移动。 但是,在3D 坐标系中,原点位于呈现区域的中心,正 x 轴值前进到右侧,而 y 轴上的正值朝上向右,而从原点向外前进到查看器。

 照相机和投影:

使用2D 的开发人员习惯于将绘图基元定位在二维屏幕上。 创建三维场景时,务必记住,确实要创建3D 对象的2D 表示形式。 由于三维场景看上去不同,具体取决于旁观者视角的观点,你必须指定该点。 Camera类允许您为三维场景指定此视图点。
了解3D 场景如何在二维图面上表示的另一种方法是将场景描述为查看图面上的投影。 ProjectionCamera允许您指定不同的投影及其属性,以更改旁观者视角查看3d 模型的方式。 PerspectiveCamera指定 foreshortens 场景的投影。 换句话说, PerspectiveCamera 提供消失点透视。 可以指定照相机在场景坐标系中的位置、照相机的方向和视野以及用来定义场景中“向上”方向的矢量。 下图说明了的 PerspectiveCamera 投影。
的 NearPlaneDistance 和 FarPlaneDistance 属性 ProjectionCamera 限制了照相机投影范围。 由于照相机可以位于场景中的任何位置,因此照相机实际上可能会位于模型内部或者紧靠模型,这使正确区分对象变得很困难。 NearPlaneDistance 允许您指定与相机之间的最小距离,超过此值后将不会绘制对象。 相反, FarPlaneDistance 使您可以指定与相机之间的距离,超过此距离将不会绘制对象,从而确保太远的对象不能被识别。

主要参数
PerspectiveCamera:透视投影相机,更符合世界实际。
OrthographicCamera:正投影相机,不会出现近大远小的效果。
LookDirection:相机镜头朝向。
UpDirection:相机的上方向是哪里。
Position:相机放在哪里。
    
AmbientLight(环境光) 
DirectionalLight(平行光) 
PointLight(点光源) 
SpotLight(聚光灯) 
    
Positions中每个点从0开始编号,TriangleIndices中每个整数就是Positions中对应编号的点比如你这个例子中:(0)-2 1 -1,(1) 0 2 0,(2) 2 1 -1,(3) -2 -1 -1,(4) 0 -2 0,(5) 2 -1 -1,所以0 3 1对应的就是(0)(-2, 1, -1)、(3)(-2, -1,-1)、(1)(0, 2, 0)
    
Material(材质),它可以是纯颜色、渐变颜色、图片等
TextureCoordinates用于描述材质如何贴在平面上

3D图形案例:

<Window x:Class="WpfCompreExample._3D"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfCompreExample"
        mc:Ignorable="d"
        Title="_3D" Height="450" Width="800">
    <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="0,0,1" 
                        UpDirection="0,1,0" 
                        NearPlaneDistance="1" 
                        Position="0,0,-3" 
                        FieldOfView="45" />
            </Viewport3D.Camera>
            <!-- Add models. -->
            <Viewport3D.Children>
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <Model3DGroup >
                            <Model3DGroup.Children>
                                <!-- Lights, MeshGeometry3D and DiffuseMaterial objects are added to the ModelVisual3D. -->
                                <DirectionalLight Color="#FFFFFFFF" Direction="3,-4,5" />
                                <!-- Define a red cone. -->
                                <GeometryModel3D>
                                    <GeometryModel3D.Geometry>
                                        <MeshGeometry3D 
    Positions="0.293893 -0.5 0.404509  0.475528 -0.5 0.154509  0 0.5 0  0.475528 -0.5 0.154509  0 0.5 0  0 0.5 0  0.475528 -0.5 0.154509  0.475528 -0.5 -0.154509  0 0.5 0  0.475528 -0.5 -0.154509  0 0.5 0  0 0.5 0  0.475528 -0.5 -0.154509  0.293893 -0.5 -0.404509  0 0.5 0  0.293893 -0.5 -0.404509  0 0.5 0  0 0.5 0  0.293893 -0.5 -0.404509  0 -0.5 -0.5  0 0.5 0  0 -0.5 -0.5  0 0.5 0  0 0.5 0  0 -0.5 -0.5  -0.293893 -0.5 -0.404509  0 0.5 0  -0.293893 -0.5 -0.404509  0 0.5 0  0 0.5 0  -0.293893 -0.5 -0.404509  -0.475528 -0.5 -0.154509  0 0.5 0  -0.475528 -0.5 -0.154509  0 0.5 0  0 0.5 0  -0.475528 -0.5 -0.154509  -0.475528 -0.5 0.154509  0 0.5 0  -0.475528 -0.5 0.154509  0 0.5 0  0 0.5 0  -0.475528 -0.5 0.154509  -0.293892 -0.5 0.404509  0 0.5 0  -0.293892 -0.5 0.404509  0 0.5 0  0 0.5 0  -0.293892 -0.5 0.404509  0 -0.5 0.5  0 0.5 0  0 -0.5 0.5  0 0.5 0  0 0.5 0  0 -0.5 0.5  0.293893 -0.5 0.404509  0 0.5 0  0.293893 -0.5 0.404509  0 0.5 0  0 0.5 0  " 
    Normals="0.7236065,0.4472139,0.5257313  0.2763934,0.4472138,0.8506507  0.5308242,0.4294462,0.7306172  0.2763934,0.4472138,0.8506507  0,0.4294458,0.9030925  0.5308242,0.4294462,0.7306172  0.2763934,0.4472138,0.8506507  -0.2763934,0.4472138,0.8506507  0,0.4294458,0.9030925  -0.2763934,0.4472138,0.8506507  -0.5308242,0.4294462,0.7306172  0,0.4294458,0.9030925  -0.2763934,0.4472138,0.8506507  -0.7236065,0.4472139,0.5257313  -0.5308242,0.4294462,0.7306172  -0.7236065,0.4472139,0.5257313  -0.858892,0.429446,0.279071  -0.5308242,0.4294462,0.7306172  -0.7236065,0.4472139,0.5257313  -0.8944269,0.4472139,0  -0.858892,0.429446,0.279071  -0.8944269,0.4472139,0  -0.858892,0.429446,-0.279071  -0.858892,0.429446,0.279071  -0.8944269,0.4472139,0  -0.7236065,0.4472139,-0.5257313  -0.858892,0.429446,-0.279071  -0.7236065,0.4472139,-0.5257313  -0.5308242,0.4294462,-0.7306172  -0.858892,0.429446,-0.279071  -0.7236065,0.4472139,-0.5257313  -0.2763934,0.4472138,-0.8506507  -0.5308242,0.4294462,-0.7306172  -0.2763934,0.4472138,-0.8506507  0,0.4294458,-0.9030925  -0.5308242,0.4294462,-0.7306172  -0.2763934,0.4472138,-0.8506507  0.2763934,0.4472138,-0.8506507  0,0.4294458,-0.9030925  0.2763934,0.4472138,-0.8506507  0.5308249,0.4294459,-0.7306169  0,0.4294458,-0.9030925  0.2763934,0.4472138,-0.8506507  0.7236068,0.4472141,-0.5257306  0.5308249,0.4294459,-0.7306169  0.7236068,0.4472141,-0.5257306  0.8588922,0.4294461,-0.27907  0.5308249,0.4294459,-0.7306169  0.7236068,0.4472141,-0.5257306  0.8944269,0.4472139,0  0.8588922,0.4294461,-0.27907  0.8944269,0.4472139,0  0.858892,0.429446,0.279071  0.8588922,0.4294461,-0.27907  0.8944269,0.4472139,0  0.7236065,0.4472139,0.5257313  0.858892,0.429446,0.279071  0.7236065,0.4472139,0.5257313  0.5308242,0.4294462,0.7306172  0.858892,0.429446,0.279071  "                   TriangleIndices="0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 " />
                                    </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>
</Window>

参考:WPF 3D开发教程(一)_lweiyue的专栏-CSDN博客_wpf3d 

全网最全3D模型实例:WPF3D模型综合实例(全网最全实例)-C#文档类资源-CSDN下载

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐