我想在AR视图中添加图像。 我可以在渲染平面时添加图像。但我想在没有渲染平面的情况下也能添加。我想把它添加到AR视图的现实世界位置--我实际上是想把那个接触点转换为AR视图的世界位置。
List<HitResult> hitResult = frame.hitTest(motionEvent);
if (hitResult.size() != 0) {
//This code works fine
HitResult hit = hitResult.get(0);
Anchor anchor = hit.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
anchors.add(anchor);
} else {
//This is not Working
if (arFragment.getArSceneView().getSession() != null) {
Anchor anchor = arFragment.getArSceneView().getSession().createAnchor(frame.getCamera().getPose().compose(Pose.makeTranslation(0, 0, -1f)).extractTranslation());
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
anchors.add(anchor);
if (drawingText.equalsIgnoreCase("imagearrow")) {
addArrow(anchorNode, arrowType, arrowColor);
}
}
}
我没有得到AR视图中触摸点的准确位置。
经过大量的工作,我能够找到答案.你必须根据你的相机的射线位置创建姿势,并根据你的相机位置和相机的射线点计算位置和旋转。
Camera camera = arFragment.getArSceneView().getScene().getCamera();
Ray ray = camera.screenPointToRay(motionEvent.getX(), motionEvent.getY());
Vector3 drawPoint = ray.getPoint(1f);
Log.d("HELLODRWAPOINT", String.valueOf(drawPoint.x));
ArSceneView arSceneView = arFragment.getArSceneView();
com.google.ar.core.Camera coreCamera = arSceneView.getArFrame().getCamera();
if (coreCamera.getTrackingState() != TrackingState.TRACKING) {
return;
}
float[] position = { drawPoint.x, drawPoint.y, drawPoint.z};
float[] rotation = { camera.getWorldRotation().x,camera.getWorldRotation().y,camera.getWorldRotation().z, camera.getWorldRotation().w };
Session session = arFragment.getArSceneView().getSession();
Anchor anchor = session.createAnchor(new Pose(position, rotation));
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
anchors.add(anchor);