ARCore - How to detect single horizontal plane only

Viewed 2168

I am working with ARCore in one of my project. There, I need to detect a plane and place some objects(Anchors) on it.

In my onResume function, I created a AR session and able to detect only horizontal planes using Session configuration.

@Override
protected void onResume() {
    super.onResume();
    if (arSceneView == null) {
        return;
    }

    if (arSceneView.getSession() == null) {
        // If the session wasn't created yet, don't resume rendering.
        // This can happen if ARCore needs to be updated or permissions are not granted yet.
        try {
            session = DemoUtils.createArSession(this, installRequested);
            mArConfig = session.getConfig();
            mArConfig.setPlaneFindingMode(Config.PlaneFindingMode.HORIZONTAL);
            mArConfig.setUpdateMode(Config.UpdateMode.LATEST_CAMERA_IMAGE);
            session.configure(mArConfig);

            if (session == null) {
                installRequested = DemoUtils.hasCameraPermission(this);
                return;
            } else {
                arSceneView.setupSession(session);
            }
        } catch (UnavailableException e) {
            DemoUtils.handleSessionException(this, e);
        }
    }

    try {
        arSceneView.resume();
    } catch (CameraNotAvailableException ex) {
        DemoUtils.displayError(this, "Unable to get camera", ex);
        finish();
        return;
    }

    if (arSceneView.getSession() != null) {
        try {
            session.resume();
        } catch (CameraNotAvailableException e) {
            e.printStackTrace();
        }
    }
}

Here I get many horizontal planes. My requirement is to get only one horizontal plane which is detected first.

So, I tried to Disable plane finding mode in setOnUpdateListener as following,

arSceneView.getScene().setOnUpdateListener(frameTime -> {
        if (loadingMessageSnackbar == null){
            return;
        }

        Frame frame = arSceneView.getArFrame();

        if (frame == null){
            return;
        }

        if (frame.getCamera().getTrackingState() != TrackingState.TRACKING){
            return;
        }

        for (Plane plane : frame.getUpdatedTrackables(Plane.class)){
            if (plane.getTrackingState() == TrackingState.TRACKING){
                Config c =arSceneView.getSession().getConfig();
                c.setPlaneFindingMode(Config.PlaneFindingMode.DISABLED);
                arSceneView.getSession().configure(c);
            }
        }
    });

Here, once the plane is detected it is getting hide.

Can anyone suggest any method to get the first detected horizontal plane and place objects in that plane only??

Thanks in advance...!!

1 Answers
Config config = arFragment.getArSceneView().getSession().getConfig();
config.setPlaneFindingMode(Config.PlaneFindingMode.DISABLED);  
arFragment.getArSceneView().getSession().configure(config);
Related