赞
踩
看了书《WPF编程宝典:使用C#2012和.net4.5 第四版》,看到这个3D效果很棒,书上有些例子跑不通,网上查了一些源码,所以做个笔记。
1.界面部分
- <Window x:Class="_3dModel2dUi.MainWindow"
- 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:_3dModel2dUi"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="800">
- <Grid>
- <Button Margin="3,3,0,0" Click="Button_Click" HorizontalAlignment="Left" VerticalAlignment="Top" Height="21" Width="62"></Button>
- <!--#region 这是旋转滑动条-->
- <Slider Minimum="0"
- Maximum="360"
- Height="20"
- Width="790"
- VerticalAlignment="Bottom"
- Value="{Binding ElementName=Roate3DOjbect, Path=Angle}" HorizontalAlignment="Left"/>
- <!--#endregion-->
- <Viewport3D>
- <Viewport3D.Camera>
- <PerspectiveCamera Position="0, 0, 4"/>
- </Viewport3D.Camera>
-
- <!-- Button on 3D -->
- <Viewport2DVisual3D>
- <!-- Give the plane a slight rotation -->
- <Viewport2DVisual3D.Transform>
- <RotateTransform3D>
- <RotateTransform3D.Rotation>
- <AxisAngleRotation3D x:Name="Roate3DOjbect" Angle="40" Axis="0, 1, 0" />
- </RotateTransform3D.Rotation>
- </RotateTransform3D>
- </Viewport2DVisual3D.Transform>
-
- <!-- The Geometry, Material, and Visual for the Viewport2DVisual3D -->
- <Viewport2DVisual3D.Geometry>
- <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0"
- TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 1 2 0 2 3"/>
- </Viewport2DVisual3D.Geometry>
-
- <Viewport2DVisual3D.Material>
- <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/>
- </Viewport2DVisual3D.Material>
-
- <StackPanel Orientation="Vertical">
- <Button Click="Button_Click_1">Hello, 3D</Button>
- <TextBox Width="100"></TextBox>
- </StackPanel>
-
- </Viewport2DVisual3D>
-
- <!-- Lights -->
- <ModelVisual3D>
- <ModelVisual3D.Content>
- <DirectionalLight Color="#FFFFFFFF" Direction="0,0,-1"/>
- </ModelVisual3D.Content>
- <!--#region 这是xaml灯光旋转的处理-->
- <ModelVisual3D.Transform>
- <RotateTransform3D>
- <RotateTransform3D.Rotation>
- <AxisAngleRotation3D x:Name="Roate3DOjbect2" Axis="1 2 1"/>
- </RotateTransform3D.Rotation>
- </RotateTransform3D>
- </ModelVisual3D.Transform>
- <!--#endregion-->
- </ModelVisual3D>
- </Viewport3D>
- </Grid>
- </Window>
2.逻辑部分
- namespace _3dModel2dUi
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
-
- private void Button_Click(object sender, RoutedEventArgs e)
- {
-
- }
-
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- MessageBox.Show("fuck you,bitch");
- }
- }
- }
完毕... 实现同样的效果,比unity3D舒服多了,生成的运行文件,一共不到50KB
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。