How to put an object without tap the screen - Android AR

Viewed 1003

I am trying to show an object without tapping on the screen using ARCore. In the basic sample of ARCore Sceneform provided by Google, you need to tap on the screen after it detects the surface.

I want to implement that AR shows the object without tapping on the screen (in Java).

I have this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!checkIsSupportedDeviceOrFinish(this)) {
        return;
    }

    setContentView(R.layout.activity_main);

    arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);

    // When you build a Renderable, Sceneform loads its resources in the background while returning
    // a CompletableFuture. Call thenAccept(), handle(), or check isDone() before calling get().
    ModelRenderable.builder()
            .setSource(this, R.raw.model)
            .build()
            .thenAccept(renderable -> andyRenderable = renderable)
            .exceptionally(
                    throwable -> {
                        Toast toast =
                                Toast.makeText(this, "Unable to load andy renderable", Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        return null;
                    });


    arFragment.setOnTapArPlaneListener(
            (HitResult hitResult, Image.Plane plane, MotionEvent motionEvent) -> {
                if (andyRenderable == null) {
                    return;
                }

                // Create the Anchor.
                Anchor anchor = hitResult.createAnchor();
                AnchorNode anchorNode = new AnchorNode(anchor);
                anchorNode.setParent(arFragment.getArSceneView().getScene());

                // Create the transformable andy and add it to the anchor.
                TransformableNode andy = new TransformableNode(arFragment.getTransformationSystem());
                andy.setParent(anchorNode);
                andy.setRenderable(andyRenderable);
                andy.select();
            });
}
1 Answers

i done similar to it in which i was receiving x,y from the user on the video call, its not exactly to that but it'll help you.create an motionEvent using, pass any point on screen by default in x,y. if it helped you, then mark as answer.

// MotionEvent parameters
                            long downTime = SystemClock.uptimeMillis();
                            long eventTime = SystemClock.uptimeMillis();
                            int action = MotionEvent.ACTION_DOWN;
                            int metaState = 2;

// dispatch the event
                            MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);

create an hit event using

HitResult hit = frame.hitTest(event )

and then create an anchor using it.

               Trackable trackable = hit.getTrackable();
                // Creates an anchor if a plane or an oriented point was hit.
                if ((trackable instanceof Plane && ((Plane) 
                trackable).isPoseInExtents(hit.getHitPose()))
                        || (trackable instanceof Point
                        && (((Point) trackable).getOrientationMode()
                        == Point.OrientationMode.ESTIMATED_SURFACE_NORMAL || ((Point) 
                  trackable).getOrientationMode()
                        == Point.OrientationMode.INITIALIZED_TO_IDENTITY))) {
                    // Hits are sorted by depth. Consider only closest hit on //a plane 
                   // or oriented point.
                    // Cap the number of objects created. This avoids //overloading 
                     // both the
                    // rendering system and ARCore.
                    if (anchors.size() >= 500) {
                        anchors.get(0).detach();
                        anchors.remove(0);
                    }
                    // Adding an Anchor tells ARCore that it should track //this
                     // position in
                    // space. This anchor is created on the Plane to place //the 3D 
                     //model
                    // in the correct position relative both to the world and //to the 
                        //plane.
                    anchors.add(hit.createAnchor());
Related