Forming the AR scene

Now that we have ARCore and Sceneform enabled and our 3D model included in the project, let's add the code that will make the model appear on the screen when a flat surface is detected and the user taps on the screen. Let's get started:

  1. Open your MainActivity.java file from app/java/com.banana.arprototype and add the following imports:
import android.net.Uri;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.ar.core.Anchor;
import com.google.ar.core.HitResult;
import com.google.ar.core.Plane;
import com.google.ar.sceneform.AnchorNode;
import com.google.ar.sceneform.rendering.ModelRenderable;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.TransformableNode;
  1. Inside our class, create the following variables to control the AR scene and the 3D model:
private ArFragment arFragment;
private ModelRenderable modelRenderable;
  1. Now, inside the OnCreate() method, add the ARFragment initialization:
protected void onCreate(Bundle savedInstanceState) {
...
arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
  1. Still inside the OnCreate() method, build the model in the scene using ModelRenderable, as shown in the following code:
ModelRenderable.builder()
.setSource(this, Uri.parse("engine.sfb"))
.build()
.thenAccept(renderable -> modelRenderable = renderable)
.exceptionally(
throwable -> {
Toast toast = Toast.makeText(this, "Unable to
load the model"
, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return null;
});

Here, we call the model from the assets folder using Uri.parse. It will launch an error message if it's unable to load it.

  1. Finally, still in the OnCreate() method, we will place the model when the user taps the screen after a planar surface has been detected:
arFragment.setOnTapArPlaneListener(
(HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
if (modelRenderable == null)
return;

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

TransformableNode model = new TransformableNode(arFragment.getTransformationSystem());
model.setParent(anchorNode);
model.setRenderable(modelRenderable);
model.select();
});

Here, when the user taps on a plane and the model renderable has been successfully loaded, an anchor is created to keep the model in place. Then, a new node for the model is created and attached to it.

  1. Now, you can run the app either in an Android emulator or on an actual device. To run it in an emulator, the device must meet some requirements, and you will have to download ARCore from the Play Store manually on it (take a look at the Prepare your device or emulator section at https://developers.google.com/ar/develop/java/quickstart). Here, we will directly run the app in our device by clicking on the play icon and then selecting our connected device:

Running the app in a physical device
  1. With this, the app will be installed on the mobile device. As we mentioned previously, the first time the app is run, it will check whether the latest ARCore version has been installed:

ARCore checking for the latest version of ARCore
  1. You will have to install it through Google Play:

Installing the ARCore app
  1. Once this initial step has been completed, you will be able to finish launching the app. Move the phone/tablet until a flat surface is detected:

The white dots form the plane surface where we can place the 3D objects
  1. Tap on the screen to anchor your model:

 

The engine will appear standing in front of the camera
  1. You can also resize, rotate, and move the model with your fingers while moving around it to see it from different angles. If you tap again, a new engine will appear:

Tapping multiple times causes multiple instances

Play around with the app and get comfortable with using it. You will see that, depending on the capacity of the device, the recognition of a flat surface will be faster/slower. You will also see that if you rotate the device (portrait/landscape), the model will disappear because the anchor is lost. Therefore, now that we have the basic setup, we are going to make some changes to improve the overall performance and give the user the option to add more models to the current scene.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset