WPF中的3D命中测试 [英] 3D Hit Testing in WPF
问题描述
我正在编写一个WPF应用程序,以3D形式显示地形。
I'm writing a WPF application that displays terrain in 3D.
当我执行点击测试时,会返回错误的3D点(不是我单击的点) )。
When I perform hit testing, the wrong 3D point is returned (not the point I clicked on).
我尝试突出显示被击中的三角形(通过创建新网格,并从 RayMeshGeometry3DHitTestResult
对象)。我看到错误的三角形被击中(三角形突出显示,但不在光标下方)。
I tried highlighting the triangle that was hit (by creating a new mesh, taking the coordinates from the RayMeshGeometry3DHitTestResult
object). I see that the wrong triangle gets hit (a triangle is highlighted, but it is not under the cursor).
我正在使用视角为60,而近平面和远平面分别为3和35000。
I'm using a perspective camera with field of view of 60, and the near and far planes are of 3 and 35000 respectively.
任何人知道为什么会发生这种情况以及我可以采取什么措施?
Any idea why it might happen and what I can do to solve it?
让我知道是否需要更多数据。
Let me know if you need any more data.
编辑:这是我用来执行点击测试的代码:
This is the code I use to perform the hit testing:
private void m_viewport3d_MouseDown(object sender, MouseButtonEventArgs e)
{
Point mousePos = e.GetPosition(m_viewport3d);
PointHitTestParameters hitParams = new PointHitTestParameters(mousePos);
HitTestResult result = VisualTreeHelper.HitTest(m_viewport3d, mousePos);
RayMeshGeometry3DHitTestResult rayMeshResult = result as RayMeshGeometry3DHitTestResult;
if (rayMeshResult != null)
{
MeshGeometry3D mesh = new MeshGeometry3D();
mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex1]);
mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex2]);
mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex3]);
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);
GeometryModel3D marker = new GeometryModel3D(mesh, new DiffuseMaterial(Brushes.Blue));
//...add marker to the scene...
}
}
推荐答案
发现这是一个 Normalize
问题。我不应该标准化相机的外观和向上矢量。在我使用的秤中,失真太大,无法通过点击测试正常工作。
Figures out it was a Normalize
issue. I shouldn't have normalized the camera's look and up vectors. In the scales I'm using, the distortion is too big for the hit test to work correctly.
这篇关于WPF中的3D命中测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!