private void RotateCube(Direction direction) { if (!_isRotating) { // Let the quaternion animation figure out how to transform between 2 quaternions... QuaternionAnimation animation = new QuaternionAnimation(); // A quaternion is way of representing a rotation around an axis // The From quaternion is the one required to display the current cube side based on the original side being the 'front' animation.From = _possibleRotationMatrix[_currentCubeRotation.CubeSide][Direction.None].Quaternion; // The To quaternion is the one required to display the next cube side based on the original side being the 'front' animation.To = _possibleRotationMatrix[_currentCubeRotation.CubeSide][direction].Quaternion; animation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 650)); _isRotating = true; animation.Completed += (o, e) => { _isRotating = false; ToggleVisualHitTesting(true); _currentCubeRotation = _possibleRotationMatrix[_currentCubeRotation.CubeSide][direction]; }; // Temporarily remove hit testing to make things a but smoother ToggleVisualHitTesting(false); this.CameraRotation.BeginAnimation(QuaternionRotation3D.QuaternionProperty, animation); } }
public void GetTotalDurationTest() { var animation = new AnimationClip <float> { Animation = new SingleFromToByAnimation { From = 100, To = 200, Duration = TimeSpan.FromSeconds(6.0), }, Delay = TimeSpan.FromSeconds(10), Speed = 2, FillBehavior = FillBehavior.Hold, }; var animation2 = new AnimationClip <float> { Animation = new SingleFromToByAnimation { From = 10, To = 20, Duration = TimeSpan.FromSeconds(5.0), }, Delay = TimeSpan.Zero, Speed = 1, FillBehavior = FillBehavior.Hold, }; var animationEx = new QuaternionAnimation(); Assert.AreEqual(TimeSpan.FromSeconds(0.0), animationEx.GetTotalDuration()); animationEx = new QuaternionAnimation(); animationEx.W = animation; Assert.AreEqual(TimeSpan.FromSeconds(13.0), animationEx.GetTotalDuration()); animationEx = new QuaternionAnimation(); animationEx.X = animation; Assert.AreEqual(TimeSpan.FromSeconds(13.0), animationEx.GetTotalDuration()); animationEx = new QuaternionAnimation(); animationEx.Y = animation; Assert.AreEqual(TimeSpan.FromSeconds(13.0), animationEx.GetTotalDuration()); animationEx = new QuaternionAnimation(); animationEx.Z = animation; Assert.AreEqual(TimeSpan.FromSeconds(13.0), animationEx.GetTotalDuration()); animationEx = new QuaternionAnimation(); animationEx.W = animation; animationEx.X = animation2; Assert.AreEqual(TimeSpan.FromSeconds(13.0), animationEx.GetTotalDuration()); animationEx = new QuaternionAnimation(); animationEx.Y = animation2; animationEx.Z = animation; Assert.AreEqual(TimeSpan.FromSeconds(13.0), animationEx.GetTotalDuration()); }
private void Window_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { Point currentPosition = e.GetPosition(Cube); Point realDifference = new Point(previousMousePosition.X - currentPosition.X, previousMousePosition.Y - currentPosition.Y); Point difference = new Point(Math.Abs(previousMousePosition.X - currentPosition.X), Math.Abs(previousMousePosition.Y - currentPosition.Y)); var swipe = difference.X > difference.Y ? (Difference : difference.X, Axis : "X") : (Difference : difference.Y, Axis : "Y"); if (swipe.Difference > 100) { double angle = swipe.Axis == "X" ? realDifference.X : realDifference.Y; if (angle < 0) { angle = 270; //поворот влево либо вверх (-90 градусов) реализован поворотом вправо/вниз на 3 грани (270 градусов) во избежания отрицательных значений и ошибки } else { angle = 90; } Vector3D axis = swipe.Axis == "X" ? new Vector3D(0, 1, 0) : new Vector3D(1, 0, 0); Quaternion newPosition; Console.WriteLine($"Current axis {currentCubePosition.Axis}"); Console.WriteLine($"Current angle {currentCubePosition.Angle}"); if (currentCubePosition.Axis != axis) { newPosition = new Quaternion(axis, angle); } else { newPosition = new Quaternion(axis, currentCubePosition.Angle + angle); } if (currentCubePosition.Axis.X == 1 && currentCubePosition.Angle == 90 && newPosition.Axis.X == 1 && newPosition.Angle == 180) { Console.WriteLine("bot to back"); newPosition = new Quaternion(new Vector3D(0, 1, 0), 180); } if (currentCubePosition.Axis.X == 1 && currentCubePosition.Angle == 270 && newPosition.Axis.X == 1 && newPosition.Angle == 180) { Console.WriteLine("top to back"); newPosition = new Quaternion(new Vector3D(0, 1, 0), 180); } var rotation = new QuaternionAnimation(currentCubePosition, newPosition, new Duration(TimeSpan.FromSeconds(1))); currentCubePosition = newPosition; CameraRotation.BeginAnimation(QuaternionRotation3D.QuaternionProperty, rotation); } }
public CameraTrackingSwitcher() { MarkerTracker = new ArMarkerCamTracker(); VrTracker = new DelayedVrCamTracker(); OptitrackTracker = new OptitrackCamTracker(); CurrentMode = TrackingMode.Optitrack; _positionAnimation = new VectorAnimation(SwitchTransitionSpeed); _rotationAnimation = new QuaternionAnimation(SwitchTransitionSpeed); _positionAnimation.Finished += AnimationFinished; _rotationAnimation.Finished += AnimationFinished; }
private void Window_Loaded(object sender, RoutedEventArgs e) { myPCamera.FarPlaneDistance = 20; myPCamera.NearPlaneDistance = 1; myPCamera.FieldOfView = 45; myPCamera.Position = new Point3D(-5, 2, 3); myPCamera.LookDirection = new Vector3D(5, -2, -3); myPCamera.UpDirection = new Vector3D(0, 1, 0); //Add light sources to the scene. myDirLight.Color = Colors.White; myDirLight.Direction = new Vector3D(-3, -4, -5); teapotModel.Geometry = (MeshGeometry3D)Application.Current.Resources["myTeapot"]; //Define material and apply to the mesh geometries. DiffuseMaterial teapotMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Tomato)); teapotModel.Material = teapotMaterial; RotateTransform3D myRotateTransform3D = new RotateTransform3D(); Quaternion q1 = new Quaternion(new Vector3D(0, 1, 0), 45); QuaternionRotation3D myQuaternionRotation3D1 = new QuaternionRotation3D(q1); myRotateTransform3D.Rotation = myQuaternionRotation3D1; teapotModel.Transform = myRotateTransform3D; QuaternionAnimation fst = new QuaternionAnimation(new Quaternion(new Vector3D(0, 1, 0), -180), new Quaternion(new Vector3D(0, 1, 0), 180), new Duration(TimeSpan.FromSeconds(1))); fst.RepeatBehavior = RepeatBehavior.Forever; fst.UseShortestPath = false; myRotateTransform3D.Rotation.BeginAnimation(QuaternionRotation3D.QuaternionProperty, fst); ColorAnimationUsingKeyFrames frameAnimation = new ColorAnimationUsingKeyFrames(); modelGroup.Children.Add(teapotModel); modelGroup.Children.Add(myDirLight); ModelVisual3D modelsVisual = new ModelVisual3D(); modelsVisual.Content = modelGroup; //Add the visual and camera to the Viewport3D. myViewport.Camera = myPCamera; myViewport.Children.Add(modelsVisual); this.Content = myViewport; }
void Rotate(ref Viewport2DVisual3D item, bool isFront, bool isLast, double centerX, double centerY, double zOffset) { Transform3DGroup trans = new Transform3DGroup(); RotateTransform3D rot = new RotateTransform3D(); rot.CenterX = centerX; rot.CenterY = centerY; rot.CenterZ = zOffset; QuaternionRotation3D quatRot = new QuaternionRotation3D(); QuaternionAnimation anim = new QuaternionAnimation(); //anim.AutoReverse = true; //anim.RepeatBehavior = new RepeatBehavior(4);// RepeatBehavior.Forever; anim.Duration = RotationLength; if (isFront == true) { quatRot.Quaternion = new Quaternion(new Vector3D(0, 1, 0), 0); anim.From = new Quaternion(new Vector3D(0, 1, 0), 0); anim.To = new Quaternion(new Vector3D(0, 1, 0), 180); anim.BeginTime = TimeSpan.FromSeconds(frontAnimationsOffsetCount); frontAnimationsOffsetCount = frontAnimationsOffsetCount + animationsOffsetSeconds; } else { quatRot.Quaternion = new Quaternion(new Vector3D(0, 1, 0), -180); anim.From = new Quaternion(new Vector3D(0, 1, 0), -180); anim.To = new Quaternion(new Vector3D(0, 1, 0), 0); anim.BeginTime = TimeSpan.FromSeconds(backAnimationsOffsetCount); backAnimationsOffsetCount = backAnimationsOffsetCount + animationsOffsetSeconds; } if (isLast == true) { anim.Completed -= new EventHandler(anim_Completed); anim.Completed += new EventHandler(anim_Completed); } rot.Rotation = quatRot; trans.Children.Add(rot); item.Transform = trans; quatRot.BeginAnimation(QuaternionRotation3D.QuaternionProperty, anim); }
internal static void AddRotationAnimation(Storyboard storyboard, TimeSpan animationTime, TimeSpan beginTime, double angleOfRotation, Vector3D axisOfRotation, Quaternion currentRotationQuaternion) { Quaternion delta = new Quaternion(axisOfRotation, angleOfRotation); Quaternion newRotation = currentRotationQuaternion * delta; QuaternionAnimation rotationAnimation = new QuaternionAnimation(newRotation, new Duration(animationTime)); rotationAnimation.AccelerationRatio = 0.5; rotationAnimation.DecelerationRatio = 0.5; rotationAnimation.BeginTime = beginTime; Storyboard.SetTargetName(rotationAnimation, "viewportCamera"); //Property path for the animation target is going to be the Transform property's Children collection. Specifically the item at index 1 (the first item is the zoom transform), //which is our rotation transform, and on it we want to animate the Rotation property's Quarternion property...whew, that was confusing :) PropertyPath path = new PropertyPath("(0).(1)[1].(2).(3)", PerspectiveCamera.TransformProperty, Transform3DGroup.ChildrenProperty, RotateTransform3D.RotationProperty, QuaternionRotation3D.QuaternionProperty); Storyboard.SetTargetProperty(rotationAnimation, path); storyboard.Children.Add(rotationAnimation); }
public void GetValueTest() { var animation = new AnimationClip <float> { Animation = new SingleFromToByAnimation { From = 100, To = 200, Duration = TimeSpan.FromSeconds(6.0), }, Delay = TimeSpan.FromSeconds(10), Speed = 2, FillBehavior = FillBehavior.Hold, }; var animation2 = new AnimationClip <float> { Animation = new SingleFromToByAnimation { From = 10, To = 20, Duration = TimeSpan.FromSeconds(5.0), }, Delay = TimeSpan.FromSeconds(0), Speed = 1, FillBehavior = FillBehavior.Hold, }; var animation3 = new AnimationClip <float> { Animation = new SingleFromToByAnimation { From = 5, To = -5, Duration = TimeSpan.FromSeconds(10), }, Delay = TimeSpan.FromSeconds(5), Speed = 1, FillBehavior = FillBehavior.Hold, }; var animation4 = new AnimationClip <float> { Animation = new SingleFromToByAnimation { From = 1000, To = 1100, Duration = TimeSpan.FromSeconds(5.0), }, Delay = TimeSpan.FromSeconds(5), Speed = 1, FillBehavior = FillBehavior.Stop, }; var animationEx = new QuaternionAnimation { W = animation, X = animation2, Y = animation3, Z = animation4, }; var defaultSource = new Quaternion(1, 2, 3, 4); var defaultTarget = new Quaternion(5, 6, 7, 8); var result = animationEx.GetValue(TimeSpan.FromSeconds(0.0), defaultSource, defaultTarget); Assert.AreEqual(defaultSource.W, result.W); // animation has not started. Assert.AreEqual(10.0f, result.X); // animation2 has started. Assert.AreEqual(defaultSource.Y, result.Y); // animation3 has not started. Assert.AreEqual(defaultSource.Z, result.Z); // animation4 has not started. result = animationEx.GetValue(TimeSpan.FromSeconds(5.0), defaultSource, defaultTarget); Assert.AreEqual(defaultSource.W, result.W); // animation has not started. Assert.AreEqual(20.0f, result.X); // animation2 has ended. Assert.AreEqual(5, result.Y); // animation3 has started. Assert.AreEqual(1000, result.Z); // animation4 has started. result = animationEx.GetValue(TimeSpan.FromSeconds(5.0), defaultSource, defaultTarget); Assert.AreEqual(defaultSource.W, result.W); // animation has not started. Assert.AreEqual(20.0f, result.X); // animation2 has ended. Assert.AreEqual(5, result.Y); // animation3 has started. Assert.AreEqual(1000, result.Z); // animation4 has started. result = animationEx.GetValue(TimeSpan.FromSeconds(13.0), defaultSource, defaultTarget); Assert.AreEqual(200, result.W); // animation has ended. Assert.AreEqual(20.0f, result.X); // animation2 is filling. Assert.AreEqual(-3, result.Y); // animation3 is active. Assert.AreEqual(defaultSource.Z, result.Z); // animation4 is stopped. }
public void TraitsTest() { var animationEx = new QuaternionAnimation(); Assert.AreEqual(QuaternionTraits.Instance, animationEx.Traits); }
private void Window_Loaded(object sender, RoutedEventArgs e) { myPCamera.FarPlaneDistance = 20; myPCamera.NearPlaneDistance = 1; myPCamera.FieldOfView = 45; myPCamera.Position = new Point3D(-5, 2, 3); myPCamera.LookDirection = new Vector3D(5, -2, -3); myPCamera.UpDirection = new Vector3D(0, 1, 0); //Add light sources to the scene. myDirLight.Color = Colors.White; myDirLight.Direction = new Vector3D(-3, -4, -5); teapotModel.Geometry = (MeshGeometry3D)Application.Current.Resources["myTeapot"]; //Define material and apply to the mesh geometries. var brush = new SolidColorBrush(Colors.Tomato); DiffuseMaterial diffuseMaterial = new DiffuseMaterial(brush); var specularMaterial = new SpecularMaterial(); var materials = new MaterialGroup(); materials.Children.Add(specularMaterial); materials.Children.Add(diffuseMaterial); teapotModel.Material = materials; RotateTransform3D myRotateTransform3D = new RotateTransform3D(); Quaternion q1 = new Quaternion(new Vector3D(0, 1, 0), 45); QuaternionRotation3D myQuaternionRotation3D1 = new QuaternionRotation3D(q1); myRotateTransform3D.Rotation = myQuaternionRotation3D1; teapotModel.Transform = myRotateTransform3D; QuaternionAnimation fst = new QuaternionAnimation( new Quaternion(new Vector3D(0, 1, 0), -180), new Quaternion(new Vector3D(0, 1, 0), 180), new Duration(TimeSpan.FromSeconds(4))); fst.RepeatBehavior = RepeatBehavior.Forever; fst.UseShortestPath = false; myRotateTransform3D.Rotation.BeginAnimation(QuaternionRotation3D.QuaternionProperty, fst); modelGroup.Children.Add(teapotModel); modelGroup.Children.Add(myDirLight); ModelVisual3D modelsVisual = new ModelVisual3D(); modelsVisual.Content = modelGroup; // Animate brush with color var animation = new ColorAnimationUsingKeyFrames(); animation.KeyFrames.Add(new DiscreteColorKeyFrame(Colors.Blue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)))); animation.KeyFrames.Add(new DiscreteColorKeyFrame(Colors.SlateBlue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)))); animation.KeyFrames.Add(new LinearColorKeyFrame(Colors.Red, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)))); animation.KeyFrames.Add(new LinearColorKeyFrame(Colors.Aqua, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)))); animation.KeyFrames.Add(new LinearColorKeyFrame(Colors.CadetBlue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)))); animation.KeyFrames.Add(new LinearColorKeyFrame(Colors.DarkGoldenrod, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)))); brush.BeginAnimation(SolidColorBrush.ColorProperty, animation, HandoffBehavior.Compose); animation.RepeatBehavior = RepeatBehavior.Forever; //Add the visual and camera to the Viewport3D. myViewport.Camera = myPCamera; myViewport.Children.Add(modelsVisual); myViewport.Children.Add(new ModelVisual3D() { Content = new DirectionalLight() }); this.Content = myViewport; }