wpf——三维学习1


以下xmal是我从msdn上复制下来的。是用于在wpf中创建3d模型的实例
链接https://msdn.microsoft.com/zh-cn/library/ms747437.aspx
看它的使用方式。我画了个比较马虎的逻辑图。

1
<UserControl x:Class="HostingWpfUserControlInWf.UserControl1" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 > 5 6 <Grid> 7 8 <!-- Place a Label control at the top of the view. --> 9 <Label 10 HorizontalAlignment="Center" 11 TextBlock.TextAlignment="Center" 12 FontSize="20" 13 Foreground="Red" 14 Content="Model: Cone"/> 15 16 <!-- Viewport3D is the rendering surface. --> 17 <Viewport3D Name="myViewport" > 18 19 <!-- Add a camera. --> 20 <Viewport3D.Camera> 21 <PerspectiveCamera 22 FarPlaneDistance="20" 23 LookDirection="0,0,1" 24 UpDirection="0,1,0" 25 NearPlaneDistance="1" 26 Position="0,0,-3" 27 FieldOfView="45" /> 28 </Viewport3D.Camera> 29 30 <!-- Add models. --> 31 <Viewport3D.Children> 32 33 <ModelVisual3D> 34 <ModelVisual3D.Content> 35 36 <Model3DGroup > 37 <Model3DGroup.Children> 38 39 <!-- Lights, MeshGeometry3D and DiffuseMaterial objects are added to the ModelVisual3D. --> 40 <DirectionalLight Color="#FFFFFFFF" Direction="3,-4,5" /> 41 42 <!-- Define a red cone. --> 43 <GeometryModel3D> 44 45 <GeometryModel3D.Geometry> 46 <MeshGeometry3D 47 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 " 48 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 " /> 49 </GeometryModel3D.Geometry> 50 51 <GeometryModel3D.Material> 52 <DiffuseMaterial> 53 <DiffuseMaterial.Brush> 54 <SolidColorBrush 55 Color="Red" 56 Opacity="1.0"/> 57 </DiffuseMaterial.Brush> 58 </DiffuseMaterial> 59 </GeometryModel3D.Material> 60 61 </GeometryModel3D> 62 63 </Model3DGroup.Children> 64 </Model3DGroup> 65 66 </ModelVisual3D.Content> 67 68 </ModelVisual3D> 69 70 </Viewport3D.Children> 71 72 </Viewport3D> 73 </Grid> 74 75 </UserControl>

 

其实,最为关键的ModelView3D还有其他用法,比如ModelView3D.Content下只放个Model3DGroup,Model3DGroup下写个DirectionalLight,、ModelView3D再另外写个Children,下面放一些模型,也就是灯光放在Content里面,模型放在Children里面。例如上面的xmal代码可以改为:

 1 <UserControl
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfControlLibrary1.UserControl1"
 5     d:DesignWidth="300" d:DesignHeight="200"
 6     >
 7 
 8     <Grid>
 9 
10         <!-- Place a Label control at the top of the view. -->
11         <Label 
12                 HorizontalAlignment="Center" 
13                 TextBlock.TextAlignment="Center" 
14                 FontSize="20" 
15                 Foreground="Red" 
16                 Content="Model: Cone"/>
17 
18         <!-- Viewport3D is the rendering surface. -->
19         <Viewport3D x:Name="myViewport" >
20 
21             <!-- Add a camera. -->
22             <Viewport3D.Camera>
23                 <PerspectiveCamera 
24                         FarPlaneDistance="20" 
25                         LookDirection="0,0,1" 
26                         UpDirection="0,1,0" 
27                         NearPlaneDistance="1" 
28                         Position="0,0,-3" 
29                         FieldOfView="45" />
30             </Viewport3D.Camera>
31 
32             <!-- Add models. -->
33 
34             <ModelVisual3D>
35                 <ModelVisual3D.Children>
36                     <ModelVisual3D>
37                         <ModelVisual3D.Content>
38                             <Model3DGroup>
39                                 <Model3DGroup.Children>
40                                     <GeometryModel3D>
41 
42                                         <GeometryModel3D.Geometry>
43                                             <MeshGeometry3D 
44                                     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
45 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
46 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
47 -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
48 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" 
49                                     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
50 -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
51 -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
52 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
53 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 " />
54                                         </GeometryModel3D.Geometry>
55 
56                                         <GeometryModel3D.Material>
57                                             <DiffuseMaterial>
58                                                 <DiffuseMaterial.Brush>
59                                                     <SolidColorBrush 
60                                             Color="Red" 
61                                             Opacity="1.0"/>
62                                                 </DiffuseMaterial.Brush>
63                                             </DiffuseMaterial>
64                                         </GeometryModel3D.Material>
65 
66                                     </GeometryModel3D>
67 
68                                 </Model3DGroup.Children>
69                             </Model3DGroup>
70                         </ModelVisual3D.Content>
71                     </ModelVisual3D>
72                 </ModelVisual3D.Children>
//上面是Children,放模型
//这里是Content,里面只放灯光 73 <ModelVisual3D.Content> 75 <Model3DGroup > 76 <Model3DGroup.Children> 77 <!-- Lights, MeshGeometry3D and DiffuseMaterial objects are added to the ModelVisual3D. --> 78 <DirectionalLight Color="#FFFFFFFF" Direction="3,-4,5" /> 79 80 <!-- Define a red cone. --> 81 </Model3DGroup.Children> 82 </Model3DGroup> 83 84 </ModelVisual3D.Content> 85 86 </ModelVisual3D> 87 88 </Viewport3D> 89 </Grid> 90 91 </UserControl>

以上是我对wpf中最基础的三维的理解,如果错误,欢迎指出。

原文地址:https://www.cnblogs.com/lelehellow/p/6149404.html