private void ps_BlogRequested(FlickrPhoto photo) { blogVisual3D = new InteractiveVisual3D(); MeshGeometry3D blogMesh = (MeshGeometry3D)Resources["PlaneMesh"]; UIElement blogMeshVisual = new BlogVisual(); blogVisual3D.Geometry = blogMesh; blogVisual3D.Visual = blogMeshVisual; Transform3DGroup transformGroup = new Transform3DGroup(); transformGroup.Children.Add(new ScaleTransform3D(0.17, 0.17, 0.17)); transformGroup.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 15))); transformGroup.Children.Add(new TranslateTransform3D(-0.20, 0, -1)); blogVisual3D.Transform = transformGroup; MainViewport.Children.Add(blogVisual3D); }
void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) { switch (connectionId) { case 1: this.UserControl = ((VideoPanel.MyCoverFlow)(target)); return; case 2: this.LayoutRoot = ((System.Windows.Controls.Grid)(target)); return; case 3: this.viewport3D = ((System.Windows.Controls.Viewport3D)(target)); return; case 4: this.Camera = ((System.Windows.Media.Media3D.PerspectiveCamera)(target)); return; case 5: this.L5 = ((_3DTools.InteractiveVisual3D)(target)); return; case 6: this.CT_L5 = ((System.Windows.Controls.Control)(target)); return; case 7: this.L4 = ((_3DTools.InteractiveVisual3D)(target)); return; case 8: this.CT_L4 = ((System.Windows.Controls.Control)(target)); return; case 9: this.L3 = ((_3DTools.InteractiveVisual3D)(target)); return; case 10: this.CT_L3 = ((System.Windows.Controls.Control)(target)); return; case 11: this.L2 = ((_3DTools.InteractiveVisual3D)(target)); return; case 12: this.CT_L2 = ((System.Windows.Controls.Control)(target)); return; case 13: this.L1 = ((_3DTools.InteractiveVisual3D)(target)); return; case 14: this.CT_L1 = ((System.Windows.Controls.Control)(target)); return; case 15: this.C = ((_3DTools.InteractiveVisual3D)(target)); return; case 16: this.CT_C = ((System.Windows.Controls.Control)(target)); return; case 17: this.R1 = ((_3DTools.InteractiveVisual3D)(target)); return; case 18: this.CT_R1 = ((System.Windows.Controls.Control)(target)); return; case 19: this.R2 = ((_3DTools.InteractiveVisual3D)(target)); return; case 20: this.CT_R2 = ((System.Windows.Controls.Control)(target)); return; case 21: this.R3 = ((_3DTools.InteractiveVisual3D)(target)); return; case 22: this.CT_R3 = ((System.Windows.Controls.Control)(target)); return; case 23: this.R4 = ((_3DTools.InteractiveVisual3D)(target)); return; case 24: this.CT_R4 = ((System.Windows.Controls.Control)(target)); return; case 25: this.R5 = ((_3DTools.InteractiveVisual3D)(target)); return; case 26: this.CT_R5 = ((System.Windows.Controls.Control)(target)); return; } this._contentLoaded = true; }
protected void OnMouseMove(InteractiveVisual3D visual3D, Object data, MouseEventArgs mouseEventArgs) { if (((UIElement)visual3D.Visual).IsMouseCaptured) { Viewport3D viewport = Visual3DHelper.GetVisual3DViewport(visual3D); Point newMouseCapturePos = Mouse.GetPosition(viewport); Vector diff = newMouseCapturePos - mouseCapturePos; mouseCapturePos = newMouseCapturePos; Transform3DGroup t3Dgroup = (Transform3DGroup)visual3D.Transform; RotateTransform3D rt3D = (RotateTransform3D)t3Dgroup.Children[0]; AxisAngleRotation3D axisAngle = (AxisAngleRotation3D)rt3D.Rotation; if (axisAngle.HasAnimatedProperties) { double oldAngle = axisAngle.Angle; axisAngle.BeginAnimation(AxisAngleRotation3D.AngleProperty, null); axisAngle.Angle = oldAngle; } axisAngle.Angle += diff.X; mouseEventArgs.Handled = true; } }
public void outerGridKeyDown(Object sender, KeyEventArgs e) { InteractiveVisual3D newVis = null; // figure out what mesh is requested if (e.Key == Key.F2) { newVis = reallyFakeIV3D; } else if (e.Key == Key.F3) { newVis = new PartialSphere(0, 360, 60, 60, 30, 30, false); } else if (e.Key == Key.F4) { newVis = new InteractiveSphere(); } else if (e.Key == Key.F5) { newVis = new InteractiveCylinder(); } else if (e.Key == Key.F6) { newVis = new InteractiveCone(); } // add it to the scene if (newVis != null) { Visual oldVisual; if (cachedNewVis == null) { oldVisual = reallyFakeIV3D.Visual; reallyFakeIV3D.Visual = null; } else { oldVisual = cachedNewVis.Visual; cachedNewVis.Visual = null; } newVis.Visual = oldVisual; if (cachedNewVis != null) { MainViewportOuter.Children.Remove(cachedNewVis); } else { MainViewportOuter.Children.Remove(reallyFakeIV3D); oldCameraTransform = MainViewportOuter.Camera.Transform; twoAxisValuator.Enabled = true; } MainViewportOuter.Children.Add(newVis); //transform if (newVis == reallyFakeIV3D) { MainViewportOuter.Camera.Transform = oldCameraTransform; twoAxisValuator.Enabled = false; } else { Transform3DGroup tgroup = new Transform3DGroup(); tgroup.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 15))); tgroup.Children.Add(new ScaleTransform3D(0.68, 0.68, 0.68)); twoAxisValuator.Up = new Vector3D(0, Math.Cos(15 * Math.PI / 180), Math.Sin(15 * Math.PI / 180)); newVis.Transform = tgroup; } if (newVis == reallyFakeIV3D) { cachedNewVis = null; } else { cachedNewVis = newVis; } e.Handled = true; } }
protected void OnMouseLeftButtonUp(InteractiveVisual3D visual3D, Object data, MouseButtonEventArgs mouseEventArgs) { if (((UIElement)visual3D.Visual).IsMouseCaptured) { UIElement uie = (UIElement)visual3D.Visual; uie.ReleaseMouseCapture(); UpdateToDefaultPositions(); mouseEventArgs.Handled = true; } }
protected void OnMouseLeftButtonDown(InteractiveVisual3D visual3D, Object data, MouseButtonEventArgs mouseEventArgs) { int index = Children.IndexOf(visual3D); if (index == 0) { // grab capture on the 2d element UIElement uie = (UIElement)visual3D.Visual; Viewport3D viewport = Visual3DHelper.GetVisual3DViewport(visual3D); mouseCapturePos = Mouse.GetPosition(viewport); // create the animation Duration d = new Duration(new TimeSpan(0, 0, 1)); DoubleAnimation doubleAnimation = new DoubleAnimation(0, -Width, d); DoubleAnimation anotherAnimation = new DoubleAnimation(0, 0.01 * Width / ZSpacing, d); doubleAnimation.IsAdditive = true; anotherAnimation.IsAdditive = true; for (int i = 1; i < Children.Count; i++) { ModelVisual3D v = (ModelVisual3D)Children[i]; TranslateTransform3D translate = (TranslateTransform3D)((Transform3DGroup)v.Transform).Children[1]; translate.BeginAnimation(TranslateTransform3D.OffsetZProperty, doubleAnimation); translate.BeginAnimation(TranslateTransform3D.OffsetXProperty, anotherAnimation); } uie.CaptureMouse(); } else { MoveToFront(data); } mouseEventArgs.Handled = true; }
public ClosestIntersectionInfo(Point p, UIElement v, InteractiveVisual3D iv3D) { _pointHit = p; _uiElemHit = v; _imv3DHit = iv3D; }
/// <summary> /// Finds the point in edges that is closest ot the mouse position. Updates closestIntersectionInfo /// with the results of this calculation /// </summary> /// <param name="mousePos">The mouse position</param> /// <param name="edges">The edges to test against</param> /// <param name="imv3DHit">The model that has the visual on it with capture</param> private void FindClosestIntersection(Point mousePos, List<HitTestEdge> edges, InteractiveVisual3D imv3DHit) { double closestDistance = Double.MaxValue; Point closestIntersection = new Point(); // the uv of the closest intersection // Find the closest point to the mouse position for (int i=0; i<edges.Count; i++) { Vector v1 = mousePos - edges[i]._p1Transformed; Vector v2 = edges[i]._p2Transformed - edges[i]._p1Transformed; Point currClosest; double distance; // calculate the distance from the mouse position to this edge // The closest distance can be computed by projecting v1 on to v2. If the // projectiong occurs between _p1Transformed and _p2Transformed, then this is the // closest point. Otherwise, depending on which side it lies, it is either _p1Transformed // or _p2Transformed. // // The projection equation is given as: (v1 DOT v2) / (v2 DOT v2) * v2. // v2 DOT v2 will always be positive. Thus, if v1 DOT v2 is negative, we know the projection // will occur before _p1Transformed (and so it is the closest point). If (v1 DOT v2) is greater // than (v2 DOT v2), then we have gone passed _p2Transformed and so it is the closest point. // Otherwise the projection gives us this value. // double denom = v2 * v2; if (denom == 0) { currClosest = edges[i]._p1Transformed; distance = v1.Length; } else { double numer = v2 * v1; if (numer < 0) { currClosest = edges[i]._p1Transformed; } else { if (numer > denom) { currClosest = edges[i]._p2Transformed; } else { currClosest = edges[i]._p1Transformed + (numer / denom) * v2; } } distance = (mousePos - currClosest).Length; } // see if we found a new closest distance if (distance < closestDistance) { closestDistance = distance; if (denom != 0) { closestIntersection = ((currClosest - edges[i]._p1Transformed).Length / Math.Sqrt(denom) * (edges[i]._uv2 - edges[i]._uv1)) + edges[i]._uv1; } else { closestIntersection = edges[i]._uv1; } } } if (closestDistance != Double.MaxValue) { UIElement uiElemWCapture = (UIElement)Mouse.Captured; UIElement uiElemOnMesh = imv3DHit.InternalVisual; Rect contBounds = VisualTreeHelper.GetDescendantBounds(uiElemWCapture); Point ptOnVisual = TextureCoordsToVisualCoords(closestIntersection, uiElemOnMesh); Point ptRelToCapture = uiElemOnMesh.TransformToDescendant(uiElemWCapture).Transform(ptOnVisual); // we want to "ring" around the outside so things like buttons are not pressed // this code here does that - the +BUFFER_SIZE and -BUFFER_SIZE are to give a bit of a // buffer for any numerical issues if (ptRelToCapture.X <= contBounds.Left + 1) ptRelToCapture.X -= BUFFER_SIZE; if (ptRelToCapture.Y <= contBounds.Top + 1) ptRelToCapture.Y -= BUFFER_SIZE; if (ptRelToCapture.X >= contBounds.Right - 1) ptRelToCapture.X += BUFFER_SIZE; if (ptRelToCapture.Y >= contBounds.Bottom - 1) ptRelToCapture.Y += BUFFER_SIZE; Point finalVisualPoint = uiElemWCapture.TransformToAncestor(uiElemOnMesh).Transform(ptRelToCapture); _closestIntersectInfo = new ClosestIntersectionInfo(VisualCoordsToTextureCoords(finalVisualPoint, uiElemOnMesh), imv3DHit.InternalVisual, imv3DHit); } }
/// <summary> /// Function to deal with mouse capture when off the mesh. /// </summary> /// <param name="imv3DHit">The model hit</param> /// <param name="mousePos">The location of the mouse</param> private void HandleMouseCaptureButOffMesh(InteractiveVisual3D imv3DHit, Point mousePos) { // process the mouse capture if it exists UIElement uie = (UIElement)Mouse.Captured; // get the size of the element Rect contBounds = VisualTreeHelper.GetDescendantBounds(uie); // translate to the parent's coordinate system GeneralTransform gt = uie.TransformToAncestor(_hiddenVisual); Point[] visCorners = new Point[4]; // get the points relative to the parent visCorners[0] = gt.Transform(new Point(contBounds.Left, contBounds.Top)); visCorners[1] = gt.Transform(new Point(contBounds.Right, contBounds.Top)); visCorners[2] = gt.Transform(new Point(contBounds.Right, contBounds.Bottom)); visCorners[3] = gt.Transform(new Point(contBounds.Left, contBounds.Bottom)); // get the u,v texture coordinate values of the above points Point[] texCoordsOfInterest = new Point[4]; for (int i = 0; i < visCorners.Length; i++) { texCoordsOfInterest[i] = VisualCoordsToTextureCoords(visCorners[i], _hiddenVisual); } // get the edges that map to the given visual List<HitTestEdge> edges = imv3DHit.GetVisualEdges(texCoordsOfInterest); if (Debug) { AdornerLayer myAdornerLayer = AdornerLayer.GetAdornerLayer(this); if (_DEBUGadorner == null) { _DEBUGadorner = new DebugEdgesAdorner(this, edges); myAdornerLayer.Add(_DEBUGadorner); } else { myAdornerLayer.Remove(_DEBUGadorner); _DEBUGadorner = new DebugEdgesAdorner(this, edges); myAdornerLayer.Add(_DEBUGadorner); } } // find the closest intersection of the mouse position and the edge list FindClosestIntersection(mousePos, edges, imv3DHit); }