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)

2 years ago

在_1300274中试试这个:

在这个循环中:

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;
      }

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

2 years ago

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

它还没有记录 .