ARCore Sceneform ViewRenderable 以随机角度旋转图像

ARCore Sceneform ViewRenderable rotates the image by random angle

我正在将 2D 图像放置在垂直墙上,就像挂在墙上的画一样。但是渲染图像以随机角度旋转。它没有正确对齐。我已经集成了以下代码:

 @RequiresApi(api = Build.VERSION_CODES.N)
    private void createViewRenderable(WeakReference<ARRoomViewActivity> weakActivity) {
        imageViewRenderable = new ImageView(this);
        Picasso.get().load(paintingImageUrl)
                    .networkPolicy(NetworkPolicy.OFFLINE)
                    .into(imageViewRenderable);

        ViewRenderable.builder()
                .setView(this, imageViewRenderable)
                .build()
                .thenAccept(renderable -> {
                    ARRoomViewActivity activity = weakActivity.get();
                    if (activity != null) {
                        activity.renderable = renderable;
                    }
                })
                .exceptionally(
                        throwable -> {
                            return null;
                        });
    }



 private void addToScene(HitResult hitResult) {
        if (renderable == null) {
            return;
        }
        Anchor anchor = hitResult.createAnchor();
        anchorNode = new AnchorNode(anchor);
        anchorNode.setParent(arFragment.getArSceneView().getScene());

        node = new Node();
        node.setRenderable(renderable);

        transformableNode = new TransformableNode(arFragment.getTransformationSystem());
        node.setLocalRotation(Quaternion.axisAngle(new Vector3(-1f, 0, 0), -90f));
        node.setLookDirection(new Vector3(0, 10f, 0));
        transformableNode.setParent(anchorNode);
        node.setParent(transformableNode);
        transformableNode.select();
    }

我正在使用以下 Sceneform 版本,也尝试了最新的 1.17.0 但无法成功

implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.7.0'

默认情况下,锚点的方向是其中一个轴略微指向相机。这导致放置在地板上的模型成为 "looking at" 用户。然而,墙上的模型最终会任意旋转。这可以通过设置节点的外观方向来解决。

在您的 addToScene 方法中,您可以引入一个中间节点,该节点固定相对于锚节点的方向。中间节点的子节点将被正确定向。

// Create an anchor node that will stay attached to the wall
Anchor anchor = hitResult.createAnchor();
anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());

// Create an intermediate node and orient it to be level by fixing look direction.
// This is needed specifically for nodes on walls.
Node intermediateNode = new Node();
intermediateNode.setParent(anchorNode);
Vector3 anchorUp = anchorNode.getUp();
intermediateNode.setLookDirection(Vector3.up(), anchorUp);

注意:上面代码示例中的最后两行是获得正确方向的关键。