Virtual Reality Viewer using ARKit

Virtual Reality Viewer using ARKit

Virtual_Reality_Swift_Jamie_Clark2.jpg

Virtual Reality is pitched to be one of the up and coming trends in education, however, like all aspects of education “Content is King”. Without the content VR Headsets are fated to sit on shelves and grow dusty. With this project I wanted students to not only develop an App using ARKit, but also experiment creating their own VR content. What makes for engaging content when dealing with 360 video? With editing capabilities in Adobe Premiere and Final Cut this has made it a very viable project for students.

As always, all of my projects are available on GitHub so you can download the project yourself.

ARKit App - Daniel Budd

UPDATE: Before I get started it is worth noting that this project was created before the release of Swift 5 and ARKit 3. We should actually be able to reduce the amount of code and simplify the project to the benefit students.

In Xcode, you can now select an AR App when you create a new project. This will setup your basic view controller and also setup the error handling that is required.

func session(_ session: ARSession, didFailWithError error: Error) {
    // Present an error message to the user
}
    
func sessionWasInterrupted(_ session: ARSession) {
    // Inform the user that the session has been interrupted, for example, by presenting an overlay
}
    
func sessionInterruptionEnded(_ session: ARSession) {
    // Reset tracking and/or remove existing anchors if consistent tracking is required 
}

This project could be developed without using ARKit, which I demonstrated in Swift Playground previously, however, the advantage of using ARKit is its ability to track your camera within the environment. This elevates some of the bugs which were experienced in my previous project. The project still creates a sphere within a scene and places the video as the texture within the sphere. The camera is placed inside the sphere.

let videoURL: NSURL? = info["UIImagePickerControllerReferenceURL"] as? NSURL
        
player = AVPlayer(url: videoURL! as URL)
self.dismiss(animated: true, completion: nil)
        
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main, using: { (notification) in
                self.player.seek(to: CMTime.zero)
                self.player.play()
        })
        
//Add Sphere to Scene
let sphere = SCNSphere(radius: 60.0)
sphere.firstMaterial!.isDoubleSided = true
sphere.firstMaterial!.diffuse.contents = player

let sphereNode = SCNNode(geometry: sphere)
sphereNode.simdTransform = cameraTransform(z: 0, sideOffset: 0)
sceneView.scene.rootNode.addChildNode(sphereNode)
        
//Play video
player.play()

I’ll be the first to admit that this project isn’t perfect so I would love to hear ways to improve the project and make it more stable. Fork the project on GitHub or reach out to me on Twitter to share ideas for improvements for students.

Swift 5 Update for Geometry

Swift 5 Update for Geometry