如何:在 Viewport3D 中进行命中测试

此示例显示如何在 Viewport3D 中对 3D 视觉对象进行命中测试。

由于 HitTest 返回 2D 和 3D 信息,因此可以循环访问测试结果以仅读取 3D 结果。

public void HitTest(object sender, System.Windows.Input.MouseButtonEventArgs args)
{
    Point mouseposition = args.GetPosition(myViewport);
    Point3D testpoint3D = new Point3D(mouseposition.X, mouseposition.Y, 0);
    Vector3D testdirection = new Vector3D(mouseposition.X, mouseposition.Y, 10);
    PointHitTestParameters pointparams = new PointHitTestParameters(mouseposition);
    RayHitTestParameters rayparams = new RayHitTestParameters(testpoint3D, testdirection);

    //test for a result in the Viewport3D
    VisualTreeHelper.HitTest(myViewport, null, HTResult, pointparams);
Public Sub HitTest(ByVal sender As Object, ByVal args As MouseButtonEventArgs)
    Dim mouseposition As Point = args.GetPosition(myViewport)
    Dim testpoint3D As New Point3D(mouseposition.X, mouseposition.Y, 0)
    Dim testdirection As New Vector3D(mouseposition.X, mouseposition.Y, 10)
    Dim pointparams As New PointHitTestParameters(mouseposition)
    Dim rayparams As New RayHitTestParameters(testpoint3D, testdirection)

    'test for a result in the Viewport3D
    VisualTreeHelper.HitTest(myViewport, Nothing, AddressOf HTResult, pointparams)

以下代码中的 HitTestResultBehavior 确定如何处理命中测试结果。 UpdateResultInfoUpdateMaterial 是本地定义的方法。

public HitTestResultBehavior HTResult(System.Windows.Media.HitTestResult rawresult)
{
    //MessageBox.Show(rawresult.ToString());
    RayHitTestResult rayResult = rawresult as RayHitTestResult;

    if (rayResult != null)
    {
        RayMeshGeometry3DHitTestResult rayMeshResult = rayResult as RayMeshGeometry3DHitTestResult;

        if (rayMeshResult != null)
        {
            GeometryModel3D hitgeo = rayMeshResult.ModelHit as GeometryModel3D;

            UpdateResultInfo(rayMeshResult);
            UpdateMaterial(hitgeo, (side1GeometryModel3D.Material as MaterialGroup));
        }
    }

    return HitTestResultBehavior.Continue;
}
Public Function HTResult(ByVal rawresult As HitTestResult) As HitTestResultBehavior
    Dim rayResult As RayHitTestResult = TryCast(rawresult, RayHitTestResult)

    If rayResult IsNot Nothing Then
        Dim rayMeshResult As RayMeshGeometry3DHitTestResult = TryCast(rayResult, RayMeshGeometry3DHitTestResult)

        If rayMeshResult IsNot Nothing Then
            Dim hitgeo As GeometryModel3D = TryCast(rayMeshResult.ModelHit, GeometryModel3D)

            UpdateResultInfo(rayMeshResult)
            UpdateMaterial(hitgeo, (TryCast(side1GeometryModel3D.Material, MaterialGroup)))
        End If
    End If

    Return HitTestResultBehavior.Continue
End Function