AR BASICS FOR ANDROID STUDIO
Tags: android, java, beginners, ar
Published: 2020-07-29 14:11:35

Hello Everyone,
This post is going to be an interesting one because this is how i created my own pokemon(pikachu).What if we can create an app that can show us our favourite character,so it can be done by a simple addition of augment reality to our android app.

so
#WHAT IS AUGMENT REALITY?#
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/glh1cwuk0tlu20j4e4vf.jpg)
Augmented reality (AR) is an interactive experience of a real-world environment where the objects that reside in the real world are enhanced by computer-generated perceptual information, sometimes across multiple sensory modalities, including visual, auditory, haptic, somatosensory and olfactory. AR can be defined as a system that fulfills three basic features: a combination of real and virtual worlds, real-time interaction, and accurate 3D registration of virtual and real objects   ~~(WIKIPEDIA)

in simple words pikachu in pokemon go,you can create it:)

so As an android learner how to implement this in our app,
#REQUIRMENTS#
 1)install your app in a device that supports google arcore

 2)**uses-permission android:name="android.permission.CAMERA"** 
     add this in a tag for asking the user to allow camera 
     permission.

 3)Now after this add the following dependencies in your (app- 
   level)build.gradle file
   **implementation "com.google.ar.sceneform.ux:sceneform-ux:1.5.0"**(use the supporting version or the errors will blow your mind) and 
**dependencies {    classpath 'com.google.ar.sceneform:plugin:1.5.0'}**(in project level build.gradle file)

this will add sceneform sdk to your project and also allows to implement different 3D assests to your project.you can download them from the internet or you can follow this link
:-https://poly.google.com
**remember to download the .obj file that is your actual model**

 4)now create a sampledata directory and add your assets there.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7ja635krtnoj3h4zfvtk.png)

5)Add a fragment to your main layout.xml file
 **fragment android:name="com.google.ar.sceneform.ux.ArFragment"        
   android:id="@+id/ux_fragment"        
   android:layout_width="match_parent"        
   android:layout_height="match_parent"**
 i.e. the sceneform fragment. 


Another important thing the sdk requires Api level-27 to run this or more so keep this in mind while creating the project(if your phone has inbuilt ar you're lucky my friend)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7ba2gxcmcd1pyy2bfmqk.jpg)

Next, we are using the ModelRenderable class to build our model. With the help of setSource method, we load our model from the .sfb file. This file was generated when we imported the assets. thenAccept method receives the model once it is built. We set the loaded model to our lampPostRenderable.

After all this thing you need the java code to run your first ar app.So here i am going to discuss about a few terms that i found confusing while implementing the code:

 a)**Scene**: This is the place where all your 3D objects will be 
          rendered. This scene is hosted by the AR Fragment which 
          we included in the layout. An anchor node is attached 
          to this screen which acts as the root of the tree and 
          all the other objects are rendered as its objects.

b)**HitResult**: This is an imaginary line (or a ray) coming from 
              infinity which gives the point of intersection of 
              itself with a real-world object.

 c)**Anchor**: An anchor is a fixed location and orientation in the 
   real world. It can be understood as the x,y,z coordinate in 
   the 3D space. You can get an anchor’s post information from 
   it. Pose is the position and orientation of the object in the 
  scene. This is used to transform the object’s local coordinate 
  space into real-world coordinate space.


d)** AnchorNode** This is the node that automatically positions 
   itself in the world. This is the first node that gets set when 
   the plane is detected.


e)**TransformableNode**: It is a node that can be interacted with. 
   It can be moved around, scaled rotated and much more. In this 
   example, we can scale the lamp and rotate it. Hence the name 
   Transformable.

             i hope now you can jump into the code https://github.com/Ashishkumarpanda/AR/blob/master/MainActivity.java)
IT consits of the following parts:

1)_build the model_
2)_add your model to scene_

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/al7ddyt21k99xg3sjkn0.png)
 

hope this article will be helpful:)