ARCore中垂直平面上添加的物体始终会旋转

7

enter image description here

我正在在Sceneform ARFragment中的垂直平面上添加一张图片。 但它总是被旋转了。在水平平面上,代码可以正常工作。我用于在垂直平面上放置图像的代码如下:

arFragment.setOnTapArPlaneListener { hitResult: HitResult, 
                                         plane: Plane, 
                                   motionEvent: MotionEvent ->

    if(!isOnceTapedOnSurface) {
        val anchor = hitResult.createAnchor()
        val anchorNode = AnchorNode(anchor)
        anchorNode.setParent(arFragment.arSceneView.scene)

        andy = TransformableNode(arFragment.transformationSystem)

        if(plane.type == Plane.Type.VERTICAL) {
            val anchorUp = anchorNode.up
            andy.setLookDirection(Vector3.up(), anchorUp)
        }

        andy.setParent(anchorNode)
        andy.renderable = andyRenderable
        andy.select()

        // arFragment.arSceneView.planeRenderer.isVisible = false
        isOnceTapedOnSurface = true
    }
}
2个回答

3
为了解决这个问题,您可以使用上述解决方案。但是您应该使用世界旋转来旋转对象,而不是使用本地旋转。我们需要将旋转值归零。如果您使用本地旋转,则对象将表现为锚点(父级)旋转。因此,通过使用世界旋转,我们可以控制对象。 String planeType = ""; //当点击表面时,您可以获取锚点方向。
if (plane.getType() == Plane.Type.VERTICAL){
                planeType = "Vertical";
            }else if (plane.getType() == Plane.Type.HORIZONTAL_UPWARD_FACING){
                planeType = "Horizontal_Upward";
            }else if (plane.getType() == Plane.Type.HORIZONTAL_DOWNWARD_FACING){
                planeType = "Horizontal_Downward";
            }else {
                planeType = "Horizontal";
            }```


// First set object world rotation zero

transformableNode.setWorldRotation(Quaternion.axisAngle(new Vector3(0, 0f, 0), 0));


// check plane type is vertical or horizontal if it is vertical below logic will work.

if (planeType.equals("Vertical")) {

   Vector3 anchorUp = anchorNode.getUp();
   transformableNode.setLookDirection(Vector3.up(), anchorUp);

}

1
为了解决此问题,您需要设置public Pose getCenterPose()。它返回检测到平面中心的姿态,定义有原点。姿态的变换+Y轴将指向平面外的法线,+X+Z轴定向边界矩形的范围。
anchor = mySession.createAnchor(plane.getCenterPose())

当其可追踪状态为TRACKING时,此姿势将与最新帧同步。当其可追踪状态为PAUSED时,将返回一个身份姿势。
您的代码可能如下所示:
Anchor newAnchor;

for (Plane plane : mSession.getAllTrackables(Plane.class)) {

    if(plane.getType() == Plane.Type.VERTICAL && 
       plane.getTrackingState() == TrackingState.TRACKING) {
          newAnchor = plane.createAnchor(plane.getCenterPose());
          break;
    }
}
来自 Google ARCore 软件工程师的另一个提示:

让物体靠近锚点

在固定物体时,请确保它们靠近您使用的锚点。避免将物体放置在距离锚点几米以上的位置,以防止由于 ARCore 对世界空间坐标的更新而导致意外的旋转运动。

如果需要将对象放置在距离现有锚点几米远的位置,则创建一个更靠近该位置的新锚点,并将对象附加到新锚点。


对我没用。所有这些做的只是将锚点从后中心改变为显然将锚点移动到模型底部。还有其他尝试的想法吗? - Sam
@Sam 这个解决方案对我有效。如果它对你无效,那么我想你可能有其他问题。 - Andy Jazz
1
没关系,@ARGeo 只是想看看你是否有其他想法,因为你似乎在这个领域有一些专业知识。无论如何,还是谢谢你。 - Sam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接