ARCore屏幕坐标转换为世界坐标openGL

3

我正在尝试放置一个锚点,并在用户触摸屏幕的位置显示它,以返回X和Y坐标。

tapX = tap.getX();
tapY = tap.getY();

我希望使用这些信息创建一个矩阵来支持我的模型。也就是说,把我的3D模型放在用户点击的位置。

目前我尝试了:

 float sceneX = (tap.getX()/mSurfaceView.getMeasuredWidth())*2.0f - 1.0f;
float sceneY = (tap.getY()/mSurfaceView.getMeasuredHeight())*-2.0f + 1.0f; //if bottom is at -1. Otherwise same as X 
Pose temp = frame.getPose().compose(Pose.makeTranslation(sceneX, sceneY, -1.0f)).extractTranslation();

目前我只是将3D对象放在距离摄像机1米的位置。但我无法得到正确的位置。

有没有一种方法可以将局部坐标转换为世界坐标?

1个回答

1
屏幕上的轻触并没有一对一地映射到现实世界。它为您提供了沿着从相机延伸的射线的无限可能的(x,y,z)位置。例如,如果我在屏幕中央轻触,那可能对应于一个浮在我一米、两米远的物体,或者在地板上、穿过地板等等。
因此,您需要一些额外的约束条件告诉ARCore沿着这条射线放置锚点的位置。在示例应用程序中,这是通过将此射线与ARCore检测到的平面相交来完成的。与平面相交的射线描述了一个点,所以您就可以设置了。这就是他们在示例应用程序中的做法:
    MotionEvent tap = mQueuedSingleTaps.poll();
    if (tap != null && frame.getTrackingState() == TrackingState.TRACKING) {
        for (HitResult hit : frame.hitTest(tap)) {
            // Check if any plane was hit, and if it was hit inside the plane polygon.
            if (hit instanceof PlaneHitResult && ((PlaneHitResult) hit).isHitInPolygon()) {
                // Cap the number of objects created. This avoids overloading both the
                // rendering system and ARCore.
                if (mTouches.size() >= 16) {
                    mSession.removeAnchors(Arrays.asList(mTouches.get(0).getAnchor()));
                    mTouches.remove(0);
                }
                // Adding an Anchor tells ARCore that it should track this position in
                // space. This anchor will be used in PlaneAttachment to place the 3d model
                // in the correct position relative both to the world and to the plane.
                mTouches.add(new PlaneAttachment(
                    ((PlaneHitResult) hit).getPlane(),
                    mSession.addAnchor(hit.getHitPose())));

                // Hits are sorted by depth. Consider only closest hit on a plane.
                break;
            }
        }
    }

针对您的情况,我会查看AR Drawing应用程序的渲染代码。他们的函数GetWorldCoords()将屏幕坐标转换为世界坐标,假设从屏幕到物体的距离已知。

他们的代码并没有太大帮助,因为他们所有的世界空间定位都是在顶点着色器中完成的。但是将其实现到我的项目中很困难。 - snowrain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接