I have an application with 3D objects that reside in Viewport3D and I want the user to be able to select them by dragging a rectangle on the screen.
I tried applying GeometryHitTestParameters (with rectangle开发者_如何学Go geometry) on the Viewport3D in order to get the results, but I get an exception telling me that it is unsuppoted with Viewport3D. Only PointHitTestParameters are supported.
Does anybody know any elegant way to do it, except calculating it myself (for example - projecting all 3D objects to 2D and doing manual geometry intersections with a rectangle)?
I doubt there is better way than iterating through selected rectangle points:
private void view_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
const double offset = 3.0; // I will test in a square 7x7
var mouse = e.GetPosition(this);
var items = new HashSet<Model3D>();
for (double x = mouse.X - offset; x <= mouse.X + offset; x++)
for (double y = mouse.Y - offset; y <= mouse.Y + offset; y++) {
PointHitTestParameters pointparams = new PointHitTestParameters(new Point(x, y));
Model3D result = null;
VisualTreeHelper.HitTest(view, null, rawresult => {
var rayResult = rawresult as RayMeshGeometry3DHitTestResult;
if (rayResult != null)
items.Add(rayResult.ModelHit);
return HitTestResultBehavior.Continue;
}, pointparams);
}
// temporary ListBox to show items
list.ItemsSource = items.Select(item => item as GeometryModel3D == null ? null : (item as GeometryModel3D).Material as object);
}
See MSDN How to: Hit Test in a Viewport3D
精彩评论