Article From:https://www.cnblogs.com/lonelyxmas/p/9689211.html
WPF 3D translation model + animation (bridge detection system)

There are many examples of rotation on WPF 3D, but there are not many examples of translation. This article is not a WPF 3D literacy article, so we need to have a certain understanding of WPF 3D, at least know the Viewport, Perspective Camera, M.OdelVisual3D and other data structures. For the basics of WPF 3D, refer to MSDN: http://msdn.microsoft.com/zh-cn/library/ms7437.aspx.

 

1. Camera translation OR object translation:

WPFThe scene is mainly composed of two parts: camera and object. You can imagine yourself taking a video camera to shoot an object.

So there are two ways when translation occurs, the first is to move the camera, the second is to move the object. Relatively speaking, the camera translation is more complex and efficient, and the object translation efficiency is lower and the implementation is simple.

This article is an object translation, if you want to do camera translation friends can not read down.

 

2. Translational operation procedure in this project: 

1. When the program starts, you can see the whole 3D scene, similar to a sand table, at this time the rotation and other operations around the center of the sand table;

2. Double-click somewhere on the sand table to move the center of the sand table to the double-click position, at this time the rotation and other operations around the new center of the sand table.

 

3. Translation algorithm:

1.Getting the corresponding point of the mouse in 3D after double-clicking the screen mainly uses a principle of ray and 3D collision. The main application of the code is the VisualTreeHelper. HitTest function, which passes the result into a callback function, which we call HTResuLtCenter.

Copy code  

     Point mouseposition = args.GetPosition(ViewPort);

   MoveCenter(mouseposition);

}

 

 public void MoveCenter(Point mouseposition)
 {
      PointHitTestParameters pointparams = new PointHitTestParameters(mouseposition);
      VisualTreeHelper.HitTest(ViewPort, null, HTResultCenter, pointparams);

 } 

 

 private HitTestResultBehavior HTResultCenter(HitTestResult result)
 {
      RayHitTestResult rayResult = result as RayHitTestResult;
      if (rayResult != null)
      {
           //This is the coordinates in the 3D after clicking the mouse.
           var hitPoint = rayResult.PointHit;
           …

      }

 } 

Copy code  

2.According to the camera position + the camera’s projection direction = get the camera’s projection point on 3D, camera. Post is the camera’s position in the 3D world, camera. Look Direction is the direction the camera looks at. Then the two add up.You can get the location after this camera is projected.  

 

Copy code ////Camera position
  var cameraPostion = Camera.Position;
               
  ////Camera direction
  var lookDirection = Camera.LookDirection;

  /// Get the point where the camera is projected on 3D. 
  var x = cameraPostion.X + lookDirection.X;
  var y = cameraPostion.Y + lookDirection.Y;
  var z = cameraPostion.Z + lookDirection.Z;

Copy code  

 

3. Then you can use the camera’s projection position – the mouse’s double-click position to get the amount of offset the object should have, which is already in WPFTransform3D This thing translates.
The animation DoubleAnimation is used here, so there is a lot of code.

Copy code new DoubleAnimation();
doubleAnimationX.BeginTime = new TimeSpan(000);
doubleAnimationX.Duration = TimeSpan.FromMilliseconds(500);
doubleAnimationX.From = Transform3D.OffsetX;
doubleAnimationX.To = x – hitPoint.X;

DoubleAnimation doubleAnimationY = new DoubleAnimation();
doubleAnimationY.BeginTime = new TimeSpan(000);
doubleAnimationY.Duration = TimeSpan.FromMilliseconds(500);
doubleAnimationY.From = _Transform3D.OffsetY;
doubleAnimationY.To = y – hitPoint.Y;

DoubleAnimation doubleAnimationZ = new DoubleAnimation();
doubleAnimationZ.BeginTime = new TimeSpan(000);
doubleAnimationZ.Duration = TimeSpan.FromMilliseconds(500);
doubleAnimationZ.From = Transform3D.OffsetZ;
doubleAnimationZ.To = z – hitPoint.Z;

Transform3D.BeginAnimation(TranslateTransform3D.OffsetXProperty, doubleAnimationX);
Transform3D.BeginAnimation(TranslateTransform3D.OffsetYProperty, doubleAnimationY);  

Copy code Transform3D.BeginAnimation(TranslateTransform3D.OffsetZProperty, doubleAnimationZ);  

 

 

4. Final effect:

1.Before the Translation:

 

 2.After the Translation:

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *