当前位置:   article > 正文

WPF自带的3D效果 学习笔记(1)_wpf的3d按钮

wpf的3d按钮

看了书《WPF编程宝典:使用C#2012和.net4.5 第四版》,看到这个3D效果很棒,书上有些例子跑不通,网上查了一些源码,所以做个笔记。

 

1.界面部分

  1. <Window x:Class="_3dModel2dUi.MainWindow"
  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"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:_3dModel2dUi"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Grid>
  10. <Button Margin="3,3,0,0" Click="Button_Click" HorizontalAlignment="Left" VerticalAlignment="Top" Height="21" Width="62"></Button>
  11. <!--#region 这是旋转滑动条-->
  12. <Slider Minimum="0"
  13. Maximum="360"
  14. Height="20"
  15. Width="790"
  16. VerticalAlignment="Bottom"
  17. Value="{Binding ElementName=Roate3DOjbect, Path=Angle}" HorizontalAlignment="Left"/>
  18. <!--#endregion-->
  19. <Viewport3D>
  20. <Viewport3D.Camera>
  21. <PerspectiveCamera Position="0, 0, 4"/>
  22. </Viewport3D.Camera>
  23. <!-- Button on 3D -->
  24. <Viewport2DVisual3D>
  25. <!-- Give the plane a slight rotation -->
  26. <Viewport2DVisual3D.Transform>
  27. <RotateTransform3D>
  28. <RotateTransform3D.Rotation>
  29. <AxisAngleRotation3D x:Name="Roate3DOjbect" Angle="40" Axis="0, 1, 0" />
  30. </RotateTransform3D.Rotation>
  31. </RotateTransform3D>
  32. </Viewport2DVisual3D.Transform>
  33. <!-- The Geometry, Material, and Visual for the Viewport2DVisual3D -->
  34. <Viewport2DVisual3D.Geometry>
  35. <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0"
  36. TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 1 2 0 2 3"/>
  37. </Viewport2DVisual3D.Geometry>
  38. <Viewport2DVisual3D.Material>
  39. <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/>
  40. </Viewport2DVisual3D.Material>
  41. <StackPanel Orientation="Vertical">
  42. <Button Click="Button_Click_1">Hello, 3D</Button>
  43. <TextBox Width="100"></TextBox>
  44. </StackPanel>
  45. </Viewport2DVisual3D>
  46. <!-- Lights -->
  47. <ModelVisual3D>
  48. <ModelVisual3D.Content>
  49. <DirectionalLight Color="#FFFFFFFF" Direction="0,0,-1"/>
  50. </ModelVisual3D.Content>
  51. <!--#region 这是xaml灯光旋转的处理-->
  52. <ModelVisual3D.Transform>
  53. <RotateTransform3D>
  54. <RotateTransform3D.Rotation>
  55. <AxisAngleRotation3D x:Name="Roate3DOjbect2" Axis="1 2 1"/>
  56. </RotateTransform3D.Rotation>
  57. </RotateTransform3D>
  58. </ModelVisual3D.Transform>
  59. <!--#endregion-->
  60. </ModelVisual3D>
  61. </Viewport3D>
  62. </Grid>
  63. </Window>

2.逻辑部分

  1. namespace _3dModel2dUi
  2. {
  3. /// <summary>
  4. /// MainWindow.xaml 的交互逻辑
  5. /// </summary>
  6. public partial class MainWindow : Window
  7. {
  8. public MainWindow()
  9. {
  10. InitializeComponent();
  11. }
  12. private void Button_Click(object sender, RoutedEventArgs e)
  13. {
  14. }
  15. private void Button_Click_1(object sender, RoutedEventArgs e)
  16. {
  17. MessageBox.Show("fuck you,bitch");
  18. }
  19. }
  20. }

完毕...   实现同样的效果,比unity3D舒服多了,生成的运行文件,一共不到50KB

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/804295
推荐阅读
相关标签
  

闽ICP备14008679号