The Charles Petzold 3D library, which can be downloaded here in the Petzold.Media3D Library section, contains a class ViewportInfowith these two static methods:
public static Point Point3DToPoint2D(Viewport3D viewport, Point3d point)
public static bool Point2DToPoint3D(Viewport3D viewport, Point, ptIn, out LineRange range)
Point2DToPoint3D PointAndZToPoint3D(), ( out) a LineRange, , , LineRange PointFromZ(double zValue), , , z = zValue.
:
using System.Windows;
using System.Windows.Input;
using Petzold.Media3D;
namespace _3DTester
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_MouseDown(object sender, MouseEventArgs e)
{
var range = new LineRange();
var isValid = ViewportInfo.Point2DtoPoint3D(Viewport, e.GetPosition(Viewport), out range);
if (!isValid)
MouseLabel.Content = "(no data)";
else
{
var point3D = range.PointFromZ(0);
var point = ViewportInfo.Point3DtoPoint2D(Viewport, point3D);
MouseLabel.Content = e.GetPosition(Viewport) + "\n" + point3D + "\n" + point;
}
}
}
}
XAML
<Window
x:Class="_3DTester.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="300"
MouseDown="Window_MouseDown">
<Grid>
<Viewport3D Name="Viewport">
<Viewport3D.Camera>
<PerspectiveCamera
Position="0,0,30"
LookDirection="0,0,-1"
UpDirection="0,1,0" />
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup>
<DirectionalLight Color="White" Direction="1,-1,-1" />
<GeometryModel3D>
<GeometryModel3D.Geometry>
<MeshGeometry3D
Positions="0,0,10 -5,-5,0 -5,5,0 5,5,0 5,-5,0"
TriangleIndices="2 1 0 2 0 3 4 3 0 1 4 0" />
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Red" />
</GeometryModel3D.Material>
</GeometryModel3D>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
<Label Name="MouseLabel" Content="(no data)" />
</Grid>
</Window>