ARKit is supported on all Apple devices that run iOS 11 and that have A11 (the newest), A10, or A9 chips.
Steps to integrate ARKit in your iOS application :
- Creating a Project :
- Adding ARKit SceneKit View
- Enabling the Camera
- Adding a 3D Object
- Displaying a 3D Object in the scene
- Setting up Control buttons
Create an empty project by selecting a single view Application.
-
You need to add ARKit SceneKit View to your Storyboard. ARSCNView is used for displaying virtual 3D content on a camera background. Don’t forget to adjust the constraints so that ARSCNView works on the full screen of the device.
-
Create an IBOutlet in the ViewController.
-
This will show an error, So you need to add ARKit Framework to your project and import it to the view controller
To enable rendering, ARKit needs to use an iOS device’s sensors (camera, accelerometer, and gyroscope).
-
AR mobile application needs to use the camera. To allow it to do so, set up the scene in the viewDidLoad method and run the session in the viewWillAppear method.
-
When users launch an augmented reality mobile application, they must allow the app to use the camera. To request access to the camera from a user, you need to add a new row to the Info.plist file: "Privacy – Camera Usage Description".
-
To check that you’ve done everything right, run your AR app; if you see the camera working, then you’re on the right track.
You can pick any virtual object you want, but let’s check what file formats are supported by ARKit.
-
ARKit SceneKit View supports several file formats, namely .dae (digital asset exchange), .abc (alembic), and .scn (SceneKit archive). When .dae or .abc files are added to an Xcode project, however, the editor automatically converts them to SceneKit’s compressed files that retain the same extensions. To serialize a SCNScene object, create a .scn file by converting the initial .dae or .abc file.
-
Here, use a .dae object and place it in the project. Open it with SceneKit editor tool, go to an editor and convert it to the scene. Place .dae and .scn in one folder inside your project directory.
As you can see, both .dae and .scn files are present in the Objects folder.
-
The object has already been added to our project, now we need to add the object to the SCNScene.
-
Since we’re using SceneKit, a 3D object model must be a subclass of SCNNode, so we need to create a new class (we’ve called it Drone, though you may call it whatever you like) and load the initial file containing the object (in our case, Drone.scn). Here’s the code we used to add the quadcopter to the scene.
If we run our augmented reality mobile app now, we’ll see a quadcopter in its default position on our iPhone’s screen. Needless to say, if you can’t see a 3D object, go back and check whether you’ve done everything correctly.
The default position of the 3D object looks unnatural, so please change the parameters like given. (To change the parameters, go to scene object, click on the object and change it.)
The application seems to be working and the quadcopter is displayed in the scene, but there’s still one major thing to do. So far, the quadcopter is a motionless AR object that looks nice but doesn’t move. It’s time to add controls.
We will place 2 controls in the bottom, One set of controls is responsible for moving the quadcopter up and down and left and right, while the other rotates it and moves the quadcopter forward and backward. To set up the controls, you need to use SCNAction and two methods.
The moveBy(x:y:z: duration:) method is in charge of creating the action that moves the node relative to its current position.
x – the distance to move the node in the X direction of its parent node’s local coordinate space y – the distance to move the node in the Y direction of its parent node’s local coordinate space z – the distance to move the node in the Z direction of its parent node’s local coordinate space duration – The duration of the animation (in seconds) The rotateTo(x:y:z: duration:) method is responsible for creating an action that rotates the node in principal angles.
x – for rotating the node counterclockwise around the X-axis of its local coordinate space (measured in radians) y – for rotating the node counterclockwise around the Y-axis of its local coordinate space (measured in radians) z – for rotating the node counterclockwise around the Z-axis of its local coordinate space (measured in radians) duration – The duration of the animation (in seconds)
To put the buttons and long-press gesture please check the code. Also if it is set up completely you can check all the actions.