wpf3d 矩形命中测试

发布于 2024-10-07 11:58:12 字数 262 浏览 1 评论 0原文

我有一个包含驻留在 Viewport3D 中的 3D 对象的应用程序,我希望用户能够通过在屏幕上拖动矩形来选择它们。

我尝试在 Viewport3D 上应用 GeometryHitTestParameters (具有矩形几何形状)以获得结果,但我收到一个异常,告诉我 Viewport3D 不支持它。仅支持 PointHitTestParameters。

有谁知道任何优雅的方法来做到这一点,除了我自己计算(例如 - 将所有 3D 对象投影到 2D 并与矩形进行手动几何交叉)?

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 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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2024-10-14 11:58:12

我怀疑是否有比迭代选定的矩形点更好的方法:

    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);
    }

请参阅 MSDN 如何:点击在 Viewport3D 中测试

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文