首页 > 解决方案 > 我们如何将场景形式与 ARCloud 锚点一起使用?

问题描述

每次我尝试托管锚点“ session.hostCloudAnchor(anchor); ”时,它都会显示NotTrackingException

我们如何托管和解析从arFragment.setOnTapArPlaneListener获得的 Anchor ?

这是我使用的代码片段,

   arFragment.setOnTapArPlaneListener(
    (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
      Camera camera = arFragment.getArSceneView().getArFrame().getCamera();
      TrackingState cameraTrackingState = camera.getTrackingState();

      if (andyRenderable == null) {
        return;
      }

      if (plane.getType() != Type.HORIZONTAL_UPWARD_FACING) {
        return;
      }

      if (cameraTrackingState == TrackingState.TRACKING && session!= null) {
        // Create the Anchor.
        anchor = hitResult.createAnchor();
        try{
        session.hostCloudAnchor(anchor);
        }
        catch (NotTrackingException e)
        {
            e.printStackTrace();

        }
        setNewAnchor(anchor);
        appAnchorState = AppAnchorState.HOSTING;
        Toast.makeText(HelloSceneformActivity.this, "Now, hosting anchor", Toast.LENGTH_SHORT)
            .show();

        AnchorNode anchorNode = new AnchorNode(anchor);
        anchorNode.setParent(arFragment.getArSceneView().getScene());

        // Create the transformable andy and add it to the anchor.
        TransformableNode andy = new TransformableNode(arFragment.getTransformationSystem());
        andy.setParent(anchorNode);
        andy.setRenderable(andyRenderable);
        andy.select();

        checkUpdatedAnchor();
      }
    });

标签: arcore

解决方案


You need to check the anchor tracking state & proceed with hosting.

override fun onTapPlane(hitResult: HitResult, plane: Plane, motionEvent: MotionEvent?) {


        if (plane.type == Plane.Type.HORIZONTAL_UPWARD_FACING) {

            val anchor = hitResult.createAnchor()
            val anchorNode = AnchorNode(anchor)
            anchorNode.setParent(arFragment.arSceneView.scene)

            if (anchor.trackingState == TrackingState.TRACKING) {

                viewModel.hostAnchorToCloud(anchor)

            } 
        }
    }

Before hosting an anchor:

  • Try to look at the anchor from different angles.
  • Move around the anchor for at least a few seconds.
  • Make sure you are not too far away from the anchor.

Refer : https://developers.google.com/ar/develop/java/cloud-anchors/cloud-anchors-developer-guide-android


推荐阅读