ARCORE:通过单击此可渲染对象来删除特定的可渲染对象 [英] ARCORE: remove a specific renderable by clicking on this renderable

查看:23
本文介绍了ARCORE:通过单击此可渲染对象来删除特定的可渲染对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ARCore 的 Sceneform 进行一个项目.我基于 ARCore 提供的 HelloSceneform 示例开发它.我想要做的是通过点击添加一个可渲染对象,然后当我单击屏幕上的特定可渲染对象时将其删除.我试过方法 AnchorNode.setOnTapListener 如下,但它没有工作(无响应):

I'm working on a project using Sceneform from ARCore. I develop it base on HelloSceneform example provided by ARCore. What I wanna do is adding a renderable object by a hit and then delete it when I click on the specific renderable on the screen. I've tried method AnchorNode.setOnTapListener as following, but it didn't work(no response):

anchorNode.setOnTapListener(new Node.OnTapListener() {
                      @Override
                      public void onTap(HitTestResult hitTestResult, MotionEvent motionEvent) {
                          if(anchorNode.getAnchor()!=null){
                              arFragment.getArSceneView().getScene().removeChild(anchorNode);
                              anchorNode.getAnchor().detach();
                              anchorNode.setParent(null);
                          }
                      }
                  });

我也尝试了以下方法,导致意外关闭:

I also tried the following method, which causes unexpected close:

      Scene scene = arFragment.getArSceneView().getScene();
      scene.addOnPeekTouchListener(new Scene.OnPeekTouchListener() {
          @Override
          public void onPeekTouch(HitTestResult hitTestResult, MotionEvent motionEvent) {
              Node node = hitTestResult.getNode();
              node.setParent(null);
          }
      });

有什么方法可以实现这个功能吗?

Is there any method could achieve this feature?

推荐答案

下面的代码应该检测触摸并删除节点.

The code below should detect the touch and delete the node.

如果您想有一个单独的按钮来删除选定的节点,您可以添加一个常规按钮和侦听器,然后只需使用 'touch' 事件来选择您要删除的节点.

If you want to have a separate button to delete a selected node you can add a regular button and listener and just use the 'touch' event to select the node you want to delete.

private void handleOnTouch(HitTestResult hitTestResult, MotionEvent motionEvent) {
        Log.d(TAG,"handleOnTouch");
        // First call ArFragment's listener to handle TransformableNodes.
        arFragment.onPeekTouch(hitTestResult, motionEvent);

        //We are only interested in the ACTION_UP events - anything else just return
        if (motionEvent.getAction() != MotionEvent.ACTION_UP) {
            return;
        }

        // Check for touching a Sceneform node
        if (hitTestResult.getNode() != null) {
            Log.d(TAG,"handleOnTouch hitTestResult.getNode() != null");
            Node hitNode = hitTestResult.getNode();

            if (hitNode.getRenderable() == andyRenderable) {
                Toast.makeText(LineViewMainActivity.this, "We've hit Andy!!", Toast.LENGTH_SHORT).show();
                arFragment.getArSceneView().getScene().removeChild(hitNode);
                AnchorNode hitNodeAnchor = (AnchorNode) hitNode;
                if (hitNodeAnchor != null) {
                     hitNode.getAnchor().detach();
                }
                hitNode.setParent(null);
                hitNode = null;
             }
        }

}

以上内容摘自 VR 测试应用程序的各个部分,并在此处合并为一个简洁的示例 - 完整的工作应用程序源可在此处获得:https://github.com/mickod/LineView

The above is extracted from various parts of a VR test application and combined here for a concise example - the full working application source is available here: https://github.com/mickod/LineView

更新 - Kotlin 版本(2020 年 4 月测试):

Update - Kotlin version (tested April 2020):

    private fun removeAnchorNode(nodeToRemove: AnchorNode) {
        //Remove an Anchor node
        arFragment.getArSceneView().getScene().removeChild(nodeToRemove);
        nodeToRemove.getAnchor()?.detach();
        nodeToRemove.setParent(null);
        nodeToRemove.renderable = null
    }

这篇关于ARCORE:通过单击此可渲染对象来删除特定的可渲染对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆