ViewController.swift

Now, click on ViewController.swift, which is where the main logic of our application lies.

The first thing you will see is that the class requires the ARKit library for the AR, the UIKit library for the interface, and the SceneKit library. This last one is a high-level 3D framework that Apple offers so that you can create, import, and manipulate 3D objects and scenes. We will be using it later in this chapter to import our external 3D models into the scene.

Our only variable at the moment is as follows:

@IBOUtlet var sceneView: ARSCNView!

This is the ARSCNView element from Main.storyboard. It will allow us to control the AR scene.

Our ViewController class inherits from UIViewController. From here, we have three override methods. The first one is viewDidLoad:

override func viewDidLoad() {
super.viewDidLoad()

sceneView.delegate = self
sceneView.showsStatistics = true

let scene = SCNScene(named: "art.scnassets/ship.scn")!
sceneView.scene = scene
}

This method is called when the view is loaded in memory and is used to initialize elements. In this case, we attach the sceneView element's delegate to the class itself, we activate the statistics that will appear at the bottom of the app, we create a scene with the ship model, and we assign that scene to the scene from the scenView element. This will make the battleship appear as soon as the app is launched.

The second method is viewWillAppear:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

let configuration = ARWorldTrackingConfiguration()
sceneView.session.run(configuration)
}

This method is called when the device is ready to show the interface to the user. The AR session is launched from here. The ARSession is the main element that controls the AR experience; it reads the data from the sensors of your device, controls the device's camera, and analyzes the image coming from it to create a correspondence between the real world and a virtual space where you place the AR objects.

Before running the session, we need to make use of the ARConfiguration class, which will determine the ARKit features that have been enabled for the session. In this case, it will track the device's position and orientation in relation to surfaces, people, images, or objects using the device's back camera and then run the session with that configuration. We could use a different configuration if we wanted to track only people's faces or only images, for example. (See https://developer.apple.com/documentation/arkit/arconfiguration for more information.)

The third override method is viewWillDisappear:

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}

This method is called when the view is about to be removed. When that happens, the view's session is paused.

These are the methods we have implemented at the moment. Now, we are going to start changing and adding code to see how ARKit tracks planes and find out about the different state changes.

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

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