/// <summary> /// Animates the camera position and direction /// </summary> /// <param name="camera">Camera</param> /// <param name="newPosition">The position to animate to</param> /// <param name="newDirection">The direction to animate to</param> /// <param name="newUpDirection">The up direction to animate to</param> /// <param name="animationTime">Animation time in milliseconds</param> public static void AnimateTo(ProjectionCamera camera, Point3D newPosition, Vector3D newDirection, Vector3D newUpDirection, double animationTime) { var fromPosition = camera.Position; var fromDirection = camera.LookDirection; var fromUpDirection = camera.UpDirection; camera.Position = newPosition; camera.LookDirection = newDirection; camera.UpDirection = newUpDirection; if (animationTime > 0) { var a1 = new Point3DAnimation(fromPosition, newPosition, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; camera.BeginAnimation(ProjectionCamera.PositionProperty, a1); var a2 = new Vector3DAnimation(fromDirection, newDirection, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; camera.BeginAnimation(ProjectionCamera.LookDirectionProperty, a2); var a3 = new Vector3DAnimation(fromUpDirection, newUpDirection, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; camera.BeginAnimation(ProjectionCamera.UpDirectionProperty, a3); } }
public ExObjectView(List <KeyValuePair <Node, double> > obj, double Width, double Length, double Height) { InitializeComponent(); RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 1), 180)); Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(0, 0, 0.5), new Duration(TimeSpan.FromSeconds(40))); myVectorAnimation.AccelerationRatio = 0.1; myVectorAnimation.DecelerationRatio = 0.1; myVectorAnimation.RepeatBehavior = RepeatBehavior.Forever; myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AxisProperty, myVectorAnimation); objTransformGroup.Children.Add(myRotateTransform); foreach (var node in obj) { double width = (node.Key.IsIntermediate) ? 0.05 : 0.1; byte opacity = (node.Key.IsIntermediate) ? (byte)100 : (byte)255; double preasure = Math.Abs(node.Value); byte scaleCol = (byte)((preasure * 400 > 200) ? 200 : preasure * 400); Model3DGroup cube = GetCubeMode(node.Key.X - (Width / 2), (node.Key.Z - (Height / 2)) * (-1), node.Key.Y - (Length / 2), width, Color.FromArgb(opacity, (byte)(55 + scaleCol), (byte)(200 - scaleCol), 0)); objGroup.Children.Add(cube); } }
public void Zoom() { DoubleAnimation da = new DoubleAnimation(20, TimeSpan.FromSeconds(2)); Vector3D d = new Point3D(0, 0, 0) - parent.EC_Cam.Position; Vector3DAnimation va = new Vector3DAnimation(d, TimeSpan.FromSeconds(2)); parent.EC_Cam.BeginAnimation(PerspectiveCamera.LookDirectionProperty, va); parent.EC_Cam.BeginAnimation(PerspectiveCamera.FieldOfViewProperty, da); }
// animacija preko frameova public void KamAnimiraj(KamFrame f, double trajanje) { if (f != null) { Duration dur = new Duration(TimeSpan.FromMilliseconds(trajanje)); Point3D kamRotKut_trenutno = KameraRotacijaKuteviXYZ_dp; Point3D kamRotKut_frame = f.KameraRotacijaKuteviXYZ; Point3D kamRotKut_najkraci = f.KameraRotacijaKuteviXYZ; // ako treba napraviti rotaciju za vise od pola kruga vrti u suprotnom smjeru double z = kamRotKut_frame.Z - kamRotKut_trenutno.Z; if (z > 180) { kamRotKut_najkraci.Z = kamRotKut_frame.Z - 360; } else if (z < -180) { kamRotKut_najkraci.Z = kamRotKut_frame.Z + 360; } double x = kamRotKut_frame.X - kamRotKut_trenutno.X; if (x > 180) { kamRotKut_najkraci.X = kamRotKut_frame.X - 360; } else if (x < -180) { kamRotKut_najkraci.X = kamRotKut_frame.X + 360; } double y = kamRotKut_frame.Y - kamRotKut_trenutno.Y; if (y > 180) { kamRotKut_najkraci.Y = kamRotKut_frame.Y - 360; } else if (y < -180) { kamRotKut_najkraci.Y = kamRotKut_frame.Y + 360; } Point3DAnimation anim_rot = new Point3DAnimation(kamRotKut_najkraci, dur, FillBehavior.HoldEnd); Vector3DAnimation anim_trans = new Vector3DAnimation(f.KameraTranslacija, dur, FillBehavior.HoldEnd); DoubleAnimation anim_zoom = new DoubleAnimation(f.KameraZoom, dur, FillBehavior.HoldEnd); // buduci da imaju isti duration dovoljno je da jedna animacija digne event anim_zoom.Completed += new EventHandler(anim_Completed); //FLAG_bKameraAnimating = true; this.BeginAnimation(KameraTranslacijaProperty, anim_trans); this.BeginAnimation(KameraRotacijaKuteviXYZProperty, anim_rot); this.BeginAnimation(KameraZoom_dpProperty, anim_zoom); } }
/// <summary> /// Animates the camera position and directions. /// </summary> /// <param name="camera"> /// The camera to animate. /// </param> /// <param name="newPosition"> /// The position to animate to. /// </param> /// <param name="newDirection"> /// The direction to animate to. /// </param> /// <param name="newUpDirection"> /// The up direction to animate to. /// </param> /// <param name="animationTime"> /// Animation time in milliseconds. /// </param> public static void AnimateTo( this Camera camera, Point3D newPosition, Vector3D newDirection, Vector3D newUpDirection, double animationTime) { var projectionCamera = camera as ProjectionCamera; if (projectionCamera == null) { return; } var fromPosition = projectionCamera.Position; var fromDirection = projectionCamera.LookDirection; var fromUpDirection = projectionCamera.UpDirection; projectionCamera.Position = newPosition; projectionCamera.LookDirection = newDirection; projectionCamera.UpDirection = newUpDirection; if (animationTime > 0) { var a1 = new Point3DAnimation( fromPosition, newPosition, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; a1.Completed += (s, a) => { camera.BeginAnimation(ProjectionCamera.PositionProperty, null); }; camera.BeginAnimation(ProjectionCamera.PositionProperty, a1); var a2 = new Vector3DAnimation( fromDirection, newDirection, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; a2.Completed += (s, a) => { camera.BeginAnimation(ProjectionCamera.LookDirectionProperty, null); }; camera.BeginAnimation(ProjectionCamera.LookDirectionProperty, a2); var a3 = new Vector3DAnimation( fromUpDirection, newUpDirection, new Duration(TimeSpan.FromMilliseconds(animationTime))) { AccelerationRatio = 0.3, DecelerationRatio = 0.5, FillBehavior = FillBehavior.Stop }; a3.Completed += (s, a) => { camera.BeginAnimation(ProjectionCamera.UpDirectionProperty, null); }; camera.BeginAnimation(ProjectionCamera.UpDirectionProperty, a3); } }
public void Turn(LookDirection dir) { CurrentLookDirection = dir; var anim = new Vector3DAnimation(GetLookDirectionVector(dir), new System.Windows.Duration(TimeSpan.FromMilliseconds(GlobalSettingsManager.Global.TurnSpeed))); anim.Completed += Anim_Completed; IsAnimated = true; BeginAnimation(PerspectiveCamera.PositionProperty, anim); }
///<summary>计算当前状态下合适观察的相机位置</summary> public void moveCamera() { Point3D camPos, pcenter, orgPos; Vector3D camLookDir, camUpDir; Duration duration = TimeSpan.FromSeconds(1); savePos = camera.Position; saveLookDir = camera.LookDirection; saveUpDir = camera.UpDirection; //取得占用空间,计算中心点 double x, y, z; x = curSettle.RectVec.X; y = curSettle.RectVec.Y; z = curSettle.RectVec.Z; pcenter = new Point3D(x / 2, 0, z / 4); //if (x <= para.Limit.X) pcenter = new Point3D(0, pcenter.Y, pcenter.Z); //if (z <= para.Limit.Z) pcenter = new Point3D(pcenter.X, pcenter.Y, 0); if (z < y * 2) z = y * 2; if (z < x / 2) z = x / 2; matrix = Matrix3D.Identity; orgPos = new Point3D(pcenter.X, 0, z + y * 6.5); matrix.Translate(orgPos - savePos); matrix.RotateAt(new Quaternion(new Vector3D(1, 0, 0), -30), pcenter); matrix.RotateAt(new Quaternion(new Vector3D(0, 1, 0), 30), pcenter); camPos = matrix.Transform(orgPos); camLookDir = matrix.Transform(new Vector3D(0, 0, -1)); camUpDir = matrix.Transform(new Vector3D(0, 1, 0)); camPos = matrix.Transform(savePos); camLookDir = matrix.Transform(saveLookDir); camUpDir = matrix.Transform(saveUpDir); camera.Position = (camera.Transform as MatrixTransform3D).Transform(camera.Position); camera.LookDirection = (camera.Transform as MatrixTransform3D).Transform(camera.LookDirection); camera.UpDirection = (camera.Transform as MatrixTransform3D).Transform(camera.UpDirection); (camera.Transform as MatrixTransform3D).Matrix = Matrix3D.Identity; Point3DAnimation anipos = new Point3DAnimation(camera.Position, camPos, duration, FillBehavior.Stop); anipos.Completed += new EventHandler(anipos_Completed); Vector3DAnimation anilookdir = new Vector3DAnimation(camera.LookDirection, camLookDir, duration, FillBehavior.Stop); Vector3DAnimation aniupdir = new Vector3DAnimation(camera.UpDirection, camUpDir, duration, FillBehavior.Stop); camera.Position = camPos; camera.LookDirection = camLookDir; camera.UpDirection = camUpDir; camera.BeginAnimation(PerspectiveCamera.PositionProperty, anipos); camera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, anilookdir); camera.BeginAnimation(PerspectiveCamera.UpDirectionProperty, aniupdir); }
//组合立方体并旋转 private void SetCubeRound() { ModelVisual3D myModelVisual = new ModelVisual3D(); //实例化三维模型呈现对象 myCamera.FarPlaneDistance = 20; //设置到摄像机远端剪裁平面的摄像机的距离 myCamera.NearPlaneDistance = 0; //设置到摄像机近端剪裁平面的摄像机的距离 myCamera.FieldOfView = 30; //设置摄像机的水平视角 myCamera.Position = new Point3D(-5, 2, 3); //设置以世界坐标表示的摄像机位置 myCamera.LookDirection = new Vector3D(5, -2, -3); //设置摄像机在世界坐标中的拍摄方向的三维空间置换 myCamera.UpDirection = new Vector3D(0, 1, 0); //设置摄像机向上方向的三维空间置换 //为6个三维模型赋值,值为对应的6个三角基元 side1.Geometry = baseside1; side2.Geometry = baseside2; side3.Geometry = baseside3; side4.Geometry = baseside4; side5.Geometry = baseside5; side6.Geometry = baseside6; //实例化旋转转换对象 RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 1)); //实例化动画处理对象 DoubleAnimation myAnimation = new DoubleAnimation(); myAnimation.From = 1; //设置动画的起始值 myAnimation.To = 360; //设置动画的结束值 myAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000)); //设置动画播放的时间长度 myAnimation.RepeatBehavior = RepeatBehavior.Forever; //设置动画的重复行为 //实例化三维动画处理对象 Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(-1, -1, -1), new Duration(TimeSpan.FromMilliseconds(3000))); myVectorAnimation.RepeatBehavior = RepeatBehavior.Forever;//设置三维动画的重复行为 //以普通动画的形式开始旋转 myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, myAnimation); //以三维动画的形式开始旋转 myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AxisProperty, myVectorAnimation); //将旋转转换添加到三维图形组转换对象中 transform3D.Children.Add(myRotateTransform); //将6个面添加到一个3D模型中 cubeModel.Children.Add(side1); cubeModel.Children.Add(side2); cubeModel.Children.Add(side3); cubeModel.Children.Add(side4); cubeModel.Children.Add(side5); cubeModel.Children.Add(side6); cubeModel.Transform = transform3D; //设置3D模型的转换方式 cubeModel.Children.Add(myLight); //设置3D模型的灯光 myViewport.Camera = myCamera; //设置三维呈现图面的摄像机 myModelVisual.Content = cubeModel; //设置三维模型呈现的对象为立方体 myViewport.Children.Add(myModelVisual); //将旋转的立方体添加到三维呈现图面中 MainWindow.Content = myViewport; //将三维呈现图面显示在窗体中 }
/// <summary> /// /// </summary> /// <param name="from"></param> /// <param name="to"></param> private void RotateBetween(Vector3D from, Vector3D to) { Vector3D axisOfRotation = Vector3D.CrossProduct(from, to); double num = Vector3D.AngleBetween(from, to); Quaternion quaternion = new Quaternion(axisOfRotation, -num); Quaternion quaternion2 = new Quaternion(_rotation.Axis, _rotation.Angle); quaternion2 *= quaternion; new Storyboard(); Vector3DAnimation animation = new Vector3DAnimation(_rotation.Axis, quaternion2.Axis, new Duration(new TimeSpan(0, 0, 0, 0, 500))); DoubleAnimation animation2 = new DoubleAnimation(_rotation.Angle, quaternion2.Angle, new Duration(new TimeSpan(0, 0, 0, 0, 500))); _rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation2); _rotation.BeginAnimation(AxisAngleRotation3D.AxisProperty, animation); }
public void Zoom() { Vector3D d = new Vector3D(0, 0, -1); Vector3DAnimation va = new Vector3DAnimation(d, TimeSpan.FromSeconds(3)); Point3D p = GetRoundKeyPosition(0); p.Z += 30; p.X -= 7; p.Y -= 7; Point3DAnimation pa = new Point3DAnimation(p, TimeSpan.FromSeconds(3)); DoubleAnimation da = new DoubleAnimation(opacity_high, TimeSpan.FromSeconds(2)); parent.KS_Cam.BeginAnimation(PerspectiveCamera.PositionProperty, pa); parent.KS_Cam.BeginAnimation(PerspectiveCamera.LookDirectionProperty, va); brush[0, 0].BeginAnimation(Brush.OpacityProperty, da); brush[1, 0].BeginAnimation(Brush.OpacityProperty, da); parent.lbl_KS_Text.Content = Properties.Resources.Finished_generating_32_roundkeys; parent.lbl_KS_Step.Content = Properties.Resources.Finished; }
public void AnimateScene(int i) { OriginAnimation = new Point3DAnimation(); OriginAnimation.From = this.OriginRect; OriginAnimation.To = new Point3D(DictionaryValuesRectangle.XFixedFromIndex[this.IndexOfRectangle + i], 0, DictionaryValuesRectangle.ZFixedFromIndex[this.IndexOfRectangle + i]); OriginAnimation.Duration = new Duration(TimeSpan.FromSeconds(DurationOfAnimation)); OriginAnimation.EasingFunction = easingFunction; NormalAnimation = new Vector3DAnimation(); NormalAnimation.From = this.NormalRect; NormalAnimation.To = new Vector3D(DictionaryValuesRectangle.XNormalFixedFromIndex[this.IndexOfRectangle + i], 0, 1); NormalAnimation.Duration = new Duration(TimeSpan.FromSeconds(DurationOfAnimation)); NormalAnimation.EasingFunction = easingFunction; //Checking if the animation is completed for the property release //needs to be placed BEFORE the beginning of animation, else never reached OriginAnimation.Completed += OriginAnimation_Completed; NormalAnimation.Completed += NormalAnimation_Completed; this.BeginAnimation(RectangleForCarousel3D.OriginProperty, OriginAnimation); this.BeginAnimation(RectangleForCarousel3D.NormalProperty, NormalAnimation); this.IndexOfRectangle = this.IndexOfRectangle + i; //Apply the new values to the rectangle this.OriginRect.X = DictionaryValuesRectangle.XFixedFromIndex[this.IndexOfRectangle]; this.OriginRect.Y = 0; this.OriginRect.Z = DictionaryValuesRectangle.ZFixedFromIndex[this.IndexOfRectangle]; this.Origin = this.OriginRect; this.NormalRect.X = DictionaryValuesRectangle.XNormalFixedFromIndex[this.IndexOfRectangle]; this.NormalRect.Y = 0; this.NormalRect.Z = 1; this.XPosition = DictionaryValuesRectangle.XFixedFromIndex[this.IndexOfRectangle]; this.ZPosition = DictionaryValuesRectangle.ZFixedFromIndex[this.IndexOfRectangle]; this.RotationX = DictionaryValuesRectangle.XNormalFixedFromIndex[this.IndexOfRectangle]; }
public NodeVisualizationWindow() { InitializeComponent(); //nv = Node Visualization Viewport3D nvViewport = new Viewport3D(); Model3DGroup nvModel3DGroup = new Model3DGroup(); GeometryModel3D nvGeometryModel = new GeometryModel3D(); ModelVisual3D nvModelVisual3D = new ModelVisual3D(); // Defines the camera used to view the 3D object - the 3D object representing our sensor. PerspectiveCamera nodeCamera = new PerspectiveCamera(); // Camera is two units above the XY plane nodeCamera.Position = new Point3D(0, 0, 4); // Camera is look down at the XY plane nodeCamera.LookDirection = new Vector3D(0, 0, -1); // The Camera's FOV - will need adjusted nodeCamera.FieldOfView = 60; // Asign the camera to the viewport nvViewport.Camera = nodeCamera; AmbientLight _ambLight = new AmbientLight(System.Windows.Media.Brushes.White.Color); RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 20)); Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(-1, -1, -1), new Duration(TimeSpan.FromMilliseconds(5000))); myVectorAnimation.RepeatBehavior = RepeatBehavior.Forever; myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AxisProperty, myVectorAnimation); cube1TransformGroup.Children.Add(myRotateTransform); }
void ChangeUpDirection() { Vector3D newUpDirection = new Vector3D(0, 1, 0); if (this.currentUpDirection.X == 1) { newUpDirection = new Vector3D(0, 0, 1); } else if (this.currentUpDirection.Y == 1) { newUpDirection = new Vector3D(1, 0, 0); } else if (this.currentUpDirection.Z == 1) { newUpDirection = new Vector3D(0, 1, 0); } PerspectiveCamera camera = this.mainViewport.Camera as PerspectiveCamera; Vector3DAnimation animation = new Vector3DAnimation(currentUpDirection, newUpDirection, TimeSpan.FromMilliseconds(1000)); camera.BeginAnimation(PerspectiveCamera.UpDirectionProperty, animation); this.currentUpDirection = newUpDirection; }
public void MovingView(object sender, RoutedEventArgs e) { _origCameraBeforeMovingView = Camera; var newCamera = _origCameraBeforeMovingView.Clone(); #region set new camera animation Viewport.Camera = newCamera; const int duration = 3; var currentPosition = newCamera.Position; var nextPosition = new Point3D { X = currentPosition.X, Y = currentPosition.Y + 5, Z = currentPosition.Z }; var positionAnimation = new Point3DAnimation(currentPosition, nextPosition, TimeSpan.FromSeconds(duration)); positionAnimation.Completed += PositionAnimationCompleted; var currentLookDirection = new Vector3D(0, -5, -1); var nextLookDirection = new Vector3D(-nextPosition.X, -nextPosition.Y, -nextPosition.Z); var lookAnimation = new Vector3DAnimation(currentLookDirection, nextLookDirection, TimeSpan.FromMilliseconds(duration)); var transform3DGroup = new Transform3DGroup(); var rotateTransform3D1 = new RotateTransform3D(); var axisAngleRotation3D1 = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0.0); rotateTransform3D1.Rotation = axisAngleRotation3D1; transform3DGroup.Children.Add(rotateTransform3D1); newCamera.Transform = transform3DGroup; var tl = new DoubleAnimation(360, TimeSpan.FromSeconds(duration)); axisAngleRotation3D1.BeginAnimation(AxisAngleRotation3D.AngleProperty, tl); newCamera.BeginAnimation(PerspectiveCamera.PositionProperty, positionAnimation); newCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, lookAnimation); #endregion }
public ExObjectView(ExObject obj) { InitializeComponent(); RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 1), 180)); Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(0, 0, 0.5), new Duration(TimeSpan.FromSeconds(40))); myVectorAnimation.AccelerationRatio = 0.1; myVectorAnimation.DecelerationRatio = 0.1; myVectorAnimation.RepeatBehavior = RepeatBehavior.Forever; myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AxisProperty, myVectorAnimation); objTransformGroup.Children.Add(myRotateTransform); foreach (var node in obj.AKT) { double width = (node.IsIntermediate) ? 0.05 : 0.1; byte opacity = (node.IsIntermediate) ? (byte)100 : (byte)255; Model3DGroup cube = GetCubeMode(node.X - (obj.Width / 2), (node.Z - (obj.Height / 2)) * (-1), node.Y - (obj.Length / 2), width, Color.FromArgb(opacity, 55, 200, 0)); objGroup.Children.Add(cube); } }
private void Load3DImages() { Model3DGroup myModel3DGroup = new Model3DGroup(); ModelVisual3D myModelVisual3D = new ModelVisual3D(); AmbientLight light = new AmbientLight(Color.FromRgb(96, 96, 96)); myModel3DGroup.Children.Add(light); /*PointLight light2 = new PointLight(Colors.White, new Point3D(0, 0, 0.3)); * myModel3DGroup.Children.Add(light2); * Point3DAnimation daLight = new Point3DAnimation(new Point3D(-10, 0, 0.3), new Point3D(10, 0, 0.3), TimeSpan.FromMilliseconds(25000).Duration()); * light2.BeginAnimation(PointLight.PositionProperty, daLight);*/ SpotLight light3 = new SpotLight(Colors.Red, new Point3D(0, 0, 10), new Vector3D(0, 0, -1), 90, 0); myModel3DGroup.Children.Add(light3); Vector3DAnimation daLight = new Vector3DAnimation(new Vector3D(-1, 0, -0.3), new Vector3D(1, 0, -0.3), TimeSpan.FromMilliseconds(15000).Duration()); daLight.RepeatBehavior = RepeatBehavior.Forever; daLight.AutoReverse = true; light3.BeginAnimation(SpotLight.DirectionProperty, daLight); /*ColorAnimation colorAnimation = new ColorAnimation(Colors.Red, Color.FromRgb(0, 0, 255), TimeSpan.FromSeconds(10).Duration()); * colorAnimation.RepeatBehavior = RepeatBehavior.Forever; * colorAnimation.AutoReverse = true; * light3.BeginAnimation(SpotLight.ColorProperty, colorAnimation);*/ myModelVisual3D.Content = myModel3DGroup; this.ImageWall3D.Children.Add(myModelVisual3D); allFiles = dataBase.GetAllFrontCovers(true); if (allFiles.Length == 0) { return; } // Shuffle allFiles = allFiles.OrderBy(file => random.Next()).ToArray(); int imageIndex = 0; double xPosStart = -(double)imagesHorizontal / 2.0 - 31.415; double xPos = xPosStart; double yPos = (double)imagesVertical / 2.0; int xIndex = 0; int yIndex = 0; double centerX = 0; double centerZ = 20; double radius = 20; List <BitmapImage> thumbnails = new List <BitmapImage>(); BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += delegate { for (int i = 0; i < imagesHorizontal * imagesVertical; i++) { if (imageIndex >= allFiles.Length) { imageIndex = 0; } BitmapImage bi = ImageLoader.GetThumbnailImage(allFiles[imageIndex]); if (bi == null) { i--; imageIndex++; continue; } thumbnails.Add(bi); imageIndex++; } }; bw.RunWorkerCompleted += delegate { imageIndex = 0; for (int i = 0; i < imagesHorizontal * imagesVertical; i++) { double circleXPos = xPos / 20; double circleXPos2 = xPos / 20 + 0.048; double x1 = centerX + radius * Math.Cos(circleXPos); double z1 = centerZ + radius * Math.Sin(circleXPos); double x2 = centerX + radius * Math.Cos(circleXPos2); double z2 = centerZ + radius * Math.Sin(circleXPos2); Point3D leftTop = new Point3D(x1, yPos, z1); Point3D rightTop = new Point3D(x2, yPos, z2); Point3D leftBottom = new Point3D(x1, yPos - 0.9, z1); Point3D rightBottom = new Point3D(x2, yPos - 0.9, z2); /* Point3D leftTop = new Point3D(xPos, yPos, 0); * Point3D rightTop = new Point3D(xPos+0.9, yPos, 0); * Point3D leftBottom = new Point3D(xPos, yPos-0.9, 0); * Point3D rightBottom = new Point3D(xPos+0.9, yPos-0.9, 0);*/ BitmapImage bi = thumbnails[imageIndex]; DiffuseMaterial material = new DiffuseMaterial(new ImageBrush(bi)); GeometryModel3D coverPlate = Create3DRectangle(leftTop, rightTop, leftBottom, rightBottom, material); myModel3DGroup.Children.Add(coverPlate); Transform3DGroup transformGroup = new Transform3DGroup(); TranslateTransform3D trans = new TranslateTransform3D(); DoubleAnimation daZ = new DoubleAnimation(30, 0, TimeSpan.FromMilliseconds(random.NextDouble() * 3000.0 + 5000.0).Duration()); daZ.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut }; trans.BeginAnimation(TranslateTransform3D.OffsetZProperty, daZ); transformGroup.Children.Add(trans); coverPlate.Transform = transformGroup; xPos += 1; xIndex++; if (xIndex >= imagesHorizontal) { xIndex = 0; xPos = xPosStart; yIndex++; yPos -= 1; } imageIndex++; if (imageIndex >= thumbnails.Count) { imageIndex = 0; } } }; bw.RunWorkerAsync(); }
private void DrawSomeModels() { myViewport.Name = "myViewport"; ModelVisual3D myModelVisual = new ModelVisual3D(); //Define lights and cameras myPCamera.FarPlaneDistance = 20; myPCamera.NearPlaneDistance = 0; myPCamera.FieldOfView = 50; myPCamera.Position = new Point3D(-5, 2, 3); myPCamera.LookDirection = new Vector3D(5, -2, -3); myPCamera.UpDirection = new Vector3D(0, 1, 0); myDLight.Color = Colors.White; myDLight.Direction = new Vector3D(-3, -4, -5); myAmbLight.Color = Colors.White; //set Geometry property of MeshGeometry3D side2.Geometry = side2Plane; side6.Geometry = side6Plane; side1.Geometry = side1Plane; side3.Geometry = side3Plane; side4.Geometry = side4Plane; side5.Geometry = side5Plane; //create translations //<Snippet3DOverview3DN19> TranslateTransform3D cube2Translation = new TranslateTransform3D(new Vector3D(2, 0, 0)); //</Snippet3DOverview3DN19> TranslateTransform3D cube3Translation = new TranslateTransform3D(new Vector3D(4, 0, 0)); //<Snippet3DOverview3DN1> //Define a rotation RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 1)); //</Snippet3DOverview3DN1> //<Snippet3DOverview3DN2> //Define an animation for the rotation DoubleAnimation myAnimation = new DoubleAnimation(); myAnimation.From = 1; myAnimation.To = 361; myAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(5000)); myAnimation.RepeatBehavior = RepeatBehavior.Forever; //</Snippet3DOverview3DN2> //Define another animation //<Snippet3DOverview3DN3> Vector3DAnimation myVectorAnimation = new Vector3DAnimation(new Vector3D(-1, -1, -1), new Duration(TimeSpan.FromMilliseconds(5000))); myVectorAnimation.RepeatBehavior = RepeatBehavior.Forever; //</Snippet3DOverview3DN3> myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, myAnimation); //<Snippet3DOverview3DN4> myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AxisProperty, myVectorAnimation); //</Snippet3DOverview3DN4> //<Snippet3DOverview3DN5> //Add transformation to the model cube1TransformGroup.Children.Add(myRotateTransform); //</Snippet3DOverview3DN5> cube2TransformGroup.Children.Add(myRotateTransform); cube2TransformGroup.Children.Add(cube2Translation); cube3TransformGroup.Children.Add(myRotateTransform); cube3TransformGroup.Children.Add(cube3Translation); allModelsTransformGroup.Children.Add(myRotateTransform); cubeModel_1.Children.Add(side1); cubeModel_1.Children.Add(side2); cubeModel_1.Children.Add(side3); cubeModel_1.Children.Add(side4); cubeModel_1.Children.Add(side5); cubeModel_1.Children.Add(side6); cubeModel_1.Transform = cube1TransformGroup; cubeModel_2.Children.Add(side1); cubeModel_2.Children.Add(side2); cubeModel_2.Children.Add(side3); cubeModel_2.Children.Add(side4); cubeModel_2.Children.Add(side5); cubeModel_2.Children.Add(side6); cubeModel_2.Transform = cube2TransformGroup; cubeModel_3.Children.Add(side1); cubeModel_3.Children.Add(side2); cubeModel_3.Children.Add(side3); cubeModel_3.Children.Add(side4); cubeModel_3.Children.Add(side5); cubeModel_3.Children.Add(side6); cubeModel_3.Transform = cube3TransformGroup; allModels.Transform = allModelsTransformGroup; allModels.Children.Add(cubeModel_3); allModels.Children.Add(cubeModel_2); allModels.Children.Add(cubeModel_1); allModels.Children.Add(myAmbLight); myViewport.Camera = myPCamera; myModelVisual.Content = allModels; myViewport.Children.Add(myModelVisual); mainWindow.Content = myViewport; }
/// <summary> /// Initializes a new Workshop3D instance. /// </summary> public Workshop3D() { this.Focusable = true; _viewport = new Viewport3D(); _viewport.Focusable = false; AddVisualChild(_viewport); PerspectiveCamera camera = new PerspectiveCamera(); _viewport.Camera = camera; InitializeCamera(); // INameScope ns = NameScope.GetNameScope(_viewport); // NameScope.SetNameScope(this, ns); // NameScope.SetNameScope(this, NameScope.GetNameScope(_viewport)); // this.DataContext = _viewport.DataContext; DirectionalLight light1 = new DirectionalLight(Colors.White, new Vector3D(0.0, -1.0, 1.0)); DirectionalLight light2 = new DirectionalLight(Colors.White, new Vector3D(-1.0, -1.0, -1.0)); DirectionalLight light3 = new DirectionalLight(Colors.White, new Vector3D(1.0, -1.0, -1.0)); DirectionalLight light4 = new DirectionalLight(Colors.White, new Vector3D(0.0, 1.0, 0.0)); Model3DGroup mg = new Model3DGroup(); mg.Children.Add(light1); mg.Children.Add(light2); mg.Children.Add(light3); mg.Children.Add(light4); _defaultLightingModel.Content = mg; _viewport.Children.Add(_defaultLightingModel); _rsd = new ResourceStringDecorator(); _rsd.AssemblyName = "Perspective.Wpf"; _rsd.BaseName = "Controls.Strings.Workshop3D"; AddVisualChild(_rsd); _commandPanel = new StackPanel(); _commandPanel.Focusable = false; _commandPanel.Orientation = Orientation.Vertical; ComponentResourceKey panelStyleKey = new ComponentResourceKey(typeof(ResourceKeys), "PanelStyle"); _commandPanel.Style = (Style)this.FindResource(panelStyleKey); _commandPanel.Opacity = _opacity; _rsd.Child = _commandPanel; ComponentResourceKey groupBoxStyleKey = new ComponentResourceKey(typeof(ResourceKeys), "GroupBoxStyle"); Style groupBoxStyle = (Style)this.FindResource(groupBoxStyleKey); GroupBox gbZoom = new GroupBox(); gbZoom.Style = groupBoxStyle; _commandPanel.Children.Add(gbZoom); // must be called after the insertion in the element tree // to get benefit of the ResourceManager inherited property gbZoom.Header = ResourceStringDecorator.InitializeValue(gbZoom, GroupBox.HeaderProperty, "FieldOfView"); StackPanel spZoom = new StackPanel(); gbZoom.Content = spZoom; _cameraZoomJoystick = new Joystick(); _cameraZoomJoystick.Dimensions = Dimensions.One; _cameraZoomJoystick.Focusable = false; _cameraZoomJoystick.IsTabStop = false; _cameraZoomJoystick.Startup += new StartupEventHandler(_cameraZoomJoystick_Startup); _cameraZoomJoystick.Stop += new RoutedEventHandler(_cameraZoomJoystick_Stop); spZoom.Children.Add(_cameraZoomJoystick); TextBlock tbZoom = new TextBlock(); SignalBinding bZoom = new SignalBinding(); #if DN35 // C# 3.0 : lambda expressions bZoom.Converting += (sender, e) => #else // C# 2.0 : anonymous methods bZoom.Converting += delegate(object sender, ConverterEventArgs e) #endif { e.ConvertedValue = String.Format(e.Culture, "{0:###.0}°", e.Value); }; bZoom.Source = camera; bZoom.Path = new PropertyPath("FieldOfView"); tbZoom.SetBinding(TextBlock.TextProperty, bZoom); spZoom.Children.Add(tbZoom); GroupBox gbPosition = new GroupBox(); gbPosition.Style = groupBoxStyle; _commandPanel.Children.Add(gbPosition); // must be called after the insertion in the element tree // to get benefit of the ResourceManager inherited property gbPosition.Header = ResourceStringDecorator.InitializeValue(gbPosition, GroupBox.HeaderProperty, "Position"); StackPanel spPosition = new StackPanel(); gbPosition.Content = spPosition; _cameraPositionJoystick = new Joystick(); _cameraPositionJoystick.Focusable = false; _cameraPositionJoystick.IsTabStop = false; _cameraPositionJoystick.Startup += new StartupEventHandler(_cameraPositionJoystick_Startup); _cameraPositionJoystick.Stop += new RoutedEventHandler(_cameraPositionJoystick_Stop); spPosition.Children.Add(_cameraPositionJoystick); TextBlock tbPosition = new TextBlock(); SignalBinding bPosition = new SignalBinding(); #if DN35 // C# 3.0 : lambda expressions bPosition.Converting += (sender, e) => #else // C# 2.0 : anonymous methods bPosition.Converting += delegate(object sender, ConverterEventArgs e) #endif { Point3D p = (Point3D)e.Value; e.ConvertedValue = String.Format( e.Culture, "{0:###.0}, {1:###.0}, {2:###.0}", p.X, p.Y, p.Z); }; bPosition.Source = camera; bPosition.Path = new PropertyPath("Position"); tbPosition.SetBinding(TextBlock.TextProperty, bPosition); spPosition.Children.Add(tbPosition); GroupBox gbLookDirection = new GroupBox(); gbLookDirection.Style = groupBoxStyle; _commandPanel.Children.Add(gbLookDirection); // must be called after the insertion in the element tree // to get benefit of the ResourceManager inherited property gbLookDirection.Header = ResourceStringDecorator.InitializeValue(gbLookDirection, GroupBox.HeaderProperty, "LookDirection"); StackPanel spLookDirection = new StackPanel(); gbLookDirection.Content = spLookDirection; _cameraLookDirectionJoystick = new Joystick(); _cameraLookDirectionJoystick.Dimensions = Dimensions.Two; _cameraLookDirectionJoystick.Focusable = false; _cameraLookDirectionJoystick.IsTabStop = false; _cameraLookDirectionJoystick.Startup += new StartupEventHandler(_cameraLookDirectionJoystick_Startup); _cameraLookDirectionJoystick.Stop += new RoutedEventHandler(_cameraLookDirectionJoystick_Stop); spLookDirection.Children.Add(_cameraLookDirectionJoystick); TextBlock tbLookDirection = new TextBlock(); SignalBinding bLookDirection = new SignalBinding(); #if DN35 // C# 3.0 : lambda expressions bLookDirection.Converting += (sender, e) => #else // C# 2.0 : anonymous methods bLookDirection.Converting += delegate(object sender, ConverterEventArgs e) #endif { Vector3D v = (Vector3D)e.Value; e.ConvertedValue = String.Format( e.Culture, "{0:N1}, {1:N1}, {2:N1}", v.X, v.Y, v.Z); }; bLookDirection.Source = camera; bLookDirection.Path = new PropertyPath("LookDirection"); tbLookDirection.SetBinding(TextBlock.TextProperty, bLookDirection); spLookDirection.Children.Add(tbLookDirection); // maps the Children property on the Viewport3D Children property SetValue(ChildrenPropertyKey, _viewport.Children); Duration duration = new Duration(TimeSpan.FromSeconds(2.0)); _cameraPositionAnimation = new Point3DAnimation(); _cameraPositionAnimation.Duration = duration; _cameraPositionAnimation.Completed += _cameraPositionAnimation_Completed; _cameraLookDirectionAnimation = new Vector3DAnimation(); _cameraLookDirectionAnimation.Duration = duration; _cameraLookDirectionAnimation.Completed += _cameraLookDirectionAnimation_Completed; _cameraZoomAnimation = new DoubleAnimation(); _cameraZoomAnimation.Duration = duration; _cameraZoomAnimation.AccelerationRatio = 0.2; _cameraZoomAnimation.DecelerationRatio = 0.2; _cameraPositionRotation = new AxisAngleRotation3D(); _cameraPositionRotateTransform = new RotateTransform3D(_cameraPositionRotation); _cameraLookDirectionRotation = new AxisAngleRotation3D(); _cameraLookDirectionRotateTransform = new RotateTransform3D(_cameraLookDirectionRotation); this.Focusable = true; }
void Window_Loaded(object sender, RoutedEventArgs e) { ThreeDeeType which = ThreeDeeType.Robot; _resDir = System.IO.Path.Combine(Utils.GetSourcePath(), "Resources"); // Define WPF objects. ModelVisual3D visual3d = new ModelVisual3D(); _group = new Model3DGroup(); visual3d.Content = _group; mainViewport.Children.Add(visual3d); // Define the camera. _camera = new PerspectiveCamera { FieldOfView = 60 }; _cameraController = new SphericalCameraController(_camera, mainViewport, this, mainGrid, mainGrid); // Define the lights. Color darker = Color.FromArgb(255, 96, 96, 96); Color dark = Color.FromArgb(255, 128, 128, 128); _group.Children.Add(new AmbientLight(darker)); _group.Children.Add(new DirectionalLight(dark, new Vector3D(0, -1, 0))); _group.Children.Add(new DirectionalLight(dark, new Vector3D(1, -3, -2))); _group.Children.Add(new DirectionalLight(dark, new Vector3D(-1, 3, 2))); ///// Define the model. if (which == ThreeDeeType.Robot) { // Move back a bit from the origin. Point3D coords = _cameraController.SphericalCoordinates; coords.X = 20; //coords.Y = 20; _cameraController.SphericalCoordinates = coords; DefineModelRobot(); // Some animation. red = x green = y blue = z // Mesh defines the surface. MeshGeometry3D animMesh = new MeshGeometry3D(); Point3D pt = D3.Origin; //////// box int size = 2; animMesh.AddBox(pt, new Vector3D(size, 0, 0), new Vector3D(0, size, 0), new Vector3D(0, 0, size)); //////// sphere //double radius = 2; //animMesh.AddSphere(pt, radius, 30, 10, true); //pt.Z += 2.5; //pt.X += 1; //animMesh.AddSphere(pt, radius, 30, 10, true); //////////// Common var dur = new Duration(TimeSpan.FromMilliseconds(5000)); // Model is the thing that is manipulated. GeometryModel3D animModel = animMesh.MakeModel(Brushes.Violet); _group.Children.Add(animModel); var transGroup = new Transform3DGroup(); animModel.Transform = transGroup; //////////// stretch ScaleTransform3D myScaleTransform3D = new ScaleTransform3D(); myScaleTransform3D.ScaleX = 2; myScaleTransform3D.ScaleY = 0.5; myScaleTransform3D.ScaleZ = 1; // Add the scale transform to the Transform3DGroup. //myTransform3DGroup.Children.Add(myScaleTransform3D); transGroup.Children.Add(myScaleTransform3D); //////////// Move var anim = new DoubleAnimation(0.0, 3, dur) { BeginTime = TimeSpan.FromSeconds(0), RepeatBehavior = RepeatBehavior.Forever }; var offsetTransform = new TranslateTransform3D(); //offsetTransform.BeginAnimation(TranslateTransform3D.OffsetXProperty, anim); //offsetTransform.BeginAnimation(TranslateTransform3D.OffsetYProperty, anim); offsetTransform.BeginAnimation(TranslateTransform3D.OffsetZProperty, anim); transGroup.Children.Add(offsetTransform); ///////// rotation var startAxis = new Vector3D(1, 0, 0);// (0, 1, 0); var rot = new AxisAngleRotation3D(startAxis, 180); var myRotateTransform = new RotateTransform3D(rot); // end, duration var rotateTo = new Vector3D(-1, -1, -1); var myVectorAnimation = new Vector3DAnimation(rotateTo, dur) { RepeatBehavior = RepeatBehavior.Forever }; myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AxisProperty, myVectorAnimation); transGroup.Children.Add(myRotateTransform); } else if (which == ThreeDeeType.Garden) { DefineModelGarden(); } }