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

ARCore how to add Virtual object as soon as horizontal plane detected

在 ARCore 示例中,如何在水平面后立即添加虚拟对象detected.I 尝试了以下代码但无效,

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

据我所知,您应该将 TrackingState.TRACKING 更改为 TrackingState.NEW

尚未记录。

onDrawFrame() 试试这个:

在此循环内:

 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() 时添加更多锚点。