AUGMENTED REALITY and AR CORE

IOTRIC
4 min readDec 23, 2021

We’re going to discuss how we can develop Augmented Reality applications on android the easy way.

Have you ever seen how those android applications are able to place virtual objects onto your camera? Moreover, have you ever wondered how those applications are developed? And have you ever wanted to build one for yourself? There are a number of ways by which we can develop Augmented Reality (AR) applications.

Today we’re going to discuss how we can develop AR applications on android the easy way. This post will cover all the basic information that you require to get started with your AR journey!.

Excited? You should be!

To continue further, it is assumed that you already have the basic knowledge of Android Studio and Kotlin and have an idea about how to build basic applications.

As the post title says, we will be using Google’s AR Core to develop our AR Application. AR Core is a service provided by google that enables us to build and/or run AR apps built for AR Core. To check your device support, please visit https://developers.google.com/ar/devices.

Sceneform

One important thing, playing/working with 3D objects can be a bit tricky on OpenGL (Used to render 3D objects in AR session/camera). But we have a workaround for that too! And its Sceneform. Sceneform is a library provided by google to ease the process of rendering 3D objects, providing them colors, textures and materials!.

LET’S GET STARTED!

Setting up a new project and adding support for AR Core to it.

  • Create a new android project with an empty activity, name it whatever you like and provide a package id to your project.
  • Open your Manifest.xml file and place these lines:
  • Inside your application’s build.gradle file (App level) add this:
  • Make sure your project’s build.gradle file includes Google’s Maven repository.
  • Add the latest ARCore library as a dependency in your app’s build.gradle file:
  • Add sceneform dependency:

implementation ‘com.google.ar.sceneform.ux:sceneform-ux:1.17.1’

Prerequisites to start an AR Session.

  • To check if your device supports AR Core, you can use this code snippet before initiating an AR Session. (e.g. inside main activity)
  • Make sure your app has camera permission before initiating the AR session.

Launching the AR Session.

  • As we have added sceneform, it comes with an AR fragment that can be used directly to launch an AR Session.
  • If point no. 2 was successful, we can launch another activity called AR Activity.
  • Inside of the layout file for AR Activity, add an AR Fragment like this.

That’s it, It should open your camera and start an AR Session, By default the AR Session will track horizontal and vertical planes, you need to move your camera slowly so that the AR Core can track the environment, once it has tracked, it will point a lot of dots on the planes it has found like this:

And your AR Session is ready to use!

Placing objects in the AR Session with finger tap.

  • Lets create a virtual cylinder to place in the AR Session. We will be using Sceneform to create a 3D cylinder.

The above code snippet creates an opaque material and then applies that material to the cylinder.

  • Lets create a virtual cylinder to place in the AR Session. We will be using Sceneform to create a 3D cylinder.
  • Now lets place that cylinder in the AR session where the user taps their finger. For that we need to listen to the finger taps on the AR session. To achieve this:

Implement this Scene.OnPeekTouchListener

Register your callbacks

fragment.getArSceneView().getScene().addOnPeekTouchListener(this);

And we can get touch inputs on its callback:

And voila! Your cylinder is placed.

You can try with other shapes that can be created via shapefactory. We can also use fbx, obj, gltf 3D models instead of basic shapes. We will discuss it on our next AR Core blog.

Hope you enjoyed and learnt something! If yes, please don’t forget to leave some claps!

For any queries, please write us at support@iotric.com

--

--

IOTRIC

IOTRIC stands out as a prominent, end-to-end software development firm focusing on MVP and Product development solutions.