【问题标题】:How to add screen touch coordinates to real world in Scenform (AR CORE)?如何在 Sceneform (ARCORE) 中将屏幕触摸坐标添加到现实世界?
【发布时间】:2020-04-30 07:29:02
【问题描述】:

我想将图像添加到 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 视图中获得触摸点的确切位置。

【问题讨论】:

    标签: java android augmented-reality arcore sceneform


    【解决方案1】:

    经过大量工作,我能够找到答案。 您必须根据相机的射线位置创建姿势,并且必须根据您的相机位置和相机的射线点计算位置和旋转。

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

    【讨论】:

    • 不起作用,Renderable 远离给定坐标。假设我想在屏幕的宽度/2 和高度/2(屏幕中心)输入一些东西,它显示在右下角。
    猜你喜欢
    • 2018-01-29
    • 2017-05-26
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多