0

我是 Android AR 的新手,我想实现以下行为。我正在使用场景形式。

implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.15.0'

我有一个 3D 模型(简单的长方体)。这是创建长方体的方法。

  private ModelRenderable 3dModel;

  private void createModel(AnchorNode anchorNode) {
  TransformableNode cartoonPersonNodeThree = new TransformableNode(arFragment.getTransformationSystem());
      cartoonPersonNodeThree.getScaleController()
          .setEnabled(false); // Disable scaling
      cartoonPersonNodeThree.getTranslationController()
          .setEnabled(false); // Disable moving
      cartoonPersonNodeThree.setParent(anchorNode);
      cartoonPersonNodeThree.setRenderable(3dModel);
      cartoonPersonNodeThree.select();

}

当检测到 Surface 时,我需要将此模型显示在用户面前,并且它应该随着相机的移动而移动。

下面的代码做到了。3D模型显示在中央和相机前方1m处。

arFragment.getArSceneView()
    .getScene()
    .addOnUpdateListener(frameTime -> {
      Frame frame = arFragment.getArSceneView()
          .getArFrame();
      if (frame == null) {
        return;
      }

      if (frame.getCamera()
          .getTrackingState() != TrackingState.TRACKING) {
        return;
      }

      if (isModelAlreadyInPlace) {
        return;
      }

      if (isModelLocked) {
        return;
      }

      removeAnchorNode(anchorNode); // Remove previous 3D model

      // Place the anchor 1m in front of the camera.
      Session session = arFragment.getArSceneView()
          .getSession();
      Anchor newMarkAnchor = session.createAnchor(frame.getCamera()
          .getPose()
          .compose(Pose.makeTranslation(0, 0, -1f)) //This will place the anchor 1M in front of the camera
          .extractTranslation());

      AnchorNode addedAnchorNode = new AnchorNode(newMarkAnchor);
      addedAnchorNode.setRenderable(cartoonPersonThree);
      addedAnchorNode.setParent(arFragment.getArSceneView()
          .getScene());
      anchorNode = addedAnchorNode;
    });

这里的问题是 3D 模型在空中,如果我将相机移到天花板上,3D 模型就会在那里。 在此处输入图像描述在此处输入图像描述

有没有办法将 3D 模型与地板对齐?

4

1 回答 1

0

此代码片段帮助我获得了我想要的确切行为。我们有一个命中到屏幕中心的 HitTest,因此我们可以获得“HitResult”并从中获取 Anchor。所以这是一种伪点击事件,我们执行每个帧通道。

private fun initArFragment() {
    arFragment.arSceneView.scene.addOnUpdateListener {
        arFragment.arSceneView?.let { sceneView ->
            sceneView.arFrame?.let { frame ->
                if (frame.camera.trackingState == TrackingState.TRACKING) {
                    val hitTest =
                        frame.hitTest(sceneView.width / 2f, sceneView.height / 2f)
                    val hitTestIterator = hitTest.iterator()
                    if (hitTestIterator.hasNext()) {
                        if (!placed) {
                            val hitResult = hitTestIterator.next()
                            val anchor = hitResult.createAnchor()
                            if (anchorNode == null) {
                                anchorNode = AnchorNode()
                                anchorNode?.setParent(sceneView.scene)
                                transformableNode =
                                    DragTransformableNode(arFragment.transformationSystem)
                                transformableNode?.setParent(anchorNode)
                                boxNode = createBoxNode(.4f, .6f, .4f)
                                boxNode?.setParent(transformableNode)
                            }
                            anchorNode?.anchor?.detach()
                            anchorNode?.anchor = anchor
                        }
                    }
                }
            }
        }
    }
}
于 2021-01-14T10:59:07.630 回答