5

我在中为我的一个应用程序创建了一个 3D 对象。该物体类似于人体,可以在触摸时旋转。如何检测此 3D 对象上的触摸位置。意味着如果用户触摸头部,我必须检测到它是头部。如果触摸在手上,则必须对其进行识别。即使对象旋转到其他方向,它也应该可以工作。我认为 3D 对象上的触摸坐标是必需的。
这是我在视图上获得触摸位置的方法。

- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
{
    UITouch* touch = [touches anyObject];
    CGPoint location  = [touch locationInView: self];
    m_applicationEngine->OnFingerDown(ivec2(location.x, location.y));
}

任何人都可以帮忙吗?提前致谢!

4

3 回答 3

5

忘记光线追踪和其他顶尖算法。我们在 App Store 上为我们的一个应用程序(Iyan 3D)使用了一个简单的技巧。但是,每次将场景旋转到一个新角度时,这种技术都需要一个额外的渲染通道。以不同的颜色(不是实际的颜色,而是独特的颜色)渲染不同的对象(头部、手、腿等)。读取渲染图像中与屏幕位置对应的颜色。您可以根据其颜色找到对象。

在此方法中,您可以使用更改渲染图像分辨率来平衡精度和性能。

于 2013-08-20T06:03:41.687 回答
3

为了确定物体的 3D 位置,我建议使用光线追踪。

假设模型在世界空间坐标中,您还需要知道眼睛位置的世界空间坐标和图像平面的世界空间坐标。使用这两个点,您可以计算出用于与模型相交的射线,我假设模型由三角形组成。

然后,您可以使用射线三角形测试来确定触摸的 3D 位置,方法是找到与图像平面相交最近的三角形。如果您想触摸哪个三角形,您还需要在进行相交测试时保存该信息。

此页面提供了如何进行射线三角形相交测试的示例:http ://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-9-ray-triangle-intersection/ray-triangle-intersection-geometric-解决方案/

编辑:

更新有一些示例代码。它是我从不久前做的一个 C++ 光线追踪项目中获取的一些稍微修改过的代码,因此您需要对其进行一些修改以使其适用于 iOS。此外,当前形式的代码甚至没有用,因为它不返回实际的交点,而是返回射线是否与三角形相交。

// d is the direction the ray is heading in
// o is the origin of the ray
// verts is the 3 vertices of the triangle
// faceNorm is the normal of the triangle surface
bool
Triangle::intersect(Vector3 d, Vector3 o, Vector3* verts, Vector3 faceNorm)
{
    // Check for line parallel to plane
    float r_dot_n = (dot(d, faceNorm));

    // If r_dot_n == 0, then the line and plane are parallel, but we need to 
    // do the range check due to floating point precision
    if (r_dot_n > -0.001f && r_dot_n < 0.001f)
        return false;

    // Then we calculate the distance of the ray origin to the triangle plane
    float t = ( dot(faceNorm, (verts[0] - o)) / r_dot_n);
    if (t < 0.0)
        return false;

    // We can now calculate the barycentric coords of the intersection
    Vector3 ba_ca = cross(verts[1]-verts[0], verts[2]-verts[0]);
    float denom = dot(-d,  ba_ca);

    dist_out = dot(o-verts[0], ba_ca) / denom;
    float b = dot(-d, cross(r.o-verts[0], verts[2]-verts[0])) / denom;
    float c = dot(-d, cross(verts[1]-verts[0], o-verts[0])) / denom;

    // Check if in tri or if b & c have NaN values
    if (  b < 0 || c < 0 || b+c > 1 || b != b || c != c)
        return false;

    // Use barycentric coordinates to calculate the intersection point
    Vector3 P = (1.f-b-c)*verts[0] + b*verts[1] + c*verts[2];

    return true;
}

您感兴趣的实际交点是 P。

于 2013-08-15T07:10:40.003 回答
2

光线追踪是一种选择,并在许多应用程序中用于执行此操作(拾取)。光线追踪的问题在于,这个解决方案需要大量工作才能让一个非常简单的基本功能正常工作。光线追踪也可能很慢,但如果你只有一条光线要追踪(比如手指的位置),那应该没问题。

OpenGL 的 API 还提供了一种选择对象的技术。我建议你看看例如:http ://www.lighthouse3d.com/opengl/picking/

最后一个选项包括在屏幕空间中投影对象的顶点,并使用简单的 2d 技术找出您的手指与对象的哪些面重叠。

于 2013-08-17T00:19:36.713 回答