Edit

Share via


How to: Hit Test in a Viewport3D

This example shows how to hit test for 3D Visuals in a Viewport3D.

Because HitTest returns 2D and 3D information, it is possible to iterate through the test results to read only 3D results.

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)

The HitTestResultBehavior in the following code determines how the hit test results are processed. UpdateResultInfo and UpdateMaterial are locally defined methods.

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