1

我在 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);

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

4

1 回答 1

1
 @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);
    }
于 2018-12-03T03:40:24.863 回答