首页 文章

使用ARCore检测平面并点击事件

提问于
浏览
1

我正在努力理解Google's ARCore API并推动了他们的sample project (java_arcore_hello_ar) to GitHub .

在此示例中,当您将应用程序部署到Android时,会检测到任何水平曲面/平面 . 如果您点击检测到的平面,“Andy”将会在您点击的位置渲染Andrid机器人 . 很酷 .

我试图找到代码中的位置:

  • 这是检测到水平表面/平面;和

  • 逻辑在哪里正确地调整大小并重新定位Andy(我假设你点击的点离相机更远,他会变小,等等)

我相信当检测到飞机时,Android框架会调用 onSurfaceCreated 方法:

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    GLES20.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

    // Create the texture and pass it to ARCore session to be filled during update().
    mBackgroundRenderer.createOnGlThread(/*context=*/this);
    mSession.setCameraTextureName(mBackgroundRenderer.getTextureId());

    // Prepare the other rendering objects.
    try {
        mVirtualObject.createOnGlThread(/*context=*/this, "andy.obj", "andy.png");
        mVirtualObject.setMaterialProperties(0.0f, 3.5f, 1.0f, 6.0f);

        mVirtualObjectShadow.createOnGlThread(/*context=*/this,
            "andy_shadow.obj", "andy_shadow.png");
        mVirtualObjectShadow.setBlendMode(BlendMode.Shadow);
        mVirtualObjectShadow.setMaterialProperties(1.0f, 0.0f, 0.0f, 1.0f);
    } catch (IOException e) {
        Log.e(TAG, "Failed to read obj file");
    }
    try {
        mPlaneRenderer.createOnGlThread(/*context=*/this, "trigrid.png");
    } catch (IOException e) {
        Log.e(TAG, "Failed to read plane texture");
    }
    mPointCloud.createOnGlThread(/*context=*/this);
}

但是,该代码看起来假设用户已经在表面上轻敲 . 我没有看到一个 if -有条件的,基本上说“用户已经检测到平面/表面上的渲染Andy _1300261” . 任何人都可以发现这可能发生的地方吗?

1 回答

  • 4

    点击检测由mGestureDetector完成:

    mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            onSingleTap(e);
            return true;
        }
    
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    });
    

    哪个链接到SurfaceView

    mSurfaceView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return mGestureDetector.onTouchEvent(event);
        }
    });
    

    这两件事都发生在 onCreate() 中,所以现在每次点击表面视图(活动中的"main"视图),

    private void onSingleTap(MotionEvent e) {
        // Queue tap if there is space. Tap is lost if queue is full.
        mQueuedSingleTaps.offer(e);
    }
    

    被调用并存储水龙头 . 然后在每个框架图中处理该队列(这又由系统的UI绘图周期发出)here

    MotionEvent tap = mQueuedSingleTaps.poll();
    if (tap != null && frame.getTrackingState() == TrackingState.TRACKING) {
        for (HitResult hit : frame.hitTest(tap)) {
           ...
    

    这会添加一个新的锚点(即物理世界中的一个点"locked"),在该锚点上呈现一个Android对象(cf. this line) .

相关问题