Sceneform:如何禁用表面检测并将对象放置在相机前面?

时间:2018-12-01 22:10:45

标签: android arcore sceneform

我在Android上使用带有ARCore的Sceneform,无法通过提供的文档清楚地理解这些概念。我正在尝试从github修改现有的HelloSceneform应用程序,并尝试创建一个应用程序,在该应用程序启动后,用户会直接在他/她的前面看到3D对象。这与我发现的https://github.com/google-ar/arcore-unity-sdk/issues/144非常相似,但是我无法弄清楚如何改进现有代码来获取它。

setContentView(R.layout.activity_ux);
arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
ModelRenderable.builder()
    .setSource(this, R.raw.andy)
    .build()
    .thenAccept(modelRenderable -> {
        andyRenderable=modelRenderable;
    });

arFragment.setOnTapArPlaneListener(
    (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {  
      Anchor anchor = hitResult.createAnchor(); 
      AnchorNode anchorNode = new AnchorNode(anchor);
      anchorNode.setParent(arFragment.getArSceneView().getScene()); 
      TransformableNode andy = new TransformableNode(arFragment.getTransformationSystem());
      andy.setParent(anchorNode);
      andy.setRenderable(andyRenderable);
      andy.select();
    });

我只需要禁用表面检测,获取Pose对象,锚点并直接设置对象,而无需android上的任何触摸侦听器,以及Java代码中的所有功能。当我尝试使用姿势创建锚点时,它给了我NotTrackingException。

 session=new Session(this);
 ...
 Pose pose = Pose.makeTranslation(-0.41058916f, -0.6668466f,  
 Anchor anchor = session.createAnchor(pose);

我希望有人可以花些时间来提供帮助。

1 个答案:

答案 0 :(得分:0)

 @Override
    public void onUpdate(FrameTime frameTime) {
        Frame frame = playFragment.getArSceneView().getArFrame();
        if (frame == null) {
            return;
        }
        if (frame.getCamera().getTrackingState() != TrackingState.TRACKING) {
            return;
        }
        for (Plane plane : frame.getUpdatedTrackables(Plane.class)) {
            playFragment.getPlaneDiscoveryController().hide();
            if (plane.getTrackingState() == TrackingState.TRACKING) {
                for (HitResult hit : frame.hitTest(getScreenCenter().x, getScreenCenter().y)) {
                    Trackable trackable = hit.getTrackable();
                    if (trackable instanceof Plane && ((Plane) trackable).isPoseInPolygon(hit.getHitPose())) {
                        Anchor anchor = hit.createAnchor();
                        AnchorNode anchorNode = new AnchorNode(anchor);
                        anchorNode.setParent(playFragment.getArSceneView().getScene());
                        Pose pose = hit.getHitPose();
                        Node node = new Node();
                        node.setRenderable(modelRenderable);
                        node.setLocalPosition(new Vector3(pose.tx(), pose.compose(Pose.makeTranslation(0.0f, 0.05f, 0.0f)).ty(), pose.tz()));
                        node.setParent(anchorNode);
                    }
                }
            }
        }
    }

    private Vector3 getScreenCenter() {
        View vw = findViewById(android.R.id.content);
        return new Vector3(vw.getWidth() / 2f, vw.getHeight() / 2f, 0f);
    }