Como fazer o teste de clique em um Viewport3D
Este exemplo mostra como fazer o teste de colisão para elementos visuais 3D em um Viewport3D.
Como HitTest retorna informações 2D e 3D, é possível percorrer os resultados do teste para ler apenas os resultados 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)
O HitTestResultBehavior no código a seguir determina como os resultados do teste de acerto são processados. UpdateResultInfo
e UpdateMaterial
são métodos definidos localmente.
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
Colabore connosco no GitHub
A origem deste conteúdo pode ser encontrada no GitHub, onde também pode criar e rever problemas e pedidos Pull. Para mais informações, consulte o nosso guia do contribuidor.
.NET Desktop feedback