我正在尝试在我的Android应用中将ARCore示例-hellosceneform和extendeded_image_java结合在一起,成为一个Activity。也就是说,可以触摸屏幕并放下AR对象,也可以触摸相机以扫描其识别出的对象并将AR框放在例如对象周围。请帮忙?

最佳答案

Sceneform在SceneView类中实现其自己的呈现引擎。这使其与GLSurfaceView不兼容。您可以通过组合同时使用Sceneform的hellosceneform样本和augmentedimage样本来完成您想做的事情。

要合并两者,请从AugmentedImageActivity.java开始并添加模型的成员变量:

private ModelRenderable andyRenderable;


您还需要Andy的模型。

然后在onCreate中,在onCreate()的末尾添加模型的加载和轻敲的处理:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      arFragment = (ArFragment)
      getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
      fitToScanView = findViewById(R.id.image_view_fit_to_scan);
   arFragment.getArSceneView().getScene().addOnUpdateListener(this::onUpdateFrame);

    /*** Add HelloSceneform functionality here vvvvvvvvv **/

    // When you build a Renderable, Sceneform loads its resources in
    // the background while returning a CompletableFuture.
    // Call thenAccept(), handle(), or check isDone() before calling get().
    ModelRenderable.builder()
            .setSource(this, R.raw.andy)
            .build()
            .thenAccept(renderable -> andyRenderable = renderable)
            .exceptionally(
                    throwable -> {
                      Toast toast =
                              Toast.makeText(this,
                             "Unable to load andy renderable", Toast.LENGTH_LONG);
                      toast.setGravity(Gravity.CENTER, 0, 0);
                      toast.show();
                      return null;
                    });

    arFragment.setOnTapArPlaneListener(
            (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
              if (andyRenderable == null) {
                return;
              }

              // Create the Anchor.
              Anchor anchor = hitResult.createAnchor();
              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();
            });
  }


增强的图像片段会关闭平面发现,因此您将要删除该代码,以便渲染平面。该代码在AugmentedImageFragment.java中。只需删除这些行:

// Delete these lines to keep the plane discovery.
getPlaneDiscoveryController().hide();
getPlaneDiscoveryController().setInstructionView(null);
getArSceneView().getPlaneRenderer().setEnabled(false);

09-26 12:51