如何在ARcore中的特定位置放置对象?

时间:2019-10-16 13:53:00

标签: arcore

我正在尝试将对象放置在特定位置而不点击,是否有任何直接方法将对象放置在现实世界位置。

2 个答案:

答案 0 :(得分:0)

当然可以做到这一点。

  1. 搜索平坦表面。
    2. 覆盖onUpdate 方法。
  2. 在可追踪对象之间循环,然后您的模型被投影而无需点击。

    我只是给你一个想法,onUpdate方法是什么样的,这是伪代码

       frame = arFragment.getArSceneView().getArFrame();
        if (frame != null) {
            //get the trackables 
            Iterator<Plane> var3 = frame.getUpdatedTrackables(Plane.class).iterator();
            while (var3.hasNext()) {
                Plane plane = var3.next();
    
                //If a plane has been detected & is being tracked by ARCore
                if (plane.getTrackingState() == TrackingState.TRACKING) {
    
                    //Hide the plane discovery helper animation
                    arFragment.getPlaneDiscoveryController().hide();
    
    
                    //Get all added anchors to the frame
                    Iterator<Anchor> iterableAnchor = frame.getUpdatedAnchors().iterator();
    
                    //place the first object only if no previous anchors were added
                    if (!iterableAnchor.hasNext()) {
                        //Perform a hit test at the center of the screen to place an object without tapping
                        List<HitResult> hitTest = frame.hitTest(getScreenVector3().x, getScreenVector3().y);
    
                        //iterate through all hits
                        Iterator<HitResult> hitTestIterator = hitTest.iterator();
                        while (hitTestIterator.hasNext()) {
                            HitResult hitResult = hitTestIterator.next();
    
                            //Create an anchor at the plane hit
    
                                Anchor modelAnchor = plane.createAnchor(hitResult.getHitPose());
    
                                //Attach a node to this anchor with the scene as the parent
                                anchorNode = new AnchorNode(modelAnchor);
                                anchorNode.setParent(arFragment.getArSceneView().getScene());
    
                                **create a new TranformableNode that will carry our model**
                                }
    
    
                        }
                    }
                }
            }
    
        } 
    

答案 1 :(得分:0)

您可以将节点直接添加到场景:

node.localPosition = Vector3(x, y, z)
arSceneView.scene.addChild(node)