/// <summary> /// Computes the transform from the inner space of the given /// Visual3D to the 2D space of the Viewport3DVisual which /// contains it. /// /// The result will contain the transform of the given visual. /// /// This method can fail if Camera.Transform is non-invertable /// in which case the camera clip planes will be coincident and /// nothing will render. In this case success will be false. /// </summary> /// <param name="visual"></param> /// <param name="success"></param> /// <returns></returns> public static Matrix3D TryTransformTo2DAncestor(DependencyObject visual, out Viewport3DVisual viewport, out bool success) { Matrix3D to2D = GetWorldTransformationMatrix(visual, out viewport); to2D.Append(MUtils.TryWorldToViewportTransform(viewport, out success)); if (!success) { return(ZeroMatrix); } return(to2D); }
public Point?Point3DToScreen2D(Point3D point3D, Viewport3D viewPort) { // We need a Viewport3DVisual but we only have a Viewport3D. if (viewPort.Children.Count == 0) { return(null); } Viewport3DVisual vpv = VisualTreeHelper.GetParent(viewPort.Children[0]) as Viewport3DVisual; // Get the world to viewport transform matrix Matrix3D m = MUtils.TryWorldToViewportTransform(vpv, out var bOk); if (!bOk) { return(null); } // Transform the 3D point to 2D var transformedPoint = m.Transform(point3D); var screen2DPoint = new Point(transformedPoint.X, transformedPoint.Y); return(screen2DPoint); }