ARFragment 和 GLSurfaceView 在一个 Activity 中兼容吗?

are ARFragment and GLSurfaceView compatible together in one Activity?

我正在尝试在我的 Android 应用程序中将 ARCore 示例 - hellosceneform 和 augmented_image_java 合并为一个 Activity。也就是说,可以触摸屏幕并放下 AR 对象,也可以让相机扫描它识别的对象,并在对象周围放置一个 AR 框架。请帮忙?

Sceneform 在 SceneView class. This makes it incompatible with GLSurfaceView. You can do what you are looking to do by combining the hellosceneform sample and the augmentedimage 示例中实现了自己的渲染引擎,它们都使用 Sceneform。

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

private ModelRenderable andyRenderable;

您还需要安迪的模型。

然后在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);