ARCore如何在检测到水平平面时立即添加虚拟对象

时间:2018-02-28 07:57:15

标签: android arcore

在ARCore示例中,如何在检测到水平平面时立即添加虚拟对象。我尝试了以下代码但不起作用,

// Check if we detected at least one plane. If so, hide the loading message.
      if (messageSnackbar != null) {
        for (Plane plane : session.getAllTrackables(Plane.class)) {
          if (plane.getType() == com.google.ar.core.Plane.Type.HORIZONTAL_UPWARD_FACING
                  && plane.getTrackingState() == TrackingState.TRACKING) {
            hideLoadingMessage();
            //frame.hitTest(plane.getExtentX(),plane.getExtentZ());

            long downTime = SystemClock.uptimeMillis();
            long eventTime = SystemClock.uptimeMillis() + 100;

            int metaState = 0;
            MotionEvent motionEvent = MotionEvent.obtain(
                    downTime,
                    eventTime,
                    MotionEvent.ACTION_DOWN,
                    plane.getCenterPose().qx(),
                    plane.getCenterPose().qy(),
                    metaState
            );
            //surfaceView.dispatchTouchEvent(motionEvent);
            onSingleTap(motionEvent);
            break;
          }
        }
      }

2 个答案:

答案 0 :(得分:1)

for (Plane plane : session.getAllTrackables(Plane.class)) { if (plane.getType() == com.google.ar.core.Plane.Type.HORIZONTAL_UPWARD_FACING && plane.getTrackingState() == TrackingState.TRACKING) { break; } } 中尝试此操作:

在这个循环中:

 for (Plane plane : session.getAllTrackables(Plane.class)) {
      if (plane.getType() == com.google.ar.core.Plane.Type.HORIZONTAL_UPWARD_FACING
          && plane.getTrackingState() == TrackingState.TRACKING) {

          if (newAnchor == null) {
            newAnchor = plane.createAnchor(plane.getCenterPose());
          }

        break;
      }

添加:

UPDATE

确保newAnchor是一个成员变量,每次调用onDrawFrame()时都不会添加更多锚点。

答案 1 :(得分:0)

据我记得你应该将TrackingState.TRACKING更改为TrackingState.NEW

尚未记录。