WPF 中的 3D 命中测试 [英] 3D Hit Testing in WPF
问题描述
我正在编写一个以 3D 形式显示地形的 WPF 应用程序.
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屋!