Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Media.Media3D;
  16. using System.Windows.Media.Animation;
  17.  
  18. namespace WpfApp6
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. }
  29. Model3DGroup modelGroup = new Model3DGroup();
  30. PerspectiveCamera myPCamera = new PerspectiveCamera();
  31. GeometryModel3D teapotModel = new GeometryModel3D();
  32. Transform3DCollection myTransforms = new Transform3DCollection();
  33. Viewport3D myViewport = new Viewport3D();
  34.  
  35.  
  36.  
  37. private void Window_Loaded(object sender, RoutedEventArgs e)
  38. {
  39. //Set camera viewpoint and properties.
  40. myPCamera.FarPlaneDistance = 20;
  41. myPCamera.NearPlaneDistance = 1;
  42. myPCamera.FieldOfView = 45;
  43. myPCamera.Position = new Point3D(0, 0, 10);
  44. myPCamera.LookDirection = new Vector3D(0, 0, -10);
  45. myPCamera.UpDirection = new Vector3D(0, 1, 0);
  46.  
  47. //Add light sources to the scene.
  48. //PointLight myDirLight = new PointLight(Color.FromRgb(255, 255, 255), new Point3D(0, 0, 10));
  49. DirectionalLight myDirLight = new DirectionalLight(Color.FromRgb(255, 255, 255), new Vector3D(1, 1, -3));
  50.  
  51. teapotModel.Geometry = (MeshGeometry3D)Application.Current.Resources["myTeapot"];
  52.  
  53.  
  54.  
  55. Brush objectBrush;
  56.  
  57. DiffuseMaterial diffuseMaterial;
  58. SpecularMaterial specularMaterial;
  59.  
  60. MaterialGroup matGroup = new MaterialGroup();
  61.  
  62. matGroup.Children.Clear();
  63. objectBrush = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
  64. diffuseMaterial = new DiffuseMaterial(objectBrush);
  65. specularMaterial = new SpecularMaterial(new SolidColorBrush(Color.FromRgb(255, 255, 255)), 100);
  66.  
  67. matGroup.Children.Add(diffuseMaterial);
  68. matGroup.Children.Add(specularMaterial);
  69.  
  70. ColorAnimationUsingKeyFrames objColorAnim = new ColorAnimationUsingKeyFrames();
  71.  
  72. objColorAnim.KeyFrames.Add(new DiscreteColorKeyFrame(Color.FromRgb(255, 255, 255), KeyTime.FromTimeSpan(new TimeSpan(0, 0, 5))));
  73. objColorAnim.KeyFrames.Add(new DiscreteColorKeyFrame(Color.FromRgb(255, 0, 0), KeyTime.FromTimeSpan(new TimeSpan(0, 0, 10))));
  74. objColorAnim.KeyFrames.Add(new DiscreteColorKeyFrame(Color.FromRgb(0, 255, 0), KeyTime.FromTimeSpan(new TimeSpan(0, 0, 15))));
  75. objColorAnim.KeyFrames.Add(new LinearColorKeyFrame(Color.FromRgb(0, 0, 255), KeyTime.FromTimeSpan(new TimeSpan(0, 0, 20))));
  76. objColorAnim.KeyFrames.Add(new LinearColorKeyFrame(Color.FromRgb(255,255, 0), KeyTime.FromTimeSpan(new TimeSpan(0, 0, 25))));
  77. objColorAnim.KeyFrames.Add(new LinearColorKeyFrame(Color.FromRgb(0, 255, 255), KeyTime.FromTimeSpan(new TimeSpan(0, 0, 30))));
  78.  
  79. teapotModel.Material = matGroup;
  80.  
  81. objColorAnim.RepeatBehavior = RepeatBehavior.Forever;
  82.  
  83. objectBrush.BeginAnimation(SolidColorBrush.ColorProperty, objColorAnim);
  84.  
  85.  
  86. // Quaternion Структура, представляющая поворот в трех измерениях.
  87.  
  88. RotateTransform3D myRotateTransform3D = new RotateTransform3D();
  89.  
  90. Quaternion q1 = new Quaternion(new Vector3D(1, 1, 0), 0);
  91. QuaternionRotation3D myQuaternionRotation3D1 = new QuaternionRotation3D(q1);
  92. myRotateTransform3D.Rotation = myQuaternionRotation3D1;
  93. teapotModel.Transform = myRotateTransform3D;
  94.  
  95. QuaternionAnimation qa = new QuaternionAnimation(new Quaternion(new Vector3D(1, 1, 0), 0), new Quaternion(new Vector3D(1, 1, 0), 359), new Duration(TimeSpan.FromSeconds(5)));
  96. qa.UseShortestPath = false;
  97. qa.RepeatBehavior = RepeatBehavior.Forever;
  98. myRotateTransform3D.Rotation.BeginAnimation(QuaternionRotation3D.QuaternionProperty, qa);
  99.  
  100.  
  101.  
  102. modelGroup.Children.Add(teapotModel);
  103. modelGroup.Children.Add(myDirLight);
  104.  
  105. ModelVisual3D modelsVisual = new ModelVisual3D();
  106. modelsVisual.Content = modelGroup;
  107.  
  108. //Add the visual and camera to the Viewport3D.
  109. myViewport.Camera = myPCamera;
  110. myViewport.Children.Add(modelsVisual);
  111.  
  112. this.Content = myViewport;
  113.  
  114. }
  115.  
  116.  
  117. }
  118. }
  119.  
  120.  
  121. <Window x:Class="WpfApp6.MainWindow"
  122. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  123. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  124. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  125. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  126. xmlns:local="clr-namespace:WpfApp6"
  127. mc:Ignorable="d"
  128. Title="MainWindow" Height="740.408" Width="1179.404" Loaded="Window_Loaded">
  129.  
  130.  
  131. </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement