如果ARCore 2D物体无法正确粘附到锚点,可能是因为锚点的位置或方向不正确。您可以尝试以下解决方法:
Camera camera = arFragment.getArSceneView().getArFrame().getCamera();
Vector3 cameraPosition = camera.getWorldPosition();
Quaternion cameraRotation = camera.getWorldRotation();
然后,将相机的位置和方向应用于锚点:
anchorNode.setWorldPosition(cameraPosition);
anchorNode.setWorldRotation(cameraRotation);
List planes = arFragment.getArSceneView().getArFrame().getUpdatedTrackables(Plane.class);
for (Plane plane : planes) {
if (plane.getTrackingState() == TrackingState.TRACKING) {
Vector3 planePosition = plane.getCenterPose().getPosition();
Quaternion planeRotation = plane.getCenterPose().getRotation();
// 应用位置和方向到锚点
}
}
arNode.setLocalScale(new Vector3(scale, scale, scale));
其中scale
是一个浮点数,表示缩放因子。
通过遵循上述步骤,您应该能够解决ARCore 2D物体无法正确粘附到锚点的问题。