首页 文章

检测带有ARCore的点击事件是否命中已添加的3d对象

提问于
浏览
4

我正在关注ARCore示例(https://github.com/google-ar/arcore-android-sdk),我正在尝试删除已添加的对象3d(andy) . 如何检测带有ARCore的点击事件是否会触发已添加的3d对象?

1 回答

  • 1

    这几天我有同样的问题,我试过2个解决方案,

    1. frame.hitTest(MotionEvent)

    2. 将arcore世界的顶点投影到视图中的2d坐标

    首先,我使用 1. 来获取平面上的命中姿势并与已存在的3d对象的姿势进行比较,但是一旦3d对象离开了平面,这将无法工作 .

    最后,我使用 2. 来获取视图上的3d对象的顶点,然后使用点击位置进行命中测试 .

    如果您正在关注ARCore示例,则可以在ObjectRenderer.java的draw方法中看到此行

    Matrix.multiplyMM(mModelViewProjectionMatrix, 0, 
                      cameraPerspective, 0, mModelViewMatrix, 0);
    

    “mModelViewProjectionMatrix”只是使用此ModelViewProjection矩阵将已添加的3d对象的顶点从3d arcore世界映射到2d视图 .

    在我的情况下,我做这样的事情,

    pose.toMatrix(mAnchorMatrix, 0);
    objectRenderer.updateModelMatrix(mAnchorMatrix, 1);
    objectRenderer.draw(cameraView, cameraPerspective, lightIntensity);
    
    float[] centerVertexOf3dObject = {0f, 0f, 0f, 1};
    float[] vertexResult = new float[4];
    Matrix.multiplyMV(vertexResult, 0, 
                      objectRenderer.getModelViewProjectionMatrix(), 0, 
                      centerVertexOf3dObject, 0);
    // circle hit test
    float radius = (viewWidth / 2) * (cubeHitAreaRadius/vertexResult[3]);
    float dx = event.getX() - (viewWidth / 2) * (1 + vertexResult[0]/vertexResult[3]);
    float dy = event.getY() - (viewHeight / 2) * (1 - vertexResult[1]/vertexResult[3]);
    double distance = Math.sqrt(dx * dx + dy * dy);
    boolean isHit = distance < radius;
    

    我在ARCore Measure应用程序中使用它,
    https://play.google.com/store/apps/details?id=com.hl3hl3.arcoremeasure

    和源代码,https://github.com/hl3hl3/ARCoreMeasure/blob/master/app/src/main/java/com/hl3hl3/arcoremeasure/ArMeasureActivity.java

相关问题